def ParseDeployConfig(messages, manifests, region):
    """Parses the declarative definition of the resources into message.

  Args:
    messages: module containing the definitions of messages for Cloud Deploy.
    manifests: [str], the list of parsed resource yaml definitions.
    region: str, location ID.

  Returns:
    A dictionary of resource kind and message.
  Raises:
    exceptions.CloudDeployConfigError, if the declarative definition is
    incorrect.
  """
    resource_dict = {
        DELIVERY_PIPELINE_KIND_V1BETA1: [],
        TARGET_KIND_V1BETA1: []
    }
    project = properties.VALUES.core.project.GetOrFail()
    for manifest in manifests:
        if manifest.get('apiVersion') is None:
            raise exceptions.CloudDeployConfigError(
                'missing required field .apiVersion')
        if manifest.get('kind') is None:
            raise exceptions.CloudDeployConfigError(
                'missing required field .kind')
        api_version = manifest['apiVersion']
        if api_version in {API_VERSION_V1BETA1, API_VERSION_V1}:
            _ParseV1Config(messages, manifest['kind'], manifest, project,
                           region, resource_dict)
        else:
            raise exceptions.CloudDeployConfigError(
                'api version {} not supported'.format(api_version))

    return resource_dict
def _ParseV1Config(messages, kind, manifest, project, region, resource_dict):
    """Parses the Cloud Deploy v1 and v1beta1 resource specifications into message.

       This specification version is KRM complied and should be used after
       private review.

  Args:
     messages: module containing the definitions of messages for Cloud Deploy.
     kind: str, name of the resource schema.
     manifest: dict[str,str], cloud deploy resource yaml definition.
     project: str, gcp project.
     region: str, ID of the location.
     resource_dict: dict[str,optional[message]], a dictionary of resource kind
       and message.

  Raises:
    exceptions.CloudDeployConfigError, if the declarative definition is
    incorrect.
  """
    metadata = manifest.get('metadata')
    if not metadata or not metadata.get('name'):
        raise exceptions.CloudDeployConfigError(
            'missing required field .metadata.name in {}'.format(kind))
    if kind == DELIVERY_PIPELINE_KIND_V1BETA1:
        resource_type = deploy_util.ResourceType.DELIVERY_PIPELINE
        resource, resource_ref = _CreateDeliveryPipelineResource(
            messages, metadata['name'], project, region)
    elif kind == TARGET_KIND_V1BETA1:
        resource_type = deploy_util.ResourceType.TARGET
        resource, resource_ref = _CreateTargetResource(messages,
                                                       metadata['name'],
                                                       project, region)
    else:
        raise exceptions.CloudDeployConfigError(
            'kind {} not supported'.format(kind))

    if '/' in resource_ref.Name():
        raise exceptions.CloudDeployConfigError(
            'resource ID "{}" contains /.'.format(resource_ref.Name()))

    for field in manifest:
        if field not in ['apiVersion', 'kind', 'metadata', 'deliveryPipeline']:
            value = manifest.get(field)
            if field == 'executionConfigs':
                SetExecutionConfig(messages, resource, value)
                continue
            setattr(resource, field, value)

    # Sets the properties in metadata.
    for field in metadata:
        if field not in ['name', 'annotations', 'labels']:
            setattr(resource, field, metadata.get(field))
    deploy_util.SetMetadata(messages, resource, resource_type,
                            metadata.get('annotations'),
                            metadata.get('labels'))

    resource_dict[kind].append(resource)
Ejemplo n.º 3
0
    def _ParseV1Beta1Config(self, kind, config, project, region,
                            resource_dict):
        """Parses the Cloud Deploy v1beta1 resource specifications into message.

       This specification version is KRM complied and should be used after
       private review.

    Args:
       kind: str, name of the resource schema.
       config: dict[str,str], cloud deploy resource yaml definition.
       project: str, gcp project.
       region: str, ID of the location.
       resource_dict: dict[str,optional[message]], a dictionary of resource kind
         and message.
    """
        metadata = config.get('metadata')
        if not metadata or not metadata.get('name'):
            raise exceptions.CloudDeployConfigError(
                'missing required field .metadata.name in {}'.format(kind))
        if kind == DELIVERY_PIPELINE_KIND_V1BETA1:
            resource_type = DELIVERY_PIPELINE_TYPE
            resource, resource_ref = self._CreateDeliveryPipelineResource(
                metadata['name'], project, region)
        elif kind == TARGET_KIND_V1BETA1:
            resource_type = TARGET_TYPE
            if config.get('deliveryPipeline') is None:
                raise exceptions.CloudDeployConfigError(
                    'missing required field .deliveryPipeline in target {}'.
                    format(metadata['name']))
            resource, resource_ref = self._CreateTargetResource(
                metadata['name'], config['deliveryPipeline'], project, region)
        else:
            raise exceptions.CloudDeployConfigError(
                'kind {} not supported'.format(kind))

        if '/' in resource_ref.Name():
            raise exceptions.CloudDeployConfigError(
                'resource ID "{}" contains /.'.format(resource_ref.Name()))

        for field in config:
            if field not in [
                    'apiVersion', 'kind', 'metadata', 'deliveryPipeline'
            ]:
                setattr(resource, field, config.get(field))
        # Sets the properties in metadata.
        for field in metadata:
            if field not in ['name', 'annotations', 'labels']:
                setattr(resource, field, metadata.get(field))
        SetMetadata(self.messages, resource, resource_type,
                    metadata.get('annotations'), metadata.get('labels'))

        resource_dict[kind].append(resource)
Ejemplo n.º 4
0
    def ParseDeployConfig(self, configs, region):
        """Parses the declarative definition of the resources into message.

    Args:
      configs: the list of parsed resource yaml definitions.
      region: location ID.

    Returns:
      A dictionary of resource kind and message.
    Raises:
      exception.Error, if the declarative definition is incorrect.
    """
        resource_dict = {
            DELIVERY_PIPELINE_KIND_BETA1: [],
            TARGET_KIND_BETA1: [],
            DELIVERY_PIPELINE_KIND_V1BETA1: [],
            TARGET_KIND_V1BETA1: []
        }
        project = properties.VALUES.core.project.GetOrFail()
        for config in configs:
            if config.get('apiVersion') is None:
                raise exceptions.CloudDeployConfigError(
                    'missing required field .apiVersion')
            if config.get('kind') is None:
                raise exceptions.CloudDeployConfigError(
                    'missing required field .kind')
            api_version = config['apiVersion']
            if api_version == API_VERSION_BETA1:
                self._ParseBeta1Config(config['kind'], config, project, region,
                                       resource_dict)
            elif api_version == API_VERSION_V1BETA1:
                self._ParseV1Beta1Config(config['kind'], config, project,
                                         region, resource_dict)
            else:
                raise exceptions.CloudDeployConfigError(
                    'api version {} not supported'.format(api_version))

        return resource_dict