Exemple #1
0
def MessageToFieldPaths(msg):
    """Produce field paths from a message object.

  The result is used to create a FieldMask proto message that contains all field
  paths presented in the object.
  https://github.com/protocolbuffers/protobuf/blob/master/src/google/protobuf/field_mask.proto

  Args:
    msg: A user defined message object that extends the messages.Message class.
    https://github.com/google/apitools/blob/master/apitools/base/protorpclite/messages.py

  Returns:
    The list of field paths.
  """
    fields = []
    for field in msg.all_fields():
        v = msg.get_assigned_value(field.name)
        if field.repeated and not v:
            # Repeated field is initialized as an empty list.
            continue
        if v is not None:
            name = resource_property.ConvertToSnakeCase(field.name)
            if hasattr(v, 'all_fields'):
                # message has sub-messages, constructing subpaths.
                for f in MessageToFieldPaths(v):
                    fields.append('{}.{}'.format(name, f))
            else:
                fields.append(name)
    return fields
def GetDestFromParam(param, prefix=None):
  """Returns a conventional dest name given param name with optional prefix."""
  name = param.replace('-', '_').strip('_')
  if prefix:
    name = prefix + '_' + name
  return resource_property.ConvertToSnakeCase(
      re.sub('s?I[Dd]$', '', name)).strip('_')
Exemple #3
0
 def RewriteTerm(self, key, op, operand):
     """Rewrites <key op operand>."""
     if op == ':':
         op = '='
     elif op not in ['<', '<=', '=', '!=', '>=', '>']:
         return None
     name = resource_property.ConvertToSnakeCase(key)
     if name in _STRING_FIELDS:
         return self._RewriteStrings(name, op, operand)
     elif name in _TIME_FIELDS:
         return self._RewriteTimes(name, op, operand)
     return None
 def GetListCommand(self, parameter_info):
   """Returns the list command argv given parameter_info."""
   info = resource_registry.Get(self.collection)
   if info.cache_command:
     command = info.cache_command
   elif info.list_command:
     command = info.list_command
   elif self._deprecated_list_command_callback:
     command = ' '.join(self._deprecated_list_command_callback(
         parameter_info.parsed_args))
   else:
     if self._deprecated_list_command:
       command = self._deprecated_list_command
     else:
       command = resource_property.ConvertToSnakeCase(
           self.collection).replace('_', '-') + ' list'
     command = command.replace('.', ' ')
   return command.split(' ') + ['--uri', '--quiet', '--format=disable']
Exemple #5
0
def SetMetadata(messages,
                message,
                resource_type,
                annotations=None,
                labels=None):
    """Sets the metadata of a cloud deploy resource message.

  Args:
   messages: module containing the definitions of messages for Cloud Deploy.
   message: message in googlecloudsdk.third_party.apis.cloudeploy.
   resource_type: str, the type of the resource to be updated.
   annotations: dict[str,str], a dict of annotation (key,value) pairs.
   labels: dict[str,str], a dict of label (key,value) pairs.
  """

    if annotations:
        if resource_type == DELIVERY_PIPELINE_TYPE:
            annotations_value_msg = messages.DeliveryPipeline.AnnotationsValue
        else:
            annotations_value_msg = messages.Target.AnnotationsValue
        av = annotations_value_msg()
        for k, v in annotations.items():
            av.additionalProperties.append(
                annotations_value_msg.AdditionalProperty(key=k, value=v))

        message.annotations = av

    if labels:
        if resource_type == DELIVERY_PIPELINE_TYPE:
            labels_value_msg = messages.DeliveryPipeline.LabelsValue
        else:
            labels_value_msg = messages.Target.LabelsValue
        lv = labels_value_msg()
        for k, v in labels.items():
            lv.additionalProperties.append(
                labels_value_msg.AdditionalProperty(
                    # Base on go/unified-cloud-labels-proposal,
                    # converts camel case key to snake case.
                    key=resource_property.ConvertToSnakeCase(k),
                    value=v))

        message.labels = lv
def SetMetadata(messages,
                message,
                resource_type,
                annotations=None,
                labels=None):
    """Sets the metadata of a cloud deploy resource message.

  Args:
   messages: module containing the definitions of messages for Cloud Deploy.
   message: message in googlecloudsdk.third_party.apis.clouddeploy.
   resource_type: ResourceType enum, the type of the resource to be updated,
     which is defined in the API proto.
   annotations: dict[str,str], a dict of annotation (key,value) pairs that allow
     clients to store small amounts of arbitrary data in cloud deploy resources.
   labels: dict[str,str], a dict of label (key,value) pairs that can be used to
     select cloud deploy resources and to find collections of cloud deploy
     resources that satisfy certain conditions.
  """

    if annotations:
        annotations_value_msg = getattr(messages,
                                        resource_type.value).AnnotationsValue
        annotations_value = annotations_value_msg()
        for key, value in annotations.items():
            annotations_value.additionalProperties.append(
                annotations_value_msg.AdditionalProperty(key=key, value=value))

        message.annotations = annotations_value

    if labels:
        labels_value_msg = getattr(messages, resource_type.value).LabelsValue
        labels_value = labels_value_msg()
        for key, value in labels.items():
            labels_value.additionalProperties.append(
                labels_value_msg.AdditionalProperty(
                    # Base on go/unified-cloud-labels-proposal,
                    # converts camel case key to snake case.
                    key=resource_property.ConvertToSnakeCase(key),
                    value=value))

        message.labels = labels_value
Exemple #7
0
def _extract_keys(keys, search_dict, result_dict):
    """Converts key to multiple cases and attempts to extract from search_dict."""
    for original_key in keys:
        if original_key in search_dict:
            _assign_with_error_on_duplicate(original_key,
                                            search_dict[original_key],
                                            result_dict)
        else:
            # Can error if both camel and snake case matches are present.
            # Note: The below conversion utils don't work all the time.
            # For example, they cannot handle kebab-case.
            camel_case_key = resource_property.ConvertToCamelCase(original_key)
            snake_case_key = resource_property.ConvertToSnakeCase(original_key)
            if camel_case_key in search_dict:
                _assign_with_error_on_duplicate(original_key,
                                                search_dict[camel_case_key],
                                                result_dict)
            if snake_case_key in search_dict:
                _assign_with_error_on_duplicate(original_key,
                                                search_dict[snake_case_key],
                                                result_dict)
Exemple #8
0
def _GetSchemaResourceName(schema_path):
    """Returns the schema resource name given the schema path."""
    basename = os.path.basename(schema_path).split('.')[0]
    return resource_property.ConvertToSnakeCase(basename).replace('_', ' ')
Exemple #9
0
def _ConfigTitle(section_name):
    return resource_property.ConvertToSnakeCase(section_name).replace(
        '_', ' ').replace('-', ' ').title()