Skip to content
10 changes: 6 additions & 4 deletions vmanage/api/certificate.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def generate_csr(self, device_ip):
result = ParseMethods.parse_data(response)
return result[0]['deviceCSR']

def install_device_cert(self, cert):
def install_device_cert(self, cert, wait=True):
"""Install signed cert on vManage

Args:
Expand All @@ -59,11 +59,12 @@ def install_device_cert(self, cert):
response = HttpMethods(self.session, url).request('POST', payload=cert)
utilities = Utilities(self.session, self.host)
action_id = ParseMethods.parse_id(response)
utilities.waitfor_action_completion(action_id)
if wait:
utilities.waitfor_action_completion(action_id)

return action_id

def push_certificates(self):
def push_certificates(self, wait=True):
"""Push certificates to all controllers

Returns:
Expand All @@ -74,7 +75,8 @@ def push_certificates(self):
response = HttpMethods(self.session, url).request('POST', payload={})
utilities = Utilities(self.session, self.host)
action_id = ParseMethods.parse_id(response)
utilities.waitfor_action_completion(action_id)
if wait:
utilities.waitfor_action_completion(action_id)

return action_id

Expand Down
12 changes: 8 additions & 4 deletions vmanage/api/device_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ def update_device_template(self, device_template):
ParseMethods.parse_data(response)
return response

def reattach_device_template(self, template_id, config_type, is_edited=True, is_master_edited=True):
def reattach_device_template(self, template_id, config_type, is_edited=True, is_master_edited=True, wait=True):
"""Re-Attach a template to the devices it it attached to.

Args:
Expand Down Expand Up @@ -311,12 +311,13 @@ def reattach_device_template(self, template_id, config_type, is_edited=True, is_
utils = Utilities(self.session, self.host, self.port)
response = HttpMethods(self.session, url).request('POST', payload=json.dumps(payload))
action_id = ParseMethods.parse_id(response)
utils.waitfor_action_completion(action_id)
if wait:
utils.waitfor_action_completion(action_id)
else:
raise RuntimeError(f"Could not retrieve input for template {template_id}")
return action_id

def attach_to_template(self, template_id, config_type, uuid):
def attach_to_template(self, template_id, config_type, uuid, wait=True):
"""Attach and device to a template

Args:
Expand Down Expand Up @@ -352,6 +353,8 @@ def attach_to_template(self, template_id, config_type, uuid):
if entry['variable']:
if entry['variable'] in uuid[device_uuid]['variables']:
device_template_variables[entry['property']] = uuid[device_uuid]['variables'][entry['variable']]
elif entry['property'] in uuid[device_uuid]['variables']:
device_template_variables[entry['property']] = uuid[device_uuid]['variables'][entry['property']]
else:
raise RuntimeError(
f"{entry['variable']} is missing for template {uuid[device_uuid]['host_name']}")
Expand All @@ -377,7 +380,8 @@ def attach_to_template(self, template_id, config_type, uuid):
utils = Utilities(self.session, self.host, self.port)
response = HttpMethods(self.session, url).request('POST', payload=json.dumps(payload))
action_id = ParseMethods.parse_id(response)
utils.waitfor_action_completion(action_id)
if wait:
utils.waitfor_action_completion(action_id)

return action_id

Expand Down
45 changes: 26 additions & 19 deletions vmanage/api/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,31 +50,28 @@ def get_vmanage_version(self):
version = result[0]['version']
return version

def waitfor_action_completion(self, action_id):
status = 'in_progress'
def get_action_status(self, action_id):
response = {}
action_status = None
action_activity = None
action_config = None
while status == "in_progress":
url = f"{self.base_url}device/action/status/{action_id}"
response = HttpMethods(self.session, url).request('GET')
ParseMethods.parse_data(response)

if 'json' in response:
status = response['json']['summary']['status']
if 'data' in response['json'] and response['json']['data']:
action_status = response['json']['data'][0]['statusId']
action_activity = response['json']['data'][0]['activity']
if 'actionConfig' in response['json']['data'][0]:
action_config = response['json']['data'][0]['actionConfig']
else:
action_config = None
url = f"{self.base_url}device/action/status/{action_id}"
response = HttpMethods(self.session, url).request('GET')
ParseMethods.parse_data(response)

if 'json' in response:
status = response['json']['summary']['status']
if 'data' in response['json'] and response['json']['data']:
action_status = response['json']['data'][0]['statusId']
action_activity = response['json']['data'][0]['activity']
if 'actionConfig' in response['json']['data'][0]:
action_config = response['json']['data'][0]['actionConfig']
else:
action_status = status
action_config = None
else:
raise Exception(msg="Unable to get action status: No response")
time.sleep(10)
action_status = status
else:
raise Exception(msg="Unable to get action status: No response")

return {
'action_response': response['json'],
Expand All @@ -84,6 +81,16 @@ def waitfor_action_completion(self, action_id):
'action_config': action_config
}

def waitfor_action_completion(self, action_id):
status = 'in_progress'
action_status = None
while status == "in_progress":
action_status = self.get_action_status(action_id)
status = action_status['action_response']['summary']['status']
time.sleep(10)

return action_status

def upload_file(self, input_file):
"""Upload a file to vManage.

Expand Down