def generate_schema(pkg_loader, context_manager, class_name, method_names=None, class_version=None, package_name=None): """Generate JSON schema JSON Schema is generated either for the class with all model builders or for specified model builders only. The return value is a dictionary with keys being model builder names and the values are JSON schemas for them. The class itself is represented by an empty string key. """ if method_names and not isinstance(method_names, (list, tuple)): method_names = (method_names,) version = helpers.parse_version_spec(class_version) if package_name: package = pkg_loader.load_package(package_name, version) else: package = pkg_loader.load_class_package(class_name, version) cls = package.find_class(class_name, search_requirements=False) exc = executor.MuranoDslExecutor(pkg_loader, context_manager) with helpers.with_object_store(exc.object_store): context = exc.create_object_context(cls) model_builders = set(list_model_builders(cls, context)) method_names = model_builders.intersection( method_names or model_builders) result = { name: generate_entity_schema( get_entity(cls, name), context, cls, get_meta(cls, name, context)) for name in method_names } return result
def _execute(self, name, obj, *args, **kwargs): try: final_args = [] final_kwargs = {} for arg in args: if isinstance(arg, object_model.Object): arg = object_model.build_model(arg) final_args.append(arg) for name, arg in kwargs.items(): if isinstance(arg, object_model.Object): arg = object_model.build_model(arg) final_kwargs[name] = arg cls = obj if isinstance(obj, dsl_types.MuranoType) else obj.type runtime_version = cls.package.runtime_version yaql_engine = yaql_integration.choose_yaql_engine(runtime_version) with helpers.with_object_store(self.executor.object_store): return dsl.to_mutable( cls.invoke(name, obj, tuple(final_args), final_kwargs), yaql_engine) except dsl_exception.MuranoPlException as e: if not self.preserve_exception: original_exception = getattr(e, 'original_exception', None) if original_exception and not isinstance( original_exception, dsl_exception.MuranoPlException): exc_traceback = getattr(e, 'original_traceback', None) or sys.exc_info()[2] utils.reraise(type(original_exception), original_exception, exc_traceback) raise
def _validate_model(self, obj, pkg_loader, executor): if CONF.engine.enable_model_policy_enforcer: if obj is not None: with helpers.with_object_store(executor.object_store): self._model_policy_enforcer.modify(obj, pkg_loader) self._model_policy_enforcer.validate( obj.to_dictionary(), pkg_loader)
def serialize_model(root_object, executor, allow_refs=False, make_copy=True, serialize_attributes=True, serialize_actions=True, serialization_type=DumpTypes.Serializable): designer_attributes = executor.object_store.designer_attributes if root_object is None: tree = None tree_copy = None attributes = [] serialized_objects = set() else: with helpers.with_object_store(executor.object_store): tree, serialized_objects = _serialize_object( root_object, designer_attributes, allow_refs, executor, serialize_actions, serialization_type) tree_copy = _serialize_object( root_object, None, allow_refs, executor, serialize_actions, serialization_type)[0] if make_copy else None attributes = executor.attribute_store.serialize( serialized_objects) if serialize_attributes else None return { 'Objects': tree, 'ObjectsCopy': tree_copy, 'Attributes': attributes }, serialized_objects
def serialize_model(root_object, executor, allow_refs=False, make_copy=True, serialize_attributes=True, serialize_actions=True, serialization_type=dsl_types.DumpTypes.Serializable, with_destruction_dependencies=True): designer_attributes = executor.object_store.designer_attributes if root_object is None: tree = None tree_copy = None attributes = [] else: with helpers.with_object_store(executor.object_store): tree, serialized_objects = _serialize_object( root_object, designer_attributes, allow_refs, executor, serialize_actions, serialization_type, with_destruction_dependencies) tree_copy = _serialize_object( root_object, None, allow_refs, executor, serialize_actions, serialization_type, with_destruction_dependencies)[0] if make_copy else None attributes = executor.attribute_store.serialize( serialized_objects) if serialize_attributes else None return { 'Objects': tree, 'ObjectsCopy': tree_copy, 'Attributes': attributes }
def load(self, value, owner, default_type=None, scope_type=None, context=None, **kwargs): parsed = helpers.parse_object_definition(value, scope_type, context) if not parsed: raise ValueError('Invalid object representation format') if owner is self._root_owner: self._initializing = True class_obj = parsed['type'] or default_type if not class_obj: raise ValueError( 'Invalid object representation: ' 'no type information was provided') if isinstance(class_obj, dsl_types.MuranoTypeReference): class_obj = class_obj.type object_id = parsed['id'] is_tmp_object = (object_id is None and owner is not self._root_owner and self._initializing) obj = None if object_id is None else self.get( object_id, self._keep_ids) if not obj: if is_tmp_object or helpers.is_objects_dry_run_mode(): mo_type = murano_object.MuranoObject else: mo_type = murano_object.RecyclableMuranoObject obj = mo_type( class_obj, owner, name=parsed['name'], metadata=parsed['metadata'], object_id=object_id if self._keep_ids else None) obj.load_dependencies(parsed['dependencies']) if parsed['destroyed']: obj.mark_destroyed() self.put(obj, object_id or obj.object_id) system_value = ObjectStore._get_designer_attributes( parsed['extra']) self._designer_attributes_store[object_id] = system_value if context is None: context = self.executor.create_object_context(obj) def run_initialize(): self._initializers.extend( obj.initialize(context, parsed['properties'])) run_initialize() if owner is self._root_owner: self._initializing = False run_initialize() if owner is self._root_owner: with helpers.with_object_store(self.parent_store): for fn in self._initializers: fn() return obj
def func(*args, **kwargs): self._insert_instruction() with helpers.with_object_store(self.__object_store): context = helpers.get_context() return to_mutable( self.__object.type.invoke(item, self.__object, args, kwargs, context), helpers.get_yaql_engine(context))
def serialize(obj, executor, serialization_type=DumpTypes.Serializable): with helpers.with_object_store(executor.object_store): return serialize_model( obj, executor, True, make_copy=False, serialize_attributes=False, serialize_actions=False, serialization_type=serialization_type)[0]['Objects']
def prepare_finalize(self, used_objects): used_objects = set(used_objects) if used_objects else [] sentenced_objects = [ obj for obj in self._store.values() if obj not in used_objects ] with helpers.with_object_store(self): if sentenced_objects: self._pending_destruction.update(sentenced_objects) for __ in self._destroy_garbage(sentenced_objects): pass
def serialize(obj, executor, serialization_type=dsl_types.DumpTypes.Serializable): with helpers.with_object_store(executor.object_store): return serialize_model( obj, executor, True, make_copy=False, serialize_attributes=False, serialize_actions=False, serialization_type=serialization_type, with_destruction_dependencies=False)['Objects']
def load(self, value, owner, default_type=None, scope_type=None, context=None, keep_ids=False): # do the object model load in a temporary object store and copy # loaded objects here after that model_store = InitializationObjectStore(owner, self, keep_ids) with helpers.with_object_store(model_store): result = model_store.load( value, owner, scope_type=scope_type, default_type=default_type, context=context) for obj_id in model_store.iterate(): obj = model_store.get(obj_id) self.put(obj) return result
def load(self, value, owner, default_type=None, scope_type=None, context=None, keep_ids=False): # do the object model load in a temporary object store and copy # loaded objects here after that model_store = InitializationObjectStore( owner, self, keep_ids) with helpers.with_object_store(model_store): result = model_store.load( value, owner, scope_type=scope_type, default_type=default_type, context=context) for obj_id in model_store.iterate(): obj = model_store.get(obj_id) if obj.initialized: self.put(obj) return result
def _push(self, object_store=None): template = copy.deepcopy(self._template) s_template = token_sanitizer.TokenSanitizer().sanitize(template) LOG.debug( 'Pushing: {template}'.format(template=json.dumps(s_template))) object_store = object_store or helpers.get_object_store() while True: try: with helpers.with_object_store(object_store): current_status = self._get_status() resources = template.get('Resources') or template.get( 'resources') if current_status == 'NOT_FOUND': if resources is not None: token_client = self._get_token_client() token_client.stacks.create( stack_name=self._name, parameters=self._parameters, template=template, files=self._files, environment=self._hot_environment, disable_rollback=True, tags=self._tags) self._wait_state( lambda status: status == 'CREATE_COMPLETE') else: if resources is not None: self._client.stacks.update( stack_id=self._name, parameters=self._parameters, files=self._files, environment=self._hot_environment, template=template, disable_rollback=True, tags=self._tags) self._wait_state( lambda status: status == 'UPDATE_COMPLETE', True) else: self.delete() except heat_exc.HTTPConflict as e: LOG.warning('Conflicting operation: {msg}'.format(msg=e)) eventlet.sleep(3) else: break self._applied = self._template == template
def cleanup(self, data): objects_copy = data.get(constants.DM_OBJECTS_COPY) if not objects_copy: return gc_object_store = object_store.ObjectStore(self) with helpers.with_object_store(gc_object_store): gc_object_store.load(objects_copy, None, keep_ids=True) objects_to_clean = [] for object_id in self._list_potential_object_ids(objects_copy): if (gc_object_store.has(object_id) and not self._object_store.has(object_id)): obj = gc_object_store.get(object_id) objects_to_clean.append(obj) if objects_to_clean: for obj in objects_to_clean: self._destroy_object(obj)
def load(self, value, owner, default_type=None, scope_type=None, context=None, **kwargs): parsed = helpers.parse_object_definition(value, scope_type, context) if not parsed: raise ValueError('Invalid object representation format') if owner is self._root_owner: self._initializing = True class_obj = parsed['type'] or default_type if not class_obj: raise ValueError( 'Invalid object representation: ' 'no type information was provided') if isinstance(class_obj, dsl_types.MuranoTypeReference): class_obj = class_obj.type object_id = parsed['id'] obj = None if object_id is None else self._store.get(object_id) if not obj: obj = murano_object.MuranoObject( class_obj, helpers.weak_proxy(owner), name=parsed['name'], object_id=object_id if self._keep_ids else None) self.put(obj, object_id or obj.object_id) system_value = ObjectStore._get_designer_attributes( parsed['extra']) self._designer_attributes_store[object_id] = system_value if context is None: context = self.executor.create_object_context(obj) def run_initialize(): self._initializers.extend( obj.initialize(context, parsed['properties'])) run_initialize() if owner is self._root_owner: self._initializing = False run_initialize() if owner is self._root_owner: with helpers.with_object_store(self.parent_store): for fn in self._initializers: fn() return obj
def load(self, data): with helpers.with_object_store(self.object_store): return self._load(data)
def run(self, cls, method_name, this, args, kwargs): with helpers.with_object_store(self.object_store): return cls.invoke(method_name, this, args, kwargs)
def _destroy_orphans(self, orphan_ids): with helpers.with_object_store(self.object_store): for obj_id in orphan_ids: self._destroy_object(self._object_store.get(obj_id)) self._object_store.remove(obj_id)
def finalize(self): with helpers.with_object_store(self): for t in self._store.values(): t.mark_destroyed(True) self._pending_destruction.clear() self._store.clear()
def session(self): return helpers.with_object_store(self.executor.object_store)
def wrapper(): with helpers.with_object_store(object_store): with helpers.contextual(context): return func(*args, **kwargs)
def cleanup(self): LOG.debug('Cleaning up orphan objects') with helpers.with_object_store(self): n = self._collect_garbage() LOG.debug('{} orphan objects were destroyed'.format(n)) return n
def func(*args, **kwargs): self._insert_instruction() with helpers.with_object_store(self.__object_store): return self.__object.type.invoke( item, self.__object, args, kwargs, helpers.get_context())