Skip to content
This repository has been archived by the owner on Jan 13, 2025. It is now read-only.

feat: add from_service_account_info #24

Merged
merged 1 commit into from
Mar 23, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions google/cloud/orgpolicy_v2/services/org_policy/async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,36 @@ class OrgPolicyAsyncClient:
OrgPolicyClient.parse_common_location_path
)

from_service_account_info = OrgPolicyClient.from_service_account_info
from_service_account_file = OrgPolicyClient.from_service_account_file
@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:
OrgPolicyAsyncClient: The constructed client.
"""
return OrgPolicyClient.from_service_account_info.__func__(OrgPolicyAsyncClient, info, *args, **kwargs) # type: ignore

@classmethod
def from_service_account_file(cls, filename: str, *args, **kwargs):
"""Creates an instance of this client using the provided credentials
file.

Args:
filename (str): The path to the service account private key json
file.
args: Additional arguments to pass to the constructor.
kwargs: Additional arguments to pass to the constructor.

Returns:
OrgPolicyAsyncClient: The constructed client.
"""
return OrgPolicyClient.from_service_account_file.__func__(OrgPolicyAsyncClient, filename, *args, **kwargs) # type: ignore

from_service_account_json = from_service_account_file

@property
Expand Down
99 changes: 95 additions & 4 deletions synth.metadata
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
{
"git": {
"name": ".",
"remote": "git@github.com:googleapis/python-org-policy.git",
"sha": "4797e6d354848feb3b88f718c5e676929c9e4b5c"
"remote": "https://github.com/googleapis/python-org-policy.git",
"sha": "e60fdecf3b90d1a8863f996abc23683937b2e5da"
}
},
{
"git": {
"name": "googleapis",
"remote": "https://github.com/googleapis/googleapis.git",
"sha": "c06bbe28cc7287a55bf7926ee48da2565854de7f",
"internalRef": "359364666"
"sha": "07932bb995e7dc91b43620ea8402c6668c7d102c",
"internalRef": "359562873"
}
},
{
Expand Down Expand Up @@ -40,5 +40,96 @@
"generator": "bazel"
}
}
],
"generatedFiles": [
".coveragerc",
".flake8",
".github/CONTRIBUTING.md",
".github/ISSUE_TEMPLATE/bug_report.md",
".github/ISSUE_TEMPLATE/feature_request.md",
".github/ISSUE_TEMPLATE/support_request.md",
".github/PULL_REQUEST_TEMPLATE.md",
".github/header-checker-lint.yml",
".github/release-please.yml",
".github/snippet-bot.yml",
".gitignore",
".kokoro/build.sh",
".kokoro/continuous/common.cfg",
".kokoro/continuous/continuous.cfg",
".kokoro/docker/docs/Dockerfile",
".kokoro/docker/docs/fetch_gpg_keys.sh",
".kokoro/docs/common.cfg",
".kokoro/docs/docs-presubmit.cfg",
".kokoro/docs/docs.cfg",
".kokoro/populate-secrets.sh",
".kokoro/presubmit/common.cfg",
".kokoro/presubmit/presubmit.cfg",
".kokoro/publish-docs.sh",
".kokoro/release.sh",
".kokoro/release/common.cfg",
".kokoro/release/release.cfg",
".kokoro/samples/lint/common.cfg",
".kokoro/samples/lint/continuous.cfg",
".kokoro/samples/lint/periodic.cfg",
".kokoro/samples/lint/presubmit.cfg",
".kokoro/samples/python3.6/common.cfg",
".kokoro/samples/python3.6/continuous.cfg",
".kokoro/samples/python3.6/periodic.cfg",
".kokoro/samples/python3.6/presubmit.cfg",
".kokoro/samples/python3.7/common.cfg",
".kokoro/samples/python3.7/continuous.cfg",
".kokoro/samples/python3.7/periodic.cfg",
".kokoro/samples/python3.7/presubmit.cfg",
".kokoro/samples/python3.8/common.cfg",
".kokoro/samples/python3.8/continuous.cfg",
".kokoro/samples/python3.8/periodic.cfg",
".kokoro/samples/python3.8/presubmit.cfg",
".kokoro/test-samples.sh",
".kokoro/trampoline.sh",
".kokoro/trampoline_v2.sh",
".pre-commit-config.yaml",
".trampolinerc",
"CODE_OF_CONDUCT.md",
"CONTRIBUTING.rst",
"LICENSE",
"MANIFEST.in",
"docs/_static/custom.css",
"docs/_templates/layout.html",
"docs/conf.py",
"docs/multiprocessing.rst",
"docs/orgpolicy_v2/org_policy.rst",
"docs/orgpolicy_v2/services.rst",
"docs/orgpolicy_v2/types.rst",
"google/cloud/orgpolicy/__init__.py",
"google/cloud/orgpolicy/py.typed",
"google/cloud/orgpolicy_v2/__init__.py",
"google/cloud/orgpolicy_v2/py.typed",
"google/cloud/orgpolicy_v2/services/__init__.py",
"google/cloud/orgpolicy_v2/services/org_policy/__init__.py",
"google/cloud/orgpolicy_v2/services/org_policy/async_client.py",
"google/cloud/orgpolicy_v2/services/org_policy/client.py",
"google/cloud/orgpolicy_v2/services/org_policy/pagers.py",
"google/cloud/orgpolicy_v2/services/org_policy/transports/__init__.py",
"google/cloud/orgpolicy_v2/services/org_policy/transports/base.py",
"google/cloud/orgpolicy_v2/services/org_policy/transports/grpc.py",
"google/cloud/orgpolicy_v2/services/org_policy/transports/grpc_asyncio.py",
"google/cloud/orgpolicy_v2/types/__init__.py",
"google/cloud/orgpolicy_v2/types/constraint.py",
"google/cloud/orgpolicy_v2/types/orgpolicy.py",
"mypy.ini",
"noxfile.py",
"renovate.json",
"scripts/decrypt-secrets.sh",
"scripts/fixup_orgpolicy_v2_keywords.py",
"scripts/readme-gen/readme_gen.py",
"scripts/readme-gen/templates/README.tmpl.rst",
"scripts/readme-gen/templates/auth.tmpl.rst",
"scripts/readme-gen/templates/auth_api_key.tmpl.rst",
"scripts/readme-gen/templates/install_deps.tmpl.rst",
"scripts/readme-gen/templates/install_portaudio.tmpl.rst",
"setup.cfg",
"testing/.gitignore",
"tests/unit/gapic/orgpolicy_v2/__init__.py",
"tests/unit/gapic/orgpolicy_v2/test_org_policy.py"
]
}
15 changes: 15 additions & 0 deletions tests/unit/gapic/orgpolicy_v2/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,16 @@
# -*- coding: utf-8 -*-

# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
122 changes: 120 additions & 2 deletions tests/unit/gapic/orgpolicy_v2/test_org_policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,17 @@ def test__get_default_mtls_endpoint():
assert OrgPolicyClient._get_default_mtls_endpoint(non_googleapi) == non_googleapi


def test_org_policy_client_from_service_account_info():
@pytest.mark.parametrize("client_class", [OrgPolicyClient, OrgPolicyAsyncClient,])
def test_org_policy_client_from_service_account_info(client_class):
creds = credentials.AnonymousCredentials()
with mock.patch.object(
service_account.Credentials, "from_service_account_info"
) as factory:
factory.return_value = creds
info = {"valid": True}
client = OrgPolicyClient.from_service_account_info(info)
client = client_class.from_service_account_info(info)
assert client.transport._credentials == creds
assert isinstance(client, client_class)

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

Expand All @@ -104,9 +106,11 @@ def test_org_policy_client_from_service_account_file(client_class):
factory.return_value = creds
client = client_class.from_service_account_file("dummy/file/path.json")
assert client.transport._credentials == creds
assert isinstance(client, client_class)

client = client_class.from_service_account_json("dummy/file/path.json")
assert client.transport._credentials == creds
assert isinstance(client, client_class)

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

Expand Down Expand Up @@ -463,6 +467,22 @@ def test_list_constraints_from_dict():
test_list_constraints(request_type=dict)


def test_list_constraints_empty_call():
# This test is a coverage failsafe to make sure that totally empty calls,
# i.e. request == None and no flattened fields passed, work.
client = OrgPolicyClient(
credentials=credentials.AnonymousCredentials(), transport="grpc",
)

# Mock the actual call within the gRPC stub, and fake the request.
with mock.patch.object(type(client.transport.list_constraints), "__call__") as call:
client.list_constraints()
call.assert_called()
_, args, _ = call.mock_calls[0]

assert args[0] == orgpolicy.ListConstraintsRequest()


@pytest.mark.asyncio
async def test_list_constraints_async(
transport: str = "grpc_asyncio", request_type=orgpolicy.ListConstraintsRequest
Expand Down Expand Up @@ -794,6 +814,22 @@ def test_list_policies_from_dict():
test_list_policies(request_type=dict)


def test_list_policies_empty_call():
# This test is a coverage failsafe to make sure that totally empty calls,
# i.e. request == None and no flattened fields passed, work.
client = OrgPolicyClient(
credentials=credentials.AnonymousCredentials(), transport="grpc",
)

# Mock the actual call within the gRPC stub, and fake the request.
with mock.patch.object(type(client.transport.list_policies), "__call__") as call:
client.list_policies()
call.assert_called()
_, args, _ = call.mock_calls[0]

assert args[0] == orgpolicy.ListPoliciesRequest()


@pytest.mark.asyncio
async def test_list_policies_async(
transport: str = "grpc_asyncio", request_type=orgpolicy.ListPoliciesRequest
Expand Down Expand Up @@ -1105,6 +1141,22 @@ def test_get_policy_from_dict():
test_get_policy(request_type=dict)


def test_get_policy_empty_call():
# This test is a coverage failsafe to make sure that totally empty calls,
# i.e. request == None and no flattened fields passed, work.
client = OrgPolicyClient(
credentials=credentials.AnonymousCredentials(), transport="grpc",
)

# Mock the actual call within the gRPC stub, and fake the request.
with mock.patch.object(type(client.transport.get_policy), "__call__") as call:
client.get_policy()
call.assert_called()
_, args, _ = call.mock_calls[0]

assert args[0] == orgpolicy.GetPolicyRequest()


@pytest.mark.asyncio
async def test_get_policy_async(
transport: str = "grpc_asyncio", request_type=orgpolicy.GetPolicyRequest
Expand Down Expand Up @@ -1294,6 +1346,24 @@ def test_get_effective_policy_from_dict():
test_get_effective_policy(request_type=dict)


def test_get_effective_policy_empty_call():
# This test is a coverage failsafe to make sure that totally empty calls,
# i.e. request == None and no flattened fields passed, work.
client = OrgPolicyClient(
credentials=credentials.AnonymousCredentials(), transport="grpc",
)

# Mock the actual call within the gRPC stub, and fake the request.
with mock.patch.object(
type(client.transport.get_effective_policy), "__call__"
) as call:
client.get_effective_policy()
call.assert_called()
_, args, _ = call.mock_calls[0]

assert args[0] == orgpolicy.GetEffectivePolicyRequest()


@pytest.mark.asyncio
async def test_get_effective_policy_async(
transport: str = "grpc_asyncio", request_type=orgpolicy.GetEffectivePolicyRequest
Expand Down Expand Up @@ -1491,6 +1561,22 @@ def test_create_policy_from_dict():
test_create_policy(request_type=dict)


def test_create_policy_empty_call():
# This test is a coverage failsafe to make sure that totally empty calls,
# i.e. request == None and no flattened fields passed, work.
client = OrgPolicyClient(
credentials=credentials.AnonymousCredentials(), transport="grpc",
)

# Mock the actual call within the gRPC stub, and fake the request.
with mock.patch.object(type(client.transport.create_policy), "__call__") as call:
client.create_policy()
call.assert_called()
_, args, _ = call.mock_calls[0]

assert args[0] == orgpolicy.CreatePolicyRequest()


@pytest.mark.asyncio
async def test_create_policy_async(
transport: str = "grpc_asyncio", request_type=orgpolicy.CreatePolicyRequest
Expand Down Expand Up @@ -1690,6 +1776,22 @@ def test_update_policy_from_dict():
test_update_policy(request_type=dict)


def test_update_policy_empty_call():
# This test is a coverage failsafe to make sure that totally empty calls,
# i.e. request == None and no flattened fields passed, work.
client = OrgPolicyClient(
credentials=credentials.AnonymousCredentials(), transport="grpc",
)

# Mock the actual call within the gRPC stub, and fake the request.
with mock.patch.object(type(client.transport.update_policy), "__call__") as call:
client.update_policy()
call.assert_called()
_, args, _ = call.mock_calls[0]

assert args[0] == orgpolicy.UpdatePolicyRequest()


@pytest.mark.asyncio
async def test_update_policy_async(
transport: str = "grpc_asyncio", request_type=orgpolicy.UpdatePolicyRequest
Expand Down Expand Up @@ -1876,6 +1978,22 @@ def test_delete_policy_from_dict():
test_delete_policy(request_type=dict)


def test_delete_policy_empty_call():
# This test is a coverage failsafe to make sure that totally empty calls,
# i.e. request == None and no flattened fields passed, work.
client = OrgPolicyClient(
credentials=credentials.AnonymousCredentials(), transport="grpc",
)

# Mock the actual call within the gRPC stub, and fake the request.
with mock.patch.object(type(client.transport.delete_policy), "__call__") as call:
client.delete_policy()
call.assert_called()
_, args, _ = call.mock_calls[0]

assert args[0] == orgpolicy.DeletePolicyRequest()


@pytest.mark.asyncio
async def test_delete_policy_async(
transport: str = "grpc_asyncio", request_type=orgpolicy.DeletePolicyRequest
Expand Down