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
def testParseStaticFieldsIntoMessage(self): self.MockCRUDMethods(('foo.projects.instances', True)) method = registry.GetMethod('foo.projects.instances', 'list') message_type = method.GetRequestType() message = message_type() static_fields = {'pageSize': 1} arg_utils.ParseStaticFieldsIntoMessage(message, static_fields) self.assertEqual(message.pageSize, 1)
def _MakeCloudRun(self, kind='CloudPubSubSource', api_category='sources.eventing.knative.dev', **kwargs): cloud_run_obj = cloud_run.CloudRun.New(self.mock_operator_client, 'cloud-run-system') arg_utils.ParseStaticFieldsIntoMessage(cloud_run_obj.Message(), kwargs) return cloud_run_obj
def CreateNamespace(self, namespace_ref): """Create a namespace.""" namespace_name = namespace_ref.Name() namespace_message = self._core_client.MESSAGES_MODULE.Namespace() arg_utils.ParseStaticFieldsIntoMessage( namespace_message, {'metadata.name': namespace_name}) request = (self._core_client.MESSAGES_MODULE. AnthoseventsApiV1NamespacesCreateRequest( namespace=namespace_message, parent=namespace_ref.RelativeName())) try: return self._core_client.api_v1_namespaces.Create(request) except api_exceptions.HttpConflictError: raise exceptions.NamespaceCreationError( 'Namespace [{}] already exists'.format(namespace_name))
def _MakeTrigger(self, **kwargs): """Creates a new trigger.Trigger. Args: **kwargs: fields on the underlying Trigger message mapped to the values they should be set to. Fields of arbitrary depth can be specified via dot-notation (e.g "metadata.name"). Returns: trigger.Trigger whose underlying message has been modified with the given values. """ trigger_obj = trigger.Trigger.New(self.mock_client, 'default') arg_utils.ParseStaticFieldsIntoMessage(trigger_obj.Message(), kwargs) return trigger_obj
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
def CreateSource(self, source_obj, source_crd, owner_trigger, namespace_ref, broker, parameters): """Create an source with the specified event type and owner trigger. Args: source_obj: source.Source. The source object being created. source_crd: custom_resource_definition.SourceCRD, the source crd for the source to create owner_trigger: trigger.Trigger, trigger to associate as an owner of the source. namespace_ref: googlecloudsdk.core.resources.Resource, namespace resource. broker: str, name of the broker to act as a sink. parameters: dict, additional parameters to set on the source spec. Returns: source.Source of the created source. """ source_obj.ce_overrides[trigger.SOURCE_TRIGGER_LINK_FIELD] = ( owner_trigger.filter_attributes[trigger.SOURCE_TRIGGER_LINK_FIELD]) source_obj.owners.append( self.messages.OwnerReference( apiVersion=owner_trigger.apiVersion, kind=owner_trigger.kind, name=owner_trigger.name, uid=owner_trigger.uid, controller=True)) source_obj.sink = broker arg_utils.ParseStaticFieldsIntoMessage(source_obj.spec, parameters) request_message_type = self.SourceCreateMessage(source_crd) source_field = ( source_crd.source_kind[0].lower() + source_crd.source_kind[1:]) request = request_message_type(**{ source_field: source_obj.Message(), 'parent': namespace_ref.RelativeName()}) with metrics.RecordDuration(metric_names.CREATE_SOURCE): try: client_service = self.SourceClientService(source_crd) response = client_service.Create(request) except api_exceptions.HttpConflictError: raise exceptions.SourceCreationError( 'Source [{}] already exists.'.format(source_obj.name)) return source.Source(response, self.messages, source_crd.source_kind)
def _MakeSource(self, kind='CloudPubSubSource', api_category='sources.eventing.knative.dev', **kwargs): """Creates a new source.Source. Args: kind: the Kind of source (e.g. CloudPubSubSource) api_category: the api group of the source (e.g. events.cloud.google.com) **kwargs: fields on the underlying Source message mapped to the values they should be set to. Fields of arbitrary depth can be specified via dot-notation (e.g "metadata.name"). Returns: source.Source whose underlying message has been modified with the given values. """ source_obj = source.Source.New(self.mock_client, 'default', kind, api_category) arg_utils.ParseStaticFieldsIntoMessage(source_obj.Message(), kwargs) return source_obj
def _MakeSecret(self, **kwargs): secret_obj = secret.Secret.New(self.mock_core_client, 'default') arg_utils.ParseStaticFieldsIntoMessage(secret_obj.Message(), kwargs) return secret_obj
def _MakeNamespace(self, **kwargs): namespace = self.core_messages.Namespace() arg_utils.ParseStaticFieldsIntoMessage(namespace, kwargs) return namespace
def _MakeConfigMap(self, namespace, **kwargs): configmap_obj = configmap.ConfigMap.New(self.mock_core_client, namespace) arg_utils.ParseStaticFieldsIntoMessage(configmap_obj.Message(), kwargs) return configmap_obj