Skip to content
Permalink
Browse files
feat: add from_service_account_info (#31)
  • Loading branch information
yoshi-automation committed Dec 23, 2020
1 parent 628ada4 commit d8d921fc28d294039c574e4dc327fbe1caa27337
@@ -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

@@ -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
@@ -4,7 +4,7 @@
"git": {
"name": ".",
"remote": "https://github.com/googleapis/python-os-config.git",
"sha": "b84dce022c84d79e8193c6881338fb3f79345740"
"sha": "628ada4004b1add04f5c2d95b9b1cad48616cf2c"
}
},
{
@@ -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]
)

0 comments on commit d8d921f

Please sign in to comment.