Skip to content

Commit

Permalink
Add log_config block to router_nat
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
Ty Larrabee authored and modular-magician committed May 21, 2019
1 parent a20d2eb commit 64cc252
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
57 changes: 57 additions & 0 deletions google/resource_compute_router_nat.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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))

Expand Down
4 changes: 4 additions & 0 deletions google/resource_compute_router_nat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
11 changes: 11 additions & 0 deletions website/docs/r/compute_router_nat.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
```

Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 64cc252

Please sign in to comment.