def testRequestEnablementPromptsOnlyOnce(self): properties.VALUES.core.should_prompt_to_enable_api.Set(True) prompt_to_enable_mock = self.StartPatch( 'googlecloudsdk.api_lib.util.apis.' 'PromptToEnableApi') service_enabled_response = http_wrapper.Response({'status': '200'}, '{"field": "abc"}', '') service_disabled_response = http_wrapper.Response({'status': '403'}, textwrap.dedent("""\ {{ "error": {{ "code": 403, "message": "{message}", "status": "PERMISSION_DENIED" }} }}""").format(message=self.message), '') callback = apis.CheckResponseForApiEnablement() callback(service_disabled_response) # Catch two retries while we wait for enablement propagation with self.AssertRaisesExceptionMatches( apitools_exceptions.RequestError, 'Retry'): callback(service_disabled_response) callback(service_disabled_response) callback(service_enabled_response) prompt_to_enable_mock.assert_called_once_with(self.project, self.service, mock.ANY)
def ConnectEventManaged(conn_context): """Provides an EventflowOperations instance to use, for managed events.""" # pylint: disable=protected-access client = apis_internal._GetClientInstance( conn_context.api_name, conn_context.api_version, # Only check response if not connecting to GKE check_response_func=apis.CheckResponseForApiEnablement() if conn_context.supports_one_platform else None, http_client=conn_context.HttpClient()) # This client is used for working with core resources (e.g. Secrets) in # Cloud Run for Anthos. core_client = None if not conn_context.supports_one_platform: core_client = apis_internal._GetClientInstance( conn_context.api_name, _CORE_CLIENT_VERSION, http_client=conn_context.HttpClient()) # This client is only used to get CRDs because the api group they are # under uses different versioning in k8s crd_client = apis_internal._GetClientInstance( conn_context.api_name, _CRD_CLIENT_VERSION, http_client=conn_context.HttpClient()) # pylint: enable=protected-access return EventflowOperations(client, conn_context.api_version, conn_context.region, core_client, crd_client)
def Connect(conn_context): """Provide a EventflowOperations instance to use. If we're using the GKE Serverless Add-on, connect to the relevant cluster. Otherwise, connect to the right region of GSE. Arguments: conn_context: a context manager that yields a ConnectionInfo and manages a dynamic context that makes connecting to serverless possible. Yields: A EventflowOperations instance. """ # The One Platform client is required for making requests against # endpoints that do not supported Kubernetes-style resource naming # conventions. The One Platform client must be initialized outside of a # connection context so that it does not pick up the api_endpoint_overrides # values from the connection context. op_client = apis.GetClientInstance(conn_context.api_name, conn_context.api_version) with conn_context as conn_info: # pylint: disable=protected-access client = apis_internal._GetClientInstance( conn_info.api_name, conn_info.api_version, # Only check response if not connecting to GKE check_response_func=apis.CheckResponseForApiEnablement() if conn_context.supports_one_platform else None, http_client=conn_context.HttpClient()) # This client is used for working with core resources (e.g. Secrets) in # Cloud Run for Anthos. core_client = None if not conn_context.supports_one_platform: core_client = apis_internal._GetClientInstance( conn_context.api_name, _CORE_CLIENT_VERSION, http_client=conn_context.HttpClient()) # This client is only used to get CRDs because the api group they are # under uses different versioning in k8s crd_client = apis_internal._GetClientInstance( conn_context.api_name, _CRD_CLIENT_VERSION, http_client=conn_context.HttpClient()) # pylint: enable=protected-access yield EventflowOperations(client, conn_info.region, core_client, crd_client, op_client)
def Connect(conn_context): """Provide a ServerlessOperations instance to use. If we're using the GKE Serverless Add-on, connect to the relevant cluster. Otherwise, connect to the right region of GSE. Arguments: conn_context: a context manager that yields a ConnectionInfo and manages a dynamic context that makes connecting to serverless possible. Yields: A ServerlessOperations instance. """ # The One Platform client is required for making requests against # endpoints that do not supported Kubernetes-style resource naming # conventions. The One Platform client must be initialized outside of a # connection context so that it does not pick up the api_endpoint_overrides # values from the connection context. # pylint: disable=protected-access op_client = apis.GetClientInstance( conn_context.api_name, conn_context.api_version) # pylint: enable=protected-access with conn_context as conn_info: # pylint: disable=protected-access client = apis_internal._GetClientInstance( conn_info.api_name, conn_info.api_version, # Only check response if not connecting to GKE check_response_func=apis.CheckResponseForApiEnablement() if conn_context.supports_one_platform else None, http_client=conn_context.HttpClient()) # pylint: enable=protected-access yield ServerlessOperations( client, conn_info.api_name, conn_info.api_version, conn_info.region, op_client)
def Connect(conn_info): """Provide a AnthosEventsOperations instance to use. If we're using the GKE Serverless Add-on, connect to the relevant cluster. Otherwise, connect to the right region of GSE. Arguments: conn_info: a ConnectionInfo Returns: An AnthosEventsOperation instance. """ # pylint: disable=protected-access v1beta1_client = apis_internal._GetClientInstance( _ANTHOS_EVENTS_CLIENT_NAME, _ANTHOS_EVENTS_CLIENT_VERSION, # Only check response if not connecting to GKE check_response_func=apis.CheckResponseForApiEnablement() if conn_info.supports_one_platform else None, http_client=conn_info.HttpClient()) # This client is used for working with core resources (e.g. Secrets) in # Cloud Run for Anthos. v1_client = apis_internal._GetClientInstance( _ANTHOS_EVENTS_CLIENT_NAME, _CORE_CLIENT_VERSION, http_client=conn_info.HttpClient()) v1alpha1_client = apis_internal._GetClientInstance( _ANTHOS_EVENTS_CLIENT_NAME, _OPERATOR_CLIENT_VERSION, http_client=conn_info.HttpClient()) # pylint: enable=protected-access return AnthosEventsOperations(conn_info.api_version, conn_info.region, v1_client, v1alpha1_client, v1beta1_client)