Skip to content
This repository has been archived by the owner on Nov 29, 2023. It is now read-only.

feat: add from_service_account_info #31

Merged
merged 1 commit into from Dec 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -89,6 +89,7 @@ class OsConfigServiceAsyncClient:
OsConfigServiceClient.parse_common_location_path
)

from_service_account_info = OsConfigServiceClient.from_service_account_info
from_service_account_file = OsConfigServiceClient.from_service_account_file
from_service_account_json = from_service_account_file

Expand Down
16 changes: 16 additions & 0 deletions google/cloud/osconfig_v1/services/os_config_service/client.py
Expand Up @@ -117,6 +117,22 @@ def _get_default_mtls_endpoint(api_endpoint):
DEFAULT_ENDPOINT
)

@classmethod
def from_service_account_info(cls, info: dict, *args, **kwargs):
"""Creates an instance of this client using the provided credentials info.

Args:
info (dict): The service account private key info.
args: Additional arguments to pass to the constructor.
kwargs: Additional arguments to pass to the constructor.

Returns:
{@api.name}: The constructed client.
"""
credentials = service_account.Credentials.from_service_account_info(info)
kwargs["credentials"] = credentials
return cls(*args, **kwargs)

@classmethod
def from_service_account_file(cls, filename: str, *args, **kwargs):
"""Creates an instance of this client using the provided credentials
Expand Down
2 changes: 1 addition & 1 deletion synth.metadata
Expand Up @@ -4,7 +4,7 @@
"git": {
"name": ".",
"remote": "https://github.com/googleapis/python-os-config.git",
"sha": "b84dce022c84d79e8193c6881338fb3f79345740"
"sha": "628ada4004b1add04f5c2d95b9b1cad48616cf2c"
}
},
{
Expand Down
13 changes: 13 additions & 0 deletions tests/unit/gapic/osconfig_v1/test_os_config_service.py
Expand Up @@ -93,6 +93,19 @@ def test__get_default_mtls_endpoint():
)


def test_os_config_service_client_from_service_account_info():
creds = credentials.AnonymousCredentials()
with mock.patch.object(
service_account.Credentials, "from_service_account_info"
) as factory:
factory.return_value = creds
info = {"valid": True}
client = OsConfigServiceClient.from_service_account_info(info)
assert client.transport._credentials == creds

assert client.transport._host == "osconfig.googleapis.com:443"


@pytest.mark.parametrize(
"client_class", [OsConfigServiceClient, OsConfigServiceAsyncClient]
)
Expand Down