-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiland_api.py
More file actions
150 lines (128 loc) · 5.24 KB
/
iland_api.py
File metadata and controls
150 lines (128 loc) · 5.24 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/usr/bin/env python3
#
# Copyright 2021 iland Internet Solutions Corporation
#
# Licensed under the 3-Clause BSD License (the "License"). You may not use
# this product except in compliance with the License. A copy of the License
# is located in the file LICENSE.
#
import iland, json, argparse, time, sys
def init_api_client():
with open('./creds.json', 'r') as f:
creds = json.load(f)
return Client(creds['client_id'], creds['client_secret'], creds['username'], creds['password'])
class Client:
def __init__(self, client_id, client_secret, username, password):
self.username = username
self.api = iland.Api(
client_id=client_id,
client_secret=client_secret,
username=username,
password=password)
def get_entity(self, entity):
items = []
entity_lookup = {
'company' : 'COMPANY',
'location' : 'IAAS_LOCATION',
'org' : 'IAAS_ORGANIZATION',
'vdc' : 'IAAS_VDC',
'vapp' : 'IAAS_VAPP',
'vm' : 'IAAS_VM'
}
inventory = self.api.get(f"/users/{self.username}/inventory")
api_entity = entity_lookup[entity]
for company in inventory['inventory']:
for item in company['entities'][api_entity]:
items.append(item)
return items
def get_vm(self, uuid):
vm_data = self.api.get(f"/vms/{uuid}")
return VirtualMachine(self, vm_data)
class VirtualMachine:
def __init__(self, client, vm_data):
self.client = client
self.uuid = vm_data['uuid']
self.status = vm_data['status']
def do_action(self, action):
actions = {
'power_on': '/vms/{}/actions/poweron',
'shutdown': '/vms/{}/actions/shutdown',
'power_off': '/vms/{}/actions/poweroff',
'reboot': '/vms/{}/actions/reboot',
'suspend': '/vms/{}/actions/suspend'}
if action in actions:
task_data = self.client.api.post(actions[action].format(self.uuid))
return Task(self.client, task_data)
sys.exit(f"Error: Virtual Machines do not support the {action} action.")
class Task:
def __init__(self, client, task_data):
self.client = client
self.uuid = task_data['uuid']
self.status = task_data['status']
self.active = task_data['active']
self.message = task_data['message']
self.operation = task_data['operation']
def refresh(self):
task = self.client.api.get(f"/tasks/{self.uuid}")
self.status = task['status']
self.active = task['active']
self.message = task['message']
self.operation = task['operation']
return
def watch(self):
while True:
self.refresh()
if self.active == False:
if self.status == 'success':
print(f"{self.operation} - {self.status}")
else:
print(f"{self.operation} - {self.status} ({self.message})")
return
else:
print(f"{self.operation} - {self.status}")
time.sleep(5)
def handle_input(client,args):
action_objects = {
'list' : ('company', 'location', 'org', 'vdc', 'vapp', 'vm'),
'power_on' : ('vm'),
'shutdown' : ('vm'),
'power_off' : ('vm'),
'reboot' : ('vm'),
'suspend' : ('vm')
}
if not (args.object in action_objects[args.action]):
sys.exit(f"Error: Action {args.action} not supported on object {args.object}.")
if args.action == 'list':
if args.object == 'company':
for company in client.get_entity(args.object):
print(f"{company['name']}, {company['uuid']}")
if args.object == 'location':
for location in client.get_entity(args.object):
print(f"{location['name']}")
if args.object == 'org':
for org in client.get_entity(args.object):
print(f"{org['name']}, {org['uuid']}")
if args.object == 'vdc':
for vdc in client.get_entity(args.object):
print(f"{vdc['name']}, {vdc['uuid']}")
if args.object == 'vapp':
for vapp in client.get_entity(args.object):
print(f"{vapp['name']}, {vapp['uuid']}")
if args.object == 'vm':
for vm in client.get_entity(args.object):
print(f"{vm['name']}, {vm['uuid']}")
elif 'vm' in action_objects[args.action]:
if args.uuid:
vm = client.get_vm(args.uuid)
task = vm.do_action(args.action)
task.watch()
else:
sys.exit(f"Error: UUID required to perform action {args.action} on object {args.object}.")
if __name__ == '__main__':
client = init_api_client()
parser = argparse.ArgumentParser()
parser.add_argument('action', help='available actions', choices=['list','power_on','shutdown','power_off','reboot', 'suspend'], default=None)
parser.add_argument('object', help='target object type', choices=['company', 'location','org','vdc','vapp','vm'], default=None)
parser.add_argument('--uuid', help='target object uuid', default=None, required=False)
args = parser.parse_args()
handle_input(client,args)