このセクションにある情報を利用するために、OpenStack Networking、OpenStack Compute、それらの統合について基本を理解しておくべきです。また、Networking API v2.0 を実装するプラグインへのアクセス権を持つべきです。
関連する環境変数をきちんと設定します。
例として、クレデンシャルを取得するために、これらの変数を設定するサンプルシェルファイルを参照してください。
export OS_USERNAME="admin"
export OS_PASSWORD="password"
export OS_TENANT_NAME="admin"
export OS_AUTH_URL="http://IPADDRESS/v2.0"
このセクションの例は get_credentials
メソッドを使用します。
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
このコードは、credentials.py
ファイルにあり、ここからすべてのサンプルがインポートされます。
get_credentials()
メソッドを使用して、辞書を生成し、取得します。
credentials = get_credentials()
このセクションの例は get_nova_credentials
メソッドを使用します。
def get_nova_credentials():
d = {}
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
このコードは、credentials.py
ファイルにあり、ここからすべてのサンプルがインポートされます。
get_nova_credentials()
メソッドを使用して、辞書を生成し、取得します。
nova_credentials = get_nova_credentials()
このセクションの例は print_values
メソッドと print_values_server
メソッドを使用します。
def print_values(val, type):
if type == 'ports':
val_list = val['ports']
if type == 'networks':
val_list = val['networks']
if type == 'routers':
val_list = val['routers']
for p in val_list:
for k, v in p.items():
print("%s : %s" % (k, v))
print('\n')
def print_values_server(val, server_id, type):
if type == 'ports':
val_list = val['ports']
if type == 'networks':
val_list = val['networks']
for p in val_list:
bool = False
for k, v in p.items():
if k == 'device_id' and v == server_id:
bool = True
if bool:
for k, v in p.items():
print("%s : %s" % (k, v))
print('\n')
このコードは、utils.py
ファイルにあり、ここからすべてのサンプルがインポートされます。
以下のプログラムはネットワークを作成します。
#!/usr/bin/env python
from neutronclient.v2_0 import client
from credentials import get_credentials
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")
以下のプログラムはネットワークを一覧表示します。
#!/usr/bin/env 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')
print_values
は Print values を参照してください。
以下のプログラムはポートを作成します。
#!/usr/bin/env python
from neutronclient.v2_0 import client
import novaclient.v2.client as nvclient
from credentials import get_credentials
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)
get_nova_credentials
は Get Nova credentials を参照してください。
get_credentials
は Get credentials を参照してください。
以下のプログラムはポートを一覧表示します。
#!/usr/bin/env 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_values(ports, 'ports')
get_credentials
は Get credentials を参照してください。
print_values
は Print values を参照してください。
以下のプログラムがサーバーのポートを一覧表示します。
#!/usr/bin/env python
from neutronclient.v2_0 import client
import novaclient.v2.client as nvclient
from credentials import get_credentials
from credentials import get_nova_credentials
from utils import print_values_server
credentials = get_nova_credentials()
nova_client = nvclient.Client(**credentials)
# change these values according to 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 is not None:
credentials = get_credentials()
neutron = client.Client(**credentials)
ports = neutron.list_ports()
print_values_server(ports, server_id, 'ports')
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)
この例は、ルーターを作成し、サブネットにポートを追加するために、OpenStack Networking にクエリーを発行します。
以下のモジュールをインポートします。
from neutronclient.v2_0 import client
import novaclient.v2.client as nvclient
from credentials import get_credentials
from credentials import get_nova_credentials
from utils import print_values_server
Nova クレデンシャルを取得します。:ref:』Get Nova credentials <get-nova-credentials>』 を参照してください。
credentials
辞書オブジェクトを使用して、nova_client
クライアントオブジェクトをインスタンス化します。
nova_client = nvclient.Client(**credentials)
ルーターを作成し、サブネットにポートを追加します。
# Replace with network_id from your environment
network_id = '81bf592a-9e3f-4f84-a839-ae87df188dc1'
credentials = get_credentials()
neutron = client.Client(**credentials)
neutron.format = json
request = {'router': {'name': 'router name',
'admin_state_up': True}}
router = neutron.create_router(request)
router_id = router['router']['id']
# for example: '72cf1682-60a8-4890-b0ed-6bad7d9f5466'
router = neutron.show_router(router_id)
print(router)
body_value = {'port': {
'admin_state_up': True,
'device_id': router_id,
'name': 'port1',
'network_id': network_id,
}}
response = neutron.create_port(body=body_value)
print(response)
print("Execution Completed")
#!/usr/bin/env python
from neutronclient.v2_0 import client
import novaclient.v2.client as nvclient
from credentials import get_credentials
from credentials import get_nova_credentials
from utils import print_values_server
credentials = get_nova_credentials()
nova_client = nvclient.Client(**credentials)
# Replace with network_id from your environment
network_id = '81bf592a-9e3f-4f84-a839-ae87df188dc1'
try:
credentials = get_credentials()
neutron = client.Client(**credentials)
neutron.format = 'json'
request = {'router': {'name': 'router name',
'admin_state_up': True}}
router = neutron.create_router(request)
router_id = router['router']['id']
# for example: '72cf1682-60a8-4890-b0ed-6bad7d9f5466'
router = neutron.show_router(router_id)
print(router)
body_value = {'port': {
'admin_state_up': True,
'device_id': router_id,
'name': 'port1',
'network_id': network_id,
}}
response = neutron.create_port(body=body_value)
print(response)
finally:
print("Execution completed")
この例は、ネットワークを削除するために、OpenStack Networking にクエリーを発行します。
ネットワークの削除手順:
以下のモジュールをインポートします。
from neutronclient.v2_0 import client
from credentials import get_credentials
クレデンシャルを取得します。Get Nova credentials を参照してください。
credentials
辞書オブジェクトを使用して、neutron
クライアントオブジェクトをインスタンス化します。
neutron = client.Client(**credentials)
ネットワークを削除します。
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)
neutron.delete_network(network_id)
print('Deleted Network %s' % network_id)
print("Execution completed")
#!/usr/bin/env python
from neutronclient.v2_0 import client
from credentials import get_credentials
network_name = 'temp_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)
neutron.delete_network(network_id)
print('Deleted Network %s' % network_id)
finally:
print("Execution Completed")
この例は、すべてのルーターを一覧表示するために、OpenStack Networking にクエリーを発行します。
以下のモジュールをインポートします。
from neutronclient.v2_0 import client
from credentials import get_credentials
from utils import print_values
クレデンシャルを取得します。Get Nova credentials を参照してください。
credentials
辞書オブジェクトを使用して、neutron
クライアントオブジェクトをインスタンス化します。
neutron = client.Client(**credentials)
ルーターを一覧表示します。
routers_list = neutron.list_routers(retrieve_all=True)
print_values(routers_list, 'routers')
print("Execution completed")
print_values
は Print values を参照してください。
#!/usr/bin/env python
from neutronclient.v2_0 import client
from credentials import get_credentials
from utils import print_values
try:
credentials = get_credentials()
neutron = client.Client(**credentials)
routers_list = neutron.list_routers(retrieve_all=True)
print_values(routers_list, 'routers')
finally:
print("Execution completed")
この例は、セキュリティーグループを一覧表示するために、OpenStack Networking にクエリーを発行します。
以下のモジュールをインポートします。
from neutronclient.v2_0 import client
from credentials import get_credentials
from utils import print_values
クレデンシャルを取得します。Get credentials を参照してください。
credentials
辞書オブジェクトを使用して、neutron
クライアントオブジェクトをインスタンス化します。
neutron = client.Client(**credentials)
セキュリティーグループの一覧表示
sg = neutron.list_security_groups()
print(sg)
#!/usr/bin/env python
from neutronclient.v2_0 import client
from credentials import get_credentials
from utils import print_values
credentials = get_credentials()
neutron = client.Client(**credentials)
sg = neutron.list_security_groups()
print(sg)
注釈
OpenStack Networking のセキュリティーグループでは大文字小文字は区別されますが、 nova-network では区別されません。
この例は、サブネットを一覧表示するために、OpenStack Networking にクエリーを発行します。
以下のモジュールをインポートします。
from neutronclient.v2_0 import client
from credentials import get_credentials
from utils import print_values
クレデンシャルを取得します。:ref:』Get credentials <get-credentials>』 を参照してください。
credentials
辞書オブジェクトを使用して、neutron
クライアントオブジェクトをインスタンス化します。
neutron = client.Client(**credentials)
サブネットを一覧表示します。
subnets = neutron.list_subnets()
print(subnets)
#!/usr/bin/env python
from neutronclient.v2_0 import client
from credentials import get_credentials
from utils import print_values
credentials = get_credentials()
neutron = client.Client(**credentials)
subnets = neutron.list_subnets()
print(subnets)
Except where otherwise noted, this document is licensed under Creative Commons Attribution 3.0 License. See all OpenStack Legal Documents.