diff --git a/README.md b/README.md index f219b6a7..e6beea3f 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,15 @@ module "vpc" { subnet_flow_logs = "true" description = "This subnet has a description" }, + { + subnet_name = "subnet-03" + subnet_ip = "10.10.30.0/24" + subnet_region = "us-west1" + subnet_flow_logs = "true" + subnet_flow_logs_interval = "INTERVAL_10_MIN" + subnet_flow_logs_sampling = 0.7 + subnet_flow_logs_metadata = "INCLUDE_ALL_METADATA" + } ] secondary_ranges = { @@ -142,7 +151,7 @@ The routes list contains maps, where each object represents a route. For the nex ## Requirements ### Installed Software - [Terraform](https://www.terraform.io/downloads.html) ~> 0.12.0 -- [Terraform Provider for GCP][terraform-provider-google] ~> 2.10.0 +- [Terraform Provider for GCP][terraform-provider-google] ~> 2.19.0 - [gcloud](https://cloud.google.com/sdk/gcloud/) >243.0.0 ### Configure a Service Account diff --git a/examples/delete_default_gateway_routes/main.tf b/examples/delete_default_gateway_routes/main.tf index 7b48d150..1269b0e9 100644 --- a/examples/delete_default_gateway_routes/main.tf +++ b/examples/delete_default_gateway_routes/main.tf @@ -15,7 +15,7 @@ */ provider "google" { - version = "~> 2.10.0" + version = "~> 2.19.0" } provider "null" { diff --git a/examples/multi_vpc/main.tf b/examples/multi_vpc/main.tf index 7c039a5b..1d0b7247 100644 --- a/examples/multi_vpc/main.tf +++ b/examples/multi_vpc/main.tf @@ -15,7 +15,7 @@ */ provider "google" { - version = "~> 2.10.0" + version = "~> 2.19.0" } provider "null" { diff --git a/examples/secondary_ranges/main.tf b/examples/secondary_ranges/main.tf index 1ddcb460..576ff002 100644 --- a/examples/secondary_ranges/main.tf +++ b/examples/secondary_ranges/main.tf @@ -15,7 +15,7 @@ */ provider "google" { - version = "~> 2.10.0" + version = "~> 2.19.0" } provider "null" { diff --git a/examples/simple_project/main.tf b/examples/simple_project/main.tf index 13c4a716..40519adc 100644 --- a/examples/simple_project/main.tf +++ b/examples/simple_project/main.tf @@ -15,7 +15,7 @@ */ provider "google" { - version = "~> 2.10.0" + version = "~> 2.19.0" } provider "null" { @@ -25,6 +25,7 @@ provider "null" { locals { subnet_01 = "${var.network_name}-subnet-01" subnet_02 = "${var.network_name}-subnet-02" + subnet_03 = "${var.network_name}-subnet-03" } module "test-vpc-module" { @@ -45,5 +46,14 @@ module "test-vpc-module" { subnet_private_access = "true" subnet_flow_logs = "true" }, + { + subnet_name = "${local.subnet_03}" + subnet_ip = "10.10.30.0/24" + subnet_region = "us-west1" + subnet_flow_logs = "true" + subnet_flow_logs_interval = "INTERVAL_10_MIN" + subnet_flow_logs_sampling = 0.7 + subnet_flow_logs_metadata = "INCLUDE_ALL_METADATA" + } ] } diff --git a/examples/simple_project_with_regional_network/main.tf b/examples/simple_project_with_regional_network/main.tf index 58b0ba4e..583a21db 100644 --- a/examples/simple_project_with_regional_network/main.tf +++ b/examples/simple_project_with_regional_network/main.tf @@ -15,7 +15,7 @@ */ provider "google" { - version = "~> 2.10.0" + version = "~> 2.19.0" } provider "null" { diff --git a/examples/submodule_firewall/main.tf b/examples/submodule_firewall/main.tf index 8c1ed331..4319be80 100644 --- a/examples/submodule_firewall/main.tf +++ b/examples/submodule_firewall/main.tf @@ -15,7 +15,7 @@ */ provider "google" { - version = "~> 2.10.0" + version = "~> 2.19.0" } provider "null" { diff --git a/main.tf b/main.tf index 3be7e062..e6057d9e 100644 --- a/main.tf +++ b/main.tf @@ -50,10 +50,21 @@ resource "google_compute_subnetwork" "subnetwork" { ip_cidr_range = each.value.subnet_ip region = each.value.subnet_region private_ip_google_access = lookup(each.value, "subnet_private_access", "false") - enable_flow_logs = lookup(each.value, "subnet_flow_logs", "false") - network = google_compute_network.network.name - project = var.project_id - description = lookup(each.value, "description", null) + dynamic "log_config" { + for_each = lookup(each.value, "subnet_flow_logs", false) ? [{ + aggregation_interval = lookup(each.value, "subnet_flow_logs_interval", null) + flow_sampling = lookup(each.value, "subnet_flow_logs_sampling", null) + metadata = lookup(each.value, "subnet_flow_logs_metadata", null) + }] : [] + content { + aggregation_interval = log_config.value.aggregation_interval + flow_sampling = log_config.value.flow_sampling + metadata = log_config.value.metadata + } + } + network = google_compute_network.network.name + project = var.project_id + description = lookup(each.value, "description", null) secondary_ip_range = [ for i in range( length( diff --git a/test/integration/simple_project/controls/gcloud.rb b/test/integration/simple_project/controls/gcloud.rb index a22b2d80..3e7fc768 100644 --- a/test/integration/simple_project/controls/gcloud.rb +++ b/test/integration/simple_project/controls/gcloud.rb @@ -30,12 +30,10 @@ end end - describe "enableFlowLogs" do - it "should be false" do - expect(data).to include( - "enableFlowLogs" => false - ) - end + it "logConfig should not exist" do + expect(data).to_not include( + "logConfig" + ) end end @@ -51,12 +49,39 @@ end end - describe "enableFlowLogs" do - it "should be true" do - expect(data).to include( - "enableFlowLogs" => true - ) + it "Log config should be correct" do + expect(data).to include( + "logConfig" => { + "aggregationInterval" => "INTERVAL_5_SEC", + "enable" => true, + "flowSampling" => 0.5, + "metadata" => "INCLUDE_ALL_METADATA" + } + ) + end + end + + describe command("gcloud compute networks subnets describe #{network_name}-subnet-03 --project=#{project_id} --region=us-west1 --format=json") do + its(:exit_status) { should eq 0 } + its(:stderr) { should eq '' } + + let(:data) do + if subject.exit_status == 0 + JSON.parse(subject.stdout) + else + {} end end + + it "Log config should be correct" do + expect(data).to include( + "logConfig" => { + "aggregationInterval" => "INTERVAL_10_MIN", + "enable" => true, + "flowSampling" => 0.7, + "metadata" => "INCLUDE_ALL_METADATA" + } + ) + end end end diff --git a/test/integration/simple_project/controls/gcp.rb b/test/integration/simple_project/controls/gcp.rb index b62b3f32..d48c79da 100644 --- a/test/integration/simple_project/controls/gcp.rb +++ b/test/integration/simple_project/controls/gcp.rb @@ -44,4 +44,14 @@ its('ip_cidr_range') { should eq "10.10.20.0/24" } its('private_ip_google_access') { should be true } end + + describe google_compute_subnetwork( + project: project_id, + name: "#{network_name}-subnet-03", + region: "us-west1" + ) do + it { should exist } + its('ip_cidr_range') { should eq "10.10.30.0/24" } + its('private_ip_google_access') { should be false } + end end