def test_error_to_value(self):
     struct_def = ErrorDefinition('mock', [('field1', StringDefinition())])
     actual_output = convert_data_def_to_data_value(struct_def)
     expected_output = StructValue(Introspection.DATA_DEFINITION)
     expected_output.set_field('type', StringValue('ERROR'))
     expected_output.set_field('name', OptionalValue(StringValue('mock')))
     expected_output.set_field('element_definition', OptionalValue())
     field_value = self.generate_primitive_value('STRING')
     field_entry = StructValue(MAP_ENTRY)
     field_entry.set_field('key', StringValue('field1'))
     field_entry.set_field('value', field_value)
     field_values = ListValue()
     field_values.add(field_entry)
     expected_output.set_field('fields', OptionalValue(field_values))
     self.assertEqual(actual_output, expected_output)
Exemple #2
0
def augment_method_result_with_errors(service_id, operation_id, method_result,
                                      errors_to_augment):
    """
    Returns a new method result that is identical to `method_result` except that
    the `errors_definition` field in the `output` (which is of type
    Operation.Info from Introspection service) contains the errors from the Info
    structure in `method_result` plus the errors in `errors_to_augment`. This
    code will be executed only for "get" operation in vAPI Operation
    Introspection service.

    :type  service_id: :class:`str`
    :param service_id: Service identifier
    :type  operation_id: :class:`str`
    :param operation_id: Operation identifier
    :type  method_result: :class:`vmware.vapi.core.MethodResult`
    :param method_result: Operation result
    :type  errors_to_augment: :class:`list` of
        :class:`vmware.vapi.data.value.StructValue`
    :param errors_to_augment: Errors to augment. These are struct values of type
        com.vmware.vapi.std.introspection.Operation.DataDefinition whose `type`
        field has the value ERROR
        to the DataDefinition type in Introspection service IDL.
    :rtype: :class:`vmware.vapi.data.value.DataValue`
    :return: Output data value
    """
    if method_result.success():
        if (service_id == Introspection.OPERATION_SVC
                and operation_id == 'get'):
            output = method_result.output
            augmented_output = StructValue(
                'com.vmware.vapi.std.introspection.operation.info')
            augmented_output.set_field('input_definition',
                                       output.get_field('input_definition'))
            augmented_output.set_field('output_definition',
                                       output.get_field('output_definition'))
            errors = ListValue()
            error_names = []
            for error_def in output.get_field('error_definitions'):
                errors.add(error_def)
                error_names.append(error_def.get_field('name').value.value)
            for error_def in errors_to_augment:
                if error_def.get_field('name').value.value not in error_names:
                    errors.add(error_def)
            augmented_output.set_field('error_definitions', errors)
            return MethodResult(output=augmented_output)
    return method_result
 def test_value_to_struct_ref_def(self):
     struct_value = StructValue(Introspection.DATA_DEFINITION)
     struct_value.set_field('type', StringValue('STRUCTURE'))
     struct_value.set_field('name', OptionalValue(StringValue('mock')))
     struct_value.set_field('element_definition', OptionalValue())
     field_value = self.generate_primitive_value('STRUCTURE_REF')
     field_value.set_field('name', OptionalValue(StringValue('mock')))
     field_entry = StructValue(MAP_ENTRY)
     field_entry.set_field('key', StringValue('field1'))
     field_entry.set_field('value', field_value)
     field_values = ListValue()
     field_values.add(field_entry)
     struct_value.set_field('fields', OptionalValue(field_values))
     actual_output = convert_data_value_to_data_def(struct_value)
     expected_output = StructDefinition(
         'mock', [('field1', StructRefDefinition('mock'))])
     self.assertEqual(actual_output, expected_output)
     # Check the circular reference as well
     self.assertEqual(
         actual_output.get_field('field1').target, expected_output)
def convert_data_def_to_data_value(data_def):
    """
    Convert :class:`vmware.vapi.data.definition.DataDefinition` object to
    :class:`vmware.vapi.data.value.DataValue` object. The type of the object
    returned is a struct value that corresponds to DataDefinition VMODL2 type
    present in introspection service.

    :type  data_def: :class:`vmware.vapi.data.definition.DataDefinition`
    :param data_def: Data definition
    :rtype: :class:`vmware.vapi.data.value.DataValue`
    :return: Data value representing the data definition object
    """
    result = StructValue(Introspection.DATA_DEFINITION)
    result.set_field('type', StringValue(data_type_map.get(data_def.type)))

    if (data_def.type == Type.STRUCTURE or data_def.type == Type.STRUCTURE_REF
            or data_def.type == Type.ERROR):
        result.set_field('name', OptionalValue(StringValue(data_def.name)))
    else:
        result.set_field('name', OptionalValue())

    if (data_def.type == Type.OPTIONAL or data_def.type == Type.LIST):
        element_definition = data_def.element_type
        element_value = convert_data_def_to_data_value(element_definition)
        result.set_field('element_definition', OptionalValue(element_value))
    else:
        result.set_field('element_definition', OptionalValue())

    if (data_def.type == Type.STRUCTURE or data_def.type == Type.ERROR):
        fields = ListValue()
        for field_name in data_def.get_field_names():
            element_definition = data_def.get_field(field_name)
            field_pair = StructValue(MAP_ENTRY)
            field_pair.set_field('key', StringValue(field_name))
            field_pair.set_field(
                'value', convert_data_def_to_data_value(element_definition))
            fields.add(field_pair)
        result.set_field('fields', OptionalValue(fields))
    else:
        result.set_field('fields', OptionalValue())
    return result