Esempio n. 1
0
def merge_raw_operation_definition(context, raw_operation, our_operation,
                                   interface_name, presentation, type_name):
    if not isinstance(our_operation._raw, dict):
        # Convert short form to long form
        raw_operation['implementation'] = deepcopy_with_locators(
            our_operation._raw)
        return

    # Add/merge inputs
    our_operation_inputs = our_operation.inputs
    if our_operation_inputs:
        # Make sure we have the dict
        if ('inputs'
                not in raw_operation) or (raw_operation.get('inputs') is None):
            raw_operation['inputs'] = OrderedDict()

        merge_raw_input_definitions(context, raw_operation['inputs'],
                                    our_operation_inputs, interface_name,
                                    our_operation._name, presentation,
                                    type_name)

    # Override the description
    if our_operation._raw.get('description') is not None:
        raw_operation['description'] = deepcopy_with_locators(
            our_operation._raw['description'])

    # Add/merge implementation
    if our_operation._raw.get('implementation') is not None:
        if raw_operation.get('implementation') is not None:
            merge(raw_operation['implementation'],
                  deepcopy_with_locators(our_operation._raw['implementation']))
        else:
            raw_operation['implementation'] = \
                deepcopy_with_locators(our_operation._raw['implementation'])
Esempio n. 2
0
def merge_raw_input_definition(context, the_raw_input, our_input,
                               interface_name, operation_name, presentation,
                               type_name):
    # Check if we changed the type
    # TODO: allow a sub-type?
    input_type1 = the_raw_input.get('type')
    input_type2 = our_input.type
    if input_type1 != input_type2:
        if operation_name is not None:
            context.validation.report(
                'interface %s "%s" changes operation input "%s.%s" type from "%s" to "%s" in "%s"'
                % (type_name, interface_name, operation_name, our_input._name,
                   input_type1, input_type2, presentation._fullname),
                locator=input_type2._locator,
                level=Issue.BETWEEN_TYPES)
        else:
            context.validation.report(
                'interface %s "%s" changes input "%s" type from "%s" to "%s" in "%s"'
                % (type_name, interface_name, our_input._name, input_type1,
                   input_type2, presentation._fullname),
                locator=input_type2._locator,
                level=Issue.BETWEEN_TYPES)

    # Merge
    merge(the_raw_input, our_input._raw)
Esempio n. 3
0
def get_assigned_and_defined_property_values(context,
                                             presentation,
                                             field_name='properties'):
    """
    Returns the assigned property values while making sure they are defined in our type.
    
    The property definition's default value, if available, will be used if we did not assign it.
    
    Makes sure that required properties indeed end up with a value.
    """

    values = OrderedDict()

    the_type = presentation._get_type(context)
    assignments = getattr(presentation, field_name)
    definitions = the_type._get_properties(
        context) if the_type is not None else None

    # Fill in our assignments, but make sure they are defined
    if assignments:
        for name, value in assignments.iteritems():
            if (definitions is not None) and (name in definitions):
                definition = definitions[name]
                v = value.value

                # For data type values, merge into the default value (note: this is Cloudify behavior; in TOSCA these values are always replaced)
                default = definition.default
                if isinstance(v, dict) and isinstance(
                        default, dict) and (context.presentation.get_from_dict(
                            'service_template', 'data_types', definition.type)
                                            is not None):
                    t = deepcopy_with_locators(default)
                    merge(t, v)
                    v = t
                elif v is None:
                    v = default

                values[name] = coerce_property_value(context, value,
                                                     definition, v)
            else:
                context.validation.report(
                    'assignment to undefined property "%s" in "%s"' %
                    (name, presentation._fullname),
                    locator=value._locator,
                    level=Issue.BETWEEN_TYPES)

    # Fill in defaults from the definitions
    if definitions:
        for name, definition in definitions.iteritems():
            if (values.get(name) is None) and (definition.default is not None):
                values[name] = coerce_property_value(context, presentation,
                                                     definition,
                                                     definition.default)

    validate_required_values(context, presentation, values, definitions)

    return values
