-
Notifications
You must be signed in to change notification settings - Fork 1
/
__main__.py
91 lines (73 loc) · 3.1 KB
/
__main__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import http.client
import json
def main(args):
# provide user name and api key for ibm cloud account- change it with your credentials
name = '{"username": "b.bhattacharyya@zzz.com","key": "atPNebdmMe2fDAk5hLKbcC3a2s3dAcAwO2zBSgYQkUl0","poweraction":"stop"}'
namejson = json.loads(name)
ibmcloud_iaas_user = namejson["username"]
print("Username: " + ibmcloud_iaas_user)
ibmcloud_iaas_key = namejson["key"]
# print("API Key: " + ibmcloud_iaas_key)
iamv=iamtoken(ibmcloud_iaas_key)
region = "us-south"
conn = http.client.HTTPSConnection(f"{region}.iaas.cloud.ibm.com")
headers = {
'Content-Type': 'application/json',
'Cache-Control': 'no-cache',
'Accept': 'application/json',
'Authorization': iamv,
'cache-control': 'no-cache'
}
version = "2020-06-22"
payload = ""
try:
# Connect to api endpoint for vpcs
conn.request("GET", "/v1/instances?generation=2&version=" + version, payload, headers)
# Get and read response data
res = conn.getresponse()
data = res.read()
# iterate response data and call api with stop action for each instace
namejson=json.loads(data.decode("utf-8"))
for item in namejson["instances"]:
print(item["id"])
# use start to start the VMs & stop for shutting those down
payload = f'{{"type": "stop"}}'
itemid=item["id"]
# itemid="0737_d239ced2-8a3e-41c1-9677-4a7dd98d6752"
# print(json.dumps(namejson, indent=2, sort_keys=True))
# Connect to api endpoint for vpcs
conn.request("POST", "/v1/instances/"+itemid+"/actions?generation=2&version=" + version, payload, headers)
# Get and read response data
res = conn.getresponse()
data = res.read()
# Print response data
print(data.decode("utf-8"))
except Exception as error:
print(f"Error fetching VPCs. {error}")
raise
def iamtoken(apikey):
# URL for token
conn = http.client.HTTPSConnection("iam.cloud.ibm.com")
# Payload for retrieving token. Note: An API key will need to be generated and replaced here
payload = 'grant_type=urn%3Aibm%3Aparams%3Aoauth%3Agrant-type%3Aapikey&apikey='+apikey+'&response_type=cloud_iam'
# Required headers
headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'Accept': 'application/json',
'Cache-Control': 'no-cache'
}
try:
# Connect to endpoint for retrieving a token
conn.request("POST", "/identity/token", payload, headers)
# Get and read response data
res = conn.getresponse().read()
data = res.decode("utf-8")
# Format response in JSON
json_res = json.loads(data)
# Concatenate token type and token value
return json_res['token_type'] + ' ' + json_res['access_token']
# If an error happens while retrieving token
except Exception as error:
print(f"Error getting token. {error}")
raise
main('')