From 9dc4866a91e907557015f1059fcf9ae5eccfc1c5 Mon Sep 17 00:00:00 2001 From: Alexander Koren Date: Thu, 31 May 2018 13:12:34 -0700 Subject: [PATCH 1/9] Add env/ and .vscode/ to .gitignore --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index 7e466b64271..f6f7ed7646a 100644 --- a/.gitignore +++ b/.gitignore @@ -20,3 +20,5 @@ secrets.tar junit.xml credentials.dat .nox +env/ +.vscode/ \ No newline at end of file From cb631f6cf67b0fe500404066e5a42539170e73f2 Mon Sep 17 00:00:00 2001 From: Alexander Koren Date: Thu, 14 Jun 2018 14:36:27 -0700 Subject: [PATCH 2/9] Add IAM requirements.txt --- iam/api-client/requirements.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 iam/api-client/requirements.txt diff --git a/iam/api-client/requirements.txt b/iam/api-client/requirements.txt new file mode 100644 index 00000000000..73d5bf5c6df --- /dev/null +++ b/iam/api-client/requirements.txt @@ -0,0 +1,3 @@ +google-api-python-client==1.7.3 +google-auth==1.5.0 +google-auth-httplib2==0.0.3 \ No newline at end of file From 49afe25a59f2d12a9bb58905b152a6d3176adcc5 Mon Sep 17 00:00:00 2001 From: Alexander Koren Date: Thu, 2 Aug 2018 16:40:25 -0400 Subject: [PATCH 3/9] Python Service account keys (#5) Add IAM service account keys snippets --- iam/api-client/service_account_keys.py | 98 ++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 iam/api-client/service_account_keys.py diff --git a/iam/api-client/service_account_keys.py b/iam/api-client/service_account_keys.py new file mode 100644 index 00000000000..21d953bbd69 --- /dev/null +++ b/iam/api-client/service_account_keys.py @@ -0,0 +1,98 @@ +#!/usr/bin/env python + +# Copyright 2018 Google Inc. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Demonstrates how to perform basic operations with Google Cloud IAM +service account keys. + +For more information, see the documentation at +https://cloud.google.com/iam/docs/creating-managing-service-account-keys. +""" + +import os + +from google.oauth2 import service_account +import argparse +import googleapiclient.discovery + +credentials = service_account.Credentials.from_service_account_file( + filename=os.environ['GOOGLE_APPLICATION_CREDENTIALS'], + scopes=['https://www.googleapis.com/auth/cloud-platform']) +service = googleapiclient.discovery.build( + 'iam', 'v1', credentials=credentials) + +# [START iam_create_key] +def create_key(service_account_email): + """Creates a key for a service account.""" + + # pylint: disable=no-member + key = service.projects().serviceAccounts().keys().create( + name='projects/-/serviceAccounts/' + service_account_email, body={} + ).execute() + + print('Created key: ' + key['name']) +# [END iam_create_key] + + +# [START iam_list_keys] +def list_keys(service_account_email): + """Lists all keys for a service account.""" + + # pylint: disable=no-member + keys = service.projects().serviceAccounts().keys().list( + name='projects/-/serviceAccounts/' + service_account_email).execute() + + for key in keys['keys']: + print('Key: ' + key['name']) +# [END iam_list_keys] + + +# [START iam_delete_key] +def delete_key(full_key_name): + """Deletes a service account key.""" + + # pylint: disable=no-member + service.projects().serviceAccounts().keys().delete( + name=full_key_name).execute() + + print('Deleted service account: ' + full_key_name) +# [END iam_delete_key] + + +if __name__ == '__main__': + + parser = argparse.ArgumentParser( + description=__doc__, + formatter_class=argparse.RawDescriptionHelpFormatter) + + subparsers = parser.add_subparsers(dest='command') + + create_key_parser = subparsers.add_parser('create', help=create_key.__doc__) + create_key_parser.add_argument('service_account_email') + + list_keys_parser = subparsers.add_parser('list', help=list_keys.__doc__) + list_keys_parser.add_argument('service_account_email') + + delete_key_parser = subparsers.add_parser('delete', help=delete_key.__doc__) + delete_key_parser.add_argument('full_key_name') + + args = parser.parse_args() + + if args.command == 'list': + list_keys(args.service_account_email) + elif args.command == 'create': + create_key(args.service_account_email) + elif args.command == 'delete': + delete_key(args.full_key_name) From fdfa17b9fa1f47a626e19321c05faf01ba64d84f Mon Sep 17 00:00:00 2001 From: Alexander Koren Date: Thu, 2 Aug 2018 16:40:36 -0400 Subject: [PATCH 4/9] Python Quickstart (#3) Add IAM quickstart --- iam/api-client/quickstart.py | 51 +++++++++++++++++++++++++++++++ iam/api-client/quickstart_test.py | 21 +++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 iam/api-client/quickstart.py create mode 100644 iam/api-client/quickstart_test.py diff --git a/iam/api-client/quickstart.py b/iam/api-client/quickstart.py new file mode 100644 index 00000000000..61ccb25de21 --- /dev/null +++ b/iam/api-client/quickstart.py @@ -0,0 +1,51 @@ +#!/usr/bin/env python + +# Copyright 2018 Google Inc. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +def quickstart(): + + # [START iam_quickstart] + import os + + from google.oauth2 import service_account + import googleapiclient.discovery + + # Get credentials + credentials = service_account.Credentials.from_service_account_file( + filename=os.environ['GOOGLE_APPLICATION_CREDENTIALS'], + scopes=['https://www.googleapis.com/auth/cloud-platform']) + + # Create the Cloud IAM service object + service = googleapiclient.discovery.build( + 'iam', 'v1', credentials=credentials) + + # Call the Cloud IAM Roles API + # If using pylint, disable weak-typing warnings + # pylint: disable=no-member + response = service.roles().list().execute() + roles = response['roles'] + + # Process the response + for role in roles: + print('Title: ' + role['title']) + print('Name: ' + role['name']) + print('Description: ' + role['description']) + print('') + # [END iam_quickstart] + + +if __name__ == '__main__': + quickstart() diff --git a/iam/api-client/quickstart_test.py b/iam/api-client/quickstart_test.py new file mode 100644 index 00000000000..9c7d881a6bc --- /dev/null +++ b/iam/api-client/quickstart_test.py @@ -0,0 +1,21 @@ +# Copyright 2018 Google Inc. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import quickstart + + +def test_quickstart(capsys): + quickstart.quickstart() + out, _ = capsys.readouterr() + assert 'Title' in out From d48de03aaf5eb144d3014b931846ad252678e601 Mon Sep 17 00:00:00 2001 From: Alexander Koren Date: Thu, 2 Aug 2018 16:40:52 -0400 Subject: [PATCH 5/9] Python Service accounts (#4) Add IAM service account snippets --- iam/api-client/service_accounts.py | 149 ++++++++++++++++++++++++ iam/api-client/service_accounts_test.py | 35 ++++++ 2 files changed, 184 insertions(+) create mode 100644 iam/api-client/service_accounts.py create mode 100644 iam/api-client/service_accounts_test.py diff --git a/iam/api-client/service_accounts.py b/iam/api-client/service_accounts.py new file mode 100644 index 00000000000..75c4e332e0a --- /dev/null +++ b/iam/api-client/service_accounts.py @@ -0,0 +1,149 @@ +#!/usr/bin/env python + +# Copyright 2018 Google Inc. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +"""Demonstrates how to perform basic operations with Google Cloud IAM +service accounts. +For more information, see the documentation at +https://cloud.google.com/iam/docs/creating-managing-service-accounts. +""" + +import argparse +import os + +from google.oauth2 import service_account +import googleapiclient.discovery + +credentials = service_account.Credentials.from_service_account_file( + filename=os.environ['GOOGLE_APPLICATION_CREDENTIALS'], + scopes=['https://www.googleapis.com/auth/cloud-platform']) + +service = googleapiclient.discovery.build( + 'iam', 'v1', credentials=credentials) + + +# [START iam_create_service_account] +def create_service_account(project_id, name, display_name): + """Creates a service account.""" + + # pylint: disable=no-member + service_account = service.projects().serviceAccounts().create( + name='projects/' + project_id, + body={ + 'accountId': name, + 'serviceAccount': { + 'displayName': display_name + } + }).execute() + + print('Created service account: ' + service_account['email']) + return service_account +# [END iam_create_service_account] + + +# [START iam_list_service_accounts] +def list_service_accounts(project_id): + """Lists all service accounts for the current project.""" + + # pylint: disable=no-member + service_accounts = service.projects().serviceAccounts().list( + name='projects/' + project_id).execute() + + for account in service_accounts['accounts']: + print('Name: ' + account['name']) + print('Email: ' + account['email']) + print(' ') + return service_accounts +# [END iam_list_service_accounts] + + +# [START iam_rename_service_account] +def rename_service_account(email, new_display_name): + """Changes a service account's display name.""" + + # First, get a service account using List() or Get() + resource = 'projects/-/serviceAccounts/' + email + # pylint: disable=no-member + service_account = service.projects().serviceAccounts().get( + name=resource).execute() + + # Then you can update the display name + service_account['displayName'] = new_display_name + service_account = service.projects().serviceAccounts().update( + name=resource, body=service_account).execute() + + print( + 'Updated display name for ' + service_account['email'] + + ' to: ' + service_account['displayName']) + return service_account +# [END iam_rename_service_account] + + +# [START iam_delete_service_account] +def delete_service_account(email): + """Deletes a service account.""" + + # pylint: disable=no-member + service.projects().serviceAccounts().delete( + name='projects/-/serviceAccounts/' + email).execute() + + print('Deleted service account: ' + email) +# [END iam_delete_service_account] + + +def main(): + + parser = argparse.ArgumentParser( + description=__doc__, + formatter_class=argparse.RawDescriptionHelpFormatter) + + subparsers = parser.add_subparsers(dest='command') + + # Create + create_parser = subparsers.add_parser( + 'create', help=create_service_account.__doc__) + create_parser.add_argument('project_id') + create_parser.add_argument('name') + create_parser.add_argument('display_name') + + # List + list_parser = subparsers.add_parser( + 'list', help=list_service_accounts.__doc__) + list_parser.add_argument('project_id') + + # Rename + rename_parser = subparsers.add_parser( + 'delete', help=rename_service_account.__doc__) + rename_parser.add_argument('email') + rename_parser.add_argument('new_display_name') + + # Delete + delete_parser = subparsers.add_parser( + 'delete', help=delete_service_account.__doc__) + delete_parser.add_argument('email') + + args = parser.parse_args() + + if args.command == 'create': + create_service_account(args.project_id, args.name, args.display_name) + elif args.command == 'list': + list_service_accounts(args.project_id) + elif args.command == 'rename': + rename_service_account(args.email, args.new_display_name) + elif args.command == 'delete': + delete_service_account(args.email) + + +if __name__ == '__main__': + main() diff --git a/iam/api-client/service_accounts_test.py b/iam/api-client/service_accounts_test.py new file mode 100644 index 00000000000..7cbee628c8a --- /dev/null +++ b/iam/api-client/service_accounts_test.py @@ -0,0 +1,35 @@ +# Copyright 2016 Google Inc. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import os +import random + +import service_accounts + + +def test_service_accounts(capsys): + + project_id = os.environ['GCLOUD_PROJECT'] + rand = str(random.randint(0, 1000)) + name = 'python-test-' + rand + email = name + '@' + project_id + '.iam.gserviceaccount.com' + + service_accounts.create_service_account( + project_id, name, 'Py Test Account') + service_accounts.list_service_accounts( + project_id) + service_accounts.rename_service_account( + email, 'Updated Py Test Account') + service_accounts.delete_service_account( + email) From 5918d4e5b1971a622d950a0905f464ac94a3a67e Mon Sep 17 00:00:00 2001 From: Alexander Koren Date: Thu, 2 Aug 2018 16:41:07 -0400 Subject: [PATCH 6/9] Add query grantable roles and tests (#2) --- iam/api-client/grantable_roles.py | 53 ++++++++++++++++++++++++++ iam/api-client/grantable_roles_test.py | 23 +++++++++++ 2 files changed, 76 insertions(+) create mode 100644 iam/api-client/grantable_roles.py create mode 100644 iam/api-client/grantable_roles_test.py diff --git a/iam/api-client/grantable_roles.py b/iam/api-client/grantable_roles.py new file mode 100644 index 00000000000..5f5860b7ef3 --- /dev/null +++ b/iam/api-client/grantable_roles.py @@ -0,0 +1,53 @@ +#!/usr/bin/env python + +# Copyright 2018 Google Inc. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import argparse +import os + +from google.oauth2 import service_account +import googleapiclient.discovery + +credentials = service_account.Credentials.from_service_account_file( + filename=os.environ['GOOGLE_APPLICATION_CREDENTIALS'], + scopes=['https://www.googleapis.com/auth/cloud-platform']) +service = googleapiclient.discovery.build( + 'iam', 'v1', credentials=credentials) + + +# [START iam_view_grantable_roles] +def view_grantable_roles(full_resource_name): + + roles = service.roles().queryGrantableRoles(body={ + 'fullResourceName': full_resource_name + }).execute() + + for role in roles['roles']: + print('Title: ' + role['title']) + print('Name: ' + role['name']) + print('Description: ' + role['description']) + print(' ') +# [END iam_view_grantable_roles] + + +if __name__ == '__main__': + + parser = argparse.ArgumentParser() + parser.add_argument( + 'full_resource_name', + help='The full name of the resource to query grantable roles for.') + + args = parser.parse_args() + view_grantable_roles(args.full_resource_name) diff --git a/iam/api-client/grantable_roles_test.py b/iam/api-client/grantable_roles_test.py new file mode 100644 index 00000000000..5cafb70690b --- /dev/null +++ b/iam/api-client/grantable_roles_test.py @@ -0,0 +1,23 @@ +# Copyright 2016 Google Inc. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import grantable_roles + + +def test_service_accounts(capsys): + project = os.environ['GCLOUD_PROJECT'] + resource = '//cloudresourcemanager.googleapis.com/projects/' + project + grantable_roles.view_grantable_roles(resource) + out, _ = capsys.readouterr() + assert 'Title:' in out \ No newline at end of file From 2465d86a57ad0d1b7c51950355dee1e9fd61300e Mon Sep 17 00:00:00 2001 From: Alexander Koren Date: Wed, 22 Aug 2018 13:04:28 -0700 Subject: [PATCH 7/9] Add whitespace and correct string formatting --- .gitignore | 3 +-- iam/api-client/grantable_roles.py | 2 -- iam/api-client/grantable_roles_test.py | 2 +- iam/api-client/quickstart.py | 1 - iam/api-client/service_account_keys.py | 3 +-- iam/api-client/service_accounts.py | 6 ++---- iam/api-client/service_accounts_test.py | 1 - 7 files changed, 5 insertions(+), 13 deletions(-) diff --git a/.gitignore b/.gitignore index f6f7ed7646a..369496d930f 100644 --- a/.gitignore +++ b/.gitignore @@ -20,5 +20,4 @@ secrets.tar junit.xml credentials.dat .nox -env/ -.vscode/ \ No newline at end of file +.vscode/ diff --git a/iam/api-client/grantable_roles.py b/iam/api-client/grantable_roles.py index 5f5860b7ef3..6df34732821 100644 --- a/iam/api-client/grantable_roles.py +++ b/iam/api-client/grantable_roles.py @@ -29,7 +29,6 @@ # [START iam_view_grantable_roles] def view_grantable_roles(full_resource_name): - roles = service.roles().queryGrantableRoles(body={ 'fullResourceName': full_resource_name }).execute() @@ -43,7 +42,6 @@ def view_grantable_roles(full_resource_name): if __name__ == '__main__': - parser = argparse.ArgumentParser() parser.add_argument( 'full_resource_name', diff --git a/iam/api-client/grantable_roles_test.py b/iam/api-client/grantable_roles_test.py index 5cafb70690b..85f42370e73 100644 --- a/iam/api-client/grantable_roles_test.py +++ b/iam/api-client/grantable_roles_test.py @@ -20,4 +20,4 @@ def test_service_accounts(capsys): resource = '//cloudresourcemanager.googleapis.com/projects/' + project grantable_roles.view_grantable_roles(resource) out, _ = capsys.readouterr() - assert 'Title:' in out \ No newline at end of file + assert 'Title:' in out diff --git a/iam/api-client/quickstart.py b/iam/api-client/quickstart.py index 61ccb25de21..932e6832010 100644 --- a/iam/api-client/quickstart.py +++ b/iam/api-client/quickstart.py @@ -16,7 +16,6 @@ def quickstart(): - # [START iam_quickstart] import os diff --git a/iam/api-client/service_account_keys.py b/iam/api-client/service_account_keys.py index 21d953bbd69..265679140cf 100644 --- a/iam/api-client/service_account_keys.py +++ b/iam/api-client/service_account_keys.py @@ -67,12 +67,11 @@ def delete_key(full_key_name): service.projects().serviceAccounts().keys().delete( name=full_key_name).execute() - print('Deleted service account: ' + full_key_name) + print('Deleted key: ' + full_key_name) # [END iam_delete_key] if __name__ == '__main__': - parser = argparse.ArgumentParser( description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) diff --git a/iam/api-client/service_accounts.py b/iam/api-client/service_accounts.py index 75c4e332e0a..53a4aa9fb27 100644 --- a/iam/api-client/service_accounts.py +++ b/iam/api-client/service_accounts.py @@ -83,9 +83,8 @@ def rename_service_account(email, new_display_name): service_account = service.projects().serviceAccounts().update( name=resource, body=service_account).execute() - print( - 'Updated display name for ' + service_account['email'] + - ' to: ' + service_account['displayName']) + print('Updated display name for {} to: {}' + .format(service_account['email'], service_account['displayName'])) return service_account # [END iam_rename_service_account] @@ -103,7 +102,6 @@ def delete_service_account(email): def main(): - parser = argparse.ArgumentParser( description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) diff --git a/iam/api-client/service_accounts_test.py b/iam/api-client/service_accounts_test.py index 7cbee628c8a..d96123a5f14 100644 --- a/iam/api-client/service_accounts_test.py +++ b/iam/api-client/service_accounts_test.py @@ -19,7 +19,6 @@ def test_service_accounts(capsys): - project_id = os.environ['GCLOUD_PROJECT'] rand = str(random.randint(0, 1000)) name = 'python-test-' + rand From 4998169f6a6296565db37fe6593dad0e92cca988 Mon Sep 17 00:00:00 2001 From: Alexander Koren Date: Wed, 22 Aug 2018 13:16:46 -0700 Subject: [PATCH 8/9] Add extra CR --- .gitignore | 1 + iam/api-client/grantable_roles.py | 1 + iam/api-client/grantable_roles_test.py | 1 + iam/api-client/quickstart.py | 1 + iam/api-client/quickstart_test.py | 1 + iam/api-client/service_account_keys.py | 1 + iam/api-client/service_accounts.py | 1 + iam/api-client/service_accounts_test.py | 1 + 8 files changed, 8 insertions(+) diff --git a/.gitignore b/.gitignore index 369496d930f..f6141efed6e 100644 --- a/.gitignore +++ b/.gitignore @@ -21,3 +21,4 @@ junit.xml credentials.dat .nox .vscode/ + diff --git a/iam/api-client/grantable_roles.py b/iam/api-client/grantable_roles.py index 6df34732821..0d04e18c04b 100644 --- a/iam/api-client/grantable_roles.py +++ b/iam/api-client/grantable_roles.py @@ -49,3 +49,4 @@ def view_grantable_roles(full_resource_name): args = parser.parse_args() view_grantable_roles(args.full_resource_name) + diff --git a/iam/api-client/grantable_roles_test.py b/iam/api-client/grantable_roles_test.py index 85f42370e73..8c5d117df50 100644 --- a/iam/api-client/grantable_roles_test.py +++ b/iam/api-client/grantable_roles_test.py @@ -21,3 +21,4 @@ def test_service_accounts(capsys): grantable_roles.view_grantable_roles(resource) out, _ = capsys.readouterr() assert 'Title:' in out + diff --git a/iam/api-client/quickstart.py b/iam/api-client/quickstart.py index 932e6832010..58cc6d18f84 100644 --- a/iam/api-client/quickstart.py +++ b/iam/api-client/quickstart.py @@ -48,3 +48,4 @@ def quickstart(): if __name__ == '__main__': quickstart() + diff --git a/iam/api-client/quickstart_test.py b/iam/api-client/quickstart_test.py index 9c7d881a6bc..7613940d6c3 100644 --- a/iam/api-client/quickstart_test.py +++ b/iam/api-client/quickstart_test.py @@ -19,3 +19,4 @@ def test_quickstart(capsys): quickstart.quickstart() out, _ = capsys.readouterr() assert 'Title' in out + diff --git a/iam/api-client/service_account_keys.py b/iam/api-client/service_account_keys.py index 265679140cf..1b8d555a062 100644 --- a/iam/api-client/service_account_keys.py +++ b/iam/api-client/service_account_keys.py @@ -95,3 +95,4 @@ def delete_key(full_key_name): create_key(args.service_account_email) elif args.command == 'delete': delete_key(args.full_key_name) + diff --git a/iam/api-client/service_accounts.py b/iam/api-client/service_accounts.py index 53a4aa9fb27..c6dcf8106e6 100644 --- a/iam/api-client/service_accounts.py +++ b/iam/api-client/service_accounts.py @@ -145,3 +145,4 @@ def main(): if __name__ == '__main__': main() + diff --git a/iam/api-client/service_accounts_test.py b/iam/api-client/service_accounts_test.py index d96123a5f14..c30310a2fe6 100644 --- a/iam/api-client/service_accounts_test.py +++ b/iam/api-client/service_accounts_test.py @@ -32,3 +32,4 @@ def test_service_accounts(capsys): email, 'Updated Py Test Account') service_accounts.delete_service_account( email) + From 944e969bcc91a3860354e4bdca457420e6742908 Mon Sep 17 00:00:00 2001 From: Alexander Koren Date: Wed, 22 Aug 2018 13:30:38 -0700 Subject: [PATCH 9/9] Lint --- iam/api-client/grantable_roles.py | 5 ++--- iam/api-client/grantable_roles_test.py | 3 ++- iam/api-client/quickstart.py | 1 - iam/api-client/quickstart_test.py | 1 - iam/api-client/service_account_keys.py | 19 +++++++++++-------- iam/api-client/service_accounts.py | 5 ++--- iam/api-client/service_accounts_test.py | 1 - 7 files changed, 17 insertions(+), 18 deletions(-) diff --git a/iam/api-client/grantable_roles.py b/iam/api-client/grantable_roles.py index 0d04e18c04b..e5986e7e1e7 100644 --- a/iam/api-client/grantable_roles.py +++ b/iam/api-client/grantable_roles.py @@ -30,7 +30,7 @@ # [START iam_view_grantable_roles] def view_grantable_roles(full_resource_name): roles = service.roles().queryGrantableRoles(body={ - 'fullResourceName': full_resource_name + 'fullResourceName': full_resource_name }).execute() for role in roles['roles']: @@ -46,7 +46,6 @@ def view_grantable_roles(full_resource_name): parser.add_argument( 'full_resource_name', help='The full name of the resource to query grantable roles for.') - + args = parser.parse_args() view_grantable_roles(args.full_resource_name) - diff --git a/iam/api-client/grantable_roles_test.py b/iam/api-client/grantable_roles_test.py index 8c5d117df50..87af3564b93 100644 --- a/iam/api-client/grantable_roles_test.py +++ b/iam/api-client/grantable_roles_test.py @@ -12,6 +12,8 @@ # See the License for the specific language governing permissions and # limitations under the License. +import os + import grantable_roles @@ -21,4 +23,3 @@ def test_service_accounts(capsys): grantable_roles.view_grantable_roles(resource) out, _ = capsys.readouterr() assert 'Title:' in out - diff --git a/iam/api-client/quickstart.py b/iam/api-client/quickstart.py index 58cc6d18f84..932e6832010 100644 --- a/iam/api-client/quickstart.py +++ b/iam/api-client/quickstart.py @@ -48,4 +48,3 @@ def quickstart(): if __name__ == '__main__': quickstart() - diff --git a/iam/api-client/quickstart_test.py b/iam/api-client/quickstart_test.py index 7613940d6c3..9c7d881a6bc 100644 --- a/iam/api-client/quickstart_test.py +++ b/iam/api-client/quickstart_test.py @@ -19,4 +19,3 @@ def test_quickstart(capsys): quickstart.quickstart() out, _ = capsys.readouterr() assert 'Title' in out - diff --git a/iam/api-client/service_account_keys.py b/iam/api-client/service_account_keys.py index 1b8d555a062..b73227dd794 100644 --- a/iam/api-client/service_account_keys.py +++ b/iam/api-client/service_account_keys.py @@ -18,13 +18,13 @@ service account keys. For more information, see the documentation at -https://cloud.google.com/iam/docs/creating-managing-service-account-keys. +https://cloud.google.com/iam/docs/creating-managing-service-account-keys. """ +import argparse import os from google.oauth2 import service_account -import argparse import googleapiclient.discovery credentials = service_account.Credentials.from_service_account_file( @@ -33,6 +33,7 @@ service = googleapiclient.discovery.build( 'iam', 'v1', credentials=credentials) + # [START iam_create_key] def create_key(service_account_email): """Creates a key for a service account.""" @@ -73,18 +74,21 @@ def delete_key(full_key_name): if __name__ == '__main__': parser = argparse.ArgumentParser( - description=__doc__, - formatter_class=argparse.RawDescriptionHelpFormatter) + description=__doc__, + formatter_class=argparse.RawDescriptionHelpFormatter) subparsers = parser.add_subparsers(dest='command') - create_key_parser = subparsers.add_parser('create', help=create_key.__doc__) + create_key_parser = subparsers.add_parser( + 'create', help=create_key.__doc__) create_key_parser.add_argument('service_account_email') - list_keys_parser = subparsers.add_parser('list', help=list_keys.__doc__) + list_keys_parser = subparsers.add_parser( + 'list', help=list_keys.__doc__) list_keys_parser.add_argument('service_account_email') - delete_key_parser = subparsers.add_parser('delete', help=delete_key.__doc__) + delete_key_parser = subparsers.add_parser( + 'delete', help=delete_key.__doc__) delete_key_parser.add_argument('full_key_name') args = parser.parse_args() @@ -95,4 +99,3 @@ def delete_key(full_key_name): create_key(args.service_account_email) elif args.command == 'delete': delete_key(args.full_key_name) - diff --git a/iam/api-client/service_accounts.py b/iam/api-client/service_accounts.py index c6dcf8106e6..f70e6024b2d 100644 --- a/iam/api-client/service_accounts.py +++ b/iam/api-client/service_accounts.py @@ -83,8 +83,8 @@ def rename_service_account(email, new_display_name): service_account = service.projects().serviceAccounts().update( name=resource, body=service_account).execute() - print('Updated display name for {} to: {}' - .format(service_account['email'], service_account['displayName'])) + print('Updated display name for {} to: {}'.format( + service_account['email'], service_account['displayName'])) return service_account # [END iam_rename_service_account] @@ -145,4 +145,3 @@ def main(): if __name__ == '__main__': main() - diff --git a/iam/api-client/service_accounts_test.py b/iam/api-client/service_accounts_test.py index c30310a2fe6..d96123a5f14 100644 --- a/iam/api-client/service_accounts_test.py +++ b/iam/api-client/service_accounts_test.py @@ -32,4 +32,3 @@ def test_service_accounts(capsys): email, 'Updated Py Test Account') service_accounts.delete_service_account( email) -