Esempio n. 4
0
def convert_requirement_interface_definitions_from_type_to_raw_template(
        context, raw_requirement, interface_definitions):
    if not interface_definitions:
        return
    if 'interfaces' not in raw_requirement:
        raw_requirement['interfaces'] = OrderedDict()
    for interface_name, interface_definition in interface_definitions.iteritems(
    ):
        raw_interface = convert_interface_definition_from_type_to_raw_template(
            context, interface_definition)
        if interface_name in raw_requirement['interfaces']:
            merge(raw_requirement['interfaces'][interface_name], raw_interface)
        else:
            raw_requirement['interfaces'][interface_name] = raw_interface
Esempio n. 5
0
def merge_raw_property_definition(context, presentation,
                                  raw_property_definition,
                                  our_property_definition, field_name,
                                  property_name):
    # Check if we changed the type
    # TODO: allow a sub-type?
    type1 = raw_property_definition.get('type')
    type2 = our_property_definition.type
    if type1 != type2:
        context.validation.report(
            'override changes type from "%s" to "%s" for property "%s" in "%s"'
            % (type1, type2, property_name, presentation._fullname),
            locator=presentation._get_child_locator(field_name, property_name),
            level=Issue.BETWEEN_TYPES)

    merge(raw_property_definition, our_property_definition._raw)
Esempio n. 6
0
def merge_property_definition_default(context, definition):
    the_type = definition._get_type(context)
    if the_type is None:
        return
    if not hasattr(the_type, '_get_properties'):
        # Not a complex data type
        return

    default = OrderedDict()

    properties = the_type._get_properties(context)
    for property_name, prop in properties.iteritems():
        default_value = prop.default
        if default_value is not None:
            default[property_name] = default_value

    if default:
        # Make sure we have a dict
        if 'default' not in definition._raw:
            definition._raw['default'] = OrderedDict()

        # Merge our default over the type's default
        if isinstance(definition._raw['default'], dict):
            definition._raw['default'] = merge(default,
                                               definition._raw['default'])
Esempio n. 7
0
def convert_workflow(context, operation):
    plugin_name, _, operation_name, inputs = parse_implementation(
        context, operation.implementation, True)

    r = OrderedDict(
        (('plugin', plugin_name), ('operation', operation_name),
         ('parameters',
          merge(convert_parameter_defaults(context, operation.inputs),
                inputs)), ('has_intrinsic_functions',
                           has_intrinsic_functions(context, operation.inputs)),
         ('executor', operation.executor),
         ('max_retries', operation.max_retries), ('retry_interval',
                                                  operation.retry_interval)))

    return r
Esempio n. 8
0
def convert_operation(context, operation):
    plugin_name, plugin_executor, operation_name, inputs = parse_implementation(
        context, operation.implementation)

    inputs = merge(inputs, convert_inputs(context, operation.inputs))

    if plugin_name == SCRIPT_PLUGIN_NAME:
        script_path = inputs.get('script_path')
        if script_path and (not is_file(context, script_path)):
            raise InvalidValueError(
                '"script_path" input for "script" plugin does not refer to a file: %s'
                % safe_repr(script_path),
                level=Issue.BETWEEN_TYPES)

    return OrderedDict(
        (('plugin', plugin_name), ('operation', operation_name),
         ('inputs', inputs), ('has_intrinsic_functions',
                              has_intrinsic_functions(context,
                                                      operation.inputs)),
         ('executor', operation.executor
          or plugin_executor), ('max_retries', operation.max_retries),
         ('retry_interval', operation.retry_interval)))
Esempio n. 9
0
def merge_raw_property_definition(context, presentation,
                                  raw_property_definition,
                                  our_property_definition, field_name,
                                  property_name):
    merge(raw_property_definition, our_property_definition._raw)