def commit_template(self, comments='', timeout=5): """ The commit_template updates the parent template to its next version. Once completed, the actual template can be deployed in Cisco DNA Center. :param comments: Comments for the commit action type: str required: no default: '' :return: Template object """ body = {'templateId': self.template_id, 'comments': comments} url = '%s%s%s' % (self.dnac.url, self.resource, TEMPLATE_VERSION_PATH[self.dnac.version]) results, status = self.crud.post(url, headers=self.dnac.hdrs, body=json.dumps(body), verify=self.verify, timeout=timeout) if status != ACCEPTED: raise DnacApiError(MODULE, 'version_template', REQUEST_NOT_ACCEPTED, url, ACCEPTED, status, ERROR_MSGS[status], '') # check the tasks' results task = Task(self.dnac, results['response']['taskId']) task.get_task_results() if task.is_error: raise DnacApiError(MODULE, 'version_template', TEMPLATE_VERSION_FAILED, '', '', '', '', task.failure_reason) # version succeeded - reload the template and its versions return self.load_template(self.name)
def add_project(self, project): """ Adds a project to Cisco DNA Center :param project: The project data represented as a dict :return: Project object """ # add the project to Cisco DNA Center url = '%s%s' % (self.dnac.url, self.resource) self.__clean_project__(project) body = json.dumps(project) results, status = self.crud.post(url, headers=self.dnac.hdrs, body=body, verify=self.verify, timeout=self.timeout) if status != ACCEPTED: raise DnacApiError( MODULE, 'import_project', REQUEST_NOT_ACCEPTED, url, ACCEPTED, status, ERROR_MSGS[status], '' ) # check the tasks' results task = Task(self.dnac, results['response']['taskId']) task.get_task_results() if task.is_error: raise DnacApiError(MODULE, 'import_project', PROJECT_IMPORT_FAILED, '', '', '', '', task.failure_reason) # import succeeded; create a new Project object, add it to Dnac and return it return Project(self.dnac, project['name'])
def delete(self): """ Removes the Version object from Cisco DNA Center as well as from the program's Dnac object. :return: None """ url = self.dnac.url + self.resource results, status = self.crud.delete(url, headers=self.dnac.hdrs) if status != OK: raise DnacApiError(MODULE, 'delete', REQUEST_NOT_OK, url, OK, status, ERROR_MSGS[status], '') task = Task(self.dnac, results['response']['taskId']) task.get_task_results() if task.is_error: raise DnacApiError(MODULE, 'delete', task.progress, '', '', '', task.failure_reason, '') else: # remove self from Dnac.api{} del self.dnac.api[self.name]
def add_new_template(self, template, project, timeout=5): """ Creates a new template in Cisco DNA Center and assigns it to the project provided. :param template: The template information constructed from a json formatted string type: dict required: yes default: None :param project: A reference to the project to which the template should be assigned type: Project object required: yes default: None :return: Template object """ # ensure the template is correctly formatted self.__is_versioned_template__(template) # prepare the template for import template = self.__prepare_template__(template) # add the template into DNA Center url = '%s%s/%s/template' % (self.dnac.url, PROJECT_RESOURCE_PATH[self.dnac.version], project.project_id) body = json.dumps(template) results, status = self.crud.post(url, headers=self.dnac.hdrs, body=body, verify=self.verify, timeout=timeout) if status != ACCEPTED: if status == _500_: raise DnacApiError(MODULE, 'add_new_template', REQUEST_NOT_ACCEPTED, url, ACCEPTED, status, ERROR_MSGS[status], TEMPLATE_ALREADY_EXISTS) else: raise DnacApiError(MODULE, 'add_new_template', REQUEST_NOT_ACCEPTED, url, ACCEPTED, status, ERROR_MSGS[status], '') # check the tasks' results task = Task(self.dnac, results['response']['taskId']) task.get_task_results() if task.is_error: raise DnacApiError(MODULE, 'add_new_template', TEMPLATE_IMPORT_FAILED, '', '', '', '', task.failure_reason) # import succeeded; reload the project and return the new template project.load_project(project.name) return Template(self.dnac, template['name'])
def add_version(self, version, timeout=5): """ Creates a new version of an existing template. :param version: The new version to be added to Cisco DNAC type: dict constructed from a json formatted file required: yes default: None :return: Template object """ # ensure the template is correctly formatted self.__is_versioned_template__(version) # check if the template associated with the new version is already in Dnac if version['name'] not in self.dnac.api: # if not, throw an error raise DnacApiError(MODULE, 'add_version', '%s %s' % (TEMPLATE_NOT_FOUND, version['name']), '', '', '', '', CALL_ADD_NEW_TEMPLATE) else: # if so, save a pointer to it template = self.dnac.api[version['name']] # prepare the new version self.__prepare_version__(version, template) # add the new version to DNAC url = '%s%s' % (self.dnac.url, TEMPLATE_RESOURCE_PATH[self.dnac.version]) body = json.dumps(version) results, status = self.crud.put(url, headers=self.dnac.hdrs, body=body, verify=self.verify, timeout=timeout) if status != ACCEPTED: raise DnacApiError(MODULE, 'add_version', REQUEST_NOT_ACCEPTED, url, ACCEPTED, status, ERROR_MSGS[status], '') # check the tasks' results task = Task(self.dnac, results['response']['taskId']) task.get_task_results() if task.is_error: raise DnacApiError(MODULE, 'add_version', TEMPLATE_IMPORT_FAILED, '', '', '', '', task.failure_reason) # import succeeded; reload the template return template.load_template(version['name'])
def delete_config_file(self, file_id): """ Removes the specified file, given by its UUID, from Cisco DNAC and from the Version instance. :param file_id: str :return: None """ url = '%s%s/%s/%s' % (self.dnac.url, self.resource, CONFIG_FILE_SUB_RESOURCE_PATH[self.dnac.version], file_id) results, status = self.crud.delete(url, headers=self.dnac.hdrs) if status != OK: raise DnacApiError(MODULE, 'delete_config', REQUEST_NOT_OK, url, OK, status, ERROR_MSGS[status], '') task = Task(self.dnac, results['response']['taskId']) task.get_task_results() if task.is_error: raise DnacApiError(MODULE, 'delete_config', task.progress, '', '', '', task.failure_reason, '') else: for config_file_type, config_file in self.__config_files.items(): if file_id == config_file.id: del self.__config_files[config_file_type] break del self.dnac.api['file_%s' % file_id]