Skip to content

Commit

Permalink
Merge pull request #1320 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 6419f59 + 3989f39 commit 32e18df
Show file tree
Hide file tree
Showing 4 changed files with 268 additions and 92 deletions.
125 changes: 71 additions & 54 deletions google-beta/resource_compute_subnetwork.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,34 +102,32 @@ func resourceComputeSubnetwork() *schema.Resource {
ForceNew: true,
},
"enable_flow_logs": {
Type: schema.TypeBool,
Optional: true,
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,
ForceNew: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"aggregation_interval": {
Type: schema.TypeString,
Optional: true,
ForceNew: 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,
ForceNew: true,
Default: 0.5,
},
"metadata": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{"EXCLUDE_ALL_METADATA", "INCLUDE_ALL_METADATA", ""}, false),
Default: "INCLUDE_ALL_METADATA",
},
Expand Down Expand Up @@ -670,6 +668,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 @@ -825,25 +874,17 @@ func flattenComputeSubnetworkLogConfig(v interface{}, d *schema.ResourceData) in
if len(original) == 0 {
return nil
}
transformed := make(map[string]interface{})
transformed["aggregation_interval"] =
flattenComputeSubnetworkLogConfigAggregationInterval(original["aggregationInterval"], d)
transformed["flow_sampling"] =
flattenComputeSubnetworkLogConfigFlowSampling(original["flowSampling"], d)
transformed["metadata"] =
flattenComputeSubnetworkLogConfigMetadata(original["metadata"], d)
return []interface{}{transformed}
}
func flattenComputeSubnetworkLogConfigAggregationInterval(v interface{}, d *schema.ResourceData) interface{} {
return v
}

func flattenComputeSubnetworkLogConfigFlowSampling(v interface{}, d *schema.ResourceData) interface{} {
return v
}
v, ok := original["enable"]
if ok && !v.(bool) {
return nil
}

func flattenComputeSubnetworkLogConfigMetadata(v interface{}, d *schema.ResourceData) interface{} {
return v
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) {
Expand Down Expand Up @@ -938,40 +979,16 @@ func expandComputeSubnetworkLogConfig(v interface{}, d TerraformResourceData, co
}
raw := l[0]
original := raw.(map[string]interface{})
transformed := make(map[string]interface{})

transformedAggregationInterval, err := expandComputeSubnetworkLogConfigAggregationInterval(original["aggregation_interval"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedAggregationInterval); val.IsValid() && !isEmptyValue(val) {
transformed["aggregationInterval"] = transformedAggregationInterval
}

transformedFlowSampling, err := expandComputeSubnetworkLogConfigFlowSampling(original["flow_sampling"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedFlowSampling); val.IsValid() && !isEmptyValue(val) {
transformed["flowSampling"] = transformedFlowSampling
}
v, ok := d.GetOkExists("enable_flow_logs")

transformedMetadata, err := expandComputeSubnetworkLogConfigMetadata(original["metadata"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedMetadata); val.IsValid() && !isEmptyValue(val) {
transformed["metadata"] = transformedMetadata
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
}

func expandComputeSubnetworkLogConfigAggregationInterval(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}

func expandComputeSubnetworkLogConfigFlowSampling(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}

func expandComputeSubnetworkLogConfigMetadata(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}
25 changes: 11 additions & 14 deletions google-beta/resource_compute_subnetwork_generated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ resource "google_compute_network" "custom-test" {
`, context)
}

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

context := map[string]interface{}{
Expand All @@ -77,43 +77,40 @@ func TestAccComputeSubnetwork_subnetworkLoggingConfigBetaExample(t *testing.T) {

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

func testAccComputeSubnetwork_subnetworkLoggingConfigBetaExample(context map[string]interface{}) string {
func testAccComputeSubnetwork_subnetworkLoggingConfigExample(context map[string]interface{}) string {
return Nprintf(`
resource "google_compute_subnetwork" "subnet-with-logging" {
provider = "google-beta"
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}"
enable_flow_logs = true
log_config {
aggregation_interval = "INTERVAL_10_MIN"
flow_sampling = 0.5
metadata = "INCLUDE_ALL_METADATA"
flow_sampling = 0.5
metadata = "INCLUDE_ALL_METADATA"
}
}
resource "google_compute_network" "custom-test" {
provider = "google-beta"
name = "log-test-network%{random_suffix}"
auto_create_subnetworks = false
}
provider "google-beta"{
region = "us-central1"
zone = "us-central1-a"
}
`, context)
}

Expand Down
Loading

0 comments on commit 32e18df

Please sign in to comment.