def _get_tenant_id_for_create(self, context, resource): if context.is_admin and 'tenant_id' in resource: tenant_id = resource['tenant_id'] elif ('tenant_id' in resource and resource['tenant_id'] != context.tenant_id): reason = _('Cannot create resource for another tenant') raise n_exc.AdminRequired(reason=reason) else: tenant_id = context.tenant_id return tenant_id
def delete_vnf(self, context, vnf_id, vnf=None): # Extract "force_delete" from request's body force_delete = False if vnf and vnf['vnf'].get('attributes').get('force'): force_delete = vnf['vnf'].get('attributes').get('force') if force_delete and not context.is_admin: LOG.warning("force delete is admin only operation") raise exceptions.AdminRequired(reason="Admin only operation") vnf_dict = self._delete_vnf_pre(context, vnf_id, force_delete=force_delete) driver_name, vim_auth = self._get_infra_driver(context, vnf_dict) self._vnf_monitor.delete_hosting_vnf(vnf_id) instance_id = self._instance_id(vnf_dict) placement_attr = vnf_dict['placement_attr'] region_name = placement_attr.get('region_name') kwargs = { mgmt_constants.KEY_ACTION: mgmt_constants.ACTION_DELETE_VNF, mgmt_constants.KEY_KWARGS: { 'vnf': vnf_dict }, } try: self.mgmt_delete_pre(context, vnf_dict) self.mgmt_call(context, vnf_dict, kwargs) if instance_id: self._vnf_manager.invoke(driver_name, 'delete', plugin=self, context=context, vnf_id=instance_id, auth_attr=vim_auth, region_name=region_name) except Exception as e: # TODO(yamahata): when the device is already deleted. mask # the error, and delete row in db # Other case mark error with excutils.save_and_reraise_exception(): if not force_delete: vnf_dict['status'] = constants.ERROR vnf_dict['error_reason'] = six.text_type(e) self.set_vnf_error_status_reason(context, vnf_dict['id'], vnf_dict['error_reason']) self.mgmt_delete_post(context, vnf_dict) self._delete_vnf_post(context, vnf_dict, e) if force_delete: self._delete_vnf_force(context, vnf_dict['id']) self.mgmt_delete_post(context, vnf_dict) self._delete_vnf_post(context, vnf_dict, None, force_delete=True) else: self.spawn_n(self._delete_vnf_wait, context, vnf_dict, vim_auth, driver_name)
def delete_vnf(self, context, vnf_id, vnf=None): # Extract "force_delete" from request's body force_delete = False if vnf and vnf['vnf'].get('attributes').get('force'): force_delete = vnf['vnf'].get('attributes').get('force') if force_delete and not context.is_admin: LOG.warning("force delete is admin only operation") raise exceptions.AdminRequired(reason="Admin only operation") self._delete_vnf(context, vnf_id, force_delete=force_delete)
def delete_ns(self, context, ns_id, ns=None): # Extract "force_delete" from request's body force_delete = False if ns and ns.get('ns', {}).get('attributes', {}).get('force'): force_delete = ns['ns'].get('attributes').get('force') if force_delete and not context.is_admin: LOG.warning("force delete is admin only operation") raise exceptions.AdminRequired(reason="Admin only operation") ns = super(NfvoPlugin, self).get_ns(context, ns_id) LOG.debug("Deleting ns: %s", ns) vim_res = self.vim_client.get_vim(context, ns['vim_id']) super(NfvoPlugin, self).delete_ns_pre(context, ns_id, force_delete) driver_type = vim_res['vim_type'] workflow = None try: if ns['vnf_ids']: ns['force_delete'] = force_delete workflow = self._vim_drivers.invoke( driver_type, 'prepare_and_create_workflow', resource='ns', action='delete', auth_dict=self.get_auth_dict(context), kwargs={'ns': ns}) except nfvo.NoTasksException: LOG.warning("No VNF deletion task(s).") if workflow: try: mistral_execution = self._vim_drivers.invoke( driver_type, 'execute_workflow', workflow=workflow, auth_dict=self.get_auth_dict(context)) except Exception as ex: LOG.error('Error while executing workflow: %s', ex) self._vim_drivers.invoke(driver_type, 'delete_workflow', workflow_id=workflow['id'], auth_dict=self.get_auth_dict(context)) raise ex def _delete_ns_wait(ns_id, execution_id): exec_state = "RUNNING" mistral_retries = MISTRAL_RETRIES while exec_state == "RUNNING" and mistral_retries > 0: time.sleep(MISTRAL_RETRY_WAIT) exec_state = self._vim_drivers.invoke( driver_type, 'get_execution', execution_id=execution_id, auth_dict=self.get_auth_dict(context)).state LOG.debug('status: %s', exec_state) if exec_state == 'SUCCESS' or exec_state == 'ERROR': break mistral_retries -= 1 # TODO(phuoc): add more information about error reason in case # of exec_state is 'ERROR' error_reason = None if mistral_retries == 0 and exec_state == 'RUNNING': error_reason = _( "NS deletion is not completed within" " {wait} seconds as deletion of mistral" " execution {mistral} is not completed").format( wait=MISTRAL_RETRIES * MISTRAL_RETRY_WAIT, mistral=execution_id) exec_obj = self._vim_drivers.invoke( driver_type, 'get_execution', execution_id=execution_id, auth_dict=self.get_auth_dict(context)) self._vim_drivers.invoke(driver_type, 'delete_execution', execution_id=execution_id, auth_dict=self.get_auth_dict(context)) self._vim_drivers.invoke(driver_type, 'delete_workflow', workflow_id=workflow['id'], auth_dict=self.get_auth_dict(context)) super(NfvoPlugin, self).delete_ns_post(context, ns_id, exec_obj, error_reason, force_delete=force_delete) if workflow: self.spawn_n(_delete_ns_wait, ns['id'], mistral_execution.id) else: super(NfvoPlugin, self).delete_ns_post(context, ns_id, None, None, force_delete=force_delete) return ns['id']