Adding Routes to Fujitsu K5 Routers and Subnets

K5 Networking – Routing – “API Quickie”

The following script demonstrates how to add and remove routes on both K5 Subnets and K5 Routers using the native OpenStack API calls. When time allows I’ll elaborate on these features but in the meantime it’s better to be looking at it than for it!

#!/usr/bin/python
"""Summary: Python 2.7X API example calls to add routes to subnets and routers
This is a very rough and ready example script ensure to tidy!!
Prerequisites: k5contractsettingsV7.py file in the same directory with K5 contract login details
adminUser = 'username'
adminPassword = 'password'
contract = 'contract_name'
contractid = 'contract_id'
defaultid = 'default_project_id'
region = 'uk-1'
Author: Graham Land
Date: 21/12/16
Twitter: @allthingsclowd
Github: https://github.com/allthingscloud
Blog: https://allthingscloud.eu
"""
import requests
import getopt
import sys
from k5contractsettingsV7 import *
def get_scoped_token(adminUser, adminPassword, contract, projectid, region):
"""Summary – Get a regional project scoped token using a username and password
Returns:
Object: Regionally Scoped Project Token Object
Args:
adminUser (TYPE): username
adminPassword (TYPE): password
contract (TYPE): contract name
projectid (TYPE): project id
region (TYPE): region
"""
identityURL = 'https://identity.' + region + \
'.cloud.global.fujitsu.com/v3/auth/tokens'
try:
response = requests.post(identityURL,
headers={'Content-Type': 'application/json',
'Accept': 'application/json'},
json={"auth":
{"identity":
{"methods": ["password"], "password":
{"user":
{"domain":
{"name": contract},
"name": adminUser,
"password": adminPassword
}}},
"scope":
{"project":
{"id": projectid
}}}})
return response
except:
return 'Regional Project Token Scoping Failure'
def add_static_route_to_subnet(k5token, subnetid, routes, region):
"""Summary
Args:
adminUser (TYPE): Description
adminPassword (TYPE): Description
subnet (TYPE): Description
routes (TYPE): Description
project (TYPE): Description
contract (TYPE): Description
region (TYPE): Description
Returns:
TYPE: Description
"""
subnetURL = 'https://networking.' + region + \
'.cloud.global.fujitsu.com/v2.0/subnets/' + subnetid
try:
response = requests.put(subnetURL,
headers={'X-Auth-Token': k5token,
'Content-Type': 'application/json'},
json={"subnet": {"host_routes": routes}})
return response
except:
return ("\nUnexpected error:", sys.exc_info())
def clear_routes_on_subnet(k5token, subnetid, region):
"""Summary
Args:
adminUser (TYPE): Description
adminPassword (TYPE): Description
subnet (TYPE): Description
project (TYPE): Description
contract (TYPE): Description
region (TYPE): Description
Returns:
TYPE: Description
"""
subnetURL = 'https://networking.' + region + \
'.cloud.global.fujitsu.com/v2.0/subnets/' + subnetid
try:
response = requests.put(subnetURL,
headers={'X-Auth-Token': k5token,
'Content-Type': 'application/json'},
json={"subnet": {"host_routes": []}})
return response
except:
return ("\nUnexpected error:", sys.exc_info())
def get_subnet_routes(k5token, subnetid, region):
"""Summary
Args:
adminUser (TYPE): Description
adminPassword (TYPE): Description
subnet (TYPE): Description
project (TYPE): Description
contract (TYPE): Description
region (TYPE): Description
Returns:
TYPE: Description
"""
subnetURL = 'https://networking.' + region + \
'.cloud.global.fujitsu.com/v2.0/subnets/' + subnetid
try:
response = requests.get(subnetURL,
headers={'X-Auth-Token': k5token, 'Content-Type': 'application/json', 'Accept': 'application/json'})
return response
except:
return ("\nUnexpected error:", sys.exc_info())
def get_router_routes(k5token, routerid, region):
"""Summary
Args:
adminUser (TYPE): Description
adminPassword (TYPE): Description
router (TYPE): Description
project (TYPE): Description
contract (TYPE): Description
region (TYPE): Description
Returns:
TYPE: Description
"""
try:
routerURL = 'https://networking.' + region + \
'.cloud.global.fujitsu.com/v2.0/routers/' + routerid
response = requests.get(routerURL,
headers={'X-Auth-Token': k5token, 'Content-Type': 'application/json', 'Accept': 'application/json'})
return response
except:
return ("\nUnexpected error:", sys.exc_info())
def add_static_route_to_router(k5token, routerid, routes, region):
"""Summary
Args:
adminUser (TYPE): Description
adminPassword (TYPE): Description
router (TYPE): Description
routes (TYPE): Description
project (TYPE): Description
contract (TYPE): Description
region (TYPE): Description
Returns:
TYPE: Description
"""
try:
routerURL = 'https://networking-ex.' + region + \
'.cloud.global.fujitsu.com/v2.0/routers/' + routerid
response = requests.put(routerURL,
headers={'X-Auth-Token': k5token,
'Content-Type': 'application/json'},
json={"router": {"routes": routes}})
return response
except:
# error_sum = unicode(sys.exc_info()[0])
# print error_sum
# print sys.exc_info().json()
# error_detail = unicode(sys.exc_info()[1])
# print error_detail
return sys.exc_info()
def main():
try:
# Please ensure to set all the values below to match the IDs of the subnets and routers local to you
demoProject = 'Project_A'
demoProjectid = '7015d1478a4c4bd7b970215d7b0260dd'
demoServerid = '8a33075f-0894-4b9b-8b16-047457952f74'
demoNetworkid = 'b514ab88-0a32-4b84-a73f-d5eabfd9de72'
demoSubnetid = '1588ed79-3efe-44a7-be5b-075eb653f3bd'
demoRouterid = 'df7a88eb-a8fd-4169-b292-77f14b6cd286'
demoRoutes = [{"destination":"192.168.50.0/24","nexthop":"192.168.0.43"},{"destination":"10.10.90.0/24","nexthop":"10.10.50.43"}]
# Get a project scoped token
k5token = get_scoped_token(adminUser, adminPassword, contract, demoProjectid, region).headers['X-Subject-Token']
# Print the token – just for reassurance šŸ™‚
print k5token
# List all the routes currently associated with the router
result = get_subnet_routes(k5token, demoSubnetid, region)
# Print the results
print result.json()
# Add the new routes to the desired subnet
result = add_static_route_to_subnet(k5token, demoSubnetid, demoRoutes, region)
# Print the results
print result.json()
# Clear all routes from the subnet – just sends an empty route []
result = clear_routes_on_subnet(k5token, demoSubnetid, region)
# Print the results
print result.json()
# Get the routes currently associated with a router
result = get_router_routes(k5token, demoRouterid, region)
# Print the results
print result.json()
# Add a new set of routes to the router
result = add_static_route_to_router(k5token, demoRouterid, demoRoutes, region)
# Print the results
print result.json()
except:
print("\nUnexpected error:", sys.exc_info())
'''
Example Output —————-
09855d3ce9a54c81847bb210a2225732
{u'subnet': {u'name': u'SelectaSub', u'enable_dhcp': True, u'availability_zone': u'uk-1b', u'network_id': u'b514ab88-0a32-4b84-a73f-d5eabfd9de72', u'tenant_id': u'7015d1478a4c4bd7b970215d7b0260dd', u'dns_nameservers': [], u'allocation_pools': [{u'start': u'192.168.1.1', u'end': u'192.168.1.253'}], u'host_routes': [], u'ip_version': 4, u'gateway_ip': u'192.168.1.254', u'cidr': u'192.168.1.0/24', u'id': u'1588ed79-3efe-44a7-be5b-075eb653f3bd'}}
{u'subnet': {u'name': u'SelectaSub', u'enable_dhcp': True, u'availability_zone': u'uk-1b', u'network_id': u'b514ab88-0a32-4b84-a73f-d5eabfd9de72', u'tenant_id': u'7015d1478a4c4bd7b970215d7b0260dd', u'dns_nameservers': [], u'allocation_pools': [{u'start': u'192.168.1.1', u'end': u'192.168.1.253'}], u'host_routes': [{u'destination': u'192.168.50.0/24', u'nexthop': u'192.168.0.43'}, {u'destination': u'10.10.90.0/24', u'nexthop': u'10.10.50.43'}], u'ip_version': 4, u'gateway_ip': u'192.168.1.254', u'cidr': u'192.168.1.0/24', u'id': u'1588ed79-3efe-44a7-be5b-075eb653f3bd'}}
{u'subnet': {u'name': u'SelectaSub', u'enable_dhcp': True, u'availability_zone': u'uk-1b', u'network_id': u'b514ab88-0a32-4b84-a73f-d5eabfd9de72', u'tenant_id': u'7015d1478a4c4bd7b970215d7b0260dd', u'dns_nameservers': [], u'allocation_pools': [{u'start': u'192.168.1.1', u'end': u'192.168.1.253'}], u'host_routes': [], u'ip_version': 4, u'gateway_ip': u'192.168.1.254', u'cidr': u'192.168.1.0/24', u'id': u'1588ed79-3efe-44a7-be5b-075eb653f3bd'}}
{u'router': {u'status': u'ACTIVE', u'external_gateway_info': {u'network_id': u'd730db50-0e0c-4790-9972-1f6e2b8c4915', u'enable_snat': True}, u'name': u'SelectaRtr', u'admin_state_up': True, u'tenant_id': u'7015d1478a4c4bd7b970215d7b0260dd', u'availability_zone': u'uk-1b', u'routes': [{u'destination': u'10.10.50.0/24', u'nexthop': u'10.10.50.43'}, {u'destination': u'192.168.0.0/24', u'nexthop': u'192.168.0.43'}], u'id': u'df7a88eb-a8fd-4169-b292-77f14b6cd286'}}
{u'router': {u'status': u'ACTIVE', u'external_gateway_info': {u'network_id': u'd730db50-0e0c-4790-9972-1f6e2b8c4915', u'enable_snat': True}, u'name': u'SelectaRtr', u'admin_state_up': True, u'tenant_id': u'7015d1478a4c4bd7b970215d7b0260dd', u'availability_zone': u'uk-1b', u'routes': [{u'destination': u'10.10.90.0/24', u'nexthop': u'10.10.50.43'}, {u'destination': u'192.168.50.0/24', u'nexthop': u'192.168.0.43'}], u'id': u'df7a88eb-a8fd-4169-b292-77f14b6cd286'}}
[Finished in 24.2s]
'''
if __name__ == "__main__":
main()

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s