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

Add log_config block to router_nat #3684

Merged
merged 1 commit into from
May 24, 2019
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
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": {
tysen marked this conversation as resolved.
Show resolved Hide resolved
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