神刀安全网

Python API for creating Neutron Router with internal interface and external gateway on RDO …

Python code posted down-here utilizes  NeutronClient V2 API. It creates

router RouterDVS for particular tenant, interface to private sub-net  "demo1_network" (90.0.0.0/24) and gateway to external flat network "public" (192.169.142.0/24). As usual posting was inspired by question been posted at ask.openstack.org. Code is intentionally simplified to become understandable by people without strong python development background.

I was  unable to find this sample in  http://docs.openstack.org/user-guide/sdk_neutron_apis.html , I mean creating gateway and interface for Neutron Router.

*********************************************************************************

Private network supposed to provide internal interface for RouterDVS

*********************************************************************************

[root@ServerCentOS01 test(keystone_boris)]# neutron subnet-list | grep demo1

| b3a6a55a-82fa-4f7d-bbd1-b01cf421e9cb | sub_demo1_network | 90.0.0.0/24      | {"start": "90.0.0.10", "end": "90.0.0.254"}            |

*********************************************************************************

External network supposed to provide gateway for RouterDVS

*********************************************************************************

[root@ServerCentOS01 test(keystone_boris)]# neutron net-list | grep public

| c28335ee-e8de-4a47-ba6b-f78ce88b5c69 | public        | a6bc21a0-eb84-4b11-b52f-41be2dec499a 192.169.142.0/24 |

**********************************************************************************

Before compiling and running code,  source into current shell credentials

of your tenant . This credentials will be used by python module to obtain

authorization

**********************************************************************************

[root@ServerCentOS01 test(keystone_boris)]# cat  createRouterInterGateway.py

#!/usr/bin/env python

from neutronclient.v2_0 import client

import os

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

try:

credentials = get_credentials()

neutron = client.Client(**credentials)

neutron.format = ‘json’

request = {‘router’: {‘name’: ‘RouterDVS’,

‘admin_state_up’: True}}

# Creating Router for tenant

router = neutron.create_router(request)

router_id = router[‘router’][‘id’]

router = neutron.show_router(router_id)

print(router)

# Add internal interface for RouterDVS

subnets = neutron.list_subnets(name=’sub_demo1_network’)

subnet_id = subnets[‘subnets’][0][‘id’]

print(subnet_id)

router_interface = { ‘subnet_id’: subnet_id }

neutron.add_interface_router(router_id,router_interface)

# Add  external gateway to RouterDVS

networks = neutron.list_networks(name=’public’)

network_id = networks[‘networks’][0][‘id’]

print(network_id)

router_dict = {‘network_id’: network_id }

neutron.add_gateway_router(router_id,router_dict)

finally:

print("Execution completed")

[root@ServerCentOS01 test(keystone_boris)]# python  createRouterInterGateway.py

{u’router’: {u’status’: u’ACTIVE’, u’external_gateway_info’: None, u’availability_zone_hints’: [], u’availability_zones’: [], u’description’: u”, u’admin_state_up’: True, u’tenant_id’: u’00706da09eff427ea8c4c3982749cdbd’, u’routes’: [], u’id’: u’db06c4bf-cdcc-49ae-a7b0-634038b062cc’, u’name’: u’RouterDVS’}}

b3a6a55a-82fa-4f7d-bbd1-b01cf421e9cb

c28335ee-e8de-4a47-ba6b-f78ce88b5c69

Execution completed

******************************************

Verification via CLI  RouterDVS

******************************************

[root@ServerCentOS01 test(keystone_boris)]# neutron router-port-list RouterDVS

+———————————+——+——————-+———————————-+

| id                              | name | mac_address       | fixed_ips                        |

+———————————+——+——————-+———————————-+

| fa3ffad8-ad04-42de-   |      | fa:16:3e:86:99:05 | {"subnet_id":" b3a6a55a-82fa- |

| 8c12-6edca2fe320d               |      |                   | 4f7d-bbd1-b01cf421e9cb ",         |

|                                 |      |                   | "ip_address": "90.0.0.1"}        |

+———————————+——+——————-+———————————-+

[root@ServerCentOS01 test(keystone_boris)]# neutron router-show RouterDVS

+————————-+———————————————————————+

| Field                   | Value                                                               |

+————————-+———————————————————————+

| admin_state_up          | True                                                                |

| availability_zone_hints |                                                                     |

| availability_zones      | nova                                                                |

| description             |                                                                     |

| external_gateway_info   | {"network_id": "c28335ee-e8de-4a47-ba6b-f78ce88b5c69" ,              |

|                         | "enable_snat": true, "external_fixed_ips": [{"subnet_id":           |

|                         | "a6bc21a0-eb84-4b11-b52f-41be2dec499a", "ip_address":               |

|                         | "192.169.142.165"}]}                                                |

