Skip to content
This repository was archived by the owner on Mar 15, 2025. It is now read-only.

Commit cb5881d

Browse files
feat: add from_service_account_info (#24)
PiperOrigin-RevId: 359562873 Source-Author: Google APIs <noreply@google.com> Source-Date: Thu Feb 25 10:52:32 2021 -0800 Source-Repo: googleapis/googleapis Source-Sha: 07932bb995e7dc91b43620ea8402c6668c7d102c Source-Link: googleapis/googleapis@07932bb
1 parent e60fdec commit cb5881d

File tree

4 files changed

+260
-8
lines changed

4 files changed

+260
-8
lines changed

google/cloud/orgpolicy_v2/services/org_policy/async_client.py

+30-2
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,36 @@ class OrgPolicyAsyncClient:
9595
OrgPolicyClient.parse_common_location_path
9696
)
9797

98-
from_service_account_info = OrgPolicyClient.from_service_account_info
99-
from_service_account_file = OrgPolicyClient.from_service_account_file
98+
@classmethod
99+
def from_service_account_info(cls, info: dict, *args, **kwargs):
100+
"""Creates an instance of this client using the provided credentials info.
101+
102+
Args:
103+
info (dict): The service account private key info.
104+
args: Additional arguments to pass to the constructor.
105+
kwargs: Additional arguments to pass to the constructor.
106+
107+
Returns:
108+
OrgPolicyAsyncClient: The constructed client.
109+
"""
110+
return OrgPolicyClient.from_service_account_info.__func__(OrgPolicyAsyncClient, info, *args, **kwargs) # type: ignore
111+
112+
@classmethod
113+
def from_service_account_file(cls, filename: str, *args, **kwargs):
114+
"""Creates an instance of this client using the provided credentials
115+
file.
116+
117+
Args:
118+
filename (str): The path to the service account private key json
119+
file.
120+
args: Additional arguments to pass to the constructor.
121+
kwargs: Additional arguments to pass to the constructor.
122+
123+
Returns:
124+
OrgPolicyAsyncClient: The constructed client.
125+
"""
126+
return OrgPolicyClient.from_service_account_file.__func__(OrgPolicyAsyncClient, filename, *args, **kwargs) # type: ignore
127+
100128
from_service_account_json = from_service_account_file
101129

102130
@property

synth.metadata

