Skip to content

Commit

Permalink
Merge pull request #4791 from modular-magician/codegen-pr-2572
Browse files Browse the repository at this point in the history
Subnet logconfig prepare for 3.0.0
  • Loading branch information
slevenick authored Nov 4, 2019
2 parents 3925979 + 5eaa9ba commit b633d27
Show file tree
Hide file tree
Showing 4 changed files with 394 additions and 24 deletions.
134 changes: 133 additions & 1 deletion google/resource_compute_subnetwork.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/apparentlymart/go-cidr/cidr"
"github.com/hashicorp/terraform-plugin-sdk/helper/customdiff"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"google.golang.org/api/compute/v1"
)

Expand Down Expand Up @@ -101,8 +102,37 @@ func resourceComputeSubnetwork() *schema.Resource {
ForceNew: true,
},
"enable_flow_logs": {
Type: schema.TypeBool,
Type: schema.TypeBool,
Computed: true,
Optional: true,
Deprecated: "This field is being removed in favor of log_config. If log_config is present, flow logs are enabled.",
},
"log_config": {
Type: schema.TypeList,
Computed: true,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"aggregation_interval": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{"INTERVAL_5_SEC", "INTERVAL_30_SEC", "INTERVAL_1_MIN", "INTERVAL_5_MIN", "INTERVAL_10_MIN", "INTERVAL_15_MIN", ""}, false),
Default: "INTERVAL_5_SEC",
},
"flow_sampling": {
Type: schema.TypeFloat,
Optional: true,
Default: 0.5,
},
"metadata": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{"EXCLUDE_ALL_METADATA", "INCLUDE_ALL_METADATA", ""}, false),
Default: "INCLUDE_ALL_METADATA",
},
},
},
},
"private_ip_google_access": {
Type: schema.TypeBool,
Expand Down Expand Up @@ -261,6 +291,12 @@ func resourceComputeSubnetworkCreate(d *schema.ResourceData, meta interface{}) e
} else if v, ok := d.GetOkExists("region"); !isEmptyValue(reflect.ValueOf(regionProp)) && (ok || !reflect.DeepEqual(v, regionProp)) {
obj["region"] = regionProp
}
logConfigProp, err := expandComputeSubnetworkLogConfig(d.Get("log_config"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("log_config"); !isEmptyValue(reflect.ValueOf(logConfigProp)) && (ok || !reflect.DeepEqual(v, logConfigProp)) {
obj["logConfig"] = logConfigProp
}

url, err := replaceVars(d, config, "{{ComputeBasePath}}projects/{{project}}/regions/{{region}}/subnetworks")
if err != nil {
Expand Down Expand Up @@ -359,6 +395,9 @@ func resourceComputeSubnetworkRead(d *schema.ResourceData, meta interface{}) err
if err := d.Set("region", flattenComputeSubnetworkRegion(res["region"], d)); err != nil {
return fmt.Errorf("Error reading Subnetwork: %s", err)
}
if err := d.Set("log_config", flattenComputeSubnetworkLogConfig(res["logConfig"], d)); err != nil {
return fmt.Errorf("Error reading Subnetwork: %s", err)
}
if err := d.Set("self_link", ConvertSelfLinkToV1(res["selfLink"].(string))); err != nil {
return fmt.Errorf("Error reading Subnetwork: %s", err)
}
Expand Down Expand Up @@ -548,6 +587,57 @@ func resourceComputeSubnetworkUpdate(d *schema.ResourceData, meta interface{}) e

d.SetPartial("private_ip_google_access")
}
if d.HasChange("log_config") {
obj := make(map[string]interface{})

getUrl, err := replaceVars(d, config, "{{ComputeBasePath}}projects/{{project}}/regions/{{region}}/subnetworks/{{name}}")
if err != nil {
return err
}

project, err := getProject(d, config)
if err != nil {
return err
}
getRes, err := sendRequest(config, "GET", project, getUrl, nil)
if err != nil {
return handleNotFoundError(err, d, fmt.Sprintf("ComputeSubnetwork %q", d.Id()))
}

obj["fingerprint"] = getRes["fingerprint"]

logConfigProp, err := expandComputeSubnetworkLogConfig(d.Get("log_config"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("log_config"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, logConfigProp)) {
obj["logConfig"] = logConfigProp
}

url, err := replaceVars(d, config, "{{ComputeBasePath}}projects/{{project}}/regions/{{region}}/subnetworks/{{name}}")
if err != nil {
return err
}
res, err := sendRequestWithTimeout(config, "PATCH", project, url, obj, d.Timeout(schema.TimeoutUpdate))
if err != nil {
return fmt.Errorf("Error updating Subnetwork %q: %s", d.Id(), err)
}

op := &compute.Operation{}
err = Convert(res, op)
if err != nil {
return err
}

err = computeOperationWaitTime(
config.clientCompute, op, project, "Updating Subnetwork",
int(d.Timeout(schema.TimeoutUpdate).Minutes()))

if err != nil {
return err
}

d.SetPartial("log_config")
}

d.Partial(false)

Expand Down Expand Up @@ -687,6 +777,27 @@ func flattenComputeSubnetworkRegion(v interface{}, d *schema.ResourceData) inter
return NameFromSelfLinkStateFunc(v)
}

func flattenComputeSubnetworkLogConfig(v interface{}, d *schema.ResourceData) interface{} {
if v == nil {
return nil
}
original := v.(map[string]interface{})
if len(original) == 0 {
return nil
}

v, ok := original["enable"]
if ok && !v.(bool) {
return nil
}

transformed := make(map[string]interface{})
transformed["flow_sampling"] = original["flowSampling"]
transformed["aggregation_interval"] = original["aggregationInterval"]
transformed["metadata"] = original["metadata"]
return []interface{}{transformed}
}

func expandComputeSubnetworkDescription(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}
Expand Down Expand Up @@ -763,3 +874,24 @@ func expandComputeSubnetworkRegion(v interface{}, d TerraformResourceData, confi
}
return f.RelativeLink(), nil
}

func expandComputeSubnetworkLogConfig(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
l := v.([]interface{})
if len(l) == 0 || l[0] == nil {
return nil, nil
}
raw := l[0]
original := raw.(map[string]interface{})

v, ok := d.GetOkExists("enable_flow_logs")

transformed := make(map[string]interface{})
if !ok || v.(bool) {
transformed["enable"] = true
transformed["aggregationInterval"] = original["aggregation_interval"]
transformed["flowSampling"] = original["flow_sampling"]
transformed["metadata"] = original["metadata"]
}

return transformed, nil
}
46 changes: 46 additions & 0 deletions google/resource_compute_subnetwork_generated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,52 @@ resource "google_compute_network" "custom-test" {
`, context)
}

func TestAccComputeSubnetwork_subnetworkLoggingConfigExample(t *testing.T) {
t.Parallel()

context := map[string]interface{}{
"random_suffix": acctest.RandString(10),
}

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckComputeSubnetworkDestroy,
Steps: []resource.TestStep{
{
Config: testAccComputeSubnetwork_subnetworkLoggingConfigExample(context),
},
{
ResourceName: "google_compute_subnetwork.subnet-with-logging",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccComputeSubnetwork_subnetworkLoggingConfigExample(context map[string]interface{}) string {
return Nprintf(`
resource "google_compute_subnetwork" "subnet-with-logging" {
name = "log-test-subnetwork%{random_suffix}"
ip_cidr_range = "10.2.0.0/16"
region = "us-central1"
network = "${google_compute_network.custom-test.self_link}"
log_config {
aggregation_interval = "INTERVAL_10_MIN"
flow_sampling = 0.5
metadata = "INCLUDE_ALL_METADATA"
}
}
resource "google_compute_network" "custom-test" {
name = "log-test-network%{random_suffix}"
auto_create_subnetworks = false
}
`, context)
}

func testAccCheckComputeSubnetworkDestroy(s *terraform.State) error {
for name, rs := range s.RootModule().Resources {
if rs.Type != "google_compute_subnetwork" {
Expand Down
Loading

0 comments on commit b633d27

Please sign in to comment.