From 64cc25245ebfb4777dedcf6e25704be30e1ba620 Mon Sep 17 00:00:00 2001 From: Ty Larrabee Date: Tue, 21 May 2019 22:00:51 +0000 Subject: [PATCH] Add log_config block to router_nat Signed-off-by: Modular Magician --- google/resource_compute_router_nat.go | 57 +++++++++++++++++++ google/resource_compute_router_nat_test.go | 4 ++ .../docs/r/compute_router_nat.html.markdown | 11 ++++ 3 files changed, 72 insertions(+) diff --git a/google/resource_compute_router_nat.go b/google/resource_compute_router_nat.go index ad1bb8ee05f..f5f0c1e8fc3 100644 --- a/google/resource_compute_router_nat.go +++ b/google/resource_compute_router_nat.go @@ -43,6 +43,24 @@ var ( } ) +var ( + routerNatLogConfig = &schema.Resource{ + Schema: map[string]*schema.Schema{ + "enable": { + Type: schema.TypeBool, + ForceNew: true, + Required: true, + }, + "filter": { + Type: schema.TypeString, + ForceNew: true, + Required: true, + ValidateFunc: validation.StringInSlice([]string{"ERRORS_ONLY", "TRANSLATIONS_ONLY", "ALL"}, false), + }, + }, + } +) + func resourceComputeRouterNat() *schema.Resource { return &schema.Resource{ // TODO(https://github.com/GoogleCloudPlatform/magic-modules/issues/963): Implement Update @@ -119,6 +137,13 @@ func resourceComputeRouterNat() *schema.Resource { Optional: true, ForceNew: true, }, + "log_config": { + Type: schema.TypeList, + Optional: true, + ForceNew: true, + MaxItems: 1, + Elem: routerNatLogConfig, + }, "project": { Type: schema.TypeString, Optional: true, @@ -189,6 +214,10 @@ func resourceComputeRouterNatCreate(d *schema.ResourceData, meta interface{}) er nat.Subnetworks = expandSubnetworks(v.(*schema.Set).List()) } + if v, ok := d.GetOk("log_config"); ok { + nat.LogConfig = expandLogConfig(v) + } + log.Printf("[INFO] Adding nat %s", natName) nats = append(nats, nat) patchRouter := &computeBeta.Router{ @@ -259,6 +288,10 @@ func resourceComputeRouterNatRead(d *schema.ResourceData, meta interface{}) erro return fmt.Errorf("Error reading router nat: %s", err) } + if err := d.Set("log_config", flattenRouterNatLogConfig(nat.LogConfig)); err != nil { + return fmt.Errorf("Error reading router nat: %s", err) + } + return nil } } @@ -353,6 +386,30 @@ func resourceComputeRouterNatImportState(d *schema.ResourceData, meta interface{ return []*schema.ResourceData{d}, nil } +func flattenRouterNatLogConfig(logConfig *computeBeta.RouterNatLogConfig) []map[string]interface{} { + result := make([]map[string]interface{}, 0, 1) + if logConfig != nil { + cfg := map[string]interface{}{} + cfg["filter"] = logConfig.Filter + cfg["enable"] = logConfig.Enable + result = append(result, cfg) + } + return result +} + +func expandLogConfig(logConfigs interface{}) *computeBeta.RouterNatLogConfig { + configs := logConfigs.([]interface{}) + if len(configs) == 0 || configs[0] == nil { + return nil + } + cfg := configs[0].(map[string]interface{}) + result := computeBeta.RouterNatLogConfig{ + Filter: cfg["filter"].(string), + Enable: cfg["enable"].(bool), + } + return &result +} + func expandSubnetworks(subnetworks []interface{}) []*computeBeta.RouterNatSubnetworkToNat { result := make([]*computeBeta.RouterNatSubnetworkToNat, 0, len(subnetworks)) diff --git a/google/resource_compute_router_nat_test.go b/google/resource_compute_router_nat_test.go index 09b108a5192..a0feaab10ff 100644 --- a/google/resource_compute_router_nat_test.go +++ b/google/resource_compute_router_nat_test.go @@ -157,6 +157,10 @@ func testAccComputeRouterNatBasic(testId string) string { region = "${google_compute_router.foobar.region}" nat_ip_allocate_option = "AUTO_ONLY" source_subnetwork_ip_ranges_to_nat = "ALL_SUBNETWORKS_ALL_IP_RANGES" + log_config { + enable = true + filter = "ERRORS_ONLY" + } } `, testId, testId, testId, testId) } diff --git a/website/docs/r/compute_router_nat.html.markdown b/website/docs/r/compute_router_nat.html.markdown index 207a8d29c56..9b9b7bc4e13 100644 --- a/website/docs/r/compute_router_nat.html.markdown +++ b/website/docs/r/compute_router_nat.html.markdown @@ -89,6 +89,10 @@ resource "google_compute_router_nat" "advanced-nat" { name = "${google_compute_subnetwork.default.self_link}" source_ip_ranges_to_nat = ["ALL_IP_RANGES"] } + log_config { + filter = "TRANSLATIONS_ONLY" + enable = true + } } ``` @@ -158,6 +162,13 @@ The `subnetwork` block supports: that are allowed to use NAT. This can be populated only if `LIST_OF_SECONDARY_IP_RANGES` is one of the values in `source_ip_ranges_to_nat`. +The `log_config` block supports: + +* `filter` - (Required) Specifies the desired filtering of logs on this NAT. + Valid values include: `ALL`, `ERRORS_ONLY`, `TRANSLATIONS_ONLY` + +* `enable` - (Required) Whether to export logs. + ## Import Router NATs can be imported using the `region`, `router`, and `name`, e.g.