diff --git a/vmanage/api/certificate.py b/vmanage/api/certificate.py index ef2bc1c..cb39b8e 100644 --- a/vmanage/api/certificate.py +++ b/vmanage/api/certificate.py @@ -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: @@ -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: @@ -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 diff --git a/vmanage/api/device_templates.py b/vmanage/api/device_templates.py index 1e8dad0..e9f3362 100644 --- a/vmanage/api/device_templates.py +++ b/vmanage/api/device_templates.py @@ -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: @@ -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: @@ -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']}") @@ -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 diff --git a/vmanage/api/utilities.py b/vmanage/api/utilities.py index 6943441..5a066cc 100644 --- a/vmanage/api/utilities.py +++ b/vmanage/api/utilities.py @@ -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'], @@ -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.