def GetParentRef(self, parameter_info, aggregations=None):
    # type: (...) -> typing.Optional[resources.Resource]
    """Gets the parent reference of the parsed parameters.

    Args:
      parameter_info: the runtime ResourceParameterInfo object.
      aggregations: a list of _RuntimeParameter objects.

    Returns:
      googlecloudsdk.core.resources.Resource, the parent reference | None, if
        no parent could be parsed.
    """
    param_values = {
        p: parameter_info.GetValue(p)
        for p in self.collection_info.GetParams('')[:-1]
    }
    aggregations_dict = self._GetAggregationsValuesDict(aggregations)
    for name, value in six.iteritems(aggregations_dict):
      if value and not param_values.get(name, None):
        param_values[name] = value
    final_param = self.collection_info.GetParams('')[-1]
    if param_values.get(final_param, None) is None:
      param_values[final_param] = 'fake'  # Stripped by resource.Parent() below.
    try:
      resource = resources.Resource(
          resources.REGISTRY,
          collection_info=self.collection_info,
          subcollection='',
          param_values=param_values,
          endpoint_url=None)
      return resource.Parent()
    except resources.Error:
      return None
  def Parse(self, parent_params, parameter_info, aggregations_dict):
    """Parse the parent resource from parameter info and aggregations.

    Args:
      parent_params: [str], a list of params in the current collection's parent
        collection.
      parameter_info: the runtime ResourceParameterInfo object.
      aggregations_dict: {str: str}, a dict of params to values that are
        being aggregated from earlier updates.

    Returns:
      resources.Resource | None, the parsed parent reference or None if there
        is not enough information to parse.
    """
    param_values = {
        self.param_translation.get(p, p): parameter_info.GetValue(p)
        for p in parent_params}
    for p, value in six.iteritems(aggregations_dict):
      translated_name = self.param_translation.get(p, p)
      if value and not param_values.get(translated_name, None):
        param_values[translated_name] = value
    try:
      return resources.Resource(
          resources.REGISTRY,
          collection_info=resources.REGISTRY.GetCollectionInfo(self.collection),
          subcollection='',
          param_values=param_values,
          endpoint_url=None)
    # Not all completion list calls may need to have a parent, so even if we
    # can't parse a parent, we log the error and attempt to send an update call
    # without one. (Any error returned by the API will be raised.)
    except resources.Error as e:
      log.info(six.text_type(e).rstrip())
      return None
 def _ParseDefaultParent(self, param_values):
     """Parse the parent for a resource using default collection."""
     resource = resources.Resource(resources.REGISTRY,
                                   collection_info=self.collection_info,
                                   subcollection='',
                                   param_values=param_values,
                                   endpoint_url=None)
     return resource.Parent()
Esempio n. 4
0
 def Run(self, args):
   """Returns the list of generated resources."""
   collection_info = resources.REGISTRY.GetCollectionInfo(
       args.collection, api_version=args.api_version)
   templates = {}
   for param in collection_info.GetParams(''):
     templates[param] = 'my-' + param.lower() + '-{}'
   uris = []
   for i in range(1, args.count + 1):
     params = {}
     for param, template in templates.iteritems():
       params[param] = template.format(i)
     uri = resources.Resource(collection_info, '', params, None).SelfLink()
     uris.append(uri)
   return uris
Esempio n. 5
0
 def Run(self, args):
     """Returns the list of generated resources."""
     collection_info = resources.REGISTRY.GetCollectionInfo(
         args.collection, api_version=args.api_version)
     templates = {}
     params = collection_info.GetParams('')
     if not params:
         # There are some collections that don't operate on resources but are
         # manually created in regen_apis_config.yaml. If there are no template
         # params, don't try to generate resources for them.
         return []
     for param in params:
         templates[param] = 'my-' + param.lower() + '-{}'
     uris = []
     for i in range(1, args.count + 1):
         params = {}
         for param, template in six.iteritems(templates):
             params[param] = template.format(i)
         uri = resources.Resource(None, collection_info, '', params,
                                  None).SelfLink()
         uris.append(uri)
     return uris