Skip to content

Commit

Permalink
Merge pull request #303 from terraform-google-modules/feature/api-exa…
Browse files Browse the repository at this point in the history
…mple

Fix up API services module and example
  • Loading branch information
morgante authored Oct 30, 2019
2 parents 6922559 + e791465 commit 183c4e7
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Expected variables:

| Name | Description | Type | Default | Required |
|------|-------------|:----:|:-----:|:-----:|
| credentials\_path | Path to a service account credentials file with rights to run the Project Factory. If this file is absent Terraform will fall back to Application Default Credentials. | string | `""` | no |
| enable | Actually enable the APIs listed | string | `"true"` | no |
| project\_id | The GCP project you want to enable APIs on | string | n/a | yes |

## Outputs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,25 @@
* limitations under the License.
*/

locals {
credentials_file_path = var.credentials_path
}

/******************************************
Provider configuration
*****************************************/
provider "google" {
credentials = file(local.credentials_file_path)
version = "~> 2.18.1"
version = "~> 2.18.1"
}

provider "google-beta" {
credentials = file(local.credentials_file_path)
version = "~> 2.18.1"
version = "~> 2.18.1"
}

module "project-services" {
source = "../../../../modules/project_services"
source = "../../modules/project_services"
project_id = var.project_id
enable_apis = "true"
enable_apis = var.enable
disable_services_on_destroy = "true"

activate_apis = [
"compute.googleapis.com",
"iam.googleapis.com",
"sqladmin.googleapis.com",
"bigquery-json.googleapis.com",
]
}

Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,3 @@ output "project_id" {
value = module.project-services.project_id
description = "The GCP project you want to enable APIs on"
}

Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@
* limitations under the License.
*/

variable "credentials_path" {
description = "Path to a service account credentials file with rights to run the Project Factory. If this file is absent Terraform will fall back to Application Default Credentials."
default = ""
}

variable "project_id" {
description = "The GCP project you want to enable APIs on"
}

variable "enable" {
description = "Actually enable the APIs listed"
default = true
}
6 changes: 5 additions & 1 deletion modules/project_services/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@
* limitations under the License.
*/

locals {
api_set = var.enable_apis ? toset(var.activate_apis) : []
}

/******************************************
APIs configuration
*****************************************/
resource "google_project_service" "project_services" {
for_each = toset([for api in var.activate_apis : api if var.enable_apis])
for_each = local.api_set
project = var.project_id
service = each.value
disable_on_destroy = var.disable_services_on_destroy
Expand Down
6 changes: 1 addition & 5 deletions modules/project_services/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,5 @@

output "project_id" {
description = "The GCP project you want to enable APIs on"
value = element(
[for v in google_project_service.project_services : v.project],
0,
)
value = var.enable_apis ? element([for v in google_project_service.project_services : v.project], 0) : var.project_id
}

0 comments on commit 183c4e7

Please sign in to comment.