def BuildListQuery(self, parameter_info, aggregations=None,
                     parent_translator=None):
    """Builds a list request to list values for the given argument.

    Args:
      parameter_info: the runtime ResourceParameterInfo object.
      aggregations: a list of _RuntimeParameter objects.
      parent_translator: a ParentTranslator object if needed.

    Returns:
      The apitools request.
    """
    method = self.method
    if method is None:
      return None
    message = method.GetRequestType()()
    for field, value in six.iteritems(self._static_params):
      arg_utils.SetFieldInMessage(message, field, value)
    parent = self.GetParent(parameter_info, aggregations=aggregations,
                            parent_translator=parent_translator)
    if not parent:
      return message
    resource_method_params = {}

    if parent_translator:
      resource_method_params = parent_translator.ResourceMethodParams(message)

    arg_utils.ParseResourceIntoMessage(
        parent, method, message,
        resource_method_params=resource_method_params)
    return message
  def CreateRequest(self,
                    namespace,
                    static_fields=None,
                    resource_method_params=None,
                    use_relative_name=True,
                    override_method=None,
                    parse_resource_into_request=True,
                    existing_message=None):
    """Generates the request object for the method call from the parsed args.

    Args:
      namespace: The argparse namespace.
      static_fields: {str, value}, A mapping of API field name to value to
        insert into the message. This is a convenient way to insert extra data
        while the request is being constructed for fields that don't have
        corresponding arguments.
      resource_method_params: {str: str}, A mapping of API method parameter name
        to resource ref attribute name when the API method uses non-standard
        names.
      use_relative_name: Use ref.RelativeName() if True otherwise ref.Name().
      override_method: APIMethod, The method other than self.method, this is
        used when the command has more than one API call.
      parse_resource_into_request: bool, True if the resource reference should
        be automatically parsed into the request.
      existing_message: the apitools message returned from server, which is used
        to construct the to-be-modified message when the command follows
        get-modify-update pattern.

    Returns:
      The apitools message to be send to the method.
    """
    message_type = (override_method or self.method).GetRequestType()
    message = message_type()

    # If an apitools message is provided, use the existing one by default
    # instead of creating an empty one.
    if existing_message:
      message = arg_utils.ParseExistingMessageIntoMessage(
          message, existing_message, self.method)

    # Insert static fields into message.
    arg_utils.ParseStaticFieldsIntoMessage(message, static_fields=static_fields)

    # Parse api Fields into message.
    self._ParseArguments(message, namespace)

    ref = self._ParseResourceArg(namespace)
    if not ref:
      return message

    # For each method path field, get the value from the resource reference.
    if parse_resource_into_request:
      arg_utils.ParseResourceIntoMessage(
          ref, self.method, message,
          resource_method_params=resource_method_params,
          request_id_field=self.resource_arg.request_id_field,
          use_relative_name=use_relative_name)

    return message
Ejemplo n.º 3
0
 def testParseResourceIntoMessageCreateWithParentResource(self):
   self.MockCRUDMethods(('foo.projects.locations.instances', True))
   method = registry.GetMethod('foo.projects.locations.instances', 'create')
   message = method.GetRequestType()()
   parent_ref = resources.REGISTRY.Parse(
       'projects/p/locations/l',
       collection='foo.projects.locations')
   arg_utils.ParseResourceIntoMessage(parent_ref, method, message)
   self.assertEqual('projects/p/locations/l', message.parent)
Ejemplo n.º 4
0
 def testParseResourceIntoMessageList(self):
   self.MockCRUDMethods(('foo.projects.locations.instances', True))
   method = registry.GetMethod('foo.projects.locations.instances', 'list')
   message = method.GetRequestType()()
   ref = resources.REGISTRY.Parse(
       'projects/p/locations/l',
       collection=method.request_collection.full_name)
   arg_utils.ParseResourceIntoMessage(
       ref,
       method,
       message)
   self.assertEqual('projects/p/locations/l', message.parent)
