-
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
926009c
commit 15019ee
Showing
7 changed files
with
92 additions
and
20 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
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,63 @@ | ||
#! /usr/bin/env python3 | ||
|
||
# Copyright 2018 Google LLC | ||
# | ||
# 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 | ||
# | ||
# https://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 | ||
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) |