-
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
b709706
commit d1e501e
Showing
5 changed files
with
64 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,48 @@ | ||
#! /usr/bin/env python3 | ||
|
||
import argparse | ||
from googleapiclient.errors import HttpError | ||
from googleapiclient.discovery import build | ||
from google.oauth2 import service_account | ||
|
||
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) | ||
|
||
return service_account.Credentials.from_service_account_file( | ||
sa_json_file_path, | ||
scopes=scopes, | ||
subject=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 %s does not exist' % group_email) | ||
exit(1) | ||
else: | ||
print('Error fetching groups %s %s' % 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) |