Skip to content

Commit 359c94b

Browse files
melaniedejongengelke
authored andcommitted
Add service account enable/disable snippets (GoogleCloudPlatform#2410)
* 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 * Add enable/disable functions and tests * Adding region tags for imports * Adding enable/disable functions * Lint
1 parent 5d43edb commit 359c94b

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed

iam/api-client/service_accounts.py

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@
2121

2222
import argparse
2323
# [START iam_create_service_account]
24-
# [START iam_list_service_accounts]
24+
# [START iam_list_service_account]
2525
# [START iam_rename_service_account]
26+
# [START iam_disable_service_account]
27+
# [START iam_enable_service_account]
2628
# [START iam_delete_service_account]
2729
import os
2830

@@ -32,6 +34,8 @@
3234
# [END iam_create_service_account]
3335
# [END iam_list_service_accounts]
3436
# [END iam_rename_service_account]
37+
# [END iam_disable_service_account]
38+
# [END iam_enable_service_account]
3539
# [END iam_delete_service_account]
3640

3741

@@ -110,6 +114,43 @@ def rename_service_account(email, new_display_name):
110114
# [END iam_rename_service_account]
111115

112116

117+
# [START iam_disable_service_account]
118+
def disable_service_account(email):
119+
"""Disables a service account."""
120+
121+
credentials = service_account.Credentials.from_service_account_file(
122+
filename=os.environ['GOOGLE_APPLICATION_CREDENTIALS'],
123+
scopes=['https://www.googleapis.com/auth/cloud-platform'])
124+
125+
service = googleapiclient.discovery.build(
126+
'iam', 'v1', credentials=credentials)
127+
128+
service.projects().serviceAccounts().disable(
129+
name='projects/-/serviceAccounts/' + email).execute()
130+
131+
print("Disabled service account :" + email)
132+
# [END iam_disable_service_account]
133+
134+
# [START iam_enable_service_account]
135+
136+
137+
def enable_service_account(email):
138+
"""Enables a service account."""
139+
140+
credentials = service_account.Credentials.from_service_account_file(
141+
filename=os.environ['GOOGLE_APPLICATION_CREDENTIALS'],
142+
scopes=['https://www.googleapis.com/auth/cloud-platform'])
143+
144+
service = googleapiclient.discovery.build(
145+
'iam', 'v1', credentials=credentials)
146+
147+
service.projects().serviceAccounts().enable(
148+
name='projects/-/serviceAccounts/' + email).execute()
149+
150+
print("Disabled service account :" + email)
151+
# [END iam_enable_service_account]
152+
153+
113154
# [START iam_delete_service_account]
114155
def delete_service_account(email):
115156
"""Deletes a service account."""
@@ -149,10 +190,20 @@ def main():
149190

150191
# Rename
151192
rename_parser = subparsers.add_parser(
152-
'delete', help=rename_service_account.__doc__)
193+
'rename', help=rename_service_account.__doc__)
153194
rename_parser.add_argument('email')
154195
rename_parser.add_argument('new_display_name')
155196

197+
# Disable
198+
rename_parser = subparsers.add_parser(
199+
'disable', help=disable_service_account.__doc__)
200+
list_parser.addargument('email')
201+
202+
# Enable
203+
rename_parser = subparsers.add_parser(
204+
'enable', help=enable_service_account.__doc__)
205+
list_parser.addargument('email')
206+
156207
# Delete
157208
delete_parser = subparsers.add_parser(
158209
'delete', help=delete_service_account.__doc__)

iam/api-client/service_accounts_test.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,9 @@ def test_service_accounts(capsys):
3030
project_id)
3131
service_accounts.rename_service_account(
3232
email, 'Updated Py Test Account')
33+
service_accounts.disable_service_account(
34+
email)
35+
service_accounts.enable_service_account(
36+
email)
3337
service_accounts.delete_service_account(
3438
email)

0 commit comments

Comments
 (0)