2db999438d
- structural XMLfixes Change-Id: Ieebe357de841356b40c5f2456763e69e6514d672 Closes-Bug: #1419015 Closes-Bug: #1418998 Closes-Bug: #1419003
517 lines
22 KiB
XML
517 lines
22 KiB
XML
<?xml version="1.0" encoding="UTF-8"?>
|
|
<section xmlns="http://docbook.org/ns/docbook"
|
|
xmlns:xi="http://www.w3.org/2001/XInclude"
|
|
xmlns:xlink="http://www.w3.org/1999/xlink"
|
|
version="5.0"
|
|
xml:id="sdk_compute_apis">
|
|
<title>Compute</title>
|
|
<?dbhtml stop-chunking?>
|
|
<para>To use the information in this section, you must be familiar with OpenStack Compute.</para>
|
|
<section xml:id="sdk_compute_env">
|
|
<title>Set environment variables</title>
|
|
<para>To set up environmental variables and authenticate
|
|
against Compute API endpoints, see <xref
|
|
linkend="sdk_auth"/>.</para>
|
|
</section>
|
|
<section xml:id="sdk_compute_get_openstack_credentials_v2">
|
|
<title>Get OpenStack credentials (API v2)</title>
|
|
<para>These example use the
|
|
<code>get_nova_credentials_v2</code> method:</para>
|
|
<programlisting language="python">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</programlisting>
|
|
<para>This code resides in the
|
|
<filename>credentials.py</filename> file, which all
|
|
samples import.</para>
|
|
<para>Use the <code>get_nova_credentials_v2()</code> method to
|
|
populate and get a dictionary:</para>
|
|
<programlisting language="python">credentials = get_nova_credentials_v2()</programlisting>
|
|
</section>
|
|
|
|
<section xml:id="sdk_compute_list_servers">
|
|
<title>List servers (API v2)</title>
|
|
<para>The following program lists servers by using the Compute
|
|
API v2.</para>
|
|
<procedure>
|
|
<title>To list servers</title>
|
|
<step>
|
|
<para>Import the following modules:</para>
|
|
<programlisting language="python">from credentials import get_nova_credentials_v2
|
|
from novaclient.client import Client</programlisting>
|
|
</step>
|
|
<step>
|
|
<para>Get Nova credentials. See <xref
|
|
linkend="sdk_compute_get_openstack_credentials_v2"
|
|
/>.</para>
|
|
</step>
|
|
<step>
|
|
<para>Instantiate the <literal>nova_client</literal>
|
|
client object by using the
|
|
<literal>credentials</literal> dictionary
|
|
object:</para>
|
|
<programlisting language="python">nova_client = Client(**credentials)</programlisting>
|
|
</step>
|
|
<step>
|
|
<para>List servers by calling
|
|
<literal>servers.list</literal> on
|
|
<literal>nova_client</literal> object:</para>
|
|
<programlisting language="python">print(nova_client.servers.list())</programlisting>
|
|
</step>
|
|
</procedure>
|
|
<example>
|
|
<title>List servers code listing</title>
|
|
<programlisting language="python">#!/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())</programlisting>
|
|
</example>
|
|
</section>
|
|
<section xml:id="sdk_compute_create_server_v2">
|
|
<title>Create server (API v2)</title>
|
|
<para>The following program creates a server (VM) by using the
|
|
Compute API v2.</para>
|
|
<procedure>
|
|
<title>To create a server</title>
|
|
<step>
|
|
<para>Import the following modules:</para>
|
|
<programlisting language="python">import time
|
|
from credentials import get_nova_credentials_v2
|
|
from novaclient.client import Client</programlisting>
|
|
</step>
|
|
<step>
|
|
<para>Get OpenStack credentials. See <xref
|
|
linkend="sdk_compute_get_openstack_credentials_v2"
|
|
/>.</para>
|
|
</step>
|
|
<step>
|
|
<para>Instantiate the <literal>nova_client</literal>
|
|
client object by using the
|
|
<literal>credentials</literal> dictionary
|
|
object:</para>
|
|
<programlisting language="python">nova_client = Client(**credentials)</programlisting>
|
|
</step>
|
|
<step>
|
|
<para>Get the flavor and image to use to create a
|
|
server. This code uses the
|
|
<literal>cirros</literal> image, the
|
|
<literal>m1.tiny</literal> flavor, and the
|
|
<literal>private</literal> network:</para>
|
|
<programlisting language="python">image = nova_client.images.find(name="cirros")
|
|
flavor = nova_client.flavors.find(name="m1.tiny")
|
|
net = nova_client.networks.find(label="private")</programlisting>
|
|
</step>
|
|
<step>
|
|
<para>To create the server, use the network, image, and
|
|
flavor:</para>
|
|
<programlisting language="python">nics = [{'net-id': net.id}]
|
|
instance = nova_client.servers.create(name="vm2", image=image,
|
|
flavor=flavor, key_name="keypair-1", nics=nics)</programlisting>
|
|
</step>
|
|
<step>
|
|
<para>Sleep for five seconds and determine whether the
|
|
server/vm was created by calling
|
|
<literal>nova_client.servers.list()</literal>:</para>
|
|
<programlisting language="python">print("Sleeping for 5s after create command")
|
|
time.sleep(5)
|
|
print("List of VMs")
|
|
print(nova_client.servers.list())</programlisting>
|
|
</step>
|
|
</procedure>
|
|
<example>
|
|
<title>Create server code listing</title>
|
|
<programlisting language="python">#!/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")</programlisting>
|
|
</example>
|
|
</section>
|
|
<section xml:id="sdk_compute_delete_server_v2">
|
|
<title>Delete server (API v2)</title>
|
|
<para>The following program deletes a server (VM) by using the
|
|
Compute API v2.</para>
|
|
<procedure>
|
|
<title>To delete a server</title>
|
|
<step>
|
|
<para>Import the following modules:</para>
|
|
<programlisting language="python">import time
|
|
from credentials import get_nova_credentials_v2
|
|
from novaclient.client import Client</programlisting>
|
|
</step>
|
|
<step>
|
|
<para>Get Nova credentials. See <xref
|
|
linkend="sdk_compute_get_openstack_credentials_v2"
|
|
/>.</para>
|
|
</step>
|
|
<step>
|
|
<para>Instantiate the <literal>nova_client</literal>
|
|
client object by using the
|
|
<literal>credentials</literal> dictionary
|
|
object:</para>
|
|
<programlisting language="python">nova_client = Client(**credentials)</programlisting>
|
|
</step>
|
|
<step>
|
|
<para>Determine whether the <literal>vm1</literal>
|
|
server exists:</para>
|
|
<substeps>
|
|
<step>
|
|
<para>List servers:
|
|
<literal>servers_list</literal>.</para>
|
|
</step>
|
|
<step>
|
|
<para>Iterate over
|
|
<literal>servers_list</literal> and
|
|
compare name with
|
|
<literal>vm1</literal>.</para>
|
|
</step>
|
|
<step>
|
|
<para>If true, set the variable name
|
|
<literal>server_exists</literal> to
|
|
<literal>True</literal> and break from
|
|
the for loop:</para>
|
|
<programlisting language="python">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</programlisting>
|
|
</step>
|
|
</substeps>
|
|
</step>
|
|
<step>
|
|
<para>If the server exists, run the
|
|
<literal>delete</literal> method of the
|
|
<literal>nova_client.servers</literal>
|
|
object:</para>
|
|
<programlisting language="python">nova_client.servers.delete(s)</programlisting>
|
|
</step>
|
|
|
|
</procedure>
|
|
<example>
|
|
<title>Delete server code listing</title>
|
|
<programlisting language="python">#!/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)</programlisting>
|
|
</example>
|
|
</section>
|
|
<section xml:id="sdk_update_server_v2">
|
|
<title>Update server (API v2)</title>
|
|
<para>The following program updates the name of a server (VM)
|
|
by using the Compute API v2.</para>
|
|
<procedure>
|
|
<title>To update a server</title>
|
|
<step>
|
|
<para>Import the following modules:</para>
|
|
<programlisting language="python">from credentials import get_nova_credentials_v2
|
|
from novaclient.client import Client
|
|
from utils import print_server</programlisting>
|
|
<para><literal>print_server</literal> is a method
|
|
defined in <filename>utils.py</filename> and
|
|
prints the server details as shown in the code
|
|
listing below:</para>
|
|
<programlisting language="python">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)</programlisting>
|
|
</step>
|
|
<step>
|
|
<para>Get OpenStack Credentials. See <xref
|
|
linkend="sdk_compute_get_openstack_credentials_v2"
|
|
/>.</para>
|
|
</step>
|
|
<step>
|
|
<para>Instantiate the <literal>nova_client</literal>
|
|
client object by using the
|
|
<literal>credentials</literal> dictionary
|
|
object:</para>
|
|
<programlisting language="python">nova_client = Client(**credentials)</programlisting>
|
|
</step>
|
|
<step>
|
|
<para>Get the server instance using
|
|
<literal>server_id</literal> and print the
|
|
details by calling <literal>print_server</literal>
|
|
method:</para>
|
|
<programlisting language="python">server_id = '99889c8d-113f-4a7e-970c-77f1916bfe14'
|
|
server = nova_client.servers.get(server_id)
|
|
n = server.name
|
|
print_server(server)</programlisting>
|
|
</step>
|
|
<step>
|
|
<para>Call <code>server.update</code> on the server
|
|
object with the new value for
|
|
<literal>name</literal> variable:</para>
|
|
<programlisting language="python">server.update(name = n + '1')</programlisting>
|
|
</step>
|
|
<step>
|
|
<para>Get the updated instance of the server:</para>
|
|
<programlisting language="python">server_updated = nova_client.servers.get(server_id)</programlisting>
|
|
</step>
|
|
<step>
|
|
<para>Call <literal>print_server</literal> again to
|
|
check the update server details:</para>
|
|
<programlisting language="python">print_server(server_updated)</programlisting>
|
|
</step>
|
|
|
|
</procedure>
|
|
<example>
|
|
<title>Update server code listing</title>
|
|
<programlisting language="python">#!/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)</programlisting>
|
|
</example>
|
|
</section>
|
|
<section xml:id="list_flavors_v2">
|
|
<title>List flavors (API v2)</title>
|
|
<para>The following program lists flavors and their details by
|
|
using the Compute API v2.</para>
|
|
<procedure>
|
|
<title>To list flavors</title>
|
|
<step>
|
|
<para>Import the following modules:</para>
|
|
<programlisting language="python">from credentials import get_nova_credentials_v2
|
|
from novaclient.client import Client
|
|
from utils import print_flavors</programlisting>
|
|
<para>The <literal>print_flavors</literal> method is
|
|
defined in <filename>utils.py</filename> and
|
|
prints the flavor details:</para>
|
|
<programlisting language="python">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)</programlisting>
|
|
</step>
|
|
<step>
|
|
<para>Get OpenStack credentials. See <xref
|
|
linkend="sdk_compute_get_openstack_credentials_v2"
|
|
/>.</para>
|
|
</step>
|
|
<step>
|
|
<para>Instantiate the <literal>nova_client</literal>
|
|
client object by using the
|
|
<literal>credentials</literal> dictionary
|
|
object:</para>
|
|
<programlisting language="python">nova_client = Client(**credentials)</programlisting>
|
|
</step>
|
|
<step>
|
|
<para>List flavors by calling
|
|
<literal>list()</literal> on
|
|
<literal>nova_client.flavors</literal>
|
|
object:</para>
|
|
<programlisting language="python">flavors_list = nova_client.flavors.list()</programlisting>
|
|
</step>
|
|
<step>
|
|
<para>Print the flavor details, id and name by calling
|
|
<literal>print_flavors</literal>:</para>
|
|
<programlisting language="python">print_flavors(flavors_list)</programlisting>
|
|
</step>
|
|
</procedure>
|
|
<example>
|
|
<title>List flavors code listing</title>
|
|
<programlisting language="python">#!/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)</programlisting>
|
|
</example>
|
|
</section>
|
|
<section xml:id="list_floating_ips_python_client">
|
|
<title>List floating IPs (API v2)</title>
|
|
<para>The following program lists the floating IPs and their
|
|
details by using the Compute API v2.</para>
|
|
<procedure>
|
|
<title>To list floating IPs</title>
|
|
<step>
|
|
<para>Import the following modules:</para>
|
|
<programlisting language="python">from credentials import get_nova_credentials_v2
|
|
from novaclient.client import Client
|
|
from utils import print_values_ip</programlisting>
|
|
<para>The <literal>print_values_ip</literal> method is
|
|
defined in <filename>utils.py</filename> and
|
|
prints the floating_ip object details:</para>
|
|
<programlisting language="python">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)</programlisting>
|
|
</step>
|
|
<step>
|
|
<para>Get OpenStack credentials. See <xref
|
|
linkend="sdk_compute_get_openstack_credentials_v2"
|
|
/>.</para>
|
|
</step>
|
|
<step>
|
|
<para>Instantiate the <literal>nova_client</literal>
|
|
client object by using the
|
|
<literal>credentials</literal> dictionary
|
|
object:</para>
|
|
<programlisting language="python">nova_client = Client(**credentials)</programlisting>
|
|
</step>
|
|
<step>
|
|
<para>List floating IPs by calling
|
|
<literal>list()</literal> on
|
|
<literal>nova_client.floating_ips</literal>
|
|
object:</para>
|
|
<programlisting language="python">ip_list = nova_client.floating_ips.list()</programlisting>
|
|
</step>
|
|
<step>
|
|
<para>Print the floating IP object details by calling
|
|
<literal>print_values_ip</literal>:</para>
|
|
<programlisting language="python">print_values_ip(ip_list)</programlisting>
|
|
</step>
|
|
</procedure>
|
|
<example>
|
|
<title>List floating IPs code listing</title>
|
|
<programlisting language="python">#!/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)</programlisting>
|
|
</example>
|
|
</section>
|
|
<section xml:id="list_hosts_v2">
|
|
<title>List hosts (API v2)</title>
|
|
<para>The following program lists the hosts by
|
|
using the Compute API v2.</para>
|
|
<procedure>
|
|
<title>To list hosts</title>
|
|
<step>
|
|
<para>Import the following modules:</para>
|
|
<programlisting language="python">from credentials import get_nova_credentials_v2
|
|
from novaclient.client import Client
|
|
from utils import print_hosts</programlisting>
|
|
<para>The <literal>print_hosts</literal> method is
|
|
defined in <filename>utils.py</filename> and
|
|
prints the host object details:</para>
|
|
<programlisting language="python">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)</programlisting>
|
|
</step>
|
|
<step>
|
|
<para>Get OpenStack credentials. See <xref
|
|
linkend="sdk_compute_get_openstack_credentials_v2"
|
|
/>.</para>
|
|
</step>
|
|
<step>
|
|
<para>Instantiate the <literal>nova_client</literal>
|
|
client object by using the
|
|
<literal>credentials</literal> dictionary
|
|
object:</para>
|
|
<programlisting language="python">nova_client = Client(**credentials)</programlisting>
|
|
</step>
|
|
<step>
|
|
<para>List hosts by calling <literal>list()</literal>
|
|
on <literal>nova_client.hosts</literal>
|
|
object:</para>
|
|
<programlisting language="python">host_list = nova_client.hosts.list()</programlisting>
|
|
</step>
|
|
<step>
|
|
<para>Print the host object details by calling
|
|
<literal>print_hosts(host_list)</literal>:</para>
|
|
<programlisting language="python">print_hosts(host_list)</programlisting>
|
|
</step>
|
|
</procedure>
|
|
<example>
|
|
<title>List hosts code listing</title>
|
|
<programlisting language="python">#!/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)</programlisting>
|
|
</example>
|
|
</section>
|
|
</section>
|