diff --git a/doc/user-guide/ch_sdk.xml b/doc/user-guide/ch_sdk.xml index 295e9d7c0e..8c47bb5339 100644 --- a/doc/user-guide/ch_sdk.xml +++ b/doc/user-guide/ch_sdk.xml @@ -29,4 +29,5 @@ + diff --git a/doc/user-guide/section_sdk_nova.xml b/doc/user-guide/section_sdk_nova.xml new file mode 100644 index 0000000000..321c549f3a --- /dev/null +++ b/doc/user-guide/section_sdk_nova.xml @@ -0,0 +1,237 @@ + +
+ Compute + + To use the information in this section, you should have a + general understanding of OpenStack Compute. +
+ Set environment variables + Please see on how to setup environmental variables +and authenticate against Compute API endpoints. +
+
+ Get nova credentials v2 + The examples in this section use the + get_nova_credentials_v2 method: + def get_nova_credentials_v2(): + d = {} + d['version'] = '2' + d['username'] = os.environ['OS_USERNAME'] + d['api_key'] = os.environ['OS_PASSWORD'] + d['auth_url'] = os.environ['OS_AUTH_URL'] + d['project_id'] = os.environ['OS_TENANT_NAME'] + return d + This code resides in the + credentials.py file, which all + samples import. + Use the get_nova_credentials_v2() method to + populate and get a dictionary: + credentials = get_nova_credentials_v2() +
+ +
+ List servers v2 + The following program lists servers using the v2 APIs: + + To list the servers + + Import the following modules: + from credentials import get_nova_credentials_v2 +from novaclient.client import Client + + + Get Nova Credentials. See . + + + Instantiate the nova_client + client object by using the + credentials dictionary + object: + +nova_client = Client(**credentials) + + + Get the list of servers by calling servers.list on + nova_client object: + print(nova_client.servers.list()) + + + + List servers: complete code listing + #!/usr/bin/python +# -*- coding: utf-8 -*- +from credentials import get_nova_credentials_v2 +from novaclient.client import Client + +credentials = get_nova_credentials_v2() +nova_client = Client(**credentials) + +print(nova_client.servers.list()) + +
+
+ Create a server v2 + The following program creates a server (VM) using the v2 APIs: + + To create a server + + Import the following modules: + import time +from credentials import get_nova_credentials_v2 +from novaclient.client import Client + + + Get Nova Credentials. See . + + + Instantiate the nova_client + client object by using the + credentials dictionary + object: + +nova_client = Client(**credentials) + + + In this step, search for the flavor and image to be used for creating a server. + The following code assumes + cirros image and m1.tiny are being used. + image = nova_client.images.find(name="cirros") +flavor = nova_client.flavors.find(name="m1.tiny") + + + + image = nova_client.images.find(name="cirros") +flavor = nova_client.flavors.find(name="m1.tiny") + + + In this step determine the network with which the server is going to + be attached. Use this along with flavor and image to create the server. + + net_id = 'd05a7729-4bcf-4149-9d8f-6a4764520a04' +nic_d = [{'net-id': net_id}] +instance = nova_client.servers.create(name="vm2", image=image, + flavor=flavor, key_name="keypair-1", nics=nic_d) + + + Sleep for 5 secs and check if the server/vm got created by calling + nova_client.servers.list() + + print("Sleeping for 5s after create command") +time.sleep(5) +print("List of VMs") +print(nova_client.servers.list) + + + + Create server: complete code listing + #!/usr/bin/python +# -*- coding: utf-8 -*- +import time +from credentials import get_nova_credentials_v2 +from novaclient.client import Client + +try: + credentials = get_nova_credentials_v2() + nova_client = Client(**credentials) + + image = nova_client.images.find(name="cirros") + flavor = nova_client.flavors.find(name="m1.tiny") + net_id = 'd05a7729-4bcf-4149-9d8f-6a4764520a04' + nic_d = [{'net-id': net_id}] + instance = nova_client.servers.create(name="vm2", image=image, + flavor=flavor, key_name="keypair-1", nics=nic_d) + print("Sleeping for 5s after create command") + time.sleep(5) + print("List of VMs") + print(nova_client.servers.list()) +finally: + print("Execution Completed") + +
+
+ Delete server v2 + The following program deletes a Server (VM) using the v2 API: + + To Delete a Server + + Import the following modules: + import time +from credentials import get_nova_credentials_v2 +from novaclient.client import Client + + + Get Nova Credentials. See . + + + Instantiate the nova_client + client object by using the + credentials dictionary + object: + +nova_client = Client(**credentials) + + + Check if the server "vm1" exists using the following steps + + Get the list of servers: servers_list + Iterate over the servers_list and compare name with "vm1" + + If true set the variable name server_exists as + True and break from the for loop + + + servers_list = nova_client.servers.list() +server_del = "vm1" +server_exists = False + +for s in servers_list: + if s.name == server_del: + print("This server %s exists" % server_del) + server_exists = True + break + + + If the server exists execute delete method of + nova_client.servers object. + nova_client.servers.delete(s) + + + + + Delete: complete code listing + #!/usr/bin/python +# -*- coding: utf-8 -*- +from credentials import get_nova_credentials_v2 +from novaclient.client import Client + +credentials = get_nova_credentials_v2() +nova_client = Client(**credentials) + +servers_list = nova_client.servers.list() +server_del = "vm1" +server_exists = False + +for s in servers_list: + if s.name == server_del: + print("This server %s exists" % server_del) + server_exists = True + break +if not server_exists: + print("server %s does not exist" % server_del) +else: + print("deleting server..........") + nova_client.servers.delete(s) + print("server %s deleted" % server_del) + +
+ +