Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding new App Engine support #4

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ In order to operate with the Service Account you must activate the following API
- Cloud Billing API - cloudbilling.googleapis.com
- Identity and Access Management API - iam.googleapis.com
- Admin SDK - admin.googleapis.com
- Google App Engine Admin API - appengine.googleapis.com

### GSuite
#### Admin
Expand Down
13 changes: 13 additions & 0 deletions examples/app_engine/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# App Engine Project

This example illustrates how to create a simple project with App Engine enabled.

It will do the following:
- Create a project
- Active the Google App Engine Admin API on the new project
- Create a new App Engine app

Expected variables:
- `admin_email`
- `organization_id`
- `billing_account`
46 changes: 46 additions & 0 deletions examples/app_engine/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* 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
*
* 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.
*/

locals {
credentials_file_path = "${path.module}/sa-key.json"
}

/******************************************
Provider configuration
*****************************************/
provider "gsuite" {
credentials = "${file(local.credentials_file_path)}"
impersonated_user_email = "${var.admin_email}"
}

module "project-factory" {
source = "../../"
random_project_id = "true"
name = "appeng-sample"
org_id = "${var.organization_id}"
billing_account = "${var.billing_account}"
credentials_path = "${local.credentials_file_path}"

app_engine {
location_id = "us-central"

feature_settings = [
{
split_health_checks = true
},
]
}
}
30 changes: 30 additions & 0 deletions examples/app_engine/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* 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
*
* 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.
*/

output "project_info_example" {
value = "${module.project-factory.project_id}"
description = "The ID of the created project"
}

output "domain_example" {
value = "${module.project-factory.domain}"
description = "The organization's domain"
}

output "app_engine_enabled_example" {
value = "${module.project-factory.app_engine_enabled}"
description = "Whether app engine is enabled"
}
27 changes: 27 additions & 0 deletions examples/app_engine/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* 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
*
* 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.
*/

variable "admin_email" {
description = "Admin user email on Gsuite"
}

variable "organization_id" {
description = "The organization id for the associated services"
}

variable "billing_account" {
description = "The ID of the billing account to associate this project with"
}
25 changes: 25 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ locals {
project_bucket_name = "${var.bucket_name != "" ? var.bucket_name : format("%s-state", var.name)}"
create_bucket = "${var.bucket_project != "" ? "true" : "false"}"
gsuite_group = "${var.group_name != "" || var.create_group}"
app_engine_enabled = "${length(keys(var.app_engine)) > 0 ? true : false}"

app_engine_config = {
enabled = "${list(var.app_engine)}"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this actually work? var.app_engine is a map, not a list.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It gets the length of keys in app_engine which is a list

Copy link
Contributor

@morgante morgante Jul 27, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

local.app_engine_config is a map of lists with keys enabled and disabled. Line 86 is referencing that list by key depending if local.app_engine_enabled is true or false. If local.app_engine_enabled is true, it sets app_engine to the enabled list which is the users config, otherwise it uses the disabled list which is empty. It is a dirty hack, but the only way I could see to pass a completely empty list to app_engine and still be able to accept the app_engine variable as a map in our module. Otherwise, it would set app_engine = [{}] which isnt an empty list and tells the provider to setill enable app_engine.

disabled = "${list()}"
}
}

