openstack-manuals/doc/user-guide/section_sdk_neutron.xml
Rajdeep Dua 3d6c408b9d Neutron API Section Added
Change-Id: I53d05ef30665f26226054baf2f7e71ef824ad943
2014-04-07 10:47:57 -05:00

152 lines
5.4 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_neutron_apis">
<title>Networking</title>
<?dbhtml stop-chunking?>
<para>To use the information in this section, you should
have a general understanding of OpenStack Networking,
OpenStack Compute, and the integration between the two. You
should also have access to a plug-in that implements the
Networking API v2.0.</para>
<section xml:id="sdk_neutron_env">
<title>Set environment variables</title>
<para>Make sure that you set the relevant environment variables
appropriately.</para>
<para>As an example, see the sample shell file that sets the
variables used to get credentials:</para>
<programlisting language="bash">export OS_USERNAME="<replaceable>admin</replaceable>"
export OS_PASSWORD="<replaceable>password</replaceable>"
export OS_TENANT_NAME="admin"
export OS_AUTH_URL="http://<replaceable>IPADDRESS</replaceable>/v2.0"</programlisting>
</section>
<section xml:id="sdk_neutron_common_code">
<title>Get credentials</title>
<para>The examples in this section use the following common <code>get_credentials</code> method:</para>
<programlisting language="python">def get_credentials():
d = {}
d['username'] = os.environ['OS_USERNAME']
d['password'] = os.environ['OS_PASSWORD']
d['auth_url'] = os.environ['OS_AUTH_URL']
d['tenant_name'] = os.environ['OS_TENANT_NAME']
return d</programlisting>
<para>This code is assumed to exist in the <filename>credentials.py</filename> file, which all
samples import.</para>
</section>
<?hard-pagebreak?>
<section xml:id="sdk_neutron_create_network">
<title>Create network</title>
<para>The following program creates a network:</para>
<programlisting language="python">from neutronclient.v2_0 import client
from credentials import get_credentials
from utils import print_networks
network_name = "sample_network"
credentials = get_credentials()
neutron = client.Client(**credentials)
try:
body_sample = {
"network":
{
"name": network_name,
"admin_state_up": True
}
}
netw = neutron.create_network(body=body_sample)
net_dict = netw['network']
network_id = net_dict['id']
print "Network %s created" % network_id
body_create_subnet = {
"subnets":[
{
"cidr":"192.168.199.0/24",
"ip_version":4,
"network_id": network_id
}
]
}
subnet = neutron.create_subnet(body=body_create_subnet)
print "Created subnet %s" % subnet
finally:
print "Execution completed"
#neutron.delete_network(network_id)
#print "Deleted Network %s" %network_id</programlisting>
</section>
<section xml:id="sdk_neutron_list_networks">
<title>List networks</title>
<para>The following program lists networks:</para>
<programlisting language="python">from neutronclient.v2_0 import client
from credentials import get_credentials
from utils import print_values
credentials = get_credentials()
neutron = client.Client(**credentials)
netw = neutron.list_networks()
print_values(netw, 'networks')</programlisting>
</section>
<section xml:id="sdk_neutron_create_port">
<title>Create port</title>
<para>The following program creates a port:</para>
<programlisting language="python">from neutronclient.v2_0 import client
import novaclient.v1_1.client as nvclient
from credentials import get_credentials
from utils import print_values_server
from credentials import get_nova_credentials
credentials = get_nova_credentials()
nova_client = nvclient.Client(**credentials)
#replace with server_id and network_id from your environment
server_id = '9a52795a-a70d-49a8-a5d0-5b38d78bd12d'
network_id = 'ce5d204a-93f5-43ef-bd89-3ab99ad09a9a'
server_detail = nova_client.servers.get(server_id)
print server_detail.id
if server_detail != None:
credentials = get_credentials()
neutron = client.Client(**credentials)
body_value = {
"port": {
"admin_state_up": True,
"device_id": server_id,
"name": "port1",
"network_id": network_id
}
}
response = neutron.create_port(body=body_value)
print response</programlisting>
</section>
<section xml:id="sdk_neutron_list_ports">
<title>List ports</title>
<para>The following program lists ports:</para>
<programlisting language="python">from neutronclient.v2_0 import client
from credentials import get_credentials
from utils import print_values
credentials = get_credentials()
neutron = client.Client(**credentials)
ports = neutron.list_ports()
print print_values(ports, 'ports')</programlisting>
</section>
<section xml:id="sdk_neutron_list_server_ports">
<title>List server ports</title>
<para>The following program lists the ports for a server:</para>
<programlisting language="python">from neutronclient.v2_0 import client
from credentials import get_credentials
from utils import print_values
credentials = get_credentials()
neutron = client.Client(**credentials)
ports = neutron.list_ports()
print print_values(ports, 'ports')</programlisting>
</section>
</section>