def create(self, req, body): """Creates a new scheduled operation.""" LOG.debug('Create scheduled operation start') if not self.is_valid_body(body, 'scheduled_operation'): raise exc.HTTPUnprocessableEntity() LOG.debug('Create a scheduled operation, request body: %s', body) context = req.environ['karbor.context'] context.can(scheduled_operation_policy.CREATE_POLICY) operation_info = body['scheduled_operation'] name = operation_info.get("name", None) operation_type = operation_info.get("operation_type", None) operation_definition = operation_info.get( "operation_definition", None) if not all([name, operation_type, operation_definition]): msg = _("Operation name or type or definition is not provided.") raise exc.HTTPBadRequest(explanation=msg) self.validate_name_and_description(operation_info) trigger_id = operation_info.get("trigger_id", None) trigger = self._get_trigger_by_id(context, trigger_id) if context.project_id != trigger.project_id: msg = _("Invalid trigger id provided.") raise exc.HTTPBadRequest(explanation=msg) operation_obj = { 'name': operation_info.get('name', None), 'description': operation_info.get('description', None), 'operation_type': operation_type, 'user_id': context.user_id, 'project_id': context.project_id, 'trigger_id': trigger_id, 'operation_definition': operation_definition, } try: operation = objects.ScheduledOperation(context=context, **operation_obj) operation.create() except Exception as ex: self._raise_unknown_exception(ex) try: self._create_scheduled_operation(context, operation) except Exception: try: operation.destroy() except Exception: pass raise return self._view_builder.detail(req, operation)
def _create_operation(self, trigger_id='123'): operation_info = { 'name': 'protect vm', 'operation_type': 'protect', 'user_id': '123', 'project_id': '123', 'trigger_id': trigger_id, 'operation_definition': {} } operation = objects.ScheduledOperation(self.context, **operation_info) operation.create() return operation
def _create_scheduled_operation(self, trigger_id): operation_info = { "name": "123", "description": "123", "operation_type": "protect", 'user_id': '123', "project_id": "123", "trigger_id": trigger_id, "operation_definition": { "plan_id": "" }, } operation = objects.ScheduledOperation(self.ctxt, **operation_info) operation.create() return operation
def _create_operation(self): operation_info = { 'name': 'protect vm', 'description': 'protect vm resource', 'operation_type': 'protect', 'user_id': '123', 'project_id': '123', 'trigger_id': '123', 'operation_definition': { 'provider_id': '123', 'plan_id': '123' } } operation = objects.ScheduledOperation(context.get_admin_context(), **operation_info) operation.create() return operation
def _from_db_object(context, state, db_state, expected_attrs=[]): special_fields = set(state.INSTANCE_OPTIONAL_JOINED_FIELDS) normal_fields = set(state.fields) - special_fields for name in normal_fields: state[name] = db_state.get(name) if 'operation' in expected_attrs: if db_state.get('operation', None) is None: state.operation = None else: if not state.obj_attr_is_set('operation'): state.operation = objects.ScheduledOperation(context) state.operation._from_db_object(context, state.operation, db_state['operation']) state._context = context state.obj_reset_changes() return state