| id                      | db06c4bf-cdcc-49ae-a7b0-634038b062cc                                |

| name                    | RouterDVS                                                           |

| routes                  |                                                                     |

| status                  | ACTIVE                                                              |

| tenant_id               | 00706da09eff427ea8c4c3982749cdbd                                    |

+————————-+———————————————————————+

[root@ServerCentOS01 test(keystone_boris)]# neutron port-show fa3ffad8-ad04-42de-8c12-6edca2fe320d

+———————–+———————————————————————–+

| Field                 | Value                                                                 |

+———————–+———————————————————————–+

| admin_state_up        | True                                                                  |

| allowed_address_pairs |                                                                       |

| binding:vnic_type     | normal                                                                |

| created_at            | 2016-03-23T21:09:28                                                   |

| description           |                                                                       |

| device_id             | db06c4bf-cdcc-49ae-a7b0-634038b062cc                                  |

| device_owner          | network:router_interface                                              |

| dns_name              |                                                                       |

| extra_dhcp_opts       |                                                                       |

| fixed_ips             | {"subnet_id": "b3a6a55a-82fa-4f7d-bbd1-b01cf421e9cb", "ip_address":   |

|                       | "90.0.0.1"}                                                           |

| id                    | fa3ffad8-ad04-42de-8c12-6edca2fe320d                                  |

| mac_address           | fa:16:3e:86:99:05                                                     |

| name                  |                                                                       |

| network_id            | 98c0dde6-cf57-455a-bd44-2c022282a4fc                                  |

| security_groups       |                                                                       |

| status                | ACTIVE   |

| tenant_id             | 00706da09eff427ea8c4c3982749cdbd                                      |

| updated_at            | 2016-03-23T21:09:33                                                   |

+———————–+———————————————————————–+

Python API for creating Neutron Router with internal interface and external gateway on RDO ...

************************************************************************

Verification routers list via Neutron Client API  [ 1 ]

************************************************************************

[root@ServerCentOS01 test(keystone_boris)]# cat  createRouterList.py

#!/usr/bin/env python

from neutronclient.v2_0 import client

import os

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

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’)

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")

[root@ServerCentOS01 test(keystone_boris)]# python   createRouterList.py

status : ACTIVE

external_gateway_info : {u’network_id’: u’c28335ee-e8de-4a47-ba6b-f78ce88b5c69′, u’enable_snat’: True, u’external_fixed_ips’: [{u’subnet_id’: u’a6bc21a0-eb84-4b11-b52f-41be2dec499a’, u’ip_address’: u’192.169.142.150′}]}

availability_zone_hints : []

availability_zones : [u’nova’]

description :

admin_state_up : True

tenant_id : 00706da09eff427ea8c4c3982749cdbd

routes : []

id : 1ccaf7b9-6a4a-433c-b86a-ef8f999605a8

name : RouterDSA

status : ACTIVE

external_gateway_info : {u’network_id’: u’c28335ee-e8de-4a47-ba6b-f78ce88b5c69′, u’enable_snat’: True, u’external_fixed_ips’: [{u’subnet_id’: u’a6bc21a0-eb84-4b11-b52f-41be2dec499a’, u’ip_address’: u’192.169.142.157′}]}

availability_zone_hints : []

availability_zones : [u’nova’]

description :

admin_state_up : True

tenant_id : 00706da09eff427ea8c4c3982749cdbd

routes : []

id : 5de5dfb1-f765-4ba4-894b-39feab39d347

name : RouterMitaka

status : ACTIVE

external_gateway_info : {u’network_id’: u’ c28335ee-e8de-4a47-ba6b-f78ce88b5c69 ‘, u’enable_snat’: True, u’external_fixed_ips’: [{u’subnet_id’: u’a6bc21a0-eb84-4b11-b52f-41be2dec499a’, u’ip_address’: u’192.169.142.165′}]}

availability_zone_hints : []

availability_zones : [u’nova’]

description :

admin_state_up : True

tenant_id : 00706da09eff427ea8c4c3982749cdbd

routes : []

id : db06c4bf-cdcc-49ae-a7b0-634038b062cc

name : RouterDVS

Execution completed

References

1. http://docs.openstack.org/user-guide/sdk_neutron_apis.html

2.  https://github.com/openstack/python-neutronclient/blob/master/neutronclient/neutron/v2_0/router.py

转载本站任何文章请注明:转载至神刀安全网,谢谢神刀安全网 » Python API for creating Neutron Router with internal interface and external gateway on RDO …

分享到:更多 ()

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
分享按钮