Skip to content

Commit

Permalink
Add unit test to check G Suite group is created
Browse files Browse the repository at this point in the history
  • Loading branch information
thefirstofthe300 committed Feb 20, 2019
1 parent 1958f17 commit d31f3cc
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 9 deletions.
2 changes: 1 addition & 1 deletion modules/core_project_factory/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ resource "google_compute_shared_vpc_service_project" "shared_vpc_attachment" {
Default compute service account retrieval
*****************************************/
data "google_compute_default_service_account" "default" {
project = "${google_project.main.id}"
project = "${google_project.main.project_id}"

depends_on = ["google_project_service.project_services"]
}
Expand Down
14 changes: 8 additions & 6 deletions test/fixtures/full/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ module "vpc" {
source = "terraform-google-modules/network/google"
version = "~> 0.4.0"
network_name = "pf-test-int-full-${random_string.suffix.result}"
project_id = "${var.shared_vpc}"

project_id = "${var.shared_vpc}"

# The provided project must already be a Shared VPC host
shared_vpc_host = "false"
Expand Down Expand Up @@ -89,11 +90,12 @@ module "project-factory" {
group_role = "${var.group_role}"
group_name = "${var.group_name}"
shared_vpc = "${var.shared_vpc}"
shared_vpc_subnets = "${local.shared_vpc_subnets}"
sa_role = "${var.sa_role}"
sa_group = "${var.sa_group}"
credentials_path = "${var.credentials_path}"
lien = "true"

shared_vpc_subnets = "${local.shared_vpc_subnets}"
sa_role = "${var.sa_role}"
sa_group = "${var.sa_group}"
credentials_path = "${var.credentials_path}"
lien = "true"

activate_apis = [
"compute.googleapis.com",
Expand Down
3 changes: 1 addition & 2 deletions test/fixtures/shared/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ variable "folder_id" {
default = ""
}

variable "domain" {
}
variable "domain" {}

variable "usage_bucket_name" {
default = ""
Expand Down
6 changes: 6 additions & 0 deletions test/integration/full/controls/gsuite.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
project_id = attribute('project_id')
service_account_email = attribute('service_account_email')
credentials_path = attribute('credentials_path')
gsuite_admin_account = attribute('gsuite_admin_account')

ENV['CLOUDSDK_AUTH_CREDENTIAL_FILE_OVERRIDE'] = File.absolute_path(
credentials_path,
Expand Down Expand Up @@ -74,4 +75,9 @@
)
end
end

describe command("./test/scripts/gsuite/gsuite_groups.py --sa-json-credentials='test/fixtures/shared/credentials.json' --group-email #{group_email} --impersonate-user #{gsuite_admin_account}") do
its('exit_status') { should eq 0 }
its('stderr') { should eq '' }
end
end
41 changes: 41 additions & 0 deletions test/scripts/gsuite/gsuite_groups.py
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)

0 comments on commit d31f3cc

Please sign in to comment.