Fujitsu K5ルーターとサブネットへのルート追加
2016-12-21
Machine-translated — the English original is authoritative.
K5ネットワーク – ルーティング – 「APIクイック実行」
以下のスクリプトは、ネイティブなOpenStack API呼び出しを使用して、K5サブネットとK5ルーターの両方でルートの追加と削除を行う方法を示しています。時間が許す限りこれらの機能について詳しく説明しますが、それまでの間、それを探しているよりもそれを見ておく方が良いでしょう!
このファイルには、以下に表示されるものとは異なる方法で解釈またはコンパイルされる可能性のある、隠されたUnicode文字または双方向のUnicode文字が含まれています。確認するには、隠されたUnicode文字を表示するエディタでファイルを開いてください。
双方向のUnicode文字について詳しく学ぶ
| #!/usr/bin/python | |
| """概要: サブネットとルーターにルートを追加するためのPython 2.7X API例呼び出し | |
| これは非常に粗雑な例スクリプトです。必ず整頓してください!! | |
| 前提条件: K5契約のログイン詳細を含む、同じディレクトリ内のk5contractsettingsV7.pyファイル | |
| adminUser = 'username' | |
| adminPassword = 'password' | |
| contract = 'contract_name' | |
| contractid = 'contract_id' | |
| defaultid = 'default_project_id' | |
| region = 'uk-1' | |
| 著者: Graham Land | |
| 日付: 2016年12月21日 | |
| Twitter: @allthingsclowd | |
| Github: https://github.com/allthingscloud | |
| ブログ: https://allthingscloud.eu | |
| """ | |
| import requests | |
| import getopt | |
| import sys | |
| from k5contractsettingsV7 import * | |
| def get_scoped_token(adminUser, adminPassword, contract, projectid, region): | |
| """概要 – ユーザー名とパスワードを使用してリージョンスコープのプロジェクトトークンを取得 | |
| 戻り値: | |
| オブジェクト: リージンスコープのプロジェクトトークンオブジェクト | |
| 引数: | |
| adminUser (TYPE): ユーザー名 | |
| adminPassword (TYPE): パスワード | |
| contract (TYPE): 契約名 | |
| projectid (TYPE): プロジェクトID | |
| region (TYPE): リージョン | |
| """ | |
| 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): | |
| """概要 | |
| 引数: | |
| adminUser (TYPE): 説明 | |
| adminPassword (TYPE): 説明 | |
| subnet (TYPE): 説明 | |
| routes (TYPE): 説明 | |
| project (TYPE): 説明 | |
| contract (TYPE): 説明 | |
| region (TYPE): 説明 | |
| 戻り値: | |
| TYPE: 説明 | |
| """ | |
| 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 ("\n予期しないエラー:", sys.exc_info()) | |
| def clear_routes_on_subnet(k5token, subnetid, region): | |
| """概要 | |
| 引数: | |
| adminUser (TYPE): 説明 | |
| adminPassword (TYPE): 説明 | |
| subnet (TYPE): 説明 | |
| project (TYPE): 説明 | |
| contract (TYPE): 説明 | |
| region (TYPE): 説明 | |
| 戻り値: | |
| TYPE: 説明 | |
| """ | |
| 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 ("\n予期しないエラー:", sys.exc_info()) | |
| def get_subnet_routes(k5token, subnetid, region): | |
| """概要 | |
| 引数: | |
| adminUser (TYPE): 説明 | |
| adminPassword (TYPE): 説明 | |
| subnet (TYPE): 説明 | |
| project (TYPE): 説明 | |
| contract (TYPE): 説明 | |
| region (TYPE): 説明 | |
| 戻り値: | |
| TYPE: 説明 | |
| """ | |
| 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 ("\n予期しないエラー:", sys.exc_info()) | |
| def get_router_routes(k5token, routerid, region): | |
| """概要 | |
| 引数: | |
| adminUser (TYPE): 説明 | |
| adminPassword (TYPE): 説明 | |
| router (TYPE): 説明 | |
| project (TYPE): 説明 | |
| contract (TYPE): 説明 | |
| region (TYPE): 説明 | |
| 戻り値: | |
| TYPE: 説明 | |
| """ | |
| 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 ("\n予期しないエラー:", sys.exc_info()) | |
| def add_static_route_to_router(k5token, routerid, routes, region): | |
| """概要 | |
| 引数: | |
| adminUser (TYPE): 説明 | |
| adminPassword (TYPE): 説明 | |
| router (TYPE): 説明 | |
| routes (TYPE): 説明 | |
| project (TYPE): 説明 | |
| contract (TYPE): 説明 | |
| region (TYPE): 説明 | |
| 戻り値: | |
| TYPE: 説明 | |
| """ | |
| 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: | |
| # 以下のすべての値を、ローカルなサブネットとルーターのIDに一致するように設定してください | |
| 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"}] | |
| # プロジェクトスコープのトークンを取得 | |
| k5token = get_scoped_token(adminUser, adminPassword, contract, demoProjectid, region).headers['X-Subject-Token'] | |
| # トークンを出力 – 安心のために 🙂 | |
| print k5token | |
| # ルーターに関連付けられている現在すべてのルートをリスト表示 | |
| result = get_subnet_routes(k5token, demoSubnetid, region) | |
| # 結果を出力 | |
| print result.json() | |
| # 希望するサブネットに新しいルートを追加 | |
| result = add_static_route_to_subnet(k5token, demoSubnetid, demoRoutes, region) | |
| # 結果を出力 | |
| print result.json() | |
| # サブネットからすべてのルートをクリア – 空のルート [] を送信するだけです | |
| result = clear_routes_on_subnet(k5token, demoSubnetid, region) | |
| # 結果を出力 | |
| print result.json() | |
| # ルーターに関連付けられている現在ルートを取得 | |
| result = get_router_routes(k5token, demoRouterid, region) | |
| # 結果を出力 | |
| print result.json() | |
| # ルーターに新しいルートのセットを追加 | |
| result = add_static_route_to_router(k5token, demoRouterid, demoRoutes, region) | |
| # 結果を出力 | |
| print result.json() | |
| except: | |
| print("\n予期しないエラー:", sys.exc_info()) | |
| ''' | |
| 例出力 —————- | |
| 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'}} | |
| [24.2秒で終了] | |
| ''' | |
| if __name__ == "__main__": | |
| main() | |
生ファイルを表示
K5_Example_Route_Maintenance_API.py
❤️ GitHub によってホストされています
Originally published on allthingscloud.eu (2016-12-21).