+95-4
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
33
{
44
"git": {
55
"name": ".",
6-
"remote": "git@github.com:googleapis/python-org-policy.git",
7-
"sha": "4797e6d354848feb3b88f718c5e676929c9e4b5c"
6+
"remote": "https://github.com/googleapis/python-org-policy.git",
7+
"sha": "e60fdecf3b90d1a8863f996abc23683937b2e5da"
88
}
99
},
1010
{
1111
"git": {
1212
"name": "googleapis",
1313
"remote": "https://github.com/googleapis/googleapis.git",
14-
"sha": "c06bbe28cc7287a55bf7926ee48da2565854de7f",
15-
"internalRef": "359364666"
14+
"sha": "07932bb995e7dc91b43620ea8402c6668c7d102c",
15+
"internalRef": "359562873"
1616
}
1717
},
1818
{
@@ -40,5 +40,96 @@
4040
"generator": "bazel"
4141
}
4242
}
43+
],
44+
"generatedFiles": [
45+
".coveragerc",
46+
".flake8",
47+
".github/CONTRIBUTING.md",
48+
".github/ISSUE_TEMPLATE/bug_report.md",
49+
".github/ISSUE_TEMPLATE/feature_request.md",
50+
".github/ISSUE_TEMPLATE/support_request.md",
51+
".github/PULL_REQUEST_TEMPLATE.md",
52+
".github/header-checker-lint.yml",
53+
".github/release-please.yml",
54+
".github/snippet-bot.yml",
55+
".gitignore",
56+
".kokoro/build.sh",
57+
".kokoro/continuous/common.cfg",
58+
".kokoro/continuous/continuous.cfg",
59+
".kokoro/docker/docs/Dockerfile",
60+
".kokoro/docker/docs/fetch_gpg_keys.sh",
61+
".kokoro/docs/common.cfg",
62+
".kokoro/docs/docs-presubmit.cfg",
63+
".kokoro/docs/docs.cfg",
64+
".kokoro/populate-secrets.sh",
65+
".kokoro/presubmit/common.cfg",
66+
".kokoro/presubmit/presubmit.cfg",
67+
".kokoro/publish-docs.sh",
68+
".kokoro/release.sh",
69+
".kokoro/release/common.cfg",
70+
".kokoro/release/release.cfg",
71+
".kokoro/samples/lint/common.cfg",
72+
".kokoro/samples/lint/continuous.cfg",
73+
".kokoro/samples/lint/periodic.cfg",
74+
".kokoro/samples/lint/presubmit.cfg",
75+
".kokoro/samples/python3.6/common.cfg",
76+
".kokoro/samples/python3.6/continuous.cfg",
77+
".kokoro/samples/python3.6/periodic.cfg",
78+
".kokoro/samples/python3.6/presubmit.cfg",
79+
".kokoro/samples/python3.7/common.cfg",
80+
".kokoro/samples/python3.7/continuous.cfg",
81+
".kokoro/samples/python3.7/periodic.cfg",
82+
".kokoro/samples/python3.7/presubmit.cfg",
83+
".kokoro/samples/python3.8/common.cfg",
84+
".kokoro/samples/python3.8/continuous.cfg",
85+
".kokoro/samples/python3.8/periodic.cfg",
86+
".kokoro/samples/python3.8/presubmit.cfg",
87+
".kokoro/test-samples.sh",
88+
".kokoro/trampoline.sh",
89+
".kokoro/trampoline_v2.sh",
90+
".pre-commit-config.yaml",
91+
".trampolinerc",
92+
"CODE_OF_CONDUCT.md",
93+
"CONTRIBUTING.rst",
94+
"LICENSE",
95+
"MANIFEST.in",
96+
"docs/_static/custom.css",
97+
"docs/_templates/layout.html",
98+
"docs/conf.py",
99+
"docs/multiprocessing.rst",
100+
"docs/orgpolicy_v2/org_policy.rst",
101+
"docs/orgpolicy_v2/services.rst",
102+
"docs/orgpolicy_v2/types.rst",
103+
"google/cloud/orgpolicy/__init__.py",
104+
"google/cloud/orgpolicy/py.typed",
105+
"google/cloud/orgpolicy_v2/__init__.py",
106+
"google/cloud/orgpolicy_v2/py.typed",
107+
"google/cloud/orgpolicy_v2/services/__init__.py",
108+
"google/cloud/orgpolicy_v2/services/org_policy/__init__.py",
109+
"google/cloud/orgpolicy_v2/services/org_policy/async_client.py",
110+
"google/cloud/orgpolicy_v2/services/org_policy/client.py",
111+
"google/cloud/orgpolicy_v2/services/org_policy/pagers.py",
112+
"google/cloud/orgpolicy_v2/services/org_policy/transports/__init__.py",
113+
"google/cloud/orgpolicy_v2/services/org_policy/transports/base.py",
114+
"google/cloud/orgpolicy_v2/services/org_policy/transports/grpc.py",
115+
"google/cloud/orgpolicy_v2/services/org_policy/transports/grpc_asyncio.py",
116+
"google/cloud/orgpolicy_v2/types/__init__.py",
117+
"google/cloud/orgpolicy_v2/types/constraint.py",
118+
"google/cloud/orgpolicy_v2/types/orgpolicy.py",
119+
"mypy.ini",
120+
"noxfile.py",
121+
"renovate.json",
122+
"scripts/decrypt-secrets.sh",
123+
"scripts/fixup_orgpolicy_v2_keywords.py",
124+
"scripts/readme-gen/readme_gen.py",
125+
"scripts/readme-gen/templates/README.tmpl.rst",
126+
"scripts/readme-gen/templates/auth.tmpl.rst",
127+
"scripts/readme-gen/templates/auth_api_key.tmpl.rst",
128+
"scripts/readme-gen/templates/install_deps.tmpl.rst",
129+
"scripts/readme-gen/templates/install_portaudio.tmpl.rst",
130+
"setup.cfg",
131+
"testing/.gitignore",
132+
"tests/unit/gapic/orgpolicy_v2/__init__.py",
133+
"tests/unit/gapic/orgpolicy_v2/test_org_policy.py"
43134
]
44135
}
+15
Original file line numberDiff line numberDiff line change
@@ -1 +1,16 @@
1+
# -*- coding: utf-8 -*-
12

