-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add iam methods to templates (#545)
* feat: add iam methods to templates * fix: fix typo * chore: fix formatting * fix: fix ref to option * chore(deps): add iam dependency * chore: add circleci * chore: take 2 * chore: only test with 3.8 Co-authored-by: Dov Shlachter <[email protected]>
- Loading branch information
1 parent
bbc6b36
commit 3f42c3c
Showing
12 changed files
with
1,227 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,10 @@ from google.oauth2 import service_account # type: ignore | |
{{ ref_type.ident.python_import }} | ||
{% endfor -%} | ||
{% endfor -%} | ||
{% if opts.add_iam_methods %} | ||
from google.iam.v1 import iam_policy_pb2 as iam_policy # type: ignore | ||
from google.iam.v1 import policy_pb2 as policy # type: ignore | ||
{% endif %} | ||
{% endfilter %} | ||
from .transports.base import {{ service.name }}Transport | ||
from .transports.grpc_asyncio import {{ service.grpc_asyncio_transport_name }} | ||
|
@@ -256,6 +260,272 @@ class {{ service.async_client_name }}: | |
{% endfor %} | ||
|
||
|
||
{% if opts.add_iam_methods %} | ||
async def set_iam_policy( | ||
self, | ||
request: iam_policy.SetIamPolicyRequest = None, | ||
*, | ||
retry: retries.Retry = gapic_v1.method.DEFAULT, | ||
timeout: float = None, | ||
metadata: Sequence[Tuple[str, str]] = (), | ||
) -> policy.Policy: | ||
r"""Sets the IAM access control policy on the specified | ||
function. Replaces any existing policy. | ||
Args: | ||
request (:class:`~.iam_policy.SetIamPolicyRequest`): | ||
The request object. Request message for `SetIamPolicy` | ||
method. | ||
retry (google.api_core.retry.Retry): Designation of what errors, if any, | ||
should be retried. | ||
timeout (float): The timeout for this request. | ||
metadata (Sequence[Tuple[str, str]]): Strings which should be | ||
sent along with the request as metadata. | ||
Returns: | ||
~.policy.Policy: | ||
Defines an Identity and Access Management (IAM) policy. | ||
It is used to specify access control policies for Cloud | ||
Platform resources. | ||
A ``Policy`` is a collection of ``bindings``. A | ||
``binding`` binds one or more ``members`` to a single | ||
``role``. Members can be user accounts, service | ||
accounts, Google groups, and domains (such as G Suite). | ||
A ``role`` is a named list of permissions (defined by | ||
IAM or configured by users). A ``binding`` can | ||
optionally specify a ``condition``, which is a logic | ||
expression that further constrains the role binding | ||
based on attributes about the request and/or target | ||
resource. | ||
**JSON Example** | ||
:: | ||
{ | ||
"bindings": [ | ||
{ | ||
"role": "roles/resourcemanager.organizationAdmin", | ||
"members": [ | ||
"user:[email protected]", | ||
"group:[email protected]", | ||
"domain:google.com", | ||
"serviceAccount:[email protected]" | ||
] | ||
}, | ||
{ | ||
"role": "roles/resourcemanager.organizationViewer", | ||
"members": ["user:[email protected]"], | ||
"condition": { | ||
"title": "expirable access", | ||
"description": "Does not grant access after Sep 2020", | ||
"expression": "request.time < | ||
timestamp('2020-10-01T00:00:00.000Z')", | ||
} | ||
} | ||
] | ||
} | ||
**YAML Example** | ||
:: | ||
bindings: | ||
- members: | ||
- user:[email protected] | ||
- group:[email protected] | ||
- domain:google.com | ||
- serviceAccount:[email protected] | ||
role: roles/resourcemanager.organizationAdmin | ||
- members: | ||
- user:[email protected] | ||
role: roles/resourcemanager.organizationViewer | ||
condition: | ||
title: expirable access | ||
description: Does not grant access after Sep 2020 | ||
expression: request.time < timestamp('2020-10-01T00:00:00.000Z') | ||
For a description of IAM and its features, see the `IAM | ||
developer's | ||
guide <https://cloud.google.com/iam/docs>`__. | ||
""" | ||
# Create or coerce a protobuf request object. | ||
|
||
# The request isn't a proto-plus wrapped type, | ||
# so it must be constructed via keyword expansion. | ||
if isinstance(request, dict): | ||
request = iam_policy.SetIamPolicyRequest(**request) | ||
|
||
# Wrap the RPC method; this adds retry and timeout information, | ||
# and friendly error handling. | ||
rpc = gapic_v1.method_async.wrap_method( | ||
self._client._transport.set_iam_policy, | ||
default_timeout=None, | ||
client_info=_client_info, | ||
) | ||
|
||
# Certain fields should be provided within the metadata header; | ||
# add these here. | ||
metadata = tuple(metadata) + ( | ||
gapic_v1.routing_header.to_grpc_metadata((("resource", request.resource),)), | ||
) | ||
|
||
# Send the request. | ||
response = await rpc(request, retry=retry, timeout=timeout, metadata=metadata,) | ||
|
||
# Done; return the response. | ||
return response | ||
|
||
async def get_iam_policy( | ||
self, | ||
request: iam_policy.GetIamPolicyRequest = None, | ||
*, | ||
retry: retries.Retry = gapic_v1.method.DEFAULT, | ||
timeout: float = None, | ||
metadata: Sequence[Tuple[str, str]] = (), | ||
) -> policy.Policy: | ||
r"""Gets the IAM access control policy for a function. | ||
Returns an empty policy if the function exists and does | ||
not have a policy set. | ||
Args: | ||
request (:class:`~.iam_policy.GetIamPolicyRequest`): | ||
The request object. Request message for `GetIamPolicy` | ||
method. | ||
retry (google.api_core.retry.Retry): Designation of what errors, if any, | ||
should be retried. | ||
timeout (float): The timeout for this request. | ||
metadata (Sequence[Tuple[str, str]]): Strings which should be | ||
sent along with the request as metadata. | ||
Returns: | ||
~.policy.Policy: | ||
Defines an Identity and Access Management (IAM) policy. | ||
It is used to specify access control policies for Cloud | ||
Platform resources. | ||
A ``Policy`` is a collection of ``bindings``. A | ||
``binding`` binds one or more ``members`` to a single | ||
``role``. Members can be user accounts, service | ||
accounts, Google groups, and domains (such as G Suite). | ||
A ``role`` is a named list of permissions (defined by | ||
IAM or configured by users). A ``binding`` can | ||
optionally specify a ``condition``, which is a logic | ||
expression that further constrains the role binding | ||
based on attributes about the request and/or target | ||
resource. | ||
**JSON Example** | ||
:: | ||
{ | ||
"bindings": [ | ||
{ | ||
"role": "roles/resourcemanager.organizationAdmin", | ||
"members": [ | ||
"user:[email protected]", | ||
"group:[email protected]", | ||
"domain:google.com", | ||
"serviceAccount:[email protected]" | ||
] | ||
}, | ||
{ | ||
"role": "roles/resourcemanager.organizationViewer", | ||
"members": ["user:[email protected]"], | ||
"condition": { | ||
"title": "expirable access", | ||
"description": "Does not grant access after Sep 2020", | ||
"expression": "request.time < | ||
timestamp('2020-10-01T00:00:00.000Z')", | ||
} | ||
} | ||
] | ||
} | ||
**YAML Example** | ||
:: | ||
bindings: | ||
- members: | ||
- user:[email protected] | ||
- group:[email protected] | ||
- domain:google.com | ||
- serviceAccount:[email protected] | ||
role: roles/resourcemanager.organizationAdmin | ||
- members: | ||
- user:[email protected] | ||
role: roles/resourcemanager.organizationViewer | ||
condition: | ||
title: expirable access | ||
description: Does not grant access after Sep 2020 | ||
expression: request.time < timestamp('2020-10-01T00:00:00.000Z') | ||
For a description of IAM and its features, see the `IAM | ||
developer's | ||
guide <https://cloud.google.com/iam/docs>`__. | ||
""" | ||
# Create or coerce a protobuf request object. | ||
|
||
# The request isn't a proto-plus wrapped type, | ||
# so it must be constructed via keyword expansion. | ||
if isinstance(request, dict): | ||
request = iam_policy.GetIamPolicyRequest(**request) | ||
|
||
# Wrap the RPC method; this adds retry and timeout information, | ||
# and friendly error handling. | ||
rpc = gapic_v1.method_async.wrap_method( | ||
self._client._transport.get_iam_policy, | ||
default_timeout=None, | ||
client_info=_client_info, | ||
) | ||
|
||
# Certain fields should be provided within the metadata header; | ||
# add these here. | ||
metadata = tuple(metadata) + ( | ||
gapic_v1.routing_header.to_grpc_metadata((("resource", request.resource),)), | ||
) | ||
|
||
# Send the request. | ||
response = await rpc(request, retry=retry, timeout=timeout, metadata=metadata,) | ||
|
||
# Done; return the response. | ||
return response | ||
|
||
async def test_iam_permissions( | ||
self, | ||
request: iam_policy.TestIamPermissionsRequest = None, | ||
*, | ||
retry: retries.Retry = gapic_v1.method.DEFAULT, | ||
timeout: float = None, | ||
metadata: Sequence[Tuple[str, str]] = (), | ||
) -> iam_policy.TestIamPermissionsResponse: | ||
r"""Tests the specified permissions against the IAM access control | ||
policy for a function. If the function does not exist, this will | ||
return an empty set of permissions, not a NOT_FOUND error. | ||
Args: | ||
request (:class:`~.iam_policy.TestIamPermissionsRequest`): | ||
The request object. Request message for | ||
`TestIamPermissions` method. | ||
retry (google.api_core.retry.Retry): Designation of what errors, if any, | ||
should be retried. | ||
timeout (float): The timeout for this request. | ||
metadata (Sequence[Tuple[str, str]]): Strings which should be | ||
sent along with the request as metadata. | ||
Returns: | ||
~.iam_policy.TestIamPermissionsResponse: | ||
Response message for ``TestIamPermissions`` method. | ||
""" | ||
# Create or coerce a protobuf request object. | ||
|
||
# The request isn't a proto-plus wrapped type, | ||
# so it must be constructed via keyword expansion. | ||
if isinstance(request, dict): | ||
request = iam_policy.TestIamPermissionsRequest(**request) | ||
|
||
# Wrap the RPC method; this adds retry and timeout information, | ||
# and friendly error handling. | ||
rpc = gapic_v1.method_async.wrap_method( | ||
self._client._transport.test_iam_permissions, | ||
default_timeout=None, | ||
client_info=_client_info, | ||
) | ||
|
||
# Certain fields should be provided within the metadata header; | ||
# add these here. | ||
metadata = tuple(metadata) + ( | ||
gapic_v1.routing_header.to_grpc_metadata((("resource", request.resource),)), | ||
) | ||
|
||
# Send the request. | ||
response = await rpc(request, retry=retry, timeout=timeout, metadata=metadata,) | ||
|
||
# Done; return the response. | ||
return response | ||
{% endif %} | ||
|
||
try: | ||
_client_info = gapic_v1.client_info.ClientInfo( | ||
gapic_version=pkg_resources.get_distribution( | ||
|
Oops, something went wrong.