Beispiel #1
0
    def test_assemble_object_definition_with_serializable_model_format(
            self, mock_format_type_string):
        mock_format_type_string.return_value = mock.sentinel.type

        test_parsed = {
            'type': mock.sentinel.type,
            'properties': {},
            'id': mock.sentinel.id,
            'name': mock.sentinel.name,
            'metadata': mock.sentinel.metadata,
            'destroyed': True,
            'extra': {}
        }
        expected = {
            '?': {
                'type': mock.sentinel.type,
                'id': mock.sentinel.id,
                'name': mock.sentinel.name,
                'metadata': mock.sentinel.metadata,
                'destroyed': True
            }
        }
        model_format = dsl_types.DumpTypes.Serializable

        result = helpers.assemble_object_definition(test_parsed, model_format)
        for key, val in expected['?'].items():
            self.assertEqual(val, result['?'][key])
Beispiel #2
0
        def template(engine,
                     value,
                     type_,
                     exclude_properties=None,
                     default_type=None,
                     version_spec=None):
            object_store = helpers.get_object_store()
            passkey = None
            if not default_type:
                default_type = type_
            murano_class = type_.type
            if value is None:
                return None
            if isinstance(value, dsl_types.MuranoObject):
                obj = value
            elif isinstance(value, dsl_types.MuranoObjectInterface):
                obj = value.object
            elif isinstance(value, utils.MappingType):
                passkey = utils.create_marker('<Contract Passkey>')
                if exclude_properties:
                    parsed = helpers.parse_object_definition(
                        value, calling_type, context)
                    props = dsl.to_mutable(parsed['properties'], engine)
                    for p in exclude_properties:
                        helpers.patch_dict(props, p, passkey)
                    parsed['properties'] = props
                    value = helpers.assemble_object_definition(parsed)
                with helpers.thread_local_attribute(
                        constants.TL_CONTRACT_PASSKEY, passkey):
                    with helpers.thread_local_attribute(
                            constants.TL_OBJECTS_DRY_RUN, True):
                        obj = object_store.load(value,
                                                owner,
                                                context=context,
                                                default_type=default_type,
                                                scope_type=calling_type)
            else:
                raise exceptions.ContractViolationException(
                    'Value {0} cannot be represented as class {1}'.format(
                        format_scalar(value), type_))
            if not helpers.is_instance_of(
                    obj, murano_class.name, version_spec
                    or helpers.get_type(root_context)):
                raise exceptions.ContractViolationException(
                    'Object of type {0} is not compatible with '
                    'requested type {1}'.format(obj.type.name, type_))

            with helpers.thread_local_attribute(constants.TL_CONTRACT_PASSKEY,
                                                passkey):
                result = serializer.serialize(obj.real_this,
                                              object_store.executor,
                                              dsl_types.DumpTypes.Mixed)
                if exclude_properties:
                    for p in exclude_properties:
                        helpers.patch_dict(result, p, utils.NO_VALUE)
                return result
Beispiel #3
0
    def test_assemble_object_definition_with_inline_model_format(self):
        test_parsed = {
            'type': mock.sentinel.type,
            'properties': mock.sentinel.properties,
            'id': mock.sentinel.id,
            'name': mock.sentinel.name,
            'metadata': mock.sentinel.metadata,
            'dependencies': mock.sentinel.dependencies,
            'destroyed': mock.sentinel.destroyed,
            'extra': {}
        }
        model_format = dsl_types.DumpTypes.Inline
        expected = copy.copy(test_parsed)
        expected[mock.sentinel.type] = mock.sentinel.properties
        for key in ['type', 'extra', 'properties']:
            expected.pop(key)

        result = helpers.assemble_object_definition(test_parsed, model_format)
        for key, val in expected.items():
            self.assertEqual(val, result[key])
Beispiel #4
0
    def test_assemble_object_definition(self):
        test_parsed = {
            'type': mock.sentinel.type,
            'properties': {},
            'id': mock.sentinel.id,
            'name': mock.sentinel.name,
            'metadata': mock.sentinel.metadata,
            'destroyed': True,
            'extra': {}
        }
        expected = {
            '?': {
                'type': mock.sentinel.type,
                'id': mock.sentinel.id,
                'name': mock.sentinel.name,
                'metadata': mock.sentinel.metadata,
                'destroyed': True
            }
        }

        result = helpers.assemble_object_definition(test_parsed)
        for key, val in expected.items():
            self.assertEqual(val, result[key])
Beispiel #5
0
 def transform(self):
     object_store = helpers.get_object_store()
     if self.value is None:
         return None
     if isinstance(self.value, dsl_types.MuranoObject):
         obj = self.value
     elif isinstance(self.value, dsl_types.MuranoObjectInterface):
         obj = self.value.object
     elif isinstance(self.value, utils.MappingType):
         passkey = utils.create_marker('<Contract Passkey>')
         if self.exclude_properties:
             parsed = helpers.parse_object_definition(
                 self.value, self.calling_type, self.context)
             props = dsl.to_mutable(parsed['properties'], self.engine)
             for p in self.exclude_properties:
                 helpers.patch_dict(props, p, passkey)
             parsed['properties'] = props
             value = helpers.assemble_object_definition(parsed)
         else:
             value = self.value
         with helpers.thread_local_attribute(constants.TL_CONTRACT_PASSKEY,
                                             passkey):
             with helpers.thread_local_attribute(
                     constants.TL_OBJECTS_DRY_RUN, True):
                 obj = object_store.load(value,
                                         self.owner,
                                         context=self.context,
                                         default_type=self.default_type,
                                         scope_type=self.calling_type)
                 obj.__passkey__ = passkey
     else:
         raise exceptions.ContractViolationException(
             'Value {0} cannot be represented as class {1}'.format(
                 helpers.format_scalar(self.value), self.type))
     self.value = obj
     return self.validate()