3+
# Copyright 2020 Google LLC
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
#

tests/unit/gapic/orgpolicy_v2/test_org_policy.py

+120-2
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,17 @@ def test__get_default_mtls_endpoint():
8282
assert OrgPolicyClient._get_default_mtls_endpoint(non_googleapi) == non_googleapi
8383

8484

85-
def test_org_policy_client_from_service_account_info():
85+
@pytest.mark.parametrize("client_class", [OrgPolicyClient, OrgPolicyAsyncClient,])
86+
def test_org_policy_client_from_service_account_info(client_class):
8687
creds = credentials.AnonymousCredentials()
8788
with mock.patch.object(
8889
service_account.Credentials, "from_service_account_info"
8990
) as factory:
9091
factory.return_value = creds
9192
info = {"valid": True}
92-
client = OrgPolicyClient.from_service_account_info(info)
93+
client = client_class.from_service_account_info(info)
9394
assert client.transport._credentials == creds
95+
assert isinstance(client, client_class)
9496

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

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

108111
client = client_class.from_service_account_json("dummy/file/path.json")
109112
assert client.transport._credentials == creds
113+
assert isinstance(client, client_class)
110114

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

@@ -463,6 +467,22 @@ def test_list_constraints_from_dict():
463467
test_list_constraints(request_type=dict)
464468

465469

