def __init__(self, project_id: str, service_account_name: Optional[str] = None, service_account_key_file: Optional[str] = None, version: str = _VERSION) -> None: """Initialise new instance of CloudApiUtils. Args: project_id: GCP project id. service_account_name: The service account name. If provided, the service account will be impersonated to acquire credentials. service_account_key_file: Optional. File containing service account key. If both service_account_name and not service_account_key_file are not passed the default credential will be used. There are following ways to create service accounts: 1. Use `create_service_account_key` method from `cloud_auth` module. 2. Use `gcloud` command line utility as documented here - https://cloud.google.com/iam/docs/creating-managing-service-account-keys version: The version of the service usage service. It defaults to 'v1beta1'. """ if service_account_name: self.client = cloud_auth.build_impersonated_client( 'serviceusage', service_account_name, version) else: if not service_account_key_file: logging.info( 'Neither Service account key file nor servie account name ' 'was provided. So using default credentials.') self.client = cloud_auth.build_service_client( 'serviceusage', service_account_key_file) self.project_id = project_id
def __init__(self, project_id: str, location: str = _LOCATION, service_account_name: Optional[str] = None, service_account_key_file: Optional[str] = None, version: str = _VERSION) -> None: """Initialise new instance of CloudComposerUtils. Args: project_id: GCP project id. location: Optional. Region under which the Composer environment needs to be managed. It defaults to 'us-central1'. Allowed values - https://cloud.google.com/compute/docs/regions-zones/. service_account_name: The service account name. service_account_key_file: Optional. File containing service account key. version: The version of the service. It defaults to 'v1beta1'. """ if service_account_name: self.client = cloud_auth.build_impersonated_client( _CLIENT_NAME, service_account_name, version) else: if service_account_key_file: credentials = cloud_auth.get_credentials( service_account_key_file) else: logging.info( 'Neither service account key file nor service account ' 'name was provided, so using default credentials.') credentials = cloud_auth.get_default_credentials() self.client = cloud_auth.build_service_client( _CLIENT_NAME, credentials) self.project_id = project_id self.location = location
def __init__(self, project_id: str, service_account_name: Optional[str] = None, service_account_key_file: Optional[str] = None, version: str = _VERSION) -> None: """Initialise new instance of CloudApiUtils. Args: project_id: GCP project id. service_account_name: The service account name. service_account_key_file: Optional. File containing service account key. version: The version of the service usage service. It defaults to 'v1beta1'. """ if service_account_name: self.client = cloud_auth.build_impersonated_client( 'serviceusage', service_account_name, version) else: if service_account_key_file: credentials = cloud_auth.get_credentials( service_account_key_file) else: logging.info( 'Neither service account key file nor service account ' 'name was provided, so using default credentials.') credentials = cloud_auth.get_default_credentials() self.client = cloud_auth.build_service_client( 'serviceusage', credentials) self.project_id = project_id
def test_build_impersonated_client(self, mock_impersonate_service_account): service_name = 'service_name' version = 'v2' target_scopes = ['https://www.googleapis.com/auth/devstorage.read_only'] mock_credentials = mock.Mock(spec=service_account.Credentials) mock_impersonate_service_account.return_value = mock_credentials cloud_auth.build_impersonated_client( service_name, self.service_account_name, version=version, target_scopes=target_scopes) self.mock_client.assert_called_once_with( service_name, version, credentials=mock_credentials, cache_discovery=False)
def __init__(self, cm_service_account: str, cm_profile_id: str, **kwargs) -> None: """Initializes the Campaign Manager hook. Args: cm_service_account: Name of service account authenticated as CM user. cm_profile_id: User profile id of the Campaign Manager user account. **kwargs: Other optional arguments. Raises: ValueError if service_account or profile_id is empty. """ if not cm_service_account: raise ValueError('Empty service_account arg not allowed!') if not cm_profile_id: raise ValueError('Empty profile_id arg not allowed!') self._cm_service = cloud_auth.build_impersonated_client( _API_SERVICE, cm_service_account, _API_VERSION, _API_SCOPE) self._profile_id = cm_service_account
def __init__(self, project_id: str, location: str = _LOCATION, service_account_name: Optional[str] = None, service_account_key_file: Optional[str] = None, version: str = _VERSION): """Initializes new instance of CloudSchedulerUtils. Args: project_id: GCP project id. location: Optional. Region under which the Cloud Scheduler needs to be managed. It defaults to 'us-central1'. Allowed values - https://cloud.google.com/compute/docs/regions-zones/. service_account_name: The service account name. service_account_key_file: Optional. File containing service account key. If not passed the default credential will be used. There are following ways to create service accounts: 1. Use `build_service_client` method from `cloud_auth` module. 2. Use `gcloud` command line utility as documented here - https://cloud.google.com/iam/docs/creating-managing-service-account-keys version: The version of the service. It defaults to 'v1beta1'. Raises: ValueError: If neither service_account_key_file or service_account_name were provided. """ if not service_account_key_file and not service_account_name: raise ValueError( 'Service account key file or service account name is not provided. ' 'Provide either path to service account key file or name of the ' 'service account.') if service_account_key_file: credentials = cloud_auth.get_credentials(service_account_key_file) self._client = cloud_auth.build_service_client( _CLIENT_NAME, credentials) else: self._client = cloud_auth.build_impersonated_client( _CLIENT_NAME, service_account_name, version) self._parent = f'projects/{project_id}/locations/{location}'
def __init__(self, project_id: str, location: str = _LOCATION, service_account_name: Optional[str] = None, service_account_key_file: Optional[str] = None, version: str = _VERSION) -> None: """Initialise new instance of CloudComposerUtils. Args: project_id: GCP project id. location: Optional. Region under which the Composer environment needs to be managed. It defaults to 'us-central1'. Allowed values - https://cloud.google.com/compute/docs/regions-zones/. service_account_name: The service account name. service_account_key_file: Optional. File containing service account key. If both service_account_name and not service_account_key_file are not passed the default credential will be used. There are following ways to create service accounts: 1. Use `build_service_client` method from `cloud_auth` module. 2. Use `gcloud` command line utility as documented here - https://cloud.google.com/iam/docs/creating-managing-service-account-keys version: The version of the service. It defaults to 'v1beta1'. """ if service_account_name: self.client = cloud_auth.build_impersonated_client( _CLIENT_NAME, service_account_name, version) else: if not service_account_key_file: logging.info( 'Neither Service account key file nor service account ' 'name was provided. So using default credentials.') self.client = cloud_auth.build_service_client( _CLIENT_NAME, service_account_key_file) self.project_id = project_id self.location = location