Skip to content

Commit a497aef

Browse files
committed
feat: new version of projects-data-source based on AssetInventory ds
1 parent a119ce2 commit a497aef

File tree

5 files changed

+118
-155
lines changed

5 files changed

+118
-155
lines changed
Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
# Projects Data Source Module
22

3-
This module extends functionality of [google_projects](https://registry.terraform.io/providers/hashicorp/google/latest/docs/data-sources/projects) data source by retrieving all the projects and folders under a specific `parent` recursively.
3+
This module extends functionality of [google_projects](https://registry.terraform.io/providers/hashicorp/google/latest/docs/data-sources/projects) data source by retrieving all the projects under a specific `parent` recursively with only one API call against [Cloud Asset Inventory](https://cloud.google.com/asset-inventory) service.
44

55
A good usage pattern would be when we want all the projects under a specific folder (including nested subfolders) to be included into [VPC Service Controls](../vpc-sc/). Instead of manually maintaining the list of project numbers as an input to the `vpc-sc` module we can use that module to retrieve all the project numbers dynamically.
66

7+
### IAM Permissions required
8+
9+
- `roles/cloudasset.viewer` on the `parent` level or above
10+
11+
712
## Examples
813

914
### All projects in my org
@@ -15,11 +20,7 @@ module "my-org" {
1520
}
1621
1722
output "projects" {
18-
value = module.my-org.projects
19-
}
20-
21-
output "folders" {
22-
value = module.my-org.folders
23+
value = module.my-org.projects_numbers
2324
}
2425
2526
# tftest skip (uses data sources)
@@ -31,34 +32,65 @@ output "folders" {
3132
module "my-dev" {
3233
source = "./fabric/modules/projects-data-source"
3334
parent = "folders/123456789"
34-
filter = "labels.env:DEV lifecycleState:ACTIVE"
35+
query = "labels.env:DEV state:ACTIVE"
3536
}
3637
3738
output "dev-projects" {
3839
value = module.my-dev.projects
3940
}
4041
41-
output "dev-folders" {
42-
value = module.my-dev.folders
42+
# tftest skip (uses data sources)
43+
```
44+
45+
### Projects under org with folder/project exclusions
46+
```hcl
47+
module "my-filtered" {
48+
source = "./fabric/modules/projects-data-source"
49+
parent = "organizations/123456789"
50+
ignore_projects = [
51+
"sandbox-*", # wildcard ignore
52+
"project-full-id", # specific project id
53+
"0123456789" # specific project number
54+
]
55+
56+
include_projects = [
57+
"sandbox-114", # include specific project which was excluded by wildcard
58+
"415216609246" # include specific project which was excluded by wildcard (by project number)
59+
]
60+
61+
ignore_folders = [ # subfolders are ingoner as well
62+
"343991594985",
63+
"437102807785",
64+
"345245235245"
65+
]
66+
query = "state:ACTIVE"
67+
}
68+
69+
output "filtered-projects" {
70+
value = module.my-filtered.projects
4371
}
4472
4573
# tftest skip (uses data sources)
74+
4675
```
76+
4777
<!-- BEGIN TFDOC -->
4878

4979
## Variables
5080

5181
| name | description | type | required | default |
5282
|---|---|:---:|:---:|:---:|
53-
| [parent](variables.tf#L23) | Parent folder or organization in 'folders/folder_id' or 'organizations/org_id' format. | <code>string</code> || |
54-
| [filter](variables.tf#L17) | A string filter as defined in the [REST API](https://cloud.google.com/resource-manager/reference/rest/v1/projects/list#query-parameters). | <code>string</code> | | <code>&#34;lifecycleState:ACTIVE&#34;</code> |
83+
| [parent](variables.tf#L17) | Parent folder or organization in 'folders/folder_id' or 'organizations/org_id' format. | <code>string</code> || |
84+
| [ignore_folders](variables.tf#L58) | A list of folder IDs or numbers to be excluded from the output, all the subfolders and projects are exluded from the output regardless of the include_projects variable. | <code>list&#40;string&#41;</code> | | <code>&#91;&#93;</code> |
85+
| [ignore_projects](variables.tf#L32) | A list of project IDs, numbers or prefixes to exclude matching projects from the module output. | <code>list&#40;string&#41;</code> | | <code>&#91;&#93;</code> |
86+
| [include_projects](variables.tf#L44) | A list of project IDs/numbers to include to the output if some of them are excluded by `ignore_projects` wilcard entries. | <code>list&#40;string&#41;</code> | | <code>&#91;&#93;</code> |
87+
| [query](variables.tf#L26) | A string query as defined in the [Query Syntax](https://cloud.google.com/asset-inventory/docs/query-syntax). | <code>string</code> | | <code>&#34;state:ACTIVE&#34;</code> |
5588

5689
## Outputs
5790

5891
| name | description | sensitive |
5992
|---|---|:---:|
60-
| [folders](outputs.tf#L17) | Map of folders attributes keyed by folder id. | |
61-
| [project_numbers](outputs.tf#L22) | List of project numbers. | |
62-
| [projects](outputs.tf#L27) | Map of projects attributes keyed by projects id. | |
93+
| [project_numbers](outputs.tf#L17) | List of project numbers. | |
94+
| [projects](outputs.tf#L22) | List of projects in [StandardResourceMetadata](https://cloud.google.com/asset-inventory/docs/reference/rest/v1p1beta1/resources/searchAll#StandardResourceMetadata) format. | |
6395

6496
<!-- END TFDOC -->

modules/projects-data-source/main.tf

Lines changed: 22 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2022 Google LLC
2+
* Copyright 2023 Google LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,129 +15,27 @@
1515
*/
1616

1717
locals {
18-
folders_l1_map = { for item in data.google_folders.folders_l1.folders : item.name => item }
19-
20-
folders_l2_map = merge([
21-
for _, v in data.google_folders.folders_l2 :
22-
{ for item in v.folders : item.name => item }
23-
]...)
24-
25-
folders_l3_map = merge([
26-
for _, v in data.google_folders.folders_l3 :
27-
{ for item in v.folders : item.name => item }
28-
]...)
29-
30-
folders_l4_map = merge([
31-
for _, v in data.google_folders.folders_l4 :
32-
{ for item in v.folders : item.name => item }
33-
]...)
34-
35-
folders_l5_map = merge([
36-
for _, v in data.google_folders.folders_l5 :
37-
{ for item in v.folders : item.name => item }
38-
]...)
39-
40-
folders_l6_map = merge([
41-
for _, v in data.google_folders.folders_l6 :
42-
{ for item in v.folders : item.name => item }
43-
]...)
44-
45-
folders_l7_map = merge([
46-
for _, v in data.google_folders.folders_l7 :
47-
{ for item in v.folders : item.name => item }
48-
]...)
49-
50-
folders_l8_map = merge([
51-
for _, v in data.google_folders.folders_l8 :
52-
{ for item in v.folders : item.name => item }
53-
]...)
54-
55-
folders_l9_map = merge([
56-
for _, v in data.google_folders.folders_l9 :
57-
{ for item in v.folders : item.name => item }
58-
]...)
59-
60-
folders_l10_map = merge([
61-
for _, v in data.google_folders.folders_l10 :
62-
{ for item in v.folders : item.name => item }
63-
]...)
64-
65-
all_folders = merge(
66-
local.folders_l1_map,
67-
local.folders_l2_map,
68-
local.folders_l3_map,
69-
local.folders_l4_map,
70-
local.folders_l5_map,
71-
local.folders_l6_map,
72-
local.folders_l7_map,
73-
local.folders_l8_map,
74-
local.folders_l9_map,
75-
local.folders_l10_map
18+
_ignore_folder_numbers = [for folder_id in var.ignore_folders: trimprefix(folder_id, "folders/")]
19+
_ignore_folders_query = join(" AND NOT folders:", concat([""], local._ignore_folder_numbers))
20+
query = var.query != "" ? (
21+
format("%s%s", var.query, local._ignore_folders_query)
22+
) : (
23+
format("%s%s", var.query, trimprefix(local._ignore_folders_query, " AND "))
7624
)
7725

78-
parent_ids = toset(concat(
79-
[split("/", var.parent)[1]],
80-
[for k, _ in local.all_folders : split("/", k)[1]]
81-
))
82-
83-
projects = merge([
84-
for _, v in data.google_projects.projects :
85-
{ for item in v.projects : item.project_id => item }
86-
]...)
87-
}
88-
89-
# 10 datasources are used to cover 10 possible nested layers in GCP organization hirerarcy.
90-
data "google_folders" "folders_l1" {
91-
parent_id = var.parent
92-
}
93-
94-
data "google_folders" "folders_l2" {
95-
for_each = local.folders_l1_map
96-
parent_id = each.value.name
97-
}
98-
99-
data "google_folders" "folders_l3" {
100-
for_each = local.folders_l2_map
101-
parent_id = each.value.name
102-
}
103-
104-
data "google_folders" "folders_l4" {
105-
for_each = local.folders_l3_map
106-
parent_id = each.value.name
107-
}
108-
109-
data "google_folders" "folders_l5" {
110-
for_each = local.folders_l4_map
111-
parent_id = each.value.name
112-
}
113-
114-
data "google_folders" "folders_l6" {
115-
for_each = local.folders_l5_map
116-
parent_id = each.value.name
117-
}
118-
119-
data "google_folders" "folders_l7" {
120-
for_each = local.folders_l6_map
121-
parent_id = each.value.name
122-
}
123-
124-
data "google_folders" "folders_l8" {
125-
for_each = local.folders_l7_map
126-
parent_id = each.value.name
127-
}
128-
129-
data "google_folders" "folders_l9" {
130-
for_each = local.folders_l8_map
131-
parent_id = each.value.name
132-
}
133-
134-
data "google_folders" "folders_l10" {
135-
for_each = local.folders_l9_map
136-
parent_id = each.value.name
137-
}
138-
139-
# Getting all projects parented by any of the folders in the tree including root prg/folder provided by `parent` variable.
140-
data "google_projects" "projects" {
141-
for_each = local.parent_ids
142-
filter = "parent.id:${each.value} ${var.filter}"
26+
ignore_patterns = [for item in var.ignore_projects: "^${replace(item, "*", ".*")}$"]
27+
ignore_regexp = length(local.ignore_patterns) > 0 ? join("|", local.ignore_patterns) : "^NO_PROJECTS_TO_IGNORE$"
28+
projects_after_ignore = [ for item in data.google_cloud_asset_resources_search_all.projects.results : item if (
29+
length(concat(try(regexall(local.ignore_regexp, trimprefix(item.project, "projects/")), []), try(regexall(local.ignore_regexp, trimprefix(item.name, "//cloudresourcemanager.googleapis.com/projects/")), []))) == 0
30+
) || contains(var.include_projects, trimprefix(item.name, "//cloudresourcemanager.googleapis.com/projects/")) || contains(var.include_projects, trimprefix(item.project, "projects/"))
31+
]
32+
}
33+
34+
data google_cloud_asset_resources_search_all projects {
35+
provider = google-beta
36+
scope = var.parent
37+
asset_types = [
38+
"cloudresourcemanager.googleapis.com/Project"
39+
]
40+
query = local.query
14341
}
Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2022 Google LLC
2+
* Copyright 2023 Google LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -14,17 +14,12 @@
1414
* limitations under the License.
1515
*/
1616

17-
output "folders" {
18-
description = "Map of folders attributes keyed by folder id."
19-
value = local.all_folders
20-
}
21-
2217
output "project_numbers" {
2318
description = "List of project numbers."
24-
value = [for _, v in local.projects : v.number]
19+
value = [for item in local.projects_after_ignore : trimprefix(item.project, "projects/")]
2520
}
2621

2722
output "projects" {
28-
description = "Map of projects attributes keyed by projects id."
29-
value = local.projects
23+
description = "List of projects in [StandardResourceMetadata](https://cloud.google.com/asset-inventory/docs/reference/rest/v1p1beta1/resources/searchAll#StandardResourceMetadata) format."
24+
value = local.projects_after_ignore
3025
}

modules/projects-data-source/variables.tf

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2022 Google LLC
2+
* Copyright 2023 Google LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -14,12 +14,6 @@
1414
* limitations under the License.
1515
*/
1616

17-
variable "filter" {
18-
description = "A string filter as defined in the [REST API](https://cloud.google.com/resource-manager/reference/rest/v1/projects/list#query-parameters)."
19-
type = string
20-
default = "lifecycleState:ACTIVE"
21-
}
22-
2317
variable "parent" {
2418
description = "Parent folder or organization in 'folders/folder_id' or 'organizations/org_id' format."
2519
type = string
@@ -28,3 +22,47 @@ variable "parent" {
2822
error_message = "Parent must be of the form folders/folder_id or organizations/organization_id."
2923
}
3024
}
25+
26+
variable "query" {
27+
description = "A string query as defined in the [Query Syntax](https://cloud.google.com/asset-inventory/docs/query-syntax)."
28+
type = string
29+
default = "state:ACTIVE"
30+
}
31+
32+
variable "ignore_projects" {
33+
description = "A list of project IDs, numbers or prefixes to exclude matching projects from the module output."
34+
type = list(string)
35+
default = []
36+
# example
37+
#ignore_projects = [
38+
# "dev-proj-1",
39+
# "uat-proj-2",
40+
# "0123456789",
41+
# "prd-proj-*"
42+
#]
43+
}
44+
variable "include_projects" {
45+
description = "A list of project IDs/numbers to include to the output if some of them are excluded by `ignore_projects` wilcard entries."
46+
type = list(string)
47+
default = []
48+
# example excluding all the projects starting with "prf-" except "prd-123457"
49+
#ignore_projects = [
50+
# "prd-*"
51+
#]
52+
#include_projects = [
53+
# "prd-123457",
54+
# "0123456789"
55+
#]
56+
}
57+
58+
variable "ignore_folders" {
59+
description = "A list of folder IDs or numbers to be excluded from the output, all the subfolders and projects are exluded from the output regardless of the include_projects variable."
60+
type = list(string)
61+
default = []
62+
# example exlusing a folder
63+
# ignore_folders = [
64+
# "folders/0123456789",
65+
# "2345678901"
66+
# ]
67+
}
68+

modules/projects-data-source/versions.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2022 Google LLC
1+
# Copyright 2023 Google LLC
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.

0 commit comments

Comments
 (0)