Skip to content

Commit

Permalink
Fixed TestAccNetworkServicesGateway_update (#12749) (#9056)
Browse files Browse the repository at this point in the history
[upstream:c5cd53864b3d5f8dbf18a03bc7066993e3276aa8]

Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored Jan 14, 2025
1 parent 3492116 commit 1b7ec1b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
6 changes: 6 additions & 0 deletions .changelog/12749.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
```release-note:enhancement
networkservices: added in-place update support for `ports` field on `google_network_services_gateway` resource
```
```release-note:bug
networkservices: fixed bug where `google_network_services_gateway` could not be updated in place
```
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,6 @@ func ResourceNetworkServicesGateway() *schema.Resource {
"ports": {
Type: schema.TypeList,
Required: true,
ForceNew: true,
Description: `One or more port numbers (1-65535), on which the Gateway will receive traffic.
The proxy binds to the specified ports. Gateways of type 'SECURE_WEB_GATEWAY' are
limited to 1 port. Gateways of type 'OPEN_MESH' listen on 0.0.0.0 and support multiple ports.`,
Expand Down Expand Up @@ -594,6 +593,12 @@ func resourceNetworkServicesGatewayUpdate(d *schema.ResourceData, meta interface
} else if v, ok := d.GetOkExists("description"); !tpgresource.IsEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, descriptionProp)) {
obj["description"] = descriptionProp
}
portsProp, err := expandNetworkServicesGatewayPorts(d.Get("ports"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("ports"); !tpgresource.IsEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, portsProp)) {
obj["ports"] = portsProp
}
serverTlsPolicyProp, err := expandNetworkServicesGatewayServerTlsPolicy(d.Get("server_tls_policy"), d, config)
if err != nil {
return err
Expand Down Expand Up @@ -625,6 +630,11 @@ func resourceNetworkServicesGatewayUpdate(d *schema.ResourceData, meta interface
obj["labels"] = labelsProp
}

obj, err = resourceNetworkServicesGatewayUpdateEncoder(d, meta, obj)
if err != nil {
return err
}

url, err := tpgresource.ReplaceVars(d, config, "{{NetworkServicesBasePath}}projects/{{project}}/locations/{{location}}/gateways/{{name}}")
if err != nil {
return err
Expand All @@ -638,6 +648,10 @@ func resourceNetworkServicesGatewayUpdate(d *schema.ResourceData, meta interface
updateMask = append(updateMask, "description")
}

if d.HasChange("ports") {
updateMask = append(updateMask, "ports")
}

if d.HasChange("server_tls_policy") {
updateMask = append(updateMask, "serverTlsPolicy")
}
Expand All @@ -663,11 +677,6 @@ func resourceNetworkServicesGatewayUpdate(d *schema.ResourceData, meta interface
if err != nil {
return err
}
if d.Get("type") == "SECURE_WEB_GATEWAY" {
obj["name"] = d.Get("name")
obj["type"] = d.Get("type")
obj["routingMode"] = d.Get("routingMode")
}

// err == nil indicates that the billing_project value was found
if bp, err := tpgresource.GetBillingProject(d, config); err == nil {
Expand Down Expand Up @@ -945,3 +954,14 @@ func expandNetworkServicesGatewayEffectiveLabels(v interface{}, d tpgresource.Te
}
return m, nil
}

func resourceNetworkServicesGatewayUpdateEncoder(d *schema.ResourceData, meta interface{}, obj map[string]interface{}) (map[string]interface{}, error) {
// Always force-send some attributes even if they were not modified. This works around extra API-side requirements.
obj["scope"] = d.Get("scope")
if d.Get("type") == "SECURE_WEB_GATEWAY" {
obj["name"] = d.Get("name")
obj["type"] = d.Get("type")
obj["routingMode"] = d.Get("routingMode")
}
return obj, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/hashicorp/terraform-provider-google-beta/google-beta/acctest"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/plancheck"
)

func TestAccNetworkServicesGateway_update(t *testing.T) {
Expand All @@ -32,6 +33,11 @@ func TestAccNetworkServicesGateway_update(t *testing.T) {
},
{
Config: testAccNetworkServicesGateway_update(gatewayName),
ConfigPlanChecks: resource.ConfigPlanChecks{
PreApply: []plancheck.PlanCheck{
plancheck.ExpectResourceAction("google_network_services_gateway.foobar", plancheck.ResourceActionUpdate),
},
},
},
{
ResourceName: "google_network_services_gateway.foobar",
Expand Down Expand Up @@ -61,7 +67,7 @@ resource "google_network_services_gateway" "foobar" {
name = "%s"
scope = "default-scope-update"
type = "OPEN_MESH"
ports = [443]
ports = [1000]
description = "update description"
labels = {
foo = "bar"
Expand Down

0 comments on commit 1b7ec1b

Please sign in to comment.