To use the information in this section, you should have a general understanding of OpenStack Compute.
Please see the section called “Authenticate” on how to setup environmental variables and authenticate against Compute API endpoints.
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()
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 the section called “Get nova credentials v2”.
Instantiate the
nova_client
client object by using thecredentials
dictionary object:nova_client = Client(**credentials)
Get the list of servers by calling
servers.list
onnova_client
object:print(nova_client.servers.list())
Example 3.6. 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())
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 the section called “Get nova credentials v2”.
Instantiate the
nova_client
client object by using thecredentials
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 andm1.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)
Example 3.7. 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")
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 the section called “Get nova credentials v2”.
Instantiate the
nova_client
client object by using thecredentials
dictionary object:nova_client = Client(**credentials)
Check if the server
"vm1"
exists using the following stepsGet the list of servers:
servers_list
Iterate over the
servers_list
and compare name with"vm1"
If true set the variable name
server_exists
asTrue
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 ofnova_client.servers
object.nova_client.servers.delete(s)
Example 3.8. 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)