Partager une image personnalisée Fujitsu K5 avec les projets membres
2016-10-09
Machine-translated — the English original is authoritative.
Le script suivant peut être utilisé pour partager une image personnalisée téléchargée depuis le projet par défaut vers d'autres projets membres.
Prérequis : Le script s'appuie sur un fichier de configuration, k5contractsettings.py, qui doit contenir tous vos détails de contrat et être placé dans le même répertoire – par exemple :
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 | |
| adminUser = 'username' | |
| adminPassword = 'password' | |
| contract = 'contract_name' | |
| contractid = 'contract_id' | |
| defaultid = 'default_project_id' | |
| project = 'working_project' | |
| region = 'uk-1' |
voir le code brut
k5contractsettings.py
hébergé avec ❤ par GitHub
Script de partage d'image personnalisée K5
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 | |
| # Auteur : Graham Land | |
| # Date : 08/10/2016 | |
| # | |
| # Objectif : Partager une image personnalisée depuis le projet par défaut vers un projet membre dans K5 | |
| # Paramètres de la ligne de commande – | |
| # -i image_id | |
| # -p member_project | |
| # | |
| # Prérequis : fichier k5contractsettings.py dans le même répertoire avec les identifiants de connexion | |
| # | |
| # adminUser = 'username' | |
| # adminPassword = 'password' | |
| # contract = 'contract_name' | |
| # contractid = 'contract_id' | |
| # defaultid = 'default_project_id' | |
| # project = 'working_project' | |
| # region = 'uk-1' | |
| # | |
| # blog : https://allthingscloud.eu | |
| # twitter : @allthingsclowd | |
| import sys | |
| import os | |
| import requests | |
| import uuid | |
| import base64 | |
| import time | |
| import getopt | |
| import ntpath | |
| # charger vos détails de contrat K5 depuis le fichier k5contractsettings.py | |
| from k5contractsettings import * | |
| # obtenir un jeton d'authentification avec scope | |
| def get_scoped_token(uname,upassword,uproject,udomain,uregion): | |
| identityURL = 'https://identity.' + uregion + '.cloud.global.fujitsu.com/v3/auth/tokens' | |
| response = requests.post(identityURL, | |
| headers={'Content-Type': 'application/json','Accept':'application/json'}, | |
| json={"auth": | |
| {"identity": | |
| {"methods":["password"],"password": | |
| {"user": | |
| {"domain": | |
| {"name":udomain}, | |
| "name":uname, | |
| "password": upassword | |
| }}}, | |
| "scope": | |
| { "project": | |
| {"id":uproject | |
| }}}}) | |
| return response.headers['X-Subject-Token'] | |
| def get_unscoped_token(uname,upassword,udomain,uregion): | |
| identityURL = 'https://identity.' + uregion + '.cloud.global.fujitsu.com/v3/auth/tokens' | |
| response = requests.post(identityURL, | |
| headers={'Content-Type': 'application/json','Accept':'application/json'}, | |
| json={"auth": | |
| {"identity": | |
| {"methods":["password"],"password": | |
| {"user": | |
| {"domain": | |
| {"name":udomain}, | |
| "name":uname, | |
| "password": upassword | |
| }}}}}) | |
| return response.headers['X-Subject-Token'] | |
| # obtenir un jeton du portail d'identité central | |
| def get_unscoped_idtoken(uname,upassword,udomain): | |
| response = requests.post('https://auth-api.jp-east-1.paas.cloud.global.fujitsu.com/API/paas/auth/token', | |
| headers={'Content-Type': 'application/json'}, | |
| json={"auth": | |
| {"identity": | |
| {"password": | |
| {"user": | |
| {"contract_number":udomain, | |
| "name":uname, | |
| "password": upassword | |
| }}}}}) | |
| return response.headers['X-Access-Token'] | |
| def share_image_with_project(adminUser,adminPassword,defaultid,projectid,image_id,contract,region): | |
| # obtenir un jeton avec scope de domaine régional pour effectuer des requêtes permettant la conversion des noms d'objets en identifiants | |
| scoped_k5token = get_scoped_token(adminUser,adminPassword,defaultid,contract,region) | |
| imageURL = 'https://image.' + region + '.cloud.global.fujitsu.com/v2/images/' + image_id + '/members' | |
| response = requests.post(imageURL, | |
| headers={'X-Auth-Token':scoped_k5token,'Content-Type': 'application/json','Accept':'application/json'}, | |
| json={"member": projectid}) | |
| print response.json() | |
| return response.json()['status'] | |
| def accept_image_share_from_default_project(adminUser,adminPassword,defaultid,projectid,image_id,contract,region): | |
| # obtenir un jeton avec scope de domaine régional pour effectuer des requêtes permettant la conversion des noms d'objets en identifiants | |
| scoped_k5token = get_scoped_token(adminUser,adminPassword,projectid,contract,region) | |
| imageURL = 'https://image.' + region + '.cloud.global.fujitsu.com/v2/images/' + image_id + '/members/' + projectid | |
| response = requests.put(imageURL, | |
| headers={'X-Auth-Token':scoped_k5token,'Content-Type': 'application/json','Accept':'application/json'}, | |
| json={"status": "accepted"}) | |
| print response.json() | |
| return response.json()['status'] | |
| def main(): | |
| try: | |
| # définir les variables globales à partir des paramètres de la ligne de commande | |
| global image_id | |
| global projects | |
| global status | |
| # s'assurer que les paramètres minimum de la ligne de commande ont été fournis | |
| if (len(sys.argv)<4): | |
| print("Usage1: %s -i 'image_id' -p 'project_id'" % sys.argv[0]) | |
| sys.exit(2) | |
| # charger les paramètres de la ligne de commande | |
| myopts, args = getopt.getopt(sys.argv[1:],"i:p:",["imageid=","projects="]) | |
| except getopt.GetoptError: | |
| # si les paramètres sont incorrects, afficher le message d'erreur | |
| print("Usage1: %s -i 'image_id' -p 'project_id'" % sys.argv[0]) | |
| sys.exit(2) | |
| ############################### | |
| # o == option | |
| # a == argument passé à l'option o | |
| ############################### | |
| for o, a in myopts: | |
| if o in ('-i','–imageid'): | |
| image_id=a | |
| elif o in ('-p','–projects'): | |
| projects=a | |
| else: | |
| print("Usage1: %s -i 'image_id' -p 'project_id'" % sys.argv[0]) | |
| # activer le partage du projet principal vers le projet membre | |
| print "\nPartage de l'image " + image_id + " depuis le locataire par défaut " + defaultid + " avec " + projects | |
| result = share_image_with_project(adminUser,adminPassword,defaultid,projects,image_id,contract,region) | |
| print result | |
| print "\nImage partagée " + image_id + " depuis le locataire par défaut " + defaultid + " avec " + projects | |
| # activer l'acceptation du partage dans le projet membre pour terminer le processus | |
| print "\nAcceptation de l'image " + image_id + " depuis le locataire par défaut " + defaultid + " avec " + projects | |
| result = accept_image_share_from_default_project(adminUser,adminPassword,defaultid,projects,image_id,contract,region) | |
| print result | |
| print "\nImage acceptée " + image_id + " depuis le locataire par défaut " + defaultid + " avec " + projects | |
| if __name__ == "__main__": | |
| main() |
voir le code brut
k5ShareImage.py
hébergé avec ❤ par GitHub
Exemple de sortie du script :
<
p style=”background:#f8f8f8;overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;”>
Sharing image 956a044a-c486-4894-b72c-5e3feb72625b from default tenant eadb88257
3ac40b1b101eac93009a313 with 15f5aa927ec44f1e8525a1e824997231
{u'status': u'pending', u'created_at': u'2016-10-08T19:50:13Z', u'updated_at': u
'2016-10-08T19:50:13Z', u'image_id': u'956a044a-c486-4894-b72c-5e3feb72625b', u'
member_id': u'15f5aa927ec44f1e8525a1e824997231', u'schema': u'/v2/schemas/member
'}
pending
Shared image 956a044a-c486-4894-b72c-5e3feb72625b from default tenant eadb882573
ac40b1b101eac93009a313 with 15f5aa927ec44f1e8525a1e824997231
Accepting image 956a044a-c486-4894-b72c-5e3feb72625b from default tenant eadb882
573ac40b1b101eac93009a313 with 15f5aa927ec44f1e8525a1e824997231
{u'status': u'accepted', u'created_at': u'2016-10-08T19:50:13Z', u'updated_at':
u'2016-10-08T19:50:14Z', u'image_id': u'956a044a-c486-4894-b72c-5e3feb72625b', u
'member_id': u'15f5aa927ec44f1e8525a1e824997231', u'schema': u'/v2/schemas/membe
r'}
accepted
Accepted image 956a044a-c486-4894-b72c-5e3feb72625b from default tenant eadb8825
73ac40b1b101eac93009a313 with 15f5aa927ec44f1e8525a1e824997231
Happy Stacking!
Originally published on allthingscloud.eu (2016-10-09).