Example #1
0
def _OptionsFrom(options_data):
  """Translate a dict of options data into a message object.

  Args:
    options_data: A dict containing options data.
  Returns:
    An Options message object derived from options_data.
  """
  options = dm_v2beta_base.GetMessages().Options()
  if 'virtualProperties' in options_data:
    options.virtualProperties = options_data['virtualProperties']

  if 'inputMappings' in options_data:
    options.inputMappings = [_InputMappingFrom(im_data)
                             for im_data in options_data['inputMappings']]

  if 'validationOptions' in options_data:
    validation_options_data = options_data['validationOptions']
    validation_options = dm_v2beta_base.GetMessages().ValidationOptions()
    if 'schemaValidation' in validation_options_data:
      validation_options.schemaValidation = (
          validation_options_data['schemaValidation'])
    if 'undeclaredProperties' in validation_options_data:
      validation_options.undeclaredProperties = (
          validation_options_data['undeclaredProperties'])
    options.validationOptions = validation_options

  return options
Example #2
0
def _CredentialFrom(credential_data):
  """Translate a dict of credential data into a message object.

  Args:
    credential_data: A dict containing credential data.
  Returns:
    An Credential message object derived from credential_data.
  """
  basic_auth = dm_v2beta_base.GetMessages().BasicAuth(
      password=credential_data['basicAuth']['password'],
      user=credential_data['basicAuth']['user'])
  return dm_v2beta_base.GetMessages().Credential(basicAuth=basic_auth)
def TemplateContentsFor(template_path):
    """Build a TemplateContents message from a local template or url.

  Args:
    template_path: Path to the config yaml file, with an optional list of
      imports.

  Returns:
    The TemplateContents message from the template at template_path.

  Raises:
    Error if the provided file is not a template.
  """
    config_obj = importer.BuildConfig(template_path, None)

    if not config_obj.IsTemplate():
        raise exceptions.Error('The provided file must be a template.')

    template_name = config_obj.GetBaseName()
    schema_name = template_name + '.schema'
    file_type = 'JINJA' if template_name.endswith('.jinja') else 'PYTHON'

    imports = importer.CreateImports(dm_v2beta_base.GetMessages(), config_obj)

    template = ''
    schema = ''

    # Find schema and template from imports
    for item in imports:
        if item.name == template_name:
            template = item.content
        elif item.name == schema_name:
            schema = item.content

    # Remove schema and template from imports
    imports = [
        item for item in imports
        if item.name not in [template_name, schema_name]
    ]

    return dm_v2beta_base.GetMessages().TemplateContents(imports=imports,
                                                         schema=schema,
                                                         template=template,
                                                         interpreter=file_type)
Example #4
0
def _InputMappingFrom(input_mapping_data):
  """Translate a dict of input mapping data into a message object.

  Args:
    input_mapping_data: A dict containing input mapping data.
  Returns:
    An InputMapping message object derived from options_data.
  """
  return dm_v2beta_base.GetMessages().InputMapping(
      fieldName=input_mapping_data.get('fieldName', None),
      location=input_mapping_data.get('location', None),
      methodMatch=input_mapping_data.get('methodMatch', None),
      value=input_mapping_data.get('value', None))
Example #5
0
def AddOptions(options_file, type_provider):
  """Parse api options from the file and add them to type_provider.

  Args:
    options_file: String path expression pointing to a type-provider options
        file.
    type_provider: A TypeProvider message on which the options will be set.
  Returns:
    The type_provider after applying changes.
  Raises:
    exceptions.ConfigError: the api options file couldn't be parsed as yaml
  """
  if not options_file:
    return type_provider

  file_contents = files.GetFileContents(options_file)
  yaml_content = None
  try:
    yaml_content = yaml.safe_load(file_contents)
  except yaml.YAMLError as exc:
    raise exceptions.ConfigError(
        'Could not load yaml file {0}: {1}'.format(options_file, exc))

  if yaml_content:
    if 'collectionOverrides' in yaml_content:
      type_provider.collectionOverrides = []

      for collection_override_data in yaml_content['collectionOverrides']:
        collection_override = dm_v2beta_base.GetMessages().CollectionOverride(
            collection=collection_override_data['collection'])

        if 'options' in collection_override_data:
          collection_override.options = _OptionsFrom(
              collection_override_data['options'])

        type_provider.collectionOverrides.append(collection_override)

    if 'options' in yaml_content:
      type_provider.options = _OptionsFrom(yaml_content['options'])

    if 'credential' in yaml_content:
      type_provider.credential = _CredentialFrom(yaml_content['credential'])

  return type_provider