Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support import for google_compute_instance_group #201

Merged
merged 4 commits into from
Jul 20, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion google/resource_compute_instance_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ func resourceComputeInstanceGroup() *schema.Resource {
State: resourceComputeInstanceGroupImportState,
},

SchemaVersion: 1,
SchemaVersion: 2,
MigrateState: resourceComputeInstanceGroupMigrateState,

Schema: map[string]*schema.Schema{
"name": {
Expand Down
15 changes: 15 additions & 0 deletions google/resource_compute_instance_group_migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ func resourceComputeInstanceGroupMigrateState(
if err != nil {
return is, err
}
fallthrough
case 1:
log.Println("[INFO] Found Compute Instance Group State v1; migrating to v2")
is, err := migrateInstanceGroupStateV1toV2(is)
if err != nil {
return is, err
}
return is, nil
default:
return is, fmt.Errorf("Unexpected schema version: %d", v)
Expand Down Expand Up @@ -72,3 +79,11 @@ func migrateInstanceGroupStateV0toV1(is *terraform.InstanceState) (*terraform.In
log.Printf("[DEBUG] Attributes after migration: %#v", is.Attributes)
return is, nil
}

func migrateInstanceGroupStateV1toV2(is *terraform.InstanceState) (*terraform.InstanceState, error) {
log.Printf("[DEBUG] Attributes before migration: %#v", is.Attributes)

is.ID = fmt.Sprintf("%s/%s", is.Attributes["zone"], is.Attributes["name"])

return is, nil
}
86 changes: 63 additions & 23 deletions google/resource_compute_instance_group_migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,43 @@ import (

func TestComputeInstanceGroupMigrateState(t *testing.T) {
cases := map[string]struct {
StateVersion int
Attributes map[string]string
Expected map[string]string
Meta interface{}
StateVersion int
Attributes map[string]string
ExpectedAttributes map[string]string
ExpectedId string
Meta interface{}
}{
"change instances from list to set": {
"v1 to v2": {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't look like this test is actually running:

=== RUN   TestComputeInstanceGroupMigrateState
2017/07/19 10:00:58 [INFO] Found Compute Instance Group State v0; migrating to v1
2017/07/19 10:00:58 [DEBUG] Attributes before migration: map[string]string{"instances.#":"1", "instances.0":"https://www.googleapis.com/compute/v1/projects/project_name/zones/zone_name/instances/instancegroup-test-1", "instances.1":"https://www.googleapis.com/compute/v1/projects/project_name/zones/zone_name/instances/instancegroup-test-0"}
2017/07/19 10:00:58 [DEBUG] Attributes after migration: map[string]string{"instances.#":"1", "instances.1519187872":"https://www.googleapis.com/compute/v1/projects/project_name/zones/zone_name/instances/instancegroup-test-0", "instances.764135222":"https://www.googleapis.com/compute/v1/projects/project_name/zones/zone_name/instances/instancegroup-test-1"}
--- PASS: TestComputeInstanceGroupMigrateState (0.00s)

It's not showing anything migrating to v2

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wait just kidding, I forgot to actually pull your latest commit. Nothing to see here.

StateVersion: 1,
Attributes: map[string]string{
"zone": "us-central1-c",
"name": "instancegroup-test",
},
ExpectedAttributes: map[string]string{
"zone": "us-central1-c",
"name": "instancegroup-test",
},
ExpectedId: "us-central1-c/instancegroup-test",
Meta: &Config{},
},
"v0 to v2": {
StateVersion: 0,
Attributes: map[string]string{
"zone": "us-central1-c",
"name": "instancegroup-test",
"instances.#": "1",
"instances.0": "https://www.googleapis.com/compute/v1/projects/project_name/zones/zone_name/instances/instancegroup-test-1",
"instances.1": "https://www.googleapis.com/compute/v1/projects/project_name/zones/zone_name/instances/instancegroup-test-0",
},
Expected: map[string]string{
ExpectedAttributes: map[string]string{
"zone": "us-central1-c",
"name": "instancegroup-test",
"instances.#": "1",
"instances.764135222": "https://www.googleapis.com/compute/v1/projects/project_name/zones/zone_name/instances/instancegroup-test-1",
"instances.1519187872": "https://www.googleapis.com/compute/v1/projects/project_name/zones/zone_name/instances/instancegroup-test-0",
},
Meta: &Config{},
ExpectedId: "us-central1-c/instancegroup-test",
Meta: &Config{},
},
}

Expand All @@ -41,7 +60,15 @@ func TestComputeInstanceGroupMigrateState(t *testing.T) {
t.Fatalf("bad: %s, err: %#v", tn, err)
}

for k, v := range tc.Expected {
if is.ID != tc.ExpectedId {
t.Fatalf("bad: %s\n\n expected: %s\n got: %s", tn, tc.ExpectedId, is.ID)
}

for k, v := range tc.ExpectedAttributes {
if k == "id" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

empty if statement?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oupps. Removed. The id assertions is done above.


}

if is.Attributes[k] != v {
t.Fatalf(
"bad: %s\n\n expected: %#v -> %#v\n got: %#v -> %#v\n in: %#v",
Expand All @@ -52,24 +79,37 @@ func TestComputeInstanceGroupMigrateState(t *testing.T) {
}

func TestComputeInstanceGroupMigrateState_empty(t *testing.T) {
var is *terraform.InstanceState
var meta *Config
cases := map[string]struct {
StateVersion int
}{
"v0": {
StateVersion: 0,
},
"v1": {
StateVersion: 1,
},
}

// should handle nil
is, err := resourceComputeInstanceGroupMigrateState(0, is, meta)
for tn, tc := range cases {
var is *terraform.InstanceState
var meta *Config

if err != nil {
t.Fatalf("err: %#v", err)
}
if is != nil {
t.Fatalf("expected nil instancestate, got: %#v", is)
}
// should handle nil
is, err := resourceComputeInstanceGroupMigrateState(tc.StateVersion, is, meta)

// should handle non-nil but empty
is = &terraform.InstanceState{}
is, err = resourceComputeInstanceGroupMigrateState(0, is, meta)
if err != nil {
t.Fatalf("bad %s, err: %#v", tn, err)
}
if is != nil {
t.Fatalf("bad %s, expected nil instancestate, got: %#v", tn, is)
}

if err != nil {
t.Fatalf("err: %#v", err)
// should handle non-nil but empty
is = &terraform.InstanceState{}
is, err = resourceComputeInstanceGroupMigrateState(tc.StateVersion, is, meta)

if err != nil {
t.Fatalf("bad %s, err: %#v", tn, err)
}
}
}