Ejemplo n.º 5
0
 def testParseResourceIntoMessageCreate(self):
   self.MockCRUDMethods(('foo.projects.locations.instances', True))
   method = registry.GetMethod('foo.projects.locations.instances', 'create')
   message = method.GetRequestType()()
   message.field_by_name = mock.MagicMock()
   ref = resources.REGISTRY.Parse(
       'projects/p/locations/l/instances/i',
       collection=method.resource_argument_collection.full_name)
   arg_utils.ParseResourceIntoMessage(
       ref,
       method,
       message,
       request_id_field='name')
   self.assertEqual('projects/p/locations/l', message.parent)
   self.assertEqual('i', message.name)
  def GetResult(self, operation):
    """Overrides.

    Args:
      operation: api_name_messages.Operation.

    Returns:
      result of result_service.Get request.
    """
    result = operation
    if self.resource_ref:
      method = self._ResourceGetMethod()
      request = method.GetRequestType()()
      arg_utils.ParseResourceIntoMessage(self.resource_ref, method, request)
      result = method.Call(request)
    return _GetAttribute(result, self.spec.async.result_attribute)
Ejemplo n.º 7
0
 def testParseResourceIntoMessageWithParamsNonMethodParamsRelativeName(self):
   self.MockCRUDMethods(('foo.projects.locations.instances', False))
   method = registry.GetMethod('foo.projects.locations.instances', 'get')
   method.params = []
   message = fm.FakeMessage()
   ref = resources.REGISTRY.Parse(
       'projects/p/locations/l/instances/i',
       collection=method.request_collection.full_name)
   arg_utils.ParseResourceIntoMessage(
       ref,
       method,
       message,
       resource_method_params={
           'string1': '',
       })
   self.assertEqual('projects/p/locations/l/instances/i', message.string1)
Ejemplo n.º 8
0
    def CreateRequest(self,
                      namespace,
                      static_fields=None,
                      resource_method_params=None,
                      use_relative_name=True,
                      override_method=None):
        """Generates the request object for the method call from the parsed args.

    Args:
      namespace: The argparse namespace.
      static_fields: {str, value}, A mapping of API field name to value to
        insert into the message. This is a convenient way to insert extra data
        while the request is being constructed for fields that don't have
        corresponding arguments.
      resource_method_params: {str: str}, A mapping of API method parameter name
        to resource ref attribute name when the API method uses non-standard
        names.
      use_relative_name: Use ref.RelativeName() if True otherwise ref.Name().
      override_method: APIMethod, The method other than self.method, this is
        used when the command has more than one API call.

    Returns:
      The apitools message to be send to the method.
    """
        message_type = (override_method or self.method).GetRequestType()
        message = message_type()

        # Insert static fields into message.
        arg_utils.ParseStaticFieldsIntoMessage(message,
                                               static_fields=static_fields)

        # Parse api Fields into message.
        self._ParseArguments(message, namespace)

        ref = self._ParseResourceArg(namespace)
        if not ref:
            return message

        # For each method path field, get the value from the resource reference.
        arg_utils.ParseResourceIntoMessage(
            ref,
            self.method,
            message,
            resource_method_params=resource_method_params,
            request_id_field=self.resource_arg.request_id_field,
            use_relative_name=use_relative_name)
        return message
Ejemplo n.º 9
0
 def testParseResourceIntoMessageWithParams(self):
   self.MockCRUDMethods(('foo.projects.locations.instances', False),
                        ('baz.projects.quxs.quuxs', False))
   method = registry.GetMethod('foo.projects.locations.instances', 'get')
   message = method.GetRequestType()()
   ref = resources.REGISTRY.Create(
       'baz.projects.quxs.quuxs',
       projectsId='p', quuxsId='quux', quxsId='qux')
   arg_utils.ParseResourceIntoMessage(
       ref,
       method,
       message,
       resource_method_params={
           'locationsId': 'quxsId',
           'instancesId': 'quuxsId'
       })
   self.assertEqual('p', message.projectsId)
   self.assertEqual('qux', message.locationsId)
   self.assertEqual('quux', message.instancesId)
Ejemplo n.º 10
0
    def BuildListQuery(self, parameter_info, aggregations=None):
        # type: (...) -> typing.Optional[messages.Message]
        """Builds a list request to list values for the given argument.

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

    Returns:
      The apitools request.
    """
        method = self.method
        if method is None:
            return None
        message = method.GetRequestType()()
        for field, value in six.iteritems(self._static_params):
            arg_utils.SetFieldInMessage(message, field, value)
        parent = self.GetParentRef(parameter_info, aggregations=aggregations)
        if not parent:
            return message
        arg_utils.ParseResourceIntoMessage(parent, method, message)
        return message