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 ba1b681

Browse filesBrowse files
melaniedejongandrewsg
authored andcommitted
Code adjustments to ensure that snippets for docs have all necessary info (GoogleCloudPlatform#2386)
* Moving variable definitions inside methods Ensures that variable definitions are captured in the region tags, so they'll show up in the documentation * Added region tags to capture imports * Moved variable definitions inside functions Moved the variable definitions inside the functions so they would be captured by the region tags * Added region tags to capture imports * Minor formatting adjustments * Removing whitespace from blank lines * Removing whitespace from blank lines * Lint * Lint * Lint Solved redundancy between global "service_account" and local variable "service_account" in create_service_account and rename_service_account by changing the name of the local variable to "my_service_account" * Removing pylint tags * Removing pylint tags * Update service_account to my_service_account
1 parent 4398318 commit ba1b681
Copy full SHA for ba1b681

File tree

Expand file treeCollapse file tree

2 files changed

+73
-27
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+73
-27
lines changed

‎iam/api-client/service_account_keys.py

Copy file name to clipboardExpand all lines: iam/api-client/service_account_keys.py
+27-8Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,30 @@
2222
"""
2323

2424
import argparse
25+
# [START iam_create_key]
26+
# [START iam_list_keys]
27+
# [START iam_delete_key]
2528
import os
2629

2730
from google.oauth2 import service_account
2831
import googleapiclient.discovery
2932

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-
'iam', 'v1', credentials=credentials)
33+
# [END iam_create_key]
34+
# [END iam_list_keys]
35+
# [END iam_delete_key]
3536

3637

3738
# [START iam_create_key]
3839
def create_key(service_account_email):
3940
"""Creates a key for a service account."""
4041

41-
# pylint: disable=no-member
42+
credentials = service_account.Credentials.from_service_account_file(
43+
filename=os.environ['GOOGLE_APPLICATION_CREDENTIALS'],
44+
scopes=['https://www.googleapis.com/auth/cloud-platform'])
45+
46+
service = googleapiclient.discovery.build(
47+
'iam', 'v1', credentials=credentials)
48+
4249
key = service.projects().serviceAccounts().keys().create(
4350
name='projects/-/serviceAccounts/' + service_account_email, body={}
4451
).execute()
@@ -51,7 +58,13 @@ def create_key(service_account_email):
5158
def list_keys(service_account_email):
5259
"""Lists all keys for a service account."""
5360

54-
# pylint: disable=no-member
61+
credentials = service_account.Credentials.from_service_account_file(
62+
filename=os.environ['GOOGLE_APPLICATION_CREDENTIALS'],
63+
scopes=['https://www.googleapis.com/auth/cloud-platform'])
64+
65+
service = googleapiclient.discovery.build(
66+
'iam', 'v1', credentials=credentials)
67+
5568
keys = service.projects().serviceAccounts().keys().list(
5669
name='projects/-/serviceAccounts/' + service_account_email).execute()
5770

@@ -64,7 +77,13 @@ def list_keys(service_account_email):
6477
def delete_key(full_key_name):
6578
"""Deletes a service account key."""
6679

67-
# pylint: disable=no-member
80+
credentials = service_account.Credentials.from_service_account_file(
81+
filename=os.environ['GOOGLE_APPLICATION_CREDENTIALS'],
82+
scopes=['https://www.googleapis.com/auth/cloud-platform'])
83+
84+
service = googleapiclient.discovery.build(
85+
'iam', 'v1', credentials=credentials)
86+
6887
service.projects().serviceAccounts().keys().delete(
6988
name=full_key_name).execute()
7089

‎iam/api-client/service_accounts.py

Copy file name to clipboardExpand all lines: iam/api-client/service_accounts.py
+46-19Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,33 @@
2020
"""
2121

2222
import argparse
23+
# [START iam_create_service_account]
24+
# [START iam_list_service_account]
25+
# [START iam_rename_service_account]
26+
# [START iam_delete_service_account]
2327
import os
2428

2529
from google.oauth2 import service_account
2630
import googleapiclient.discovery
2731

28-
credentials = service_account.Credentials.from_service_account_file(
29-
filename=os.environ['GOOGLE_APPLICATION_CREDENTIALS'],
30-
scopes=['https://www.googleapis.com/auth/cloud-platform'])
31-
32-
service = googleapiclient.discovery.build(
33-
'iam', 'v1', credentials=credentials)
32+
# [END iam_create_service_account]
33+
# [END iam_list_service_account]
34+
# [END iam_rename_service_account]
35+
# [END iam_delete_service_account]
3436

3537

3638
# [START iam_create_service_account]
3739
def create_service_account(project_id, name, display_name):
3840
"""Creates a service account."""
3941

40-
# pylint: disable=no-member
41-
service_account = service.projects().serviceAccounts().create(
42+
credentials = service_account.Credentials.from_service_account_file(
43+
filename=os.environ['GOOGLE_APPLICATION_CREDENTIALS'],
44+
scopes=['https://www.googleapis.com/auth/cloud-platform'])
45+
46+
service = googleapiclient.discovery.build(
47+
'iam', 'v1', credentials=credentials)
48+
49+
my_service_account = service.projects().serviceAccounts().create(
4250
name='projects/' + project_id,
4351
body={
4452
'accountId': name,
@@ -47,16 +55,22 @@ def create_service_account(project_id, name, display_name):
4755
}
4856
}).execute()
4957

50-
print('Created service account: ' + service_account['email'])
51-
return service_account
58+
print('Created service account: ' + my_service_account['email'])
59+
return my_service_account
5260
# [END iam_create_service_account]
5361

5462

5563
# [START iam_list_service_accounts]
5664
def list_service_accounts(project_id):
5765
"""Lists all service accounts for the current project."""
5866

59-
# pylint: disable=no-member
67+
credentials = service_account.Credentials.from_service_account_file(
68+
filename=os.environ['GOOGLE_APPLICATION_CREDENTIALS'],
69+
scopes=['https://www.googleapis.com/auth/cloud-platform'])
70+
71+
service = googleapiclient.discovery.build(
72+
'iam', 'v1', credentials=credentials)
73+
6074
service_accounts = service.projects().serviceAccounts().list(
6175
name='projects/' + project_id).execute()
6276

@@ -73,27 +87,40 @@ def rename_service_account(email, new_display_name):
7387
"""Changes a service account's display name."""
7488

7589
# First, get a service account using List() or Get()
90+
credentials = service_account.Credentials.from_service_account_file(
91+
filename=os.environ['GOOGLE_APPLICATION_CREDENTIALS'],
92+
scopes=['https://www.googleapis.com/auth/cloud-platform'])
93+
94+
service = googleapiclient.discovery.build(
95+
'iam', 'v1', credentials=credentials)
96+
7697
resource = 'projects/-/serviceAccounts/' + email
77-
# pylint: disable=no-member
78-
service_account = service.projects().serviceAccounts().get(
98+
99+
my_service_account = service.projects().serviceAccounts().get(
79100
name=resource).execute()
80101

81102
# Then you can update the display name
82-
service_account['displayName'] = new_display_name
83-
service_account = service.projects().serviceAccounts().update(
84-
name=resource, body=service_account).execute()
103+
my_service_account['displayName'] = new_display_name
104+
my_service_account = service.projects().serviceAccounts().update(
105+
name=resource, body=my_service_account).execute()
85106

86107
print('Updated display name for {} to: {}'.format(
87-
service_account['email'], service_account['displayName']))
88-
return service_account
108+
my_service_account['email'], my_service_account['displayName']))
109+
return my_service_account
89110
# [END iam_rename_service_account]
90111

91112

92113
# [START iam_delete_service_account]
93114
def delete_service_account(email):
94115
"""Deletes a service account."""
95116

96-
# pylint: disable=no-member
117+
credentials = service_account.Credentials.from_service_account_file(
118+
filename=os.environ['GOOGLE_APPLICATION_CREDENTIALS'],
119+
scopes=['https://www.googleapis.com/auth/cloud-platform'])
120+
121+
service = googleapiclient.discovery.build(
122+
'iam', 'v1', credentials=credentials)
123+
97124
service.projects().serviceAccounts().delete(
98125
name='projects/-/serviceAccounts/' + email).execute()
99126

0 commit comments

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