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

Fix/17/lifecycle rules #19

Merged
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -51,6 +51,7 @@ Functional examples are included in the
| creators | IAM-style members who will be granted roles/storage.objectCreators on all buckets. | list | `<list>` | no |
| force\_destroy | Optional map of lowercase unprefixed name => boolean, defaults to false. | map | `<map>` | no |
| labels | Labels to be attached to the buckets | map | `<map>` | no |
| lifecycle\_rules | List of lifecycle rules to configure. Format is the same as described in provider documentation https://www.terraform.io/docs/providers/google/r/storage_bucket.html#lifecycle_rule except condition.matches_storage_class should be a comma delimited string. | object | `<list>` | no |
| location | Bucket location. | string | `"EU"` | no |
| names | Bucket name suffixes. | list(string) | n/a | yes |
| prefix | Prefix used to generate the bucket name. | string | n/a | yes |
Expand Down
11 changes: 11 additions & 0 deletions examples/simple_example/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,16 @@ module "cloud_storage" {
prefix = var.prefix
names = var.names
bucket_policy_only = var.bucket_policy_only

lifecycle_rules = [{
action = {
type = "SetStorageClass"
storage_class = "NEARLINE"
}
condition = {
age = "10"
matches_storage_class = "MULTI_REGIONAL,STANDARD,DURABLE_REDUCED_AVAILABILITY"
}
}]
}

1 change: 0 additions & 1 deletion examples/simple_example/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,3 @@ variable "bucket_policy_only" {
description = "Disable ad-hoc ACLs on specified buckets. Defaults to true. Map of lowercase unprefixed name => boolean"
type = map(string)
}

18 changes: 18 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,24 @@ resource "google_storage_bucket" "buckets" {
false,
)
}
dynamic "lifecycle_rule" {
for_each = var.lifecycle_rules
content {
action {
type = lifecycle_rule.value.action.type
storage_class = lifecycle_rule.value.action.storage_class
}
condition {
age = lookup(lifecycle_rule.value.condition, "age", null)
nick4fake marked this conversation as resolved.
Show resolved Hide resolved
created_before = lookup(lifecycle_rule.value.condition, "created_before", null)
with_state = lookup(lifecycle_rule.value.condition, "with_state", null)
is_live = lookup(lifecycle_rule.value.condition, "is_live", null)
matches_storage_class = contains(keys(lifecycle_rule.value.condition), "matches_storage_class") ? split(",", lifecycle_rule.value.condition["matches_storage_class"]) : null
num_newer_versions = lookup(lifecycle_rule.value.condition, "num_newer_versions", null)
}
}
}

}

resource "google_storage_bucket_iam_binding" "admins" {
Expand Down
1 change: 0 additions & 1 deletion test/fixtures/simple_example/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,3 @@ module "example" {
"two" = false
}
}

1 change: 0 additions & 1 deletion test/fixtures/simple_example/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,3 @@ variable "project_id" {
description = "The ID of the project in which to provision resources."
type = string
}

35 changes: 35 additions & 0 deletions test/integration/simple_example/controls/gsutil.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

require 'json'

control "gsutil" do
title "gsutil"

Expand All @@ -33,4 +35,37 @@
its(:stderr) { should eq "" }
its(:stdout) { should include "Enabled: False" }
end

get_lifecycle_0_out = command("gsutil lifecycle get gs://#{attribute("names_list")[0]}")
rule = JSON.parse(get_lifecycle_0_out.stdout)['rule'][0]

describe get_lifecycle_0_out do
let(:action) { rule['action'] }
let(:condition) { rule['condition'] }

its(:exit_status) { should eq 0 }
its(:stderr) { should eq "" }
it { expect(action['storageClass']).to eq("NEARLINE") }
it { expect(action['type']).to eq("SetStorageClass") }
it { expect(condition['age']).to eq(10) }
it { expect(condition['isLive']).to eq(false) }
it { expect(condition['matchesStorageClass']).to eq(%w(MULTI_REGIONAL STANDARD DURABLE_REDUCED_AVAILABILITY)) }
end

get_lifecycle_1_out = command("gsutil lifecycle get gs://#{attribute("names_list")[1]}")
rule_1 = JSON.parse(get_lifecycle_1_out.stdout)['rule'][0]

describe command("gsutil lifecycle get gs://#{attribute("names_list")[1]}") do
let(:action) { rule_1['action'] }
let(:condition) { rule_1['condition'] }

its(:exit_status) { should eq 0 }
its(:stderr) { should eq "" }
it { expect(action['storageClass']).to eq("NEARLINE") }
it { expect(action['type']).to eq("SetStorageClass") }
it { expect(condition['age']).to eq(10) }
it { expect(condition['isLive']).to eq(false) }
it { expect(condition['matchesStorageClass']).to eq(%w(MULTI_REGIONAL STANDARD DURABLE_REDUCED_AVAILABILITY)) }
end

end
11 changes: 11 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,14 @@ variable "set_viewer_roles" {
default = false
}

variable "lifecycle_rules" {
type = set(object({
action = object({
type = string
storage_class = string
})
condition = map(string)
}))
default = []
description = "List of lifecycle rules to configure. Format is the same as described in provider documentation https://www.terraform.io/docs/providers/google/r/storage_bucket.html#lifecycle_rule except condition.matches_storage_class should be a comma delimited string."
}