Skip to content

Commit 183c4e7

Browse files
authored
Merge pull request #303 from terraform-google-modules/feature/api-example
Fix up API services module and example
2 parents 6922559 + e791465 commit 183c4e7

File tree

6 files changed

+17
-26
lines changed

6 files changed

+17
-26
lines changed

modules/project_services/examples/project_services/README.md renamed to examples/project_services/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Expected variables:
1111

1212
| Name | Description | Type | Default | Required |
1313
|------|-------------|:----:|:-----:|:-----:|
14-
| 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 |
14+
| enable | Actually enable the APIs listed | string | `"true"` | no |
1515
| project\_id | The GCP project you want to enable APIs on | string | n/a | yes |
1616

1717
## Outputs

modules/project_services/examples/project_services/main.tf renamed to examples/project_services/main.tf

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,32 +14,25 @@
1414
* limitations under the License.
1515
*/
1616

17-
locals {
18-
credentials_file_path = var.credentials_path
19-
}
20-
2117
/******************************************
2218
Provider configuration
2319
*****************************************/
2420
provider "google" {
25-
credentials = file(local.credentials_file_path)
26-
version = "~> 2.18.1"
21+
version = "~> 2.18.1"
2722
}
2823

2924
provider "google-beta" {
30-
credentials = file(local.credentials_file_path)
31-
version = "~> 2.18.1"
25+
version = "~> 2.18.1"
3226
}
3327

3428
module "project-services" {
35-
source = "../../../../modules/project_services"
29+
source = "../../modules/project_services"
3630
project_id = var.project_id
37-
enable_apis = "true"
31+
enable_apis = var.enable
3832
disable_services_on_destroy = "true"
3933

4034
activate_apis = [
41-
"compute.googleapis.com",
42-
"iam.googleapis.com",
35+
"sqladmin.googleapis.com",
36+
"bigquery-json.googleapis.com",
4337
]
4438
}
45-

modules/project_services/examples/project_services/outputs.tf renamed to examples/project_services/outputs.tf

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,3 @@ output "project_id" {
1818
value = module.project-services.project_id
1919
description = "The GCP project you want to enable APIs on"
2020
}
21-
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,11 @@
1414
* limitations under the License.
1515
*/
1616

17-
variable "credentials_path" {
18-
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."
19-
default = ""
20-
}
21-
2217
variable "project_id" {
2318
description = "The GCP project you want to enable APIs on"
2419
}
2520

21+
variable "enable" {
22+
description = "Actually enable the APIs listed"
23+
default = true
24+
}

modules/project_services/main.tf

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,15 @@
1414
* limitations under the License.
1515
*/
1616

17+
locals {
18+
api_set = var.enable_apis ? toset(var.activate_apis) : []
19+
}
20+
1721
/******************************************
1822
APIs configuration
1923
*****************************************/
2024
resource "google_project_service" "project_services" {
21-
for_each = toset([for api in var.activate_apis : api if var.enable_apis])
25+
for_each = local.api_set
2226
project = var.project_id
2327
service = each.value
2428
disable_on_destroy = var.disable_services_on_destroy

modules/project_services/outputs.tf

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,5 @@
1616

1717
output "project_id" {
1818
description = "The GCP project you want to enable APIs on"
19-
value = element(
20-
[for v in google_project_service.project_services : v.project],
21-
0,
22-
)
19+
value = var.enable_apis ? element([for v in google_project_service.project_services : v.project], 0) : var.project_id
2320
}
24-

0 commit comments

Comments
 (0)