Skip to content

Commit

Permalink
Add drainNatIps to router nat (#4480)
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 emilymye committed Sep 18, 2019
1 parent 7d22e78 commit 6f0346a
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 9 deletions.
53 changes: 53 additions & 0 deletions google/resource_compute_router_nat.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,57 @@ import (
"google.golang.org/api/googleapi"
)

func resourceNameSetFromSelfLinkSet(v interface{}) *schema.Set {
if v == nil {
return schema.NewSet(schema.HashString, nil)
}
vSet := v.(*schema.Set)
ls := make([]interface{}, 0, vSet.Len())
for _, v := range vSet.List() {
if v == nil {
continue
}
ls = append(ls, GetResourceNameFromSelfLink(v.(string)))
}
return schema.NewSet(schema.HashString, ls)
}

// drain_nat_ips MUST be set from (just set) previous values of nat_ips
// so this customizeDiff func makes sure drainNatIps values:
// - aren't set at creation time
// - are in old value of nat_ips but not in new values
func resourceComputeRouterNatDrainNatIpsCustomDiff(diff *schema.ResourceDiff, meta interface{}) error {
o, n := diff.GetChange("drain_nat_ips")
oSet := resourceNameSetFromSelfLinkSet(o)
nSet := resourceNameSetFromSelfLinkSet(n)
addDrainIps := nSet.Difference(oSet)

// We don't care if there are no new drainNatIps
if addDrainIps.Len() == 0 {
return nil
}

// Resource hasn't been created yet - return error
if diff.Id() == "" {
return fmt.Errorf("New RouterNat cannot have drain_nat_ips, got values %+v", addDrainIps.List())
}
//
o, n = diff.GetChange("nat_ips")
oNatSet := resourceNameSetFromSelfLinkSet(o)
nNatSet := resourceNameSetFromSelfLinkSet(n)

// Resource is being updated - make sure new drainNatIps were in natIps prior d and no longer are in natIps.
for _, v := range addDrainIps.List() {
if !oNatSet.Contains(v) {
return fmt.Errorf("drain_nat_ip %q was not previously set in nat_ips %+v", v.(string), oNatSet.List())
}
if nNatSet.Contains(v) {
return fmt.Errorf("drain_nat_ip %q cannot be drained if still set in nat_ips %+v", v.(string), nNatSet.List())
}
}
return nil
}

func resourceComputeRouterNat() *schema.Resource {
return &schema.Resource{
Create: resourceComputeRouterNatCreate,
Expand All @@ -44,6 +95,8 @@ func resourceComputeRouterNat() *schema.Resource {
Delete: schema.DefaultTimeout(10 * time.Minute),
},

CustomizeDiff: resourceComputeRouterNatDrainNatIpsCustomDiff,

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Expand Down
10 changes: 1 addition & 9 deletions google/resource_compute_router_nat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,6 @@ resource "google_compute_router" "foobar"{
name = "router-nat-test-%s"
region = "${google_compute_subnetwork.foobar.region}"
network = "${google_compute_network.foobar.self_link}"
bgp {
asn = 64514
}
}
resource "google_compute_router_nat" "foobar" {
name = "router-nat-test-%s"
Expand All @@ -229,9 +226,6 @@ resource "google_compute_router" "foobar"{
name = "router-nat-test-%s"
region = "${google_compute_subnetwork.foobar.region}"
network = "${google_compute_network.foobar.self_link}"
bgp {
asn = 64514
}
}
resource "google_compute_network" "foobar" {
Expand Down Expand Up @@ -368,12 +362,10 @@ resource "google_compute_subnetwork" "foobar" {
ip_cidr_range = "10.0.0.0/16"
region = "us-central1"
}
resource "google_compute_router" "foobar"{
name = "router-nat-test-%s"
region = "${google_compute_subnetwork.foobar.region}"
network = "${google_compute_network.foobar.self_link}"
bgp {
asn = 64514
}
}`, testId, testId, testId)
}

0 comments on commit 6f0346a

Please sign in to comment.