Change-Id: If421d82e4c3522a008054c31a2003e81d591bbd3
14 KiB
Compute
To use the information in this section, you must be familiar with OpenStack Compute.
Set environment variables
To set up environmental variables and authenticate against Compute
API endpoints, see sdk_authenticate
.
Get OpenStack credentials (API v2)
This example uses the get_nova_credentials_v2
method:
def get_nova_credentials_v2():
= {}
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']
d[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:
= get_nova_credentials_v2() credentials
List servers (API v2)
The following program lists servers by using the Compute API v2.
Import the following modules:
from credentials import get_nova_credentials_v2 from novaclient.client import Client
Get Nova credentials. See
Get OpenStack credentials (API v2) <get-openstack-credentials>
.Instantiate the
nova_client
client object by using thecredentials
dictionary object:= Client(**credentials) nova_client
List servers by calling
servers.list
onnova_client
object:print(nova_client.servers.list())
List server code listing example
#!/usr/bin/env python
from credentials import get_nova_credentials_v2
from novaclient.client import Client
= get_nova_credentials_v2()
credentials = Client(**credentials)
nova_client
print(nova_client.servers.list())
Create server (API v2)
The following program creates a server (VM) by using the Compute API v2.
Import the following modules:
import time from credentials import get_nova_credentials_v2 from novaclient.client import Client
Get OpenStack credentials. See
Get OpenStack credentials (API v2) <get-openstack-credentials>
.Instantiate the
nova_client
client object by using thecredentials
dictionary object:= Client(**credentials) nova_client
Get the flavor and image to use to create a server. This code uses the
cirros
image, them1.tiny
flavor, and theprivate
network:= nova_client.images.find(name="cirros") image = nova_client.flavors.find(name="m1.tiny") flavor = nova_client.networks.find(label="private") net
To create the server, use the network, image, and flavor:
= [{'net-id': net.id}] nics = nova_client.servers.create(name="vm2", image=image, instance =flavor, key_name="keypair-1", nics=nics) flavor
Run the "Sleep for five seconds" command, and determine whether the server/vm was created by calling
nova_client.servers.list()
:print("Sleeping for 5s after create command") 5) time.sleep(print("List of VMs") print(nova_client.servers.list())
Create server code listing example
#!/usr/bin/env python
import time
from credentials import get_nova_credentials_v2
from novaclient.client import Client
try:
= get_nova_credentials_v2()
credentials = Client(**credentials)
nova_client
= nova_client.images.find(name="cirros")
image = nova_client.flavors.find(name="m1.tiny")
flavor = nova_client.networks.find(label="private")
net = [{'net-id': net.id}]
nics = nova_client.servers.create(name="vm2", image=image,
instance =flavor, key_name="keypair-1", nics=nics)
flavorprint("Sleeping for 5s after create command")
5)
time.sleep(print("List of VMs")
print(nova_client.servers.list())
finally:
print("Execution Completed")
Delete server (API v2)
The following program deletes a server (VM) by using the Compute API v2.
Import the following modules:
import time from credentials import get_nova_credentials_v2 from novaclient.client import Client
Get Nova credentials. See
Get OpenStack credentials (API v2) <get-openstack-credentials>
.Instantiate the
nova_client
client object by using thecredentials
dictionary object:= Client(**credentials) nova_client
Determine whether the
vm1
server exists:- List servers:
servers_list
. - Iterate over
servers_list
and compare name withvm1
. - If true, set the variable name
server_exists
toTrue
and break from the for loop:
= nova_client.servers.list() servers_list = "vm1" server_del = False server_exists for s in servers_list: if s.name == server_del: print("This server %s exists" % server_del) = True server_exists break
- List servers:
If the server exists, run the
delete
method of thenova_client.servers
object:nova_client.servers.delete(s)
Delete server code example
#!/usr/bin/env python
from credentials import get_nova_credentials_v2
from novaclient.client import Client
= get_nova_credentials_v2()
credentials = Client(**credentials)
nova_client
= nova_client.servers.list()
servers_list = "vm1"
server_del = False
server_exists
for s in servers_list:
if s.name == server_del:
print("This server %s exists" % server_del)
= True
server_exists 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)
Update server (API v2)
The following program updates the name of a server (VM) by using the Compute API v2.
Import the following modules:
from credentials import get_nova_credentials_v2 from novaclient.client import Client from utils import print_server
print_server
is a method defined inutils.py
and prints the server details as shown in the code listing below:def print_server(server): print("-"*35) print("server id: %s" % server.id) print("server name: %s" % server.name) print("server image: %s" % server.image) print("server flavor: %s" % server.flavor) print("server key name: %s" % server.key_name) print("user_id: %s" % server.user_id) print("-"*35)
Get OpenStack Credentials. See
Get OpenStack credentials (API v2) <get-openstack-credentials>
.Instantiate the
nova_client
client object by using thecredentials
dictionary object:= Client(**credentials) nova_client
Get the server instance using
server_id
and print the details by callingprint_server
method:= '99889c8d-113f-4a7e-970c-77f1916bfe14' server_id = nova_client.servers.get(server_id) server = server.name n print_server(server)
Call
server.update
on the server object with the new value forname
variable:= n + '1') server.update(name
Get the updated instance of the server:
= nova_client.servers.get(server_id) server_updated
Call
print_server
again to check the update server details:print_server(server_updated)
Update server code listing example
#!/usr/bin/env python
from credentials import get_nova_credentials_v2
from novaclient.client import Client
from utils import print_server
= get_nova_credentials_v2()
credentials = Client(**credentials)
nova_client
# Change the server_id specific to your environment
= '99889c8d-113f-4a7e-970c-77f1916bfe14'
server_id = nova_client.servers.get(server_id)
server = server.name
n
print_server(server)
=n +'1')
server.update(name= nova_client.servers.get(server_id)
server_updated print_server(server_updated)
List flavors (API v2)
The following program lists flavors and their details by using the Compute API v2.
Import the following modules:
from credentials import get_nova_credentials_v2 from novaclient.client import Client from utils import print_flavors
The
print_flavors
method is defined inutils.py
and prints the flavor details:def print_flavors(flavor_list): for flavor in flavor_list: print("-"*35) print("flavor id : %s" % flavor.id) print("flavor name : %s" % flavor.name) print("-"*35)
Get OpenStack credentials.
Get OpenStack credentials (API v2) <get-openstack-credentials>
.Instantiate the
nova_client
client object by using thecredentials
dictionary object:= Client(**credentials) nova_client
List flavors by calling
list()
onnova_client.flavors
object:= nova_client.flavors.list() flavors_list
Print the flavor details, id and name by calling
print_flavors
:print_flavors(flavors_list)
List flavors code listing example
#!/usr/bin/env python
from credentials import get_nova_credentials_v2
from novaclient.client import Client
from utils import print_flavors
= get_nova_credentials_v2()
credentials = Client(**credentials)
nova_client
= nova_client.flavors.list()
flavors_list print_flavors(flavors_list)
List floating IPs (API v2)
The following program lists the floating IPs and their details by using the Compute API v2.
Import the following modules:
from credentials import get_nova_credentials_v2 from novaclient.client import Client from utils import print_values_ip
The
print_values_ip
method is defined inutils.py
and prints the floating_ip object details:def print_values_ip(ip_list): = [] ip_dict_lisl for ip in ip_list: print("-"*35) print("fixed_ip : %s" % ip.fixed_ip) print("id : %s" % ip.id) print("instance_id : %s" % ip.instance_id) print("ip : %s" % ip.ip) print("pool : %s" % ip.pool)
Get OpenStack credentials. See
Get OpenStack credentials (API v2) <get-openstack-credentials>
.Instantiate the
nova_client
client object by using thecredentials
dictionary object:= Client(**credentials) nova_client
List floating IPs by calling
list()
onnova_client.floating_ips
object:= nova_client.floating_ips.list() ip_list
Print the floating IP object details by calling
print_values_ip
:print_values_ip(ip_list)
List floating IPs code listing example
#!/usr/bin/env python
from credentials import get_nova_credentials_v2
from novaclient.client import Client
from utils import print_values_ip
= get_nova_credentials_v2()
credentials = Client(**credentials)
nova_client = nova_client.floating_ips.list()
ip_list print_values_ip(ip_list)
List hosts (API v2)
The following program lists the hosts by using the Compute API v2.
Import the following modules:
from credentials import get_nova_credentials_v2 from novaclient.client import Client from utils import print_hosts
The
print_hosts
method is defined inutils.py
and prints the host object details:def print_hosts(host_list): for host in host_list: print("-"*35) print("host_name : %s" % host.host_name) print("service : %s" % host.service) print("zone : %s" % host.zone) print("-"*35)
Get OpenStack credentials. See
Get OpenStack credentials (API v2) <get-openstack-credentials>
.Instantiate the
nova_client
client object by using thecredentials
dictionary object:= Client(**credentials) nova_client
List hosts by calling
list()
onnova_client.hosts
object:= nova_client.hosts.list() host_list
Print the host object details by calling
print_hosts(host_list)
:print_hosts(host_list)
List hosts code listing example
#!/usr/bin/env python
from credentials import get_nova_credentials_v2
from novaclient.client import Client
from utils import print_hosts
= get_nova_credentials_v2()
credentials = Client(**credentials)
nova_client = nova_client.hosts.list()
host_list
print_hosts(host_list)