Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit f25c5d2

Browse filesBrowse files
askmeegstswast
authored andcommitted
[IAM] Add client initialization to each access.py sample (GoogleCloudPlatform#2297)
* Initialize client in each IAM access sample * lint * lint
1 parent f634c44 commit f25c5d2
Copy full SHA for f25c5d2

File tree

Expand file treeCollapse file tree

1 file changed

+13
-7
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+13
-7
lines changed

‎iam/api-client/access.py

Copy file name to clipboardExpand all lines: iam/api-client/access.py
+13-7Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,16 @@
2727
import googleapiclient.discovery
2828

2929

30-
credentials = service_account.Credentials.from_service_account_file(
31-
filename=os.environ['GOOGLE_APPLICATION_CREDENTIALS'],
32-
scopes=['https://www.googleapis.com/auth/cloud-platform'])
33-
service = googleapiclient.discovery.build(
34-
'cloudresourcemanager', 'v1', credentials=credentials)
35-
36-
3730
# [START iam_get_policy]
3831
def get_policy(project_id):
3932
"""Gets IAM policy for a project."""
4033

4134
# pylint: disable=no-member
35+
credentials = service_account.Credentials.from_service_account_file(
36+
filename=os.environ['GOOGLE_APPLICATION_CREDENTIALS'],
37+
scopes=['https://www.googleapis.com/auth/cloud-platform'])
38+
service = googleapiclient.discovery.build(
39+
'cloudresourcemanager', 'v1', credentials=credentials)
4240
policy = service.projects().getIamPolicy(
4341
resource=project_id, body={}).execute()
4442
print(policy)
@@ -49,6 +47,7 @@ def get_policy(project_id):
4947
# [START iam_modify_policy_add_member]
5048
def modify_policy_add_member(policy, role, member):
5149
"""Adds a new member to a role binding."""
50+
5251
binding = next(b for b in policy['bindings'] if b['role'] == role)
5352
binding['members'].append(member)
5453
print(binding)
@@ -59,6 +58,7 @@ def modify_policy_add_member(policy, role, member):
5958
# [START iam_modify_policy_add_role]
6059
def modify_policy_add_role(policy, role, member):
6160
"""Adds a new role binding to a policy."""
61+
6262
binding = {
6363
'role': role,
6464
'members': [member]
@@ -74,6 +74,12 @@ def set_policy(project_id, policy):
7474
"""Sets IAM policy for a project."""
7575

7676
# pylint: disable=no-member
77+
credentials = service_account.Credentials.from_service_account_file(
78+
filename=os.environ['GOOGLE_APPLICATION_CREDENTIALS'],
79+
scopes=['https://www.googleapis.com/auth/cloud-platform'])
80+
service = googleapiclient.discovery.build(
81+
'cloudresourcemanager', 'v1', credentials=credentials)
82+
7783
policy = service.projects().setIamPolicy(
7884
resource=project_id, body={
7985
'policy': policy

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.