470+
def test_list_constraints_empty_call():
471+
# This test is a coverage failsafe to make sure that totally empty calls,
472+
# i.e. request == None and no flattened fields passed, work.
473+
client = OrgPolicyClient(
474+
credentials=credentials.AnonymousCredentials(), transport="grpc",
475+
)
476+
477+
# Mock the actual call within the gRPC stub, and fake the request.
478+
with mock.patch.object(type(client.transport.list_constraints), "__call__") as call:
479+
client.list_constraints()
480+
call.assert_called()
481+
_, args, _ = call.mock_calls[0]
482+
483+
assert args[0] == orgpolicy.ListConstraintsRequest()
484+
485+
466486
@pytest.mark.asyncio
467487
async def test_list_constraints_async(
468488
transport: str = "grpc_asyncio", request_type=orgpolicy.ListConstraintsRequest
@@ -794,6 +814,22 @@ def test_list_policies_from_dict():
794814
test_list_policies(request_type=dict)
795815

796816

817+
def test_list_policies_empty_call():
818+
# This test is a coverage failsafe to make sure that totally empty calls,
819+
# i.e. request == None and no flattened fields passed, work.
820+
client = OrgPolicyClient(
821+
credentials=credentials.AnonymousCredentials(), transport="grpc",
822+
)
823+
824+
# Mock the actual call within the gRPC stub, and fake the request.
825+
with mock.patch.object(type(client.transport.list_policies), "__call__") as call:
826+
client.list_policies()
827+
call.assert_called()
828+
_, args, _ = call.mock_calls[0]
829+
830+
assert args[0] == orgpolicy.ListPoliciesRequest()
831+
832+
797833
@pytest.mark.asyncio
798834
async def test_list_policies_async(
799835
transport: str = "grpc_asyncio", request_type=orgpolicy.ListPoliciesRequest
@@ -1105,6 +1141,22 @@ def test_get_policy_from_dict():
11051141
test_get_policy(request_type=dict)
11061142

11071143

1144+
def test_get_policy_empty_call():
1145+
# This test is a coverage failsafe to make sure that totally empty calls,
1146+
# i.e. request == None and no flattened fields passed, work.
1147+
client = OrgPolicyClient(
1148+
credentials=credentials.AnonymousCredentials(), transport="grpc",
1149+
)
1150+
1151+
# Mock the actual call within the gRPC stub, and fake the request.
1152+
with mock.patch.object(type(client.transport.get_policy), "__call__") as call:
1153+
client.get_policy()
1154+
call.assert_called()
1155+
_, args, _ = call.mock_calls[0]
1156+
1157+
assert args[0] == orgpolicy.GetPolicyRequest()
1158+
1159+
11081160
@pytest.mark.asyncio
11091161
async def test_get_policy_async(
11101162
transport: str = "grpc_asyncio", request_type=orgpolicy.GetPolicyRequest
@@ -1294,6 +1346,24 @@ def test_get_effective_policy_from_dict():
12941346
test_get_effective_policy(request_type=dict)
12951347

12961348

1349+
def test_get_effective_policy_empty_call():
1350+
# This test is a coverage failsafe to make sure that totally empty calls,
1351+
# i.e. request == None and no flattened fields passed, work.
1352+
client = OrgPolicyClient(
1353+
credentials=credentials.AnonymousCredentials(), transport="grpc",
1354+
)
1355+
1356+
# Mock the actual call within the gRPC stub, and fake the request.
1357+
with mock.patch.object(
1358+
type(client.transport.get_effective_policy), "__call__"
1359+
) as call:
1360+
client.get_effective_policy()
1361+
call.assert_called()
1362+
_, args, _ = call.mock_calls[0]
1363+
1364+
assert args[0] == orgpolicy.GetEffectivePolicyRequest()
1365+
1366+
12971367
@pytest.mark.asyncio
12981368
async def test_get_effective_policy_async(
12991369
transport: str = "grpc_asyncio", request_type=orgpolicy.GetEffectivePolicyRequest
@@ -1491,6 +1561,22 @@ def test_create_policy_from_dict():
14911561
test_create_policy(request_type=dict)
14921562

14931563

1564+
def test_create_policy_empty_call():
1565+
# This test is a coverage failsafe to make sure that totally empty calls,
1566+
# i.e. request == None and no flattened fields passed, work.
1567+
client = OrgPolicyClient(
1568+
credentials=credentials.AnonymousCredentials(), transport="grpc",
1569+
)
1570+
1571+
# Mock the actual call within the gRPC stub, and fake the request.
1572+
with mock.patch.object(type(client.transport.create_policy), "__call__") as call:
1573+
client.create_policy()
1574+
call.assert_called()
1575+
_, args, _ = call.mock_calls[0]
1576+
1577+
assert args[0] == orgpolicy.CreatePolicyRequest()
1578+
1579+
14941580
@pytest.mark.asyncio
14951581
async def test_create_policy_async(
14961582
transport: str = "grpc_asyncio", request_type=orgpolicy.CreatePolicyRequest
@@ -1690,6 +1776,22 @@ def test_update_policy_from_dict():
16901776
test_update_policy(request_type=dict)
16911777

16921778

1779+
def test_update_policy_empty_call():
1780+
# This test is a coverage failsafe to make sure that totally empty calls,
1781+
# i.e. request == None and no flattened fields passed, work.
1782+
client = OrgPolicyClient(
1783+
credentials=credentials.AnonymousCredentials(), transport="grpc",
1784+
)
1785+
1786+
# Mock the actual call within the gRPC stub, and fake the request.
1787+
with mock.patch.object(type(client.transport.update_policy), "__call__") as call:
1788+
client.update_policy()
1789+
call.assert_called()
1790+
_, args, _ = call.mock_calls[0]
1791+
1792+
assert args[0] == orgpolicy.UpdatePolicyRequest()
1793+
1794+
16931795
@pytest.mark.asyncio
16941796
async def test_update_policy_async(
16951797
transport: str = "grpc_asyncio", request_type=orgpolicy.UpdatePolicyRequest
@@ -1876,6 +1978,22 @@ def test_delete_policy_from_dict():
18761978
test_delete_policy(request_type=dict)
18771979

18781980

1981+
def test_delete_policy_empty_call():
1982+
# This test is a coverage failsafe to make sure that totally empty calls,
1983+
# i.e. request == None and no flattened fields passed, work.
1984+
client = OrgPolicyClient(
1985+
credentials=credentials.AnonymousCredentials(), transport="grpc",
1986+
)
1987+
1988+
# Mock the actual call within the gRPC stub, and fake the request.
1989+
with mock.patch.object(type(client.transport.delete_policy), "__call__") as call:
1990+
client.delete_policy()
1991+
call.assert_called()
1992+
_, args, _ = call.mock_calls[0]
1993+
1994+
assert args[0] == orgpolicy.DeletePolicyRequest()
1995+
1996+
18791997
@pytest.mark.asyncio
18801998
async def test_delete_policy_async(
18811999
transport: str = "grpc_asyncio", request_type=orgpolicy.DeletePolicyRequest

0 commit comments

Comments
 (0)