Esempio n. 1
0
def _FilterYAML(parsed_yaml, schema_path):
    """Filter out fields from the yaml that are not in the schema.

  Args:
    parsed_yaml: yaml to filter
    schema_path: Path to schema.
  """
    has_warnings = False
    for error in yaml_validator.Validator(schema_path).Iterate(parsed_yaml):
        # There are other types of errors (for example, missing a required field),
        # but these are the only ones we expect to see on export and the only ones
        # we want to act on. There is no way to distinguish disallowed fields from
        # unrecognized fields. If we attempt to export an unrecognized value for a
        # recognized field (this will happen whenever we add a new enum value), or
        # if we attempt to export a resource that is missing a required field, we
        # will log the errors as warnings and the exported data will not be able to
        # be imported via the import command until the import command is updated.
        if _IsDisallowedPropertiesError(error):
            fields_to_remove = _ParseProperties(error.message)
            _ClearFields(fields_to_remove, error.path, parsed_yaml)
        else:
            log.warning(error.message)
            has_warnings = True
        if has_warnings:
            log.warning('The import command may need to be updated to handle '
                        'the export data.')
Esempio n. 2
0
def ValidateMessageMappingFile(file_data):
  """Mapping file against krm mapping schema.

  Args:
    file_data: YAMLObject, parsed mapping file data.

  Raises:
    IOError: if schema not found in installed resources.
    ValidationError: if the template doesn't obey the schema.
  """
  validator = yaml_validator.Validator(GetMappingSchema())
  validator.ValidateWithDetailedError(file_data)
Esempio n. 3
0
def ValidateYAML(parsed_yaml, schema_path):
    """Validates YAML against JSON schema.

  Args:
    parsed_yaml: YAML to validate
    schema_path: JSON schema file path.

  Raises:
    IOError: if schema not found in installed resources.
    files.Error: if schema file not found.
    ValidationError: if the template doesn't obey the schema.
    SchemaError: if the schema is invalid.
  """
    yaml_validator.Validator(schema_path).Validate(parsed_yaml)
    def ValidateSchema(self):
        """Validates the parsed_yaml against the JSON schema at SCHEMA_PATH.

    Returns:
      InvalidSchemaError: If the config file does not match the schema.
    """
        schema_errors = []
        list_of_invalid_schema = yaml_validator.Validator(SCHEMA_PATH).Iterate(
            self.parsed_yaml)
        for error in list_of_invalid_schema:
            schema_errors.append('{}'.format(error))
        if schema_errors:
            return InvalidSchemaError(invalid_schema_reasons=schema_errors)
        return None
Esempio n. 5
0
def Import(message_type, stream, schema_path=None):
    """Reads YAML from a stream as a message.

  Args:
    message_type: Type of message to load YAML into.
    stream: Input stream or buffer containing the YAML.
    schema_path: JSON schema file path. None for no YAML validation.

  Raises:
    ParseError: if yaml could not be parsed as the given message type.

  Returns:
    message_type object.
  """
    parsed_yaml = yaml.load(stream)
    if schema_path:
        # If a schema is provided, validate against it.
        yaml_validator.Validator(schema_path).Validate(parsed_yaml)
    try:
        message = api_encoding.PyValueToMessage(message_type, parsed_yaml)
    except Exception as e:
        raise exceptions.ParseError('Cannot parse YAML: [{0}]'.format(e))
    return message
Esempio n. 6
0
 def Run(self, args):
     contents = console_io.ReadFromFileOrStdin(args.yaml_file, binary=False)
     parsed_yaml = yaml.load(contents)
     yaml_validator.Validator(args.schema_file).Validate(parsed_yaml)
Esempio n. 7
0
 def _validate_resource_map(self):
     """Validates resource map against ~/resource_map_schema.yaml."""
     yaml_validator.Validator(self._schema_file_path).Validate(
         self._resource_map_data)
import collections
import json
import os

from googlecloudsdk.command_lib.util import time_util
from googlecloudsdk.core import exceptions as core_exceptions
from googlecloudsdk.core import yaml
from googlecloudsdk.core import yaml_validator

import ruamel.yaml as ryaml

SCHEMA_VERSION = '1.0.0'
_SCHEMA_PATH = (os.path.join(os.path.dirname(__file__),
                             'structured_output_schema.yaml'))
_MSG_VALIDATOR = yaml_validator.Validator(_SCHEMA_PATH)

_INVALID_RESOURCE_VALUE_MSG = ('Resources should be a JSON serializeable '
                               'object or list of JSON serializeable objects.')


class MessageParsingError(core_exceptions.Error):
    """Error Raised if there is a problem converting to/from OutputMessage."""


class InvalidMessageError(core_exceptions.Error):
    """Error Raised if there an input string is not a valid OutputMessage."""


def IsResourceLike(item):
    """Return True if item is a dict like object or list of dict like objects."""
 def _GetValidateFunction(self, name):
     return yaml_validator.Validator(self._GetSchemaPath(name)).Validate
 def testSchemaNotFound(self):
     schema_path = os.path.join(self.schema_dir, 'Oops.yaml')
     with self.assertRaisesRegex(yaml_validator.InvalidSchemaError,
                                 'File not found .*Oops.yaml'):
         yaml_validator.Validator(schema_path)