-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unit test to check G Suite group is created
- Loading branch information
1 parent
1958f17
commit d31f3cc
Showing
5 changed files
with
57 additions
and
9 deletions.
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
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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#! /usr/bin/env python2 | ||
|
||
import argparse | ||
from googleapiclient.errors import HttpError | ||
from googleapiclient.discovery import build | ||
from oauth2client.service_account import ServiceAccountCredentials | ||
|
||
SCOPES = ['https://www.googleapis.com/auth/admin.directory.group'] | ||
|
||
def authenticate(impersonated_user, sa_json_file_path, scopes): | ||
print 'Getting delegated credentials for %s' % impersonated_user | ||
|
||
credentials = ServiceAccountCredentials.from_json_keyfile_name( | ||
sa_json_file_path, | ||
scopes=scopes | ||
) | ||
|
||
return credentials.create_delegated(impersonated_user) | ||
|
||
def group_exists(service, group_email): | ||
try: | ||
return service.groups().get(groupKey=group_email).execute() | ||
except HttpError as e: | ||
if e.resp.status == 404: | ||
print 'Group {0} does not exist'.format(group_email) | ||
exit(1) | ||
else: | ||
print 'Error fetching groups {0} {1}'.format(e.content, e.error_details) | ||
exit(2) | ||
|
||
if __name__ == "__main__": | ||
parser = argparse.ArgumentParser(description='Test if the specified G Suite exists') | ||
parser.add_argument('--sa-json-credentials', dest='sa_json_credentials') | ||
parser.add_argument('--group-email', dest='group_email') | ||
parser.add_argument('--impersonate-user', dest='impersonate_user') | ||
args = parser.parse_args() | ||
|
||
service = build("admin", "directory_v1", credentials=authenticate(args.impersonate_user, | ||
args.sa_json_credentials, | ||
SCOPES)) | ||
group_exists(service, args.group_email) |