-
Notifications
You must be signed in to change notification settings - Fork 9.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
provider/google: Support Import of 'google_compute_firewall'
- Loading branch information
Showing
5 changed files
with
241 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package google | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/helper/acctest" | ||
"github.com/hashicorp/terraform/helper/resource" | ||
) | ||
|
||
func TestAccComputeFirewall_importBasic(t *testing.T) { | ||
resourceName := "google_compute_firewall.foobar" | ||
networkName := fmt.Sprintf("firewall-test-%s", acctest.RandString(10)) | ||
firewallName := fmt.Sprintf("firewall-test-%s", acctest.RandString(10)) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckComputeFirewallDestroy, | ||
Steps: []resource.TestStep{ | ||
resource.TestStep{ | ||
Config: testAccComputeFirewall_basic(networkName, firewallName), | ||
}, | ||
|
||
resource.TestStep{ | ||
ResourceName: resourceName, | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
}, | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
builtin/providers/google/resource_compute_firewall_migrate.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package google | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"sort" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/hashicorp/terraform/terraform" | ||
) | ||
|
||
func resourceComputeFirewallMigrateState( | ||
v int, is *terraform.InstanceState, meta interface{}) (*terraform.InstanceState, error) { | ||
if is.Empty() { | ||
log.Println("[DEBUG] Empty FirewallState; nothing to migrate.") | ||
return is, nil | ||
} | ||
|
||
switch v { | ||
case 0: | ||
log.Println("[INFO] Found Compute Firewall State v0; migrating to v1") | ||
is, err := migrateFirewallStateV0toV1(is) | ||
if err != nil { | ||
return is, err | ||
} | ||
return is, nil | ||
default: | ||
return is, fmt.Errorf("Unexpected schema version: %d", v) | ||
} | ||
} | ||
|
||
func migrateFirewallStateV0toV1(is *terraform.InstanceState) (*terraform.InstanceState, error) { | ||
log.Printf("[DEBUG] Attributes before migration: %#v", is.Attributes) | ||
idx := 0 | ||
portCount := 0 | ||
newPorts := make(map[string]string) | ||
keys := make([]string, len(is.Attributes)) | ||
for k, _ := range is.Attributes { | ||
keys[idx] = k | ||
idx++ | ||
|
||
} | ||
sort.Strings(keys) | ||
for _, k := range keys { | ||
if !strings.HasPrefix(k, "allow.") { | ||
continue | ||
} | ||
|
||
if k == "allow.#" { | ||
continue | ||
} | ||
|
||
if strings.HasSuffix(k, ".ports.#") { | ||
continue | ||
} | ||
|
||
if strings.HasSuffix(k, ".protocol") { | ||
continue | ||
} | ||
|
||
// We have a key that looks like "allow.<hash>.ports.*" and we know it's not | ||
// allow.<hash>.ports.# because we deleted it above, so it must be allow.<hash1>.ports.<hash2> | ||
// from the Set of Ports. Just need to convert it to a list by | ||
// replacing second hash with sequential numbers. | ||
kParts := strings.Split(k, ".") | ||
|
||
// Sanity check: all four parts should be there and <hash> should be a number | ||
badFormat := false | ||
if len(kParts) != 4 { | ||
badFormat = true | ||
} else if _, err := strconv.Atoi(kParts[1]); err != nil { | ||
badFormat = true | ||
} | ||
|
||
if badFormat { | ||
return is, fmt.Errorf( | ||
"migration error: found port key in unexpected format: %s", k) | ||
} | ||
allowHash, _ := strconv.Atoi(kParts[1]) | ||
newK := fmt.Sprintf("allow.%d.ports.%d", allowHash, portCount) | ||
portCount++ | ||
newPorts[newK] = is.Attributes[k] | ||
delete(is.Attributes, k) | ||
} | ||
|
||
for k, v := range newPorts { | ||
is.Attributes[k] = v | ||
} | ||
|
||
log.Printf("[DEBUG] Attributes after migration: %#v", is.Attributes) | ||
return is, nil | ||
} |
81 changes: 81 additions & 0 deletions
81
builtin/providers/google/resource_compute_firewall_migrate_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package google | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/terraform" | ||
) | ||
|
||
func TestComputeFirewallMigrateState(t *testing.T) { | ||
cases := map[string]struct { | ||
StateVersion int | ||
Attributes map[string]string | ||
Expected map[string]string | ||
Meta interface{} | ||
}{ | ||
"change scope from list to set": { | ||
StateVersion: 0, | ||
Attributes: map[string]string{ | ||
"allow.#": "1", | ||
"allow.0.protocol": "udp", | ||
"allow.0.ports.#": "4", | ||
"allow.0.ports.1693978638": "8080", | ||
"allow.0.ports.172152165": "8081", | ||
"allow.0.ports.299962681": "7072", | ||
"allow.0.ports.3435931483": "4044", | ||
}, | ||
Expected: map[string]string{ | ||
"allow.#": "1", | ||
"allow.0.protocol": "udp", | ||
"allow.0.ports.#": "4", | ||
"allow.0.ports.0": "8080", | ||
"allow.0.ports.1": "8081", | ||
"allow.0.ports.2": "7072", | ||
"allow.0.ports.3": "4044", | ||
}, | ||
}, | ||
} | ||
for tn, tc := range cases { | ||
is := &terraform.InstanceState{ | ||
ID: "i-abc123", | ||
Attributes: tc.Attributes, | ||
} | ||
is, err := resourceComputeFirewallMigrateState( | ||
tc.StateVersion, is, tc.Meta) | ||
|
||
if err != nil { | ||
t.Fatalf("bad: %s, err: %#v", tn, err) | ||
} | ||
|
||
for k, v := range tc.Expected { | ||
if is.Attributes[k] != v { | ||
t.Fatalf( | ||
"bad: %s\n\n expected: %#v -> %#v\n got: %#v -> %#v\n in: %#v", | ||
tn, k, v, k, is.Attributes[k], is.Attributes) | ||
} | ||
} | ||
} | ||
} | ||
|
||
func TestComputeFirewallMigrateState_empty(t *testing.T) { | ||
var is *terraform.InstanceState | ||
var meta interface{} | ||
|
||
// should handle nil | ||
is, err := resourceComputeFirewallMigrateState(0, is, meta) | ||
|
||
if err != nil { | ||
t.Fatalf("err: %#v", err) | ||
} | ||
if is != nil { | ||
t.Fatalf("expected nil instancestate, got: %#v", is) | ||
} | ||
|
||
// should handle non-nil but empty | ||
is = &terraform.InstanceState{} | ||
is, err = resourceComputeFirewallMigrateState(0, is, meta) | ||
|
||
if err != nil { | ||
t.Fatalf("err: %#v", err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters