Aggiungere rotte ai router e alle subnet di Fujitsu K5
2016-12-21
Machine-translated — the English original is authoritative.
Rete K5 – Routing – “API Quickie”
Lo script seguente dimostra come aggiungere e rimuovere rotte sia sulle subnet K5 che sui router K5 utilizzando le chiamate API native di OpenStack. Quando avrò tempo, approfondirò queste funzionalità, ma nel frattempo è meglio dare un'occhiata ora che aspettare!
Questo file contiene caratteri Unicode nascosti o bidirezionali che potrebbero essere interpretati o compilati in modo diverso rispetto a quanto appare di seguito. Per esaminarli, apri il file in un editor che riveli i caratteri Unicode nascosti.
Maggiori informazioni sui caratteri Unicode bidirezionali
| #!/usr/bin/python | |
| """Riepilogo: Esempi di chiamate API Python 2.7X per aggiungere rotte alle subnet e ai router | |
| Questo è uno script di esempio molto grezzo e approssimativo, assicurati di pulirlo!! | |
| Prerequisiti: file k5contractsettingsV7.py nella stessa directory con i dettagli di accesso al contratto K5 | |
| adminUser = 'username' | |
| adminPassword = 'password' | |
| contract = 'contract_name' | |
| contractid = 'contract_id' | |
| defaultid = 'default_project_id' | |
| region = 'uk-1' | |
| Autore: Graham Land | |
| Data: 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): | |
| """Riepilogo – Ottieni un token scoped per progetto regionale utilizzando nome utente e password | |
| Restituisce: | |
| Oggetto: Oggetto Token Progetto Regionalmente Scoped | |
| Argomenti: | |
| adminUser (TYPE): nome utente | |
| adminPassword (TYPE): password | |
| contract (TYPE): nome del contratto | |
| projectid (TYPE): id del progetto | |
| region (TYPE): regione | |
| """ | |
| 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 'Fallimento del scoped del Token Progetto Regionale' | |
| def add_static_route_to_subnet(k5token, subnetid, routes, region): | |
| """Riepilogo | |
| Argomenti: | |
| adminUser (TYPE): Descrizione | |
| adminPassword (TYPE): Descrizione | |
| subnet (TYPE): Descrizione | |
| routes (TYPE): Descrizione | |
| project (TYPE): Descrizione | |
| contract (TYPE): Descrizione | |
| region (TYPE): Descrizione | |
| Restituisce: | |
| TYPE: Descrizione | |
| """ | |
| 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 ("\nErrore imprevisto:", sys.exc_info()) | |
| def clear_routes_on_subnet(k5token, subnetid, region): | |
| """Riepilogo | |
| Argomenti: | |
| adminUser (TYPE): Descrizione | |
| adminPassword (TYPE): Descrizione | |
| subnet (TYPE): Descrizione | |
| project (TYPE): Descrizione | |
| contract (TYPE): Descrizione | |
| region (TYPE): Descrizione | |
| Restituisce: | |
| TYPE: Descrizione | |
| """ | |
| 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 ("\nErrore imprevisto:", sys.exc_info()) | |
| def get_subnet_routes(k5token, subnetid, region): | |
| """Riepilogo | |
| Argomenti: | |
| adminUser (TYPE): Descrizione | |
| adminPassword (TYPE): Descrizione | |
| subnet (TYPE): Descrizione | |
| project (TYPE): Descrizione | |
| contract (TYPE): Descrizione | |
| region (TYPE): Descrizione | |
| Restituisce: | |
| TYPE: Descrizione | |
| """ | |
| 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 ("\nErrore imprevisto:", sys.exc_info()) | |
| def get_router_routes(k5token, routerid, region): | |
| """Riepilogo | |
| Argomenti: | |
| adminUser (TYPE): Descrizione | |
| adminPassword (TYPE): Descrizione | |
| router (TYPE): Descrizione | |
| project (TYPE): Descrizione | |
| contract (TYPE): Descrizione | |
| region (TYPE): Descrizione | |
| Restituisce: | |
| TYPE: Descrizione | |
| """ | |
| 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 ("\nErrore imprevisto:", sys.exc_info()) | |
| def add_static_route_to_router(k5token, routerid, routes, region): | |
| """Riepilogo | |
| Argomenti: | |
| adminUser (TYPE): Descrizione | |
| adminPassword (TYPE): Descrizione | |
| router (TYPE): Descrizione | |
| routes (TYPE): Descrizione | |
| project (TYPE): Descrizione | |
| contract (TYPE): Descrizione | |
| region (TYPE): Descrizione | |
| Restituisce: | |
| TYPE: Descrizione | |
| """ | |
| 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: | |
| # Assicurati di impostare tutti i valori seguenti per corrispondere agli ID delle subnet e dei router locali | |
| 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"}] | |
| # Ottieni un token scoped per progetto | |
| k5token = get_scoped_token(adminUser, adminPassword, contract, demoProjectid, region).headers['X-Subject-Token'] | |
| # Stampa il token – solo per rassicurazione 🙂 | |
| print k5token | |
| # Elenca tutte le rotte attualmente associate al router | |
| result = get_subnet_routes(k5token, demoSubnetid, region) | |
| # Stampa i risultati | |
| print result.json() | |
| # Aggiungi le nuove rotte alla subnet desiderata | |
| result = add_static_route_to_subnet(k5token, demoSubnetid, demoRoutes, region) | |
| # Stampa i risultati | |
| print result.json() | |
| # Cancella tutte le rotte dalla subnet – invia semplicemente una rotta vuota [] | |
| result = clear_routes_on_subnet(k5token, demoSubnetid, region) | |
| # Stampa i risultati | |
| print result.json() | |
| # Ottieni le rotte attualmente associate a un router | |
| result = get_router_routes(k5token, demoRouterid, region) | |
| # Stampa i risultati | |
| print result.json() | |
| # Aggiungi un nuovo insieme di rotte al router | |
| result = add_static_route_to_router(k5token, demoRouterid, demoRoutes, region) | |
| # Stampa i risultati | |
| print result.json() | |
| except: | |
| print("\nErrore imprevisto:", sys.exc_info()) | |
| ''' | |
| Output di esempio —————- | |
| 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'}} | |
| [Terminato in 24.2s] | |
| ''' | |
| if __name__ == "__main__": | |
| main() | |
visualizza raw
K5_Example_Route_Maintenance_API.py
ospitato con ❤ da GitHub
Originally published on allthingscloud.eu (2016-12-21).