Share a Fujitsu K5 Custom Image with Member Projects

2016-10-09

Share a Fujitsu K5 Custom Image with Member Projects

The following script can be used to share a custom uploaded image from the default project to other member projects.

Prerequisites : The script relies on a settings file, k5contractsettings.py, which must contain all your contract details and be placed in the same directory  – for example:

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters

Show hidden characters

#!/usr/bin/python
adminUser = 'username'
adminPassword = 'password'
contract = 'contract_name'
contractid = 'contract_id'
defaultid = 'default_project_id'
project = 'working_project'
region = 'uk-1'

view raw
k5contractsettings.py
hosted with ❤ by GitHub

K5 Custom Image Sharing Script

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters

Show hidden characters

#!/usr/bin/python
# Author : Graham Land
# Date: 08/10/2016
#
# Purpose: Share a custom image from the default project to a member project in K5
# Command line parameters –
# -i image_id
# -p member_project
#
# Prerequisites: k5contractsettings.py file in the same directory with login details
#
# 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
# load your K5 contract details from k5contractsettings.py file
from k5contractsettings import *
# get a scoped auth token
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']
# get a central identity portal token
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):
# get a regional domain scoped token to make queries to facilitate conversion of object names to ids
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):
# get a regional domain scoped token to make queries to facilitate conversion of object names to ids
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:
# define global variables from the command line parameters
global image_id
global projects
global status
# ensure minimium commandline paramaters have been supplied
if (len(sys.argv)<4):
print("Usage1: %s -i 'image_id' -p 'project_id'" % sys.argv[0])
sys.exit(2)
# load the command line parameters
myopts, args = getopt.getopt(sys.argv[1:],"i:p:",["imageid=","projects="])
except getopt.GetoptError:
# if the parameters are incorrect display error message
print("Usage1: %s -i 'image_id' -p 'project_id'" % sys.argv[0])
sys.exit(2)
###############################
# o == option
# a == argument passed to the 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])
# enable sharing from primary project to member project
print "\nSharing image " + image_id + " from default tenant " + defaultid + " with " + projects
result = share_image_with_project(adminUser,adminPassword,defaultid,projects,image_id,contract,region)
print result
print "\nShared image " + image_id + " from default tenant " + defaultid + " with " + projects
# activate sharing within member project to complete process
print "\nAccepting image " + image_id + " from default tenant " + defaultid + " with " + projects
result = accept_image_share_from_default_project(adminUser,adminPassword,defaultid,projects,image_id,contract,region)
print result
print "\nAccepted image " + image_id + " from default tenant " + defaultid + " with " + projects
if __name__ == "__main__":
main()

view raw
k5ShareImage.py
hosted with ❤ by GitHub

Example script output:

<

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).

← All posts