Ajout de routes aux routeurs et sous-réseaux Fujitsu K5
2016-12-21
Machine-translated — the English original is authoritative.
Réseau K5 – Routage – « API Quickie »
Le script suivant démontre comment ajouter et supprimer des routes sur les sous-réseaux K5 et les routeurs K5 en utilisant les appels API natifs d'OpenStack. Lorsque le temps le permettra, j'approfondirai ces fonctionnalités, mais en attendant, il est préférable de les examiner plutôt que d'attendre !
Ce fichier contient des caractères Unicode cachés ou bidirectionnels qui peuvent être interprétés ou compilés différemment de ce qui est affiché ci-dessous. Pour les examiner, ouvrez le fichier dans un éditeur qui révèle les caractères Unicode cachés.
En savoir plus sur les caractères Unicode bidirectionnels
Afficher les caractères cachés
| #!/usr/bin/python | |
| """Résumé : Exemples d'appels API Python 2.7X pour ajouter des routes aux sous-réseaux et routeurs | |
| Il s'agit d'un exemple de script très brut et rapide, assurez-vous de le nettoyer !! | |
| Prérequis : fichier k5contractsettingsV7.py dans le même répertoire avec les détails de connexion du contrat K5 | |
| adminUser = 'username' | |
| adminPassword = 'password' | |
| contract = 'contract_name' | |
| contractid = 'contract_id' | |
| defaultid = 'default_project_id' | |
| region = 'uk-1' | |
| Auteur : 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): | |
| """Résumé – Obtenir un jeton de projet régional en utilisant un nom d'utilisateur et un mot de passe | |
| Retourne : | |
| Objet : Objet de jeton de projet régional | |
| Args : | |
| adminUser (TYPE) : nom d'utilisateur | |
| adminPassword (TYPE) : mot de passe | |
| contract (TYPE) : nom du contrat | |
| projectid (TYPE) : identifiant du projet | |
| region (TYPE) : région | |
| """ | |
| 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 'Échec du ciblage du jeton de projet régional' | |
| def add_static_route_to_subnet(k5token, subnetid, routes, region): | |
| """Résumé | |
| Args : | |
| adminUser (TYPE) : Description | |
| adminPassword (TYPE) : Description | |
| subnet (TYPE) : Description | |
| routes (TYPE) : Description | |
| project (TYPE) : Description | |
| contract (TYPE) : Description | |
| region (TYPE) : Description | |
| Retourne : | |
| 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 ("\nErreur inattendue :", sys.exc_info()) | |
| def clear_routes_on_subnet(k5token, subnetid, region): | |
| """Résumé | |
| Args : | |
| adminUser (TYPE) : Description | |
| adminPassword (TYPE) : Description | |
| subnet (TYPE) : Description | |
| project (TYPE) : Description | |
| contract (TYPE) : Description | |
| region (TYPE) : Description | |
| Retourne : | |
| 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 ("\nErreur inattendue :", sys.exc_info()) | |
| def get_subnet_routes(k5token, subnetid, region): | |
| """Résumé | |
| Args : | |
| adminUser (TYPE) : Description | |
| adminPassword (TYPE) : Description | |
| subnet (TYPE) : Description | |
| project (TYPE) : Description | |
| contract (TYPE) : Description | |
| region (TYPE) : Description | |
| Retourne : | |
| 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 ("\nErreur inattendue :", sys.exc_info()) | |
| def get_router_routes(k5token, routerid, region): | |
| """Résumé | |
| Args : | |
| adminUser (TYPE) : Description | |
| adminPassword (TYPE) : Description | |
| router (TYPE) : Description | |
| project (TYPE) : Description | |
| contract (TYPE) : Description | |
| region (TYPE) : Description | |
| Retourne : | |
| 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 ("\nErreur inattendue :", sys.exc_info()) | |
| def add_static_route_to_router(k5token, routerid, routes, region): | |
| """Résumé | |
| Args : | |
| adminUser (TYPE) : Description | |
| adminPassword (TYPE) : Description | |
| router (TYPE) : Description | |
| routes (TYPE) : Description | |
| project (TYPE) : Description | |
| contract (TYPE) : Description | |
| region (TYPE) : Description | |
| Retourne : | |
| 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: | |
| # Veuillez vous assurer de définir toutes les valeurs ci-dessous pour qu'elles correspondent aux ID des sous-réseaux et routeurs locaux | |
| 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"}] | |
| # Obtenir un jeton ciblé sur le projet | |
| k5token = get_scoped_token(adminUser, adminPassword, contract, demoProjectid, region).headers['X-Subject-Token'] | |
| # Imprimer le jeton – juste pour réassurance 🙂 | |
| print k5token | |
| # Lister toutes les routes actuellement associées au routeur | |
| result = get_subnet_routes(k5token, demoSubnetid, region) | |
| # Imprimer les résultats | |
| print result.json() | |
| # Ajouter les nouvelles routes au sous-réseau souhaité | |
| result = add_static_route_to_subnet(k5token, demoSubnetid, demoRoutes, region) | |
| # Imprimer les résultats | |
| print result.json() | |
| # Effacer toutes les routes du sous-réseau – envoie simplement une route vide [] | |
| result = clear_routes_on_subnet(k5token, demoSubnetid, region) | |
| # Imprimer les résultats | |
| print result.json() | |
| # Obtenir les routes actuellement associées à un routeur | |
| result = get_router_routes(k5token, demoRouterid, region) | |
| # Imprimer les résultats | |
| print result.json() | |
| # Ajouter un nouvel ensemble de routes au routeur | |
| result = add_static_route_to_router(k5token, demoRouterid, demoRoutes, region) | |
| # Imprimer les résultats | |
| print result.json() | |
| except: | |
| print("\nErreur inattendue :", sys.exc_info()) | |
| ''' | |
| Exemple de sortie —————- | |
| 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'}} | |
| [Terminé en 24,2 s] | |
| ''' | |
| if __name__ == "__main__": | |
| main() | |
voir brut
K5_Example_Route_Maintenance_API.py
hébergé avec ❤ par GitHub
Originally published on allthingscloud.eu (2016-12-21).