def test_create_signed_read_url_v4_w_access_token( storage_client, signing_bucket, service_account, ): client = iam_credentials_v1.IAMCredentialsClient() service_account_email = service_account.service_account_email name = path_template.expand( "projects/{project}/serviceAccounts/{service_account}", project="-", service_account=service_account_email, ) scope = [ "https://www.googleapis.com/auth/devstorage.read_write", "https://www.googleapis.com/auth/iam", ] response = client.generate_access_token(name=name, scope=scope) _create_signed_read_url_helper( storage_client, signing_bucket, version="v4", service_account_email=service_account_email, access_token=response.access_token, )
def list_instances( self, location: str, project_id: str, retry: Optional[Retry] = None, timeout: Optional[float] = None, metadata: Optional[Sequence[Tuple[str, str]]] = None, ): """ Lists all Memcached instances owned by a project in either the specified location (region) or all locations. :param location: The location of the Cloud Memorystore instance (for example europe-west1) If it is specified as ``-`` (wildcard), then all regions available to the project are queried, and the results are aggregated. :type location: str :param project_id: Project ID of the project that contains the instance. If set to None or missing, the default project_id from the GCP connection is used. :type project_id: str :param retry: A retry object used to retry requests. If ``None`` is specified, requests will not be retried. :type retry: google.api_core.retry.Retry :param timeout: The amount of time, in seconds, to wait for the request to complete. Note that if ``retry`` is specified, the timeout applies to each individual attempt. :type timeout: float :param metadata: Additional metadata that is provided to the method. :type metadata: Sequence[Tuple[str, str]] """ client = self.get_conn() metadata = metadata or () parent = path_template.expand( "projects/{project}/locations/{location}", project=project_id, location=location) result = client.list_instances( parent=parent, retry=retry, timeout=timeout, metadata=metadata or (), ) self.log.info("Fetched instances") return result
def test_expanded_failure(tmpl, args, kwargs, exc_match): with pytest.raises(ValueError, match=exc_match): path_template.expand(tmpl, *args, **kwargs)
def test_expand_success(tmpl, args, kwargs, expected_result): result = path_template.expand(tmpl, *args, **kwargs) assert result == expected_result assert path_template.validate(tmpl, result)
def create_instance( self, location: str, instance_id: str, instance: Union[Dict, cloud_memcache.Instance], project_id: str, retry: Optional[Retry] = None, timeout: Optional[float] = None, metadata: Optional[Sequence[Tuple[str, str]]] = None, ): """ Creates a Memcached instance based on the specified tier and memory size. By default, the instance is accessible from the project's `default network <https://cloud.google.com/compute/docs/networks-and-firewalls#networks>`__. :param location: The location of the Cloud Memorystore instance (for example europe-west1) :type location: str :param instance_id: Required. The logical name of the Memcached instance in the customer project with the following restrictions: - Must contain only lowercase letters, numbers, and hyphens. - Must start with a letter. - Must be between 1-40 characters. - Must end with a number or a letter. - Must be unique within the customer project / location :type instance_id: str :param instance: Required. A Memcached [Instance] resource If a dict is provided, it must be of the same form as the protobuf message :class:`~google.cloud.memcache_v1beta2.types.cloud_memcache.Instance` :type instance: Union[Dict, google.cloud.memcache_v1beta2.types.cloud_memcache.Instance] :param project_id: Project ID of the project that contains the instance. If set to None or missing, the default project_id from the GCP connection is used. :type project_id: str :param retry: A retry object used to retry requests. If ``None`` is specified, requests will not be retried. :type retry: google.api_core.retry.Retry :param timeout: The amount of time, in seconds, to wait for the request to complete. Note that if ``retry`` is specified, the timeout applies to each individual attempt. :type timeout: float :param metadata: Additional metadata that is provided to the method. :type metadata: Sequence[Tuple[str, str]] """ client = self.get_conn() metadata = metadata or () parent = path_template.expand( "projects/{project}/locations/{location}", project=project_id, location=location) instance_name = CloudMemcacheClient.instance_path( project_id, location, instance_id) try: instance = client.get_instance(name=instance_name, retry=retry, timeout=timeout, metadata=metadata) self.log.info("Instance exists. Skipping creation.") return instance except NotFound: self.log.info("Instance not exists.") if isinstance(instance, dict): instance = cloud_memcache.Instance(instance) elif not isinstance(instance, cloud_memcache.Instance): raise AirflowException( "instance is not instance of Instance type or python dict") self._append_label(instance, "airflow-version", "v" + version.version) result = client.create_instance( parent=parent, instance_id=instance_id, resource=instance, retry=retry, timeout=timeout, metadata=metadata or (), ) result.result() self.log.info("Instance created.") return client.get_instance( name=instance_name, retry=retry, timeout=timeout, metadata=metadata or (), )