From 3d6c408b9de69cab0d4dbe34b1513e294d5ff5f8 Mon Sep 17 00:00:00 2001 From: Rajdeep Dua Date: Mon, 7 Apr 2014 09:44:05 +0530 Subject: [PATCH] Neutron API Section Added Change-Id: I53d05ef30665f26226054baf2f7e71ef824ad943 --- doc/user-guide/ch_sdk.xml | 13 ++ doc/user-guide/section_sdk_auth_keystone.xml | 4 +- doc/user-guide/section_sdk_auth_nova.xml | 12 +- doc/user-guide/section_sdk_authenticate.xml | 12 +- .../section_sdk_configure_instances.xml | 42 +++-- doc/user-guide/section_sdk_neutron.xml | 151 ++++++++++++++++++ 6 files changed, 197 insertions(+), 37 deletions(-) create mode 100644 doc/user-guide/section_sdk_neutron.xml diff --git a/doc/user-guide/ch_sdk.xml b/doc/user-guide/ch_sdk.xml index 5576b18de2..295e9d7c0e 100644 --- a/doc/user-guide/ch_sdk.xml +++ b/doc/user-guide/ch_sdk.xml @@ -12,8 +12,21 @@ Python by making calls on Python objects rather than making REST calls directly. All OpenStack command-line tools are implemented using the Python SDK. + You should also be familiar with: + + + RESTful web services + + + HTTP/1.1 + + + JSON and XML data serialization formats + + + diff --git a/doc/user-guide/section_sdk_auth_keystone.xml b/doc/user-guide/section_sdk_auth_keystone.xml index b60d2ff4c5..09d9c30363 100644 --- a/doc/user-guide/section_sdk_auth_keystone.xml +++ b/doc/user-guide/section_sdk_auth_keystone.xml @@ -3,8 +3,8 @@ xmlns:xi="http://www.w3.org/2001/XInclude" xmlns:xlink="http://www.w3.org/1999/xlink" version="5.0" xml:id="sdk_auth_keystone"> - Authenticate against an Identity Service endpoint - To authenticate against the Identity Service v2.0 endpoint, + Authenticate against an Identity endpoint + To authenticate against the Identity v2.0 endpoint, instantiate a keystoneclient.v_20.client.Client object: diff --git a/doc/user-guide/section_sdk_auth_nova.xml b/doc/user-guide/section_sdk_auth_nova.xml index 2d405ad7fa..5398d96fd5 100644 --- a/doc/user-guide/section_sdk_auth_nova.xml +++ b/doc/user-guide/section_sdk_auth_nova.xml @@ -28,16 +28,16 @@ nova = novaclient.client.Client("1.1", auth_url=env['OS_AUTH_URL'], If you authenticate against an endpoint that uses a custom authentication back-end, you must load the authentication plug-in and pass it to the constructor. - The Rackspace Public Cloud is an OpenStack deployment that - uses a custom authentication back-end. To authenticate against + The Rackspace public cloud is an OpenStack deployment that + uses a custom authentication back end. To authenticate against this cloud, you must install the rackspace-novaclient library that contains the Rackspace authentication plug-in, called rackspace. The following Python code shows the additional modifications required to instantiate a Client object that can - authenticate against the Rackspace custom authentication - back-end. + authenticate against the Rackspace custom authentication back + end. import novaclient.auth_plugin import novaclient.v1_1.client as nvclient from os import environ as env @@ -52,8 +52,8 @@ nova = nvclient.Client(auth_url=env['OS_AUTH_URL'], auth_plugin=auth_plugin) If you set the OS_AUTH_SYSTEM environment variable, check for this variable in your Python script to - determine whether you need to load a custom authentication - back-end: + determine whether you need to load a custom authentication back + end: import novaclient.auth_plugin import novaclient.v1_1.client as nvclient from os import environ as env diff --git a/doc/user-guide/section_sdk_authenticate.xml b/doc/user-guide/section_sdk_authenticate.xml index 337fabb910..76b618e567 100644 --- a/doc/user-guide/section_sdk_authenticate.xml +++ b/doc/user-guide/section_sdk_authenticate.xml @@ -2,13 +2,13 @@
- Authentication - You must authenticate against an OpenStack endpoint before you can - use OpenStack services. Each project uses a slightly different syntax for doing - authentication. + Authenticate + You must authenticate against an OpenStack endpoint before you + can use OpenStack services. Each project uses a slightly different + syntax for authentication. You must typically authenticate against a specific version of a service. For example, a client might need to authenticate - against the Identity Service v2.0. + against Identity v2.0. Python scripts that use the OpenStack SDK must have access to the credentials contained in the OpenStack RC file. Because credentials @@ -17,6 +17,6 @@ xmlns:xlink="http://www.w3.org/1999/xlink" version="5.0" xml:id="sdk_auth"> the credentials by using the environment variables in the Python scripts. - +
diff --git a/doc/user-guide/section_sdk_configure_instances.xml b/doc/user-guide/section_sdk_configure_instances.xml index ad78715046..a9427d88f2 100644 --- a/doc/user-guide/section_sdk_configure_instances.xml +++ b/doc/user-guide/section_sdk_configure_instances.xml @@ -11,27 +11,25 @@ novaclient.v1_1.keypairs.KeypairManager.create - method: + method: import novaclient.v1_1.client as nvclient nova = nvclient.Client(...) keypair_name = "staging" keypair = nova.keypairs.create(name=keypair_name) print keypair.private_key - - The Python script output looks something like this: + The Python script output looks something like this: -----BEGIN RSA PRIVATE KEY----- MIIEowIBAAKCAQEA8XkaMqInSPfy0hMfWO+OZRtIgrQAbQkNcaNHmv2GN2G6xZlb\nuBRux5Xk/6SZ ABaNPm1nRWm/ZDHnxCsFTcAl2LYOQXx3Cl2qKNY4r2di4G48GAkd\n7k5lDP2RgQatUM8npO0CD9PU ... mmrceYYK08/lQ7JKLmVkdzdQKt77+v1oBBuHiykLfI6h1m77NRDw9r8cV\nzczYeoALifpjTPMkKS8 ECfDCuDn/vc9K1He8CRaJHf8AMLQLM3MN ------END RSA PRIVATE KEY----- - - You will typically write the private key to a file in order to use it +-----END RSA PRIVATE KEY----- + You typically write the private key to a file to use it later. The file must be readable and writeable by only the file owner, otherwise the ssh client will refuse to read the private key file. It is safest to create the file with the appropriate permissions, as shown in - the following example: + the following example: import novaclient.v1_1.client as nvclient import os nova = nvclient.Client(...) @@ -43,9 +41,7 @@ keypair = nova.keypairs.create(name=keypair_name) fp = os.open(private_key_filename, os.O_WRONLY | os.O_CREAT, 0o600) with os.fdopen(fp, 'w') as f: f.write(keypair.private_key) - -
Import a keypair If you have already generated a keypair with the @@ -53,58 +49,58 @@ with os.fdopen(fp, 'w') as f: pass the contents of the file to the novaclient.v1_1.keypairs.KeypairManager.create - method to import the public key to Compute: + method to import the public key to Compute: import novaclient.v1_1.client as nvclient import os.path with open(os.path.expanduser('~/.ssh/id_rsa.pub')) as f: public_key = f.read() nova = nvclient.Client(...) -nova.keypairs.create('mykey', public_key) +nova.keypairs.create('mykey', public_key)
List keypairs To list keypairs, call the novaclient.v1_1.keypairs.KeypairManager.list - method: + method: import novaclient.v1_1.client as nvclient nova = nvclient.Client(...) -keypairs = nova.keypairs.list() +keypairs = nova.keypairs.list()
Create and manage security groups To list security groups for the current project, call the - novaclient.v_1.security_groups.SecurityGroupManager.list method: + novaclient.v_1.security_groups.SecurityGroupManager.list method: import novaclient.v1_1.client as nvclient nova = nvclient.Client(...) -security_groups = nova.security_groups.list() +security_groups = nova.security_groups.list() To create a security group with a specified name and description, - call the novaclient.v_1.security_groups.SecurityGroupManager.create method: + call the novaclient.v_1.security_groups.SecurityGroupManager.create method: import novaclient.v1_1.client as nvclient nova = nvclient.Client(...) -nova.security_groups.create(name="web", description="Web servers") +nova.security_groups.create(name="web", description="Web servers") To delete a security group, call the novaclient.v_1.security_groups.SecurityGroupManager.delete method, passing either a novaclient.v1_1.security_groups.SecurityGroup - object or group ID as an argument: + object or group ID as an argument: import novaclient.v1_1.client as nvclient nova = nvclient.Client(...) group = nova.security_groups.find(name="web") nova.security_groups.delete(group) # The following lines would also delete the group: # nova.security_groups.delete(group.id) -# group.delete() +# group.delete()
Create and manage security group rules Access the security group rules from the rules - attribute of a novaclient.v1_1.security_groups.SecurityGroup object: + attribute of a novaclient.v1_1.security_groups.SecurityGroup object: import novaclient.v1_1.client as nvclient nova = nvclient.Client(...) group = nova.security_groups.find(name="web") -print group.rules +print group.rules To add a rule, to a security group, call the novaclient.v1_1.security_group_rules.SecurityGroupRuleManager.create - method: + method: import novaclient.v1_1.client as nvclient nova = nvclient.Client(...) group = nova.security_groups.find(name="web") @@ -114,6 +110,6 @@ nova.security_group_rules.create(group.id, ip_protocol="icmp", nova.security_group_rules.create(group.id, ip_protocol="tcp", from_port=80, to_port=80) nova.security_group_rules.create(group.id, ip_protocol="tcp", - from_port=443, to_port=443) + from_port=443, to_port=443)
diff --git a/doc/user-guide/section_sdk_neutron.xml b/doc/user-guide/section_sdk_neutron.xml new file mode 100644 index 0000000000..c57698e0ed --- /dev/null +++ b/doc/user-guide/section_sdk_neutron.xml @@ -0,0 +1,151 @@ + +
+ Networking + + To use the information in this section, you should + have a general understanding of OpenStack Networking, + OpenStack Compute, and the integration between the two. You + should also have access to a plug-in that implements the + Networking API v2.0. +
+ Set environment variables + Make sure that you set the relevant environment variables + appropriately. + As an example, see the sample shell file that sets the + variables used to get credentials: + export OS_USERNAME="admin" +export OS_PASSWORD="password" +export OS_TENANT_NAME="admin" +export OS_AUTH_URL="http://IPADDRESS/v2.0" +
+
+ Get credentials + The examples in this section use the following common get_credentials method: + def get_credentials(): + d = {} + d['username'] = os.environ['OS_USERNAME'] + d['password'] = os.environ['OS_PASSWORD'] + d['auth_url'] = os.environ['OS_AUTH_URL'] + d['tenant_name'] = os.environ['OS_TENANT_NAME'] + return d + This code is assumed to exist in the credentials.py file, which all + samples import. +
+ +
+ Create network + The following program creates a network: + from neutronclient.v2_0 import client +from credentials import get_credentials +from utils import print_networks + +network_name = "sample_network" +credentials = get_credentials() +neutron = client.Client(**credentials) +try: + body_sample = { + "network": + { + "name": network_name, + "admin_state_up": True + } + } + netw = neutron.create_network(body=body_sample) + net_dict = netw['network'] + network_id = net_dict['id'] + print "Network %s created" % network_id + + body_create_subnet = { + "subnets":[ + { + "cidr":"192.168.199.0/24", + "ip_version":4, + "network_id": network_id + } + ] + } + + subnet = neutron.create_subnet(body=body_create_subnet) + print "Created subnet %s" % subnet +finally: + print "Execution completed" + #neutron.delete_network(network_id) + #print "Deleted Network %s" %network_id +
+
+ List networks + The following program lists networks: + from neutronclient.v2_0 import client +from credentials import get_credentials +from utils import print_values + + +credentials = get_credentials() +neutron = client.Client(**credentials) +netw = neutron.list_networks() + +print_values(netw, 'networks') +
+
+ Create port + The following program creates a port: + from neutronclient.v2_0 import client +import novaclient.v1_1.client as nvclient +from credentials import get_credentials +from utils import print_values_server +from credentials import get_nova_credentials + +credentials = get_nova_credentials() +nova_client = nvclient.Client(**credentials) + +#replace with server_id and network_id from your environment + +server_id = '9a52795a-a70d-49a8-a5d0-5b38d78bd12d' +network_id = 'ce5d204a-93f5-43ef-bd89-3ab99ad09a9a' +server_detail = nova_client.servers.get(server_id) +print server_detail.id + +if server_detail != None: + credentials = get_credentials() + neutron = client.Client(**credentials) + + body_value = { + "port": { + "admin_state_up": True, + "device_id": server_id, + "name": "port1", + "network_id": network_id + } + } + response = neutron.create_port(body=body_value) + print response +
+
+ List ports + The following program lists ports: + from neutronclient.v2_0 import client +from credentials import get_credentials +from utils import print_values + +credentials = get_credentials() +neutron = client.Client(**credentials) +ports = neutron.list_ports() +print print_values(ports, 'ports') +
+
+ List server ports + The following program lists the ports for a server: + from neutronclient.v2_0 import client +from credentials import get_credentials +from utils import print_values + + +credentials = get_credentials() +neutron = client.Client(**credentials) +ports = neutron.list_ports() +print print_values(ports, 'ports') +
+