/******************************************
Expand Down Expand Up @@ -76,6 +82,8 @@ resource "google_project" "project" {
auto_create_network = "${var.auto_create_network}"

labels = "${var.labels}"

app_engine = "${local.app_engine_config["${local.app_engine_enabled ? "enabled" : "disabled"}"]}"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ryanckoch This is hard to parse and somewhat confusing.

Wouldn't app_engine = "${local.app_engine_enabled ? var.app_engine : list()}" work just as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It wont allow for a list in a conditional. That would definitely be much cleaner, but that is the reason for the hack app_engine_config map.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about "${local.app_engine_enabled ? local.app_engine_config["enabled"] : local.app_engine_config["disabled"]}"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same:

Error: module.project-factory.google_project.project: 1 error(s) occurred:

* module.project-factory.google_project.project: At column 3, line 1: conditional operator cannot be used with list values in:

${local.app_engine_enabled ? local.app_engine_config["enabled"] : local.app_engine_config["disabled"]}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also worth noting, that although the examples in the documentation show app_engine as accepting a map, it accepts a list with a max length of 1.
https://github.com/terraform-providers/terraform-provider-google/blob/master/google/resource_google_project.go#L95

Have created an issue on the provider: hashicorp/terraform-provider-google#1821

}

/******************************************
Expand Down Expand Up @@ -119,6 +127,23 @@ resource "null_resource" "delete_default_compute_service_account" {
depends_on = ["google_project_service.project_services"]
}

/******************************************
Default app engine service account deletion
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ryanckoch We should not delete the app engine service account—deleting it will break App Engine.

*****************************************/
resource "null_resource" "delete_default_app_engine_service_account" {
count = "${local.app_engine_enabled ? 1 : 0}"

provisioner "local-exec" {
command = "${path.module}/scripts/delete-service-account.sh ${local.project_id} ${var.credentials_path} ${local.project_id}@appspot.gserviceaccount.com"
}

triggers {
app_engine_enabled = "${local.app_engine_enabled}"
}

depends_on = ["google_project_service.project_services"]
}

/******************************************
Default Service Account configuration
*****************************************/
Expand Down
5 changes: 5 additions & 0 deletions outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,8 @@ output "project_bucket_url" {
value = "${google_storage_bucket.project_bucket.*.url}"
description = "Project's bucket url"
}

output "app_engine_enabled" {
value = "${local.app_engine_enabled}"
description = "Whether app engine is enabled"
}
19 changes: 16 additions & 3 deletions test/integration/gcloud/integration.bats
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,18 @@
[[ "${lines[5]}" = "projectId: $PROJECT_ID" ]]
}

@test "Test the compute api must be activated" {
@test "Test the correct apis are activated" {

export PROJECT_ID="$(terraform output project_info_example)"
export GROUP_EMAIL="$(terraform output group_email_example)"

run gcloud services list
[ "$status" -eq 0 ]
[[ "${lines[1]}" = *"compute.googleapis.com"* ]]
[[ "${lines[2]}" = *"compute.googleapis.com"* ]]

run gcloud services list
[ "$status" -eq 0 ]
[[ "${lines[1]}" = *"appengine.googleapis.com"* ]]
}

@test "Test that project has the shared vpc associated (host project)" {
Expand All @@ -79,7 +83,7 @@
[[ "${lines[1]}" = " email: project-service-account@$PROJECT_ID.iam.gserviceaccount.com" ]]
}

@test "Test project has not the default service account" {
@test "Test project has not the default compute or app engine service account" {

export PROJECT_ID="$(terraform output project_info_example)"
export GROUP_EMAIL="$(terraform output group_email_example)"
Expand Down Expand Up @@ -132,6 +136,15 @@
[[ "$output" = *"{u'role': u'roles/compute.networkUser', u'members': [u'group:$GROUP_EMAIL', u'serviceAccount:project-service-account@$PROJECT_ID.iam.gserviceaccount.com']}"* ]]
}

@test "Test App Engine app created" {

export PROJECT_ID="$(terraform output project_info_example)"

run gcloud app describe --format="get(name)"
[ "$status" -eq 0 ]
[[ "$output" = "apps/$PROJECT_ID" ]]
}

# #################################### #
# Terraform destroy test #
# #################################### #
Expand Down
3 changes: 3 additions & 0 deletions test/integration/gcloud/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ module "project-factory" {
sa_group = "$SA_GROUP"
folder_id = "$FOLDER_ID"
credentials_path = "\${local.credentials_file_path}"
app_engine {
location_id = "$LOCATION_ID"
}
}
EOF
}
Expand Down
1 change: 1 addition & 0 deletions test/integration/gcloud/sample.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ export GROUP_NAME="test-group$RANDOM"
export CREATE_GROUP="true"
export FOLDER_ID=""
export GROUP_ROLE="roles/editor"
export LOCATION_ID="us-central"
export CLOUDSDK_AUTH_CREDENTIAL_FILE_OVERRIDE=$CREDENTIALS_PATH
6 changes: 6 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,9 @@ variable "auto_create_network" {
description = "Create the default network"
default = "false"
}

variable "app_engine" {
description = "A map for app engine configuration"
type = "map"
default = {}
}