Skip to content

Commit

Permalink
Add support for one-field-at-a-time update to Subnetwork (#1269)
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
modular-magician authored and rileykarson committed Oct 16, 2019
1 parent f902343 commit 92a794b
Show file tree
Hide file tree
Showing 22 changed files with 174 additions and 7 deletions.
1 change: 1 addition & 0 deletions google-beta/resource_compute_address.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ func resourceComputeAddressUpdate(d *schema.ResourceData, meta interface{}) erro

if d.HasChange("labels") || d.HasChange("label_fingerprint") {
obj := make(map[string]interface{})

labelsProp, err := expandComputeAddressLabels(d.Get("labels"), d, config)
if err != nil {
return err
Expand Down
2 changes: 2 additions & 0 deletions google-beta/resource_compute_disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,7 @@ func resourceComputeDiskUpdate(d *schema.ResourceData, meta interface{}) error {

if d.HasChange("label_fingerprint") || d.HasChange("labels") {
obj := make(map[string]interface{})

labelFingerprintProp, err := expandComputeDiskLabelFingerprint(d.Get("label_fingerprint"), d, config)
if err != nil {
return err
Expand Down Expand Up @@ -730,6 +731,7 @@ func resourceComputeDiskUpdate(d *schema.ResourceData, meta interface{}) error {
}
if d.HasChange("size") {
obj := make(map[string]interface{})

sizeGbProp, err := expandComputeDiskSize(d.Get("size"), d, config)
if err != nil {
return err
Expand Down
2 changes: 2 additions & 0 deletions google-beta/resource_compute_forwarding_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,7 @@ func resourceComputeForwardingRuleUpdate(d *schema.ResourceData, meta interface{

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

targetProp, err := expandComputeForwardingRuleTarget(d.Get("target"), d, config)
if err != nil {
return err
Expand Down Expand Up @@ -510,6 +511,7 @@ func resourceComputeForwardingRuleUpdate(d *schema.ResourceData, meta interface{
}
if d.HasChange("labels") || d.HasChange("label_fingerprint") {
obj := make(map[string]interface{})

labelsProp, err := expandComputeForwardingRuleLabels(d.Get("labels"), d, config)
if err != nil {
return err
Expand Down
1 change: 1 addition & 0 deletions google-beta/resource_compute_global_address.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@ func resourceComputeGlobalAddressUpdate(d *schema.ResourceData, meta interface{}

if d.HasChange("labels") || d.HasChange("label_fingerprint") {
obj := make(map[string]interface{})

labelsProp, err := expandComputeGlobalAddressLabels(d.Get("labels"), d, config)
if err != nil {
return err
Expand Down
2 changes: 2 additions & 0 deletions google-beta/resource_compute_global_forwarding_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,7 @@ func resourceComputeGlobalForwardingRuleUpdate(d *schema.ResourceData, meta inte

if d.HasChange("labels") || d.HasChange("label_fingerprint") {
obj := make(map[string]interface{})

labelsProp, err := expandComputeGlobalForwardingRuleLabels(d.Get("labels"), d, config)
if err != nil {
return err
Expand Down Expand Up @@ -435,6 +436,7 @@ func resourceComputeGlobalForwardingRuleUpdate(d *schema.ResourceData, meta inte
}
if d.HasChange("target") {
obj := make(map[string]interface{})

targetProp, err := expandComputeGlobalForwardingRuleTarget(d.Get("target"), d, config)
if err != nil {
return err
Expand Down
1 change: 1 addition & 0 deletions google-beta/resource_compute_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@ func resourceComputeImageUpdate(d *schema.ResourceData, meta interface{}) error

if d.HasChange("labels") || d.HasChange("label_fingerprint") {
obj := make(map[string]interface{})

labelsProp, err := expandComputeImageLabels(d.Get("labels"), d, config)
if err != nil {
return err
Expand Down
1 change: 1 addition & 0 deletions google-beta/resource_compute_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ func resourceComputeNetworkUpdate(d *schema.ResourceData, meta interface{}) erro

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

routingConfigProp, err := expandComputeNetworkRoutingConfig(nil, d, config)
if err != nil {
return err
Expand Down
1 change: 1 addition & 0 deletions google-beta/resource_compute_node_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ func resourceComputeNodeGroupUpdate(d *schema.ResourceData, meta interface{}) er

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

nodeTemplateProp, err := expandComputeNodeGroupNodeTemplate(d.Get("node_template"), d, config)
if err != nil {
return err
Expand Down
2 changes: 2 additions & 0 deletions google-beta/resource_compute_region_disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,7 @@ func resourceComputeRegionDiskUpdate(d *schema.ResourceData, meta interface{}) e

if d.HasChange("label_fingerprint") || d.HasChange("labels") {
obj := make(map[string]interface{})

labelFingerprintProp, err := expandComputeRegionDiskLabelFingerprint(d.Get("label_fingerprint"), d, config)
if err != nil {
return err
Expand Down Expand Up @@ -465,6 +466,7 @@ func resourceComputeRegionDiskUpdate(d *schema.ResourceData, meta interface{}) e
}
if d.HasChange("size") {
obj := make(map[string]interface{})

sizeGbProp, err := expandComputeRegionDiskSize(d.Get("size"), d, config)
if err != nil {
return err
Expand Down
1 change: 1 addition & 0 deletions google-beta/resource_compute_region_target_http_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ func resourceComputeRegionTargetHttpProxyUpdate(d *schema.ResourceData, meta int

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

urlMapProp, err := expandComputeRegionTargetHttpProxyUrlMap(d.Get("url_map"), d, config)
if err != nil {
return err
Expand Down
2 changes: 2 additions & 0 deletions google-beta/resource_compute_region_target_https_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ func resourceComputeRegionTargetHttpsProxyUpdate(d *schema.ResourceData, meta in

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

sslCertificatesProp, err := expandComputeRegionTargetHttpsProxySslCertificates(d.Get("ssl_certificates"), d, config)
if err != nil {
return err
Expand Down Expand Up @@ -268,6 +269,7 @@ func resourceComputeRegionTargetHttpsProxyUpdate(d *schema.ResourceData, meta in
}
if d.HasChange("url_map") {
obj := make(map[string]interface{})

urlMapProp, err := expandComputeRegionTargetHttpsProxyUrlMap(d.Get("url_map"), d, config)
if err != nil {
return err
Expand Down
1 change: 1 addition & 0 deletions google-beta/resource_compute_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ func resourceComputeSnapshotUpdate(d *schema.ResourceData, meta interface{}) err

if d.HasChange("labels") || d.HasChange("label_fingerprint") {
obj := make(map[string]interface{})

labelsProp, err := expandComputeSnapshotLabels(d.Get("labels"), d, config)
if err != nil {
return err
Expand Down
114 changes: 107 additions & 7 deletions google-beta/resource_compute_subnetwork.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,7 @@ func resourceComputeSubnetworkUpdate(d *schema.ResourceData, meta interface{}) e

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

ipCidrRangeProp, err := expandComputeSubnetworkIpCidrRange(d.Get("ip_cidr_range"), d, config)
if err != nil {
return err
Expand Down Expand Up @@ -481,26 +482,127 @@ func resourceComputeSubnetworkUpdate(d *schema.ResourceData, meta interface{}) e

d.SetPartial("ip_cidr_range")
}
if d.HasChange("enable_flow_logs") || d.HasChange("fingerprint") || d.HasChange("role") || d.HasChange("secondary_ip_range") {
if d.HasChange("enable_flow_logs") {
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"]

enableFlowLogsProp, err := expandComputeSubnetworkEnableFlowLogs(d.Get("enable_flow_logs"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("enable_flow_logs"); ok || !reflect.DeepEqual(v, enableFlowLogsProp) {
obj["enableFlowLogs"] = enableFlowLogsProp
}
fingerprintProp, err := expandComputeSubnetworkFingerprint(d.Get("fingerprint"), d, config)

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
} else if v, ok := d.GetOkExists("fingerprint"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, fingerprintProp)) {
obj["fingerprint"] = fingerprintProp
}

d.SetPartial("enable_flow_logs")
}
if d.HasChange("role") {
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"]

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

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("role")
}
if d.HasChange("secondary_ip_range") {
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"]

secondaryIpRangesProp, err := expandComputeSubnetworkSecondaryIpRange(d.Get("secondary_ip_range"), d, config)
if err != nil {
return err
Expand Down Expand Up @@ -531,13 +633,11 @@ func resourceComputeSubnetworkUpdate(d *schema.ResourceData, meta interface{}) e
return err
}

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

privateIpGoogleAccessProp, err := expandComputeSubnetworkPrivateIpGoogleAccess(d.Get("private_ip_google_access"), d, config)
if err != nil {
return err
Expand Down
35 changes: 35 additions & 0 deletions google-beta/resource_compute_subnetwork_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,19 @@ func TestAccComputeSubnetwork_update(t *testing.T) {
"google_compute_subnetwork.network-with-private-google-access", &subnetwork),
),
},
{
// Add a secondary range and enable flow logs at once
Config: testAccComputeSubnetwork_update3(cnName, "10.2.0.0/24", subnetworkName),
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeSubnetworkExists(
"google_compute_subnetwork.network-with-private-google-access", &subnetwork),
),
},
{
ResourceName: "google_compute_subnetwork.network-with-private-google-access",
ImportState: true,
ImportStateVerify: true,
},
},
})

Expand Down Expand Up @@ -356,6 +369,28 @@ resource "google_compute_subnetwork" "network-with-private-google-access" {
`, cnName, subnetworkName, cidrRange)
}

func testAccComputeSubnetwork_update3(cnName, cidrRange, subnetworkName string) string {
return fmt.Sprintf(`
resource "google_compute_network" "custom-test" {
name = "%s"
auto_create_subnetworks = false
}
resource "google_compute_subnetwork" "network-with-private-google-access" {
name = "%s"
ip_cidr_range = "%s"
region = "us-central1"
network = "${google_compute_network.custom-test.self_link}"
enable_flow_logs = true
secondary_ip_range {
range_name = "tf-test-secondary-range-update"
ip_cidr_range = "192.168.10.0/24"
}
}
`, cnName, subnetworkName, cidrRange)
}

func testAccComputeSubnetwork_secondaryIpRanges_update1(cnName, subnetworkName string) string {
return fmt.Sprintf(`
resource "google_compute_network" "custom-test" {
Expand Down
1 change: 1 addition & 0 deletions google-beta/resource_compute_target_http_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ func resourceComputeTargetHttpProxyUpdate(d *schema.ResourceData, meta interface

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

urlMapProp, err := expandComputeTargetHttpProxyUrlMap(d.Get("url_map"), d, config)
if err != nil {
return err
Expand Down
4 changes: 4 additions & 0 deletions google-beta/resource_compute_target_https_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ func resourceComputeTargetHttpsProxyUpdate(d *schema.ResourceData, meta interfac

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

quicOverrideProp, err := expandComputeTargetHttpsProxyQuicOverride(d.Get("quic_override"), d, config)
if err != nil {
return err
Expand Down Expand Up @@ -282,6 +283,7 @@ func resourceComputeTargetHttpsProxyUpdate(d *schema.ResourceData, meta interfac
}
if d.HasChange("ssl_certificates") {
obj := make(map[string]interface{})

sslCertificatesProp, err := expandComputeTargetHttpsProxySslCertificates(d.Get("ssl_certificates"), d, config)
if err != nil {
return err
Expand Down Expand Up @@ -316,6 +318,7 @@ func resourceComputeTargetHttpsProxyUpdate(d *schema.ResourceData, meta interfac
}
if d.HasChange("ssl_policy") {
obj := make(map[string]interface{})

sslPolicyProp, err := expandComputeTargetHttpsProxySslPolicy(d.Get("ssl_policy"), d, config)
if err != nil {
return err
Expand Down Expand Up @@ -350,6 +353,7 @@ func resourceComputeTargetHttpsProxyUpdate(d *schema.ResourceData, meta interfac
}
if d.HasChange("url_map") {
obj := make(map[string]interface{})

urlMapProp, err := expandComputeTargetHttpsProxyUrlMap(d.Get("url_map"), d, config)
if err != nil {
return err
Expand Down
Loading

0 comments on commit 92a794b

Please sign in to comment.