Skip to content

Commit 534a547

Browse files
authored
fix!: added concrete types for simple sa variables (#386)
1 parent 27829d2 commit 534a547

File tree

5 files changed

+88
-16
lines changed

5 files changed

+88
-16
lines changed

examples/simple_bucket/main.tf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,18 @@ module "bucket" {
2222
project_id = var.project_id
2323
location = "us"
2424

25+
website = {
26+
main_page_suffix = "index.html"
27+
not_found_page = "404.html"
28+
}
29+
30+
cors = [{
31+
origin = ["http://image-store.com"]
32+
method = ["GET", "HEAD", "PUT", "POST", "DELETE"]
33+
response_header = ["*"]
34+
max_age_seconds = 3600
35+
}]
36+
2537
lifecycle_rules = [{
2638
action = {
2739
type = "Delete"

modules/simple_bucket/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,14 @@ Functional examples are included in the
4040
|------|-------------|------|---------|:--------:|
4141
| autoclass | While set to true, autoclass is enabled for this bucket. | `bool` | `false` | no |
4242
| bucket\_policy\_only | Enables Bucket Policy Only access to a bucket. | `bool` | `true` | no |
43-
| cors | Configuration of CORS for bucket with structure as defined in https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/storage_bucket#cors. | `any` | `[]` | no |
43+
| cors | Configuration of CORS for bucket with structure as defined in https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/storage_bucket#cors. | <pre>list(object({<br> origin = optional(list(string))<br> method = optional(list(string))<br> response_header = optional(list(string))<br> max_age_seconds = optional(number)<br> }))</pre> | `[]` | no |
4444
| custom\_placement\_config | Configuration of the bucket's custom location in a dual-region bucket setup. If the bucket is designated a single or multi-region, the variable are null. | <pre>object({<br> data_locations = list(string)<br> })</pre> | `null` | no |
4545
| encryption | A Cloud KMS key that will be used to encrypt objects inserted into this bucket. The key name should follow the format of `projects/<project-name>/locations/<location-name>/keyRings/<keyring-name>/cryptoKeys/<key-name>`. To use a Cloud KMS key automatically created by this module use the `internal_encryption_config` input variable. | <pre>object({<br> default_kms_key_name = string<br> })</pre> | `null` | no |
4646
| force\_destroy | When deleting a bucket, this boolean option will delete all contained objects. If false, Terraform will fail to delete buckets which contain objects. | `bool` | `false` | no |
4747
| iam\_members | The list of IAM members to grant permissions on the bucket. | <pre>list(object({<br> role = string<br> member = string<br> }))</pre> | `[]` | no |
4848
| internal\_encryption\_config | Configuration for the creation of an internal Google Cloud Key Management Service (KMS) Key for use as Customer-managed encryption key (CMEK) for the GCS Bucket<br> instead of creating one in advance and providing the key in the variable `encryption.default_kms_key_name`.<br> create\_encryption\_key: If `true` a Google Cloud Key Management Service (KMS) KeyRing and a Key will be created<br> prevent\_destroy: Set the prevent\_destroy lifecycle attribute on keys.<br> key\_destroy\_scheduled\_duration: Set the period of time that versions of keys spend in the `DESTROY_SCHEDULED` state before transitioning to `DESTROYED`.<br> key\_rotation\_period: Generate a new key every time this period passes. | <pre>object({<br> create_encryption_key = optional(bool, false)<br> prevent_destroy = optional(bool, false)<br> key_destroy_scheduled_duration = optional(string, null)<br> key_rotation_period = optional(string, "7776000s")<br> })</pre> | `{}` | no |
4949
| labels | A set of key/value label pairs to assign to the bucket. | `map(string)` | `null` | no |
50-
| lifecycle\_rules | The bucket's Lifecycle Rules configuration. | <pre>list(object({<br> # Object with keys:<br> # - type - The type of the action of this Lifecycle Rule. Supported values: Delete and SetStorageClass.<br> # - storage_class - (Required if action type is SetStorageClass) The target Storage Class of objects affected by this Lifecycle Rule.<br> action = any<br><br> # Object with keys:<br> # - age - (Optional) Minimum age of an object in days to satisfy this condition.<br> # - created_before - (Optional) Creation date of an object in RFC 3339 (e.g. 2017-06-13) to satisfy this condition.<br> # - with_state - (Optional) Match to live and/or archived objects. Supported values include: "LIVE", "ARCHIVED", "ANY".<br> # - matches_storage_class - (Optional) Storage Class of objects to satisfy this condition. Supported values include: MULTI_REGIONAL, REGIONAL, NEARLINE, COLDLINE, STANDARD, DURABLE_REDUCED_AVAILABILITY.<br> # - matches_prefix - (Optional) One or more matching name prefixes to satisfy this condition.<br> # - matches_suffix - (Optional) One or more matching name suffixes to satisfy this condition<br> # - num_newer_versions - (Optional) Relevant only for versioned objects. The number of newer versions of an object to satisfy this condition.<br> condition = any<br> }))</pre> | `[]` | no |
50+
| lifecycle\_rules | The bucket's Lifecycle Rules configuration. | <pre>list(object({<br> # Object with keys:<br> # - type - The type of the action of this Lifecycle Rule. Supported values: Delete and SetStorageClass.<br> # - storage_class - (Required if action type is SetStorageClass) The target Storage Class of objects affected by this Lifecycle Rule.<br> action = object({<br> type = string<br> storage_class = optional(string)<br> })<br><br> # Object with keys:<br> # - age - (Optional) Minimum age of an object in days to satisfy this condition.<br> # - send_age_if_zero - (Optional) While set true, num_newer_versions value will be sent in the request even for zero value of the field.<br> # - created_before - (Optional) Creation date of an object in RFC 3339 (e.g. 2017-06-13) to satisfy this condition.<br> # - with_state - (Optional) Match to live and/or archived objects. Supported values include: "LIVE", "ARCHIVED", "ANY".<br> # - matches_storage_class - (Optional) Storage Class of objects to satisfy this condition. Supported values include: MULTI_REGIONAL, REGIONAL, NEARLINE, COLDLINE, STANDARD, DURABLE_REDUCED_AVAILABILITY.<br> # - matches_prefix - (Optional) One or more matching name prefixes to satisfy this condition.<br> # - matches_suffix - (Optional) One or more matching name suffixes to satisfy this condition<br> # - num_newer_versions - (Optional) Relevant only for versioned objects. The number of newer versions of an object to satisfy this condition.<br> # - custom_time_before - (Optional) A date in the RFC 3339 format YYYY-MM-DD. This condition is satisfied when the customTime metadata for the object is set to an earlier date than the date used in this lifecycle condition.<br> # - days_since_custom_time - (Optional) Days since the date set in the customTime metadata for the object.<br> # - days_since_noncurrent_time - (Optional) Relevant only for versioned objects. Number of days elapsed since the noncurrent timestamp of an object.<br> # - noncurrent_time_before - (Optional) Relevant only for versioned objects. The date in RFC 3339 (e.g. 2017-06-13) when the object became nonconcurrent.<br> condition = object({<br> age = optional(number)<br> send_age_if_zero = optional(bool)<br> created_before = optional(string)<br> with_state = optional(string)<br> matches_storage_class = optional(string)<br> matches_prefix = optional(string)<br> matches_suffix = optional(string)<br> num_newer_versions = optional(number)<br> custom_time_before = optional(string)<br> days_since_custom_time = optional(number)<br> days_since_noncurrent_time = optional(number)<br> noncurrent_time_before = optional(string)<br> })<br> }))</pre> | `[]` | no |
5151
| location | The location of the bucket. See https://cloud.google.com/storage/docs/locations. | `string` | n/a | yes |
5252
| log\_bucket | The bucket that will receive log objects. | `string` | `null` | no |
5353
| log\_object\_prefix | The object prefix for log objects. If it's not provided, by default GCS sets this to this bucket's name | `string` | `null` | no |
@@ -58,7 +58,7 @@ Functional examples are included in the
5858
| soft\_delete\_policy | Soft delete policies to apply. Format is the same as described in provider documentation https://www.terraform.io/docs/providers/google/r/storage_bucket.html#nested_soft_delete_policy | <pre>object({<br> retention_duration_seconds = optional(number)<br> })</pre> | `{}` | no |
5959
| storage\_class | The Storage Class of the new bucket. | `string` | `null` | no |
6060
| versioning | While set to true, versioning is fully enabled for this bucket. | `bool` | `true` | no |
61-
| website | Map of website values. Supported attributes: main\_page\_suffix, not\_found\_page | `map(any)` | `{}` | no |
61+
| website | Map of website values. Supported attributes: main\_page\_suffix, not\_found\_page | <pre>object({<br> main_page_suffix = optional(string)<br> not_found_page = optional(string)<br> })</pre> | `{}` | no |
6262

6363
## Outputs
6464

modules/simple_bucket/main.tf

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ resource "google_storage_bucket" "bucket" {
5353
}
5454

5555
dynamic "website" {
56-
for_each = length(keys(var.website)) == 0 ? toset([]) : toset([var.website])
56+
for_each = (var.website.main_page_suffix == null && var.website.not_found_page == null) ? toset([]) : toset([var.website])
5757
content {
5858
main_page_suffix = lookup(website.value, "main_page_suffix", null)
5959
not_found_page = lookup(website.value, "not_found_page", null)
@@ -89,9 +89,9 @@ resource "google_storage_bucket" "bucket" {
8989
send_age_if_zero = lookup(lifecycle_rule.value.condition, "send_age_if_zero", null)
9090
created_before = lookup(lifecycle_rule.value.condition, "created_before", null)
9191
with_state = lookup(lifecycle_rule.value.condition, "with_state", contains(keys(lifecycle_rule.value.condition), "is_live") ? (lifecycle_rule.value.condition["is_live"] ? "LIVE" : null) : null)
92-
matches_storage_class = contains(keys(lifecycle_rule.value.condition), "matches_storage_class") ? split(",", lifecycle_rule.value.condition["matches_storage_class"]) : null
93-
matches_prefix = contains(keys(lifecycle_rule.value.condition), "matches_prefix") ? split(",", lifecycle_rule.value.condition["matches_prefix"]) : null
94-
matches_suffix = contains(keys(lifecycle_rule.value.condition), "matches_suffix") ? split(",", lifecycle_rule.value.condition["matches_suffix"]) : null
92+
matches_storage_class = lifecycle_rule.value.condition["matches_storage_class"] != null ? split(",", lifecycle_rule.value.condition["matches_storage_class"]) : null
93+
matches_prefix = lifecycle_rule.value.condition["matches_prefix"] != null ? split(",", lifecycle_rule.value.condition["matches_prefix"]) : null
94+
matches_suffix = lifecycle_rule.value.condition["matches_suffix"] != null ? split(",", lifecycle_rule.value.condition["matches_suffix"]) : null
9595
num_newer_versions = lookup(lifecycle_rule.value.condition, "num_newer_versions", null)
9696
custom_time_before = lookup(lifecycle_rule.value.condition, "custom_time_before", null)
9797
days_since_custom_time = lookup(lifecycle_rule.value.condition, "days_since_custom_time", null)

modules/simple_bucket/metadata.yaml

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,13 @@ spec:
111111
})
112112
- name: cors
113113
description: Configuration of CORS for bucket with structure as defined in https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/storage_bucket#cors.
114-
varType: any
114+
varType: |-
115+
list(object({
116+
origin = optional(list(string))
117+
method = optional(list(string))
118+
response_header = optional(list(string))
119+
max_age_seconds = optional(number)
120+
}))
115121
defaultValue: []
116122
- name: encryption
117123
description: A Cloud KMS key that will be used to encrypt objects inserted into this bucket. The key name should follow the format of `projects/<project-name>/locations/<location-name>/keyRings/<keyring-name>/cryptoKeys/<key-name>`. To use a Cloud KMS key automatically created by this module use the `internal_encryption_config` input variable.
@@ -126,17 +132,38 @@ spec:
126132
# Object with keys:
127133
# - type - The type of the action of this Lifecycle Rule. Supported values: Delete and SetStorageClass.
128134
# - storage_class - (Required if action type is SetStorageClass) The target Storage Class of objects affected by this Lifecycle Rule.
129-
action = any
135+
action = object({
136+
type = string
137+
storage_class = optional(string)
138+
})
130139
131140
# Object with keys:
132141
# - age - (Optional) Minimum age of an object in days to satisfy this condition.
142+
# - send_age_if_zero - (Optional) While set true, num_newer_versions value will be sent in the request even for zero value of the field.
133143
# - created_before - (Optional) Creation date of an object in RFC 3339 (e.g. 2017-06-13) to satisfy this condition.
134144
# - with_state - (Optional) Match to live and/or archived objects. Supported values include: "LIVE", "ARCHIVED", "ANY".
135145
# - matches_storage_class - (Optional) Storage Class of objects to satisfy this condition. Supported values include: MULTI_REGIONAL, REGIONAL, NEARLINE, COLDLINE, STANDARD, DURABLE_REDUCED_AVAILABILITY.
136146
# - matches_prefix - (Optional) One or more matching name prefixes to satisfy this condition.
137147
# - matches_suffix - (Optional) One or more matching name suffixes to satisfy this condition
138148
# - num_newer_versions - (Optional) Relevant only for versioned objects. The number of newer versions of an object to satisfy this condition.
139-
condition = any
149+
# - custom_time_before - (Optional) A date in the RFC 3339 format YYYY-MM-DD. This condition is satisfied when the customTime metadata for the object is set to an earlier date than the date used in this lifecycle condition.
150+
# - days_since_custom_time - (Optional) Days since the date set in the customTime metadata for the object.
151+
# - days_since_noncurrent_time - (Optional) Relevant only for versioned objects. Number of days elapsed since the noncurrent timestamp of an object.
152+
# - noncurrent_time_before - (Optional) Relevant only for versioned objects. The date in RFC 3339 (e.g. 2017-06-13) when the object became nonconcurrent.
153+
condition = object({
154+
age = optional(number)
155+
send_age_if_zero = optional(bool)
156+
created_before = optional(string)
157+
with_state = optional(string)
158+
matches_storage_class = optional(string)
159+
matches_prefix = optional(string)
160+
matches_suffix = optional(string)
161+
num_newer_versions = optional(number)
162+
custom_time_before = optional(string)
163+
days_since_custom_time = optional(number)
164+
days_since_noncurrent_time = optional(number)
165+
noncurrent_time_before = optional(string)
166+
})
140167
}))
141168
defaultValue: []
142169
- name: log_bucket
@@ -147,7 +174,11 @@ spec:
147174
varType: string
148175
- name: website
149176
description: "Map of website values. Supported attributes: main_page_suffix, not_found_page"
150-
varType: map(any)
177+
varType: |-
178+
object({
179+
main_page_suffix = optional(string)
180+
not_found_page = optional(string)
181+
})
151182
defaultValue: {}
152183
- name: public_access_prevention
153184
description: Prevents public access to a bucket. Acceptable values are inherited or enforced. If inherited, the bucket uses public access prevention, only if the bucket is subject to the public access prevention organization policy constraint.

modules/simple_bucket/variables.tf

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,13 @@ variable "custom_placement_config" {
9494

9595
variable "cors" {
9696
description = "Configuration of CORS for bucket with structure as defined in https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/storage_bucket#cors."
97-
type = any
98-
default = []
97+
type = list(object({
98+
origin = optional(list(string))
99+
method = optional(list(string))
100+
response_header = optional(list(string))
101+
max_age_seconds = optional(number)
102+
}))
103+
default = []
99104
}
100105

101106
variable "encryption" {
@@ -112,17 +117,38 @@ variable "lifecycle_rules" {
112117
# Object with keys:
113118
# - type - The type of the action of this Lifecycle Rule. Supported values: Delete and SetStorageClass.
114119
# - storage_class - (Required if action type is SetStorageClass) The target Storage Class of objects affected by this Lifecycle Rule.
115-
action = any
120+
action = object({
121+
type = string
122+
storage_class = optional(string)
123+
})
116124

117125
# Object with keys:
118126
# - age - (Optional) Minimum age of an object in days to satisfy this condition.
127+
# - send_age_if_zero - (Optional) While set true, num_newer_versions value will be sent in the request even for zero value of the field.
119128
# - created_before - (Optional) Creation date of an object in RFC 3339 (e.g. 2017-06-13) to satisfy this condition.
120129
# - with_state - (Optional) Match to live and/or archived objects. Supported values include: "LIVE", "ARCHIVED", "ANY".
121130
# - matches_storage_class - (Optional) Storage Class of objects to satisfy this condition. Supported values include: MULTI_REGIONAL, REGIONAL, NEARLINE, COLDLINE, STANDARD, DURABLE_REDUCED_AVAILABILITY.
122131
# - matches_prefix - (Optional) One or more matching name prefixes to satisfy this condition.
123132
# - matches_suffix - (Optional) One or more matching name suffixes to satisfy this condition
124133
# - num_newer_versions - (Optional) Relevant only for versioned objects. The number of newer versions of an object to satisfy this condition.
125-
condition = any
134+
# - custom_time_before - (Optional) A date in the RFC 3339 format YYYY-MM-DD. This condition is satisfied when the customTime metadata for the object is set to an earlier date than the date used in this lifecycle condition.
135+
# - days_since_custom_time - (Optional) Days since the date set in the customTime metadata for the object.
136+
# - days_since_noncurrent_time - (Optional) Relevant only for versioned objects. Number of days elapsed since the noncurrent timestamp of an object.
137+
# - noncurrent_time_before - (Optional) Relevant only for versioned objects. The date in RFC 3339 (e.g. 2017-06-13) when the object became nonconcurrent.
138+
condition = object({
139+
age = optional(number)
140+
send_age_if_zero = optional(bool)
141+
created_before = optional(string)
142+
with_state = optional(string)
143+
matches_storage_class = optional(string)
144+
matches_prefix = optional(string)
145+
matches_suffix = optional(string)
146+
num_newer_versions = optional(number)
147+
custom_time_before = optional(string)
148+
days_since_custom_time = optional(number)
149+
days_since_noncurrent_time = optional(number)
150+
noncurrent_time_before = optional(string)
151+
})
126152
}))
127153
default = []
128154
}
@@ -140,7 +166,10 @@ variable "log_object_prefix" {
140166
}
141167

142168
variable "website" {
143-
type = map(any)
169+
type = object({
170+
main_page_suffix = optional(string)
171+
not_found_page = optional(string)
172+
})
144173
default = {}
145174
description = "Map of website values. Supported attributes: main_page_suffix, not_found_page"
146175
}

0 commit comments

Comments
 (0)