-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprovision-a-vm-via-api.py
85 lines (76 loc) · 2.33 KB
/
provision-a-vm-via-api.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
# coding: UTF-8
import os
import sys
import re
from manageiq_client.api import ManageIQClient as MiqApi
from manageiq_client.filters import Q
from time import sleep
url = os.environ.get('MIQURL') or 'http://localhost:3000/api'
username = os.environ.get('MIQUSERNAME') or 'admin'
password = os.environ.get('MIQPASSWORD') or 'smartvm'
token = os.environ.get('MIQTOKEN')
# CF API 認証
client = None
if token:
print("\nAuthenticating with the API token")
client = MiqApi(url, dict(token=token))
else:
print("\nAuthenticating with the user credentials")
client = MiqApi(url, dict(user=username, password=password))
payload = {
'action_method': 'POST',
'version': '1.1',
'template_fields': {
'guid': 'cb62132b-9c89-49ba-8abb-e4f2a6ec5c33',
},
'vm_fields': {
'number_of_cpus': 1,
'cores_per_socket': 1,
'vm_name': 'api-test-001',
'vm_memory': '1024',
'network_adapters': 1,
'vlan': 'VM Network',
'sysprep_custom_spec': 'rhel_customization',
'sysprep_spec_override': True,
'linux_host_name': 'api-test-001',
'number_of_vms': 1,
'provision_type': 'vmware',
'schedule_type': 'immediately',
'retirement': 0,
},
'requester': {
'user_name': 'admin',
'owner_email': '[email protected]',
'auto_approve': True
},
'tags': {
},
'additional_values': {
'placement_auto': True
},
'ems_custom_attributes': { },
'miq_custom_attributes': { }
}
print("REST API payload = {}".format(payload))
action = client.collections.provision_requests.action
requests = action.execute_action("create", **payload)
request = next((item for item in requests if item), None)
while True:
request_task = client.get(request.href)
print("REST API Request Task = {}".format(request_task))
if request_task['request_state'] != 'pending':
print("Request task active")
break
sleep(5)
sys.stdout.write('Waiting for 60 seconds ...')
sys.stdout.flush()
sleep(60)
print
# request_status が 'finished' に変化するまで5秒に1回ポーリング
while True:
request_task = client.get(request.href)
print("REST API Request Task = {}".format(request_task))
if request_task['request_state'] == 'finished':
print("Request task finished")
break
sleep(5)