To use the information in this section, you must be familiar with OpenStack Compute.
This example uses 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 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).
Instantiate the nova_client
client object by using the
credentials
dictionary object:
nova_client = Client(**credentials)
List servers by calling servers.list
on nova_client
object:
print(nova_client.servers.list())
#!/usr/bin/env python
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) 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).
Instantiate the nova_client
client object by using the
credentials
dictionary object:
nova_client = Client(**credentials)
Get the flavor and image to use to create a server. This code uses
the cirros
image, the m1.tiny
flavor, and the private
network:
image = nova_client.images.find(name="cirros")
flavor = nova_client.flavors.find(name="m1.tiny")
net = nova_client.networks.find(label="private")
To create the server, use the network, image, and flavor:
nics = [{'net-id': net.id}]
instance = nova_client.servers.create(name="vm2", image=image,
flavor=flavor, key_name="keypair-1", nics=nics)
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")
time.sleep(5)
print("List of VMs")
print(nova_client.servers.list())
#!/usr/bin/env python
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 = nova_client.networks.find(label="private")
nics = [{'net-id': net.id}]
instance = nova_client.servers.create(name="vm2", image=image,
flavor=flavor, key_name="keypair-1", nics=nics)
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) 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).
Instantiate the nova_client
client object by using the
credentials
dictionary object:
nova_client = Client(**credentials)
Determine whether the vm1
server exists:
servers_list
.servers_list
and compare name with vm1
.server_exists
to 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, run the delete
method of the
nova_client.servers
object:
nova_client.servers.delete(s)
#!/usr/bin/env python
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)
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 in utils.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).
Instantiate the nova_client
client object by using the
credentials
dictionary object:
nova_client = Client(**credentials)
Get the server instance using server_id
and print the details by
calling print_server
method:
server_id = '99889c8d-113f-4a7e-970c-77f1916bfe14'
server = nova_client.servers.get(server_id)
n = server.name
print_server(server)
Call server.update
on the server object with the new value for
name
variable:
server.update(name = n + '1')
Get the updated instance of the server:
server_updated = nova_client.servers.get(server_id)
Call print_server
again to check the update server details:
print_server(server_updated)
#!/usr/bin/env python
from credentials import get_nova_credentials_v2
from novaclient.client import Client
from utils import print_server
credentials = get_nova_credentials_v2()
nova_client = Client(**credentials)
# Change the server_id specific to your environment
server_id = '99889c8d-113f-4a7e-970c-77f1916bfe14'
server = nova_client.servers.get(server_id)
n = server.name
print_server(server)
server.update(name=n +'1')
server_updated = nova_client.servers.get(server_id)
print_server(server_updated)
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 in utils.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).
Instantiate the nova_client
client object by using the
credentials
dictionary object:
nova_client = Client(**credentials)
List flavors by calling list()
on nova_client.flavors
object:
flavors_list = nova_client.flavors.list()
Print the flavor details, id and name by calling print_flavors
:
print_flavors(flavors_list)
#!/usr/bin/env python
from credentials import get_nova_credentials_v2
from novaclient.client import Client
from utils import print_flavors
credentials = get_nova_credentials_v2()
nova_client = Client(**credentials)
flavors_list = nova_client.flavors.list()
print_flavors(flavors_list)
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 in utils.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).
Instantiate the nova_client
client object by using the
credentials
dictionary object:
nova_client = Client(**credentials)
List floating IPs by calling list()
on nova_client.floating_ips
object:
ip_list = nova_client.floating_ips.list()
Print the floating IP object details by calling print_values_ip
:
print_values_ip(ip_list)
#!/usr/bin/env python
from credentials import get_nova_credentials_v2
from novaclient.client import Client
from utils import print_values_ip
credentials = get_nova_credentials_v2()
nova_client = Client(**credentials)
ip_list = nova_client.floating_ips.list()
print_values_ip(ip_list)
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 in utils.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).
Instantiate the nova_client
client object by using the
credentials
dictionary object:
nova_client = Client(**credentials)
通过调用``nova_client.hosts``对象上的``list()``方法列出主机:
host_list = nova_client.hosts.list()
通过调用``print_hosts(host_list)``打印出主机对象详情:
print_hosts(host_list)
#!/usr/bin/env python
from credentials import get_nova_credentials_v2
from novaclient.client import Client
from utils import print_hosts
credentials = get_nova_credentials_v2()
nova_client = Client(**credentials)
host_list = nova_client.hosts.list()
print_hosts(host_list)
Except where otherwise noted, this document is licensed under Creative Commons Attribution 3.0 License. See all OpenStack Legal Documents.