Skip to content

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 authored Dec 23, 2020
1 parent 7c5f8c0 commit 73885e4
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 packages/google-cloud-os-config/synth.metadata
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"git": {
"name": ".",
"remote": "https://github.com/googleapis/python-os-config.git",
"sha": "b84dce022c84d79e8193c6881338fb3f79345740"
"sha": "628ada4004b1add04f5c2d95b9b1cad48616cf2c"
}
},
{
Expand Down
Original file line number Diff line number Diff line change
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 73885e4

Please sign in to comment.