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

Commit

Permalink
feat: add from_service_account_info (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
yoshi-automation committed Dec 23, 2020
1 parent 628ada4 commit d8d921f
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
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

0 comments on commit d8d921f

Please sign in to comment.