-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathclaim_device.py
More file actions
155 lines (134 loc) · 6.55 KB
/
claim_device.py
File metadata and controls
155 lines (134 loc) · 6.55 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
151
152
153
154
155
#!/usr/bin/env python
"""Intersight Device Connector API configuration and device claim via the Intersight API."""
import sys
import argparse
import os.path
import json
from time import sleep
from intersight.intersight_api_client import IntersightApiClient
from intersight.apis import asset_device_claim_api
import device_connector
if __name__ == "__main__":
return_code = 0
try:
parser = argparse.ArgumentParser()
help_str = 'JSON file with Intersight API parameters. Default: intersight_api_params.json'
parser.add_argument('-a', '--api_params', default='intersight_api_params.json', help=help_str)
help_str = 'JSON file with device access information (device hostname, username, password, and proxy settings if requred)'
parser.add_argument('-d', '--devices', required=True, help=help_str)
args = parser.parse_args()
with open(args.api_params, 'r') as api_file:
intersight_api_params = json.load(api_file)
if os.path.isfile(args.devices):
with open(args.devices, 'r') as devices_file:
devices_list = json.load(devices_file)
else:
# Argument devices can be a JSON string instead of file.
# JSON string input can be used with Ansible to directly pass all info on the command line.
devices_list = json.loads(args.devices)
for device in devices_list:
result = dict(changed=False)
result['msg'] = " Host: %s" % device['hostname']
# default access mode to allow control (Read-only False) and set to a boolean value if a string
if not device.get('read_only'):
device['read_only'] = False
else:
if device['read_only'] == 'True' or device['read_only'] == 'true':
device['read_only'] = True
elif device['read_only'] == 'False' or device['read_only'] == 'false':
device['read_only'] = False
# create device connector object based on device type
if device['device_type'] == 'ucs' or device['device_type'] == 'ucsm' or device['device_type'] == 'ucspe':
dc_obj = device_connector.UcsDeviceConnector(device)
elif device['device_type'] == 'hx':
dc_obj = device_connector.HxDeviceConnector(device)
elif device['device_type'] == 'imc':
# attempt ucs connection and if that doesn't login revert to older imc login
dc_obj = device_connector.UcsDeviceConnector(device)
if not dc_obj.logged_in:
dc_obj = device_connector.ImcDeviceConnector(device)
else:
result['msg'] += " Unknown device_type %s" % device['device_type']
return_code = 1
print(json.dumps(result))
continue
if not dc_obj.logged_in:
result['msg'] += " Login error"
return_code = 1
print(json.dumps(result))
continue
ro_json = dc_obj.configure_connector()
if not ro_json['AdminState']:
return_code = 1
if ro_json.get('ApiError'):
result['msg'] += ro_json['ApiError']
print(json.dumps(result))
continue
# set access mode (ReadOnlyMode True/False) to desired state
if (ro_json.get('ReadOnlyMode') is not None) and (ro_json['ReadOnlyMode'] != device['read_only']):
ro_json = dc_obj.configure_access_mode(ro_json)
if ro_json.get('ApiError'):
result['msg'] += ro_json['ApiError']
return_code = 1
print(json.dumps(result))
continue
result['changed'] = True
# configure proxy settings (changes reported in called function)
ro_json = dc_obj.configure_proxy(ro_json, result)
if ro_json.get('ApiError'):
result['msg'] += ro_json['ApiError']
return_code = 1
print(json.dumps(result))
continue
# wait for a connection to establish before checking claim state
for _ in range(10):
if ro_json['ConnectionState'] != 'Connected':
sleep(1)
ro_json = dc_obj.get_status()
else:
break
result['msg'] += " AdminState: %s" % ro_json['AdminState']
result['msg'] += " ConnectionState: %s" % ro_json['ConnectionState']
result['msg'] += " Claimed state: %s" % ro_json['AccountOwnershipState']
if ro_json['ConnectionState'] != 'Connected':
return_code = 1
print(json.dumps(result))
continue
if ro_json['AccountOwnershipState'] != 'Claimed':
# attempt to claim
(claim_resp, device_id, claim_code) = dc_obj.get_claim_info(ro_json)
if claim_resp.get('ApiError'):
result['msg'] += claim_resp['ApiError']
return_code = 1
print(json.dumps(result))
continue
result['msg'] += " Id: %s" % device_id
result['msg'] += " Token: %s" % claim_code
# Create Intersight API instance and post ID/claim code
# ----------------------
api_instance = IntersightApiClient(
host=intersight_api_params['api_base_uri'],
private_key=intersight_api_params['api_private_key_file'],
api_key_id=intersight_api_params['api_key_id'],
)
# create device claim API handle
api_handle = asset_device_claim_api.AssetDeviceClaimApi(api_instance)
# post ID/Claim Code
claim_body = {'SecurityToken': claim_code, 'SerialNumber': device_id}
claim_result = api_handle.asset_device_claims_post(claim_body)
result['changed'] = True
print(json.dumps(result))
# logout of any open sessions
dc_obj.logout()
except Exception as err:
print("Exception:", str(err))
import traceback
print('-' * 60)
traceback.print_exc(file=sys.stdout)
print('-' * 60)
sys.exit(1)
finally:
# logout of any sessions active after exception handling
if ('dc_obj' in locals() or 'dc_obj' in globals()):
dc_obj.logout()
sys.exit(return_code)