From 480fe17a61f857d5084dc37e5f555bc457d55bcd Mon Sep 17 00:00:00 2001 From: netinf <23668453+jeremypng@users.noreply.github.com> Date: Thu, 17 Dec 2020 14:36:04 -0600 Subject: [PATCH 1/6] Issue #97 - added wait parameter, added get_action_status function --- vmanage/api/certificate.py | 10 ++++++---- vmanage/api/device_templates.py | 10 ++++++---- vmanage/api/utilities.py | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 8 deletions(-) diff --git a/vmanage/api/certificate.py b/vmanage/api/certificate.py index 4b3ed1e..3e3f206 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,12 +59,13 @@ def install_device_cert(self, cert): response = HttpMethods(self.session, url).request('POST', payload=cert) utilities = Utilities(self.session, self.host) if 'json' in response and 'id' in response['json']: - utilities.waitfor_action_completion(response['json']['id']) + if wait: + utilities.waitfor_action_completion(response['json']['id']) else: raise Exception('Did not get action ID after installing certificate.') return response['json']['id'] - def push_certificates(self): + def push_certificates(self, wait=True): """Push certificates to all controllers Returns: @@ -76,7 +77,8 @@ def push_certificates(self): utilities = Utilities(self.session, self.host) if 'json' in response and 'id' in response['json']: - utilities.waitfor_action_completion(response['json']['id']) + if wait: + utilities.waitfor_action_completion(response['json']['id']) else: raise Exception('Did not get action ID after pushing certificates.') return response['json']['id'] diff --git a/vmanage/api/device_templates.py b/vmanage/api/device_templates.py index 7cf50ec..9c58144 100644 --- a/vmanage/api/device_templates.py +++ b/vmanage/api/device_templates.py @@ -273,7 +273,7 @@ def update_device_template(self, device_template): response = HttpMethods(self.session, url).request('PUT', payload=json.dumps(device_template)) return ParseMethods.parse_data(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: @@ -308,14 +308,15 @@ def reattach_device_template(self, template_id, config_type, is_edited=True, is_ response = HttpMethods(self.session, url).request('POST', payload=json.dumps(payload)) if 'json' in response and 'id' in response['json']: action_id = response['json']['id'] - utils.waitfor_action_completion(action_id) + if wait: + utils.waitfor_action_completion(action_id) else: raise Exception(f"Did not get action ID after attaching device to template {template_id}.") else: raise Exception(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: @@ -376,7 +377,8 @@ def attach_to_template(self, template_id, config_type, uuid): response = HttpMethods(self.session, url).request('POST', payload=json.dumps(payload)) if 'json' in response and 'id' in response['json']: action_id = response['json']['id'] - utils.waitfor_action_completion(action_id) + if wait: + utils.waitfor_action_completion(action_id) else: raise Exception('Did not get action ID after attaching device to template.') diff --git a/vmanage/api/utilities.py b/vmanage/api/utilities.py index 6943441..f7976a5 100644 --- a/vmanage/api/utilities.py +++ b/vmanage/api/utilities.py @@ -50,6 +50,38 @@ def get_vmanage_version(self): version = result[0]['version'] return version + def get_action_status(self, action_id): + response = {} + action_status = None + action_activity = None + 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_config = None + else: + action_status = status + else: + raise Exception(msg="Unable to get action status: No response") + + return { + 'action_response': response['json'], + 'action_id': action_id, + 'action_status': action_status, + 'action_activity': action_activity, + 'action_config': action_config + } + + def waitfor_action_completion(self, action_id): status = 'in_progress' response = {} From 5f6a86e4cea2d5717650338ff4a46109c6b66444 Mon Sep 17 00:00:00 2001 From: netinf <23668453+jeremypng@users.noreply.github.com> Date: Fri, 18 Dec 2020 11:55:36 -0600 Subject: [PATCH 2/6] fixing spacing in certificate.py and utlities.py --- vmanage/api/certificate.py | 4 ++-- vmanage/api/utilities.py | 3 +-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/vmanage/api/certificate.py b/vmanage/api/certificate.py index 3e3f206..005b4e2 100644 --- a/vmanage/api/certificate.py +++ b/vmanage/api/certificate.py @@ -59,8 +59,8 @@ def install_device_cert(self, cert, wait=True): response = HttpMethods(self.session, url).request('POST', payload=cert) utilities = Utilities(self.session, self.host) if 'json' in response and 'id' in response['json']: - if wait: - utilities.waitfor_action_completion(response['json']['id']) + if wait: + utilities.waitfor_action_completion(response['json']['id']) else: raise Exception('Did not get action ID after installing certificate.') return response['json']['id'] diff --git a/vmanage/api/utilities.py b/vmanage/api/utilities.py index f7976a5..fb64998 100644 --- a/vmanage/api/utilities.py +++ b/vmanage/api/utilities.py @@ -71,7 +71,7 @@ def get_action_status(self, action_id): else: action_status = status else: - raise Exception(msg="Unable to get action status: No response") + raise Exception(msg="Unable to get action status: No response") return { 'action_response': response['json'], @@ -81,7 +81,6 @@ def get_action_status(self, action_id): 'action_config': action_config } - def waitfor_action_completion(self, action_id): status = 'in_progress' response = {} From 59e90e2d8ec8b64d8e74a8d4a85c3a02aa881146 Mon Sep 17 00:00:00 2001 From: netinf <23668453+jeremypng@users.noreply.github.com> Date: Mon, 21 Dec 2020 15:07:55 -0600 Subject: [PATCH 3/6] Issue #99 - added property names as opposed to just variable names as acceptable identifiers for attach_to_device --- vmanage/api/device_templates.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/vmanage/api/device_templates.py b/vmanage/api/device_templates.py index 9c58144..069f9a5 100644 --- a/vmanage/api/device_templates.py +++ b/vmanage/api/device_templates.py @@ -352,6 +352,8 @@ def attach_to_template(self, template_id, config_type, uuid, wait=True): 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 Exception(f"{entry['variable']} is missing for template {uuid[device_uuid]['host_name']}") From 5a5bfeb9d9c5769b134f60756565a8bba743cd21 Mon Sep 17 00:00:00 2001 From: netinf <23668453+jeremypng@users.noreply.github.com> Date: Mon, 21 Dec 2020 15:36:56 -0600 Subject: [PATCH 4/6] fixing tab --- vmanage/api/device_templates.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vmanage/api/device_templates.py b/vmanage/api/device_templates.py index 069f9a5..4036c82 100644 --- a/vmanage/api/device_templates.py +++ b/vmanage/api/device_templates.py @@ -352,7 +352,7 @@ def attach_to_template(self, template_id, config_type, uuid, wait=True): 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']: + elif entry['property'] in uuid[device_uuid]['variables']: device_template_variables[entry['property']] = uuid[device_uuid]['variables'][entry['property']] else: raise Exception(f"{entry['variable']} is missing for template {uuid[device_uuid]['host_name']}") From 47ab8bccff535198b112e146f5f7d97f452fb9e6 Mon Sep 17 00:00:00 2001 From: netinf <23668453+jeremypng@users.noreply.github.com> Date: Mon, 21 Dec 2020 15:39:05 -0600 Subject: [PATCH 5/6] fixing indent --- vmanage/api/device_templates.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vmanage/api/device_templates.py b/vmanage/api/device_templates.py index 4036c82..84ed672 100644 --- a/vmanage/api/device_templates.py +++ b/vmanage/api/device_templates.py @@ -352,7 +352,7 @@ def attach_to_template(self, template_id, config_type, uuid, wait=True): 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']: + elif entry['property'] in uuid[device_uuid]['variables']: device_template_variables[entry['property']] = uuid[device_uuid]['variables'][entry['property']] else: raise Exception(f"{entry['variable']} is missing for template {uuid[device_uuid]['host_name']}") From a36efc7ddf15482c46e85c8d3a81b4e0ddf7b83e Mon Sep 17 00:00:00 2001 From: Jeremy Sanders <23668453+jeremypng@users.noreply.github.com> Date: Fri, 19 Nov 2021 22:08:44 -0600 Subject: [PATCH 6/6] removing duplicated code in utilities.Utilities.waitfor_action_completion --- vmanage/api/utilities.py | 30 +++--------------------------- 1 file changed, 3 insertions(+), 27 deletions(-) diff --git a/vmanage/api/utilities.py b/vmanage/api/utilities.py index fb64998..5a066cc 100644 --- a/vmanage/api/utilities.py +++ b/vmanage/api/utilities.py @@ -83,37 +83,13 @@ def get_action_status(self, action_id): def waitfor_action_completion(self, action_id): status = 'in_progress' - 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 - else: - action_status = status - else: - raise Exception(msg="Unable to get action status: No response") + action_status = self.get_action_status(action_id) + status = action_status['action_response']['summary']['status'] time.sleep(10) - return { - 'action_response': response['json'], - 'action_id': action_id, - 'action_status': action_status, - 'action_activity': action_activity, - 'action_config': action_config - } + return action_status def upload_file(self, input_file): """Upload a file to vManage.