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 3 commits
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
28 changes: 28 additions & 0 deletions google/import_compute_instance_group_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package google

import (
"fmt"
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"testing"
)

func TestAccComputeInstanceGroup_import(t *testing.T) {
instanceName := fmt.Sprintf("instancegroup-test-%s", acctest.RandString(10))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccComputeInstanceGroup_destroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccComputeInstanceGroup_basic(instanceName),
},
resource.TestStep{
ResourceName: "google_compute_instance_group.basic",
ImportState: true,
ImportStateVerify: true,
},
},
})
}
62 changes: 45 additions & 17 deletions google/resource_compute_instance_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@ func resourceComputeInstanceGroup() *schema.Resource {
Read: resourceComputeInstanceGroupRead,
Update: resourceComputeInstanceGroupUpdate,
Delete: resourceComputeInstanceGroupDelete,
Importer: &schema.ResourceImporter{
State: resourceComputeInstanceGroupImportState,
},

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

Schema: map[string]*schema.Schema{
"name": {
Expand Down Expand Up @@ -117,9 +121,12 @@ func resourceComputeInstanceGroupCreate(d *schema.ResourceData, meta interface{}
return err
}

zone := d.Get("zone").(string)
name := d.Get("name").(string)

// Build the parameter
instanceGroup := &compute.InstanceGroup{
Name: d.Get("name").(string),
Name: name,
}

// Set optional fields
Expand All @@ -137,17 +144,18 @@ func resourceComputeInstanceGroupCreate(d *schema.ResourceData, meta interface{}

log.Printf("[DEBUG] InstanceGroup insert request: %#v", instanceGroup)
op, err := config.clientCompute.InstanceGroups.Insert(
project, d.Get("zone").(string), instanceGroup).Do()
project, zone, instanceGroup).Do()
if err != nil {
return fmt.Errorf("Error creating InstanceGroup: %s", err)
}

// It probably maybe worked, so store the ID now
d.SetId(instanceGroup.Name)
d.SetId(fmt.Sprintf("%s/%s", zone, name))

// Wait for the operation to complete
err = computeOperationWaitZone(config, op, project, d.Get("zone").(string), "Creating InstanceGroup")
err = computeOperationWaitZone(config, op, project, zone, "Creating InstanceGroup")
if err != nil {
d.SetId("")
return err
}

Expand All @@ -163,13 +171,13 @@ func resourceComputeInstanceGroupCreate(d *schema.ResourceData, meta interface{}

log.Printf("[DEBUG] InstanceGroup add instances request: %#v", addInstanceReq)
op, err := config.clientCompute.InstanceGroups.AddInstances(
project, d.Get("zone").(string), d.Id(), addInstanceReq).Do()
project, zone, name, addInstanceReq).Do()
if err != nil {
return fmt.Errorf("Error adding instances to InstanceGroup: %s", err)
}

// Wait for the operation to complete
err = computeOperationWaitZone(config, op, project, d.Get("zone").(string), "Adding instances to InstanceGroup")
err = computeOperationWaitZone(config, op, project, zone, "Adding instances to InstanceGroup")
if err != nil {
return err
}
Expand All @@ -186,17 +194,20 @@ func resourceComputeInstanceGroupRead(d *schema.ResourceData, meta interface{})
return err
}

zone := d.Get("zone").(string)
name := d.Get("name").(string)

// retrieve instance group
instanceGroup, err := config.clientCompute.InstanceGroups.Get(
project, d.Get("zone").(string), d.Id()).Do()
project, zone, name).Do()
if err != nil {
return handleNotFoundError(err, d, fmt.Sprintf("Instance Group %q", d.Get("name").(string)))
return handleNotFoundError(err, d, fmt.Sprintf("Instance Group %q", name))
}

// retrieve instance group members
var memberUrls []string
members, err := config.clientCompute.InstanceGroups.ListInstances(
project, d.Get("zone").(string), d.Id(), &compute.InstanceGroupsListInstancesRequest{
project, zone, name, &compute.InstanceGroupsListInstancesRequest{
InstanceState: "ALL",
}).Do()
if err != nil {
Expand All @@ -216,6 +227,7 @@ func resourceComputeInstanceGroupRead(d *schema.ResourceData, meta interface{})
}

d.Set("named_port", flattenNamedPorts(instanceGroup.NamedPorts))
d.Set("description", instanceGroup.Description)

// Set computed fields
d.Set("network", instanceGroup.Network)
Expand All @@ -232,6 +244,9 @@ func resourceComputeInstanceGroupUpdate(d *schema.ResourceData, meta interface{}
return err
}

zone := d.Get("zone").(string)
name := d.Get("name").(string)

d.Partial(true)

if d.HasChange("instances") {
Expand All @@ -257,7 +272,7 @@ func resourceComputeInstanceGroupUpdate(d *schema.ResourceData, meta interface{}

log.Printf("[DEBUG] InstanceGroup remove instances request: %#v", removeReq)
removeOp, err := config.clientCompute.InstanceGroups.RemoveInstances(
project, d.Get("zone").(string), d.Id(), removeReq).Do()
project, zone, name, removeReq).Do()
if err != nil {
if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 {
log.Printf("[WARN] Instances already removed from InstanceGroup: %s", remove)
Expand All @@ -266,7 +281,7 @@ func resourceComputeInstanceGroupUpdate(d *schema.ResourceData, meta interface{}
}
} else {
// Wait for the operation to complete
err = computeOperationWaitZone(config, removeOp, project, d.Get("zone").(string), "Updating InstanceGroup")
err = computeOperationWaitZone(config, removeOp, project, zone, "Updating InstanceGroup")
if err != nil {
return err
}
Expand All @@ -281,13 +296,13 @@ func resourceComputeInstanceGroupUpdate(d *schema.ResourceData, meta interface{}

log.Printf("[DEBUG] InstanceGroup adding instances request: %#v", addReq)
addOp, err := config.clientCompute.InstanceGroups.AddInstances(
project, d.Get("zone").(string), d.Id(), addReq).Do()
project, zone, name, addReq).Do()
if err != nil {
return fmt.Errorf("Error adding instances from InstanceGroup: %s", err)
}

// Wait for the operation to complete
err = computeOperationWaitZone(config, addOp, project, d.Get("zone").(string), "Updating InstanceGroup")
err = computeOperationWaitZone(config, addOp, project, zone, "Updating InstanceGroup")
if err != nil {
return err
}
Expand All @@ -305,12 +320,12 @@ func resourceComputeInstanceGroupUpdate(d *schema.ResourceData, meta interface{}

log.Printf("[DEBUG] InstanceGroup updating named ports request: %#v", namedPortsReq)
op, err := config.clientCompute.InstanceGroups.SetNamedPorts(
project, d.Get("zone").(string), d.Id(), namedPortsReq).Do()
project, zone, name, namedPortsReq).Do()
if err != nil {
return fmt.Errorf("Error updating named ports for InstanceGroup: %s", err)
}

err = computeOperationWaitZone(config, op, project, d.Get("zone").(string), "Updating InstanceGroup")
err = computeOperationWaitZone(config, op, project, zone, "Updating InstanceGroup")
if err != nil {
return err
}
Expand All @@ -331,7 +346,8 @@ func resourceComputeInstanceGroupDelete(d *schema.ResourceData, meta interface{}
}

zone := d.Get("zone").(string)
op, err := config.clientCompute.InstanceGroups.Delete(project, zone, d.Id()).Do()
name := d.Get("name").(string)
op, err := config.clientCompute.InstanceGroups.Delete(project, zone, name).Do()
if err != nil {
return fmt.Errorf("Error deleting InstanceGroup: %s", err)
}
Expand All @@ -344,3 +360,15 @@ func resourceComputeInstanceGroupDelete(d *schema.ResourceData, meta interface{}
d.SetId("")
return nil
}

func resourceComputeInstanceGroupImportState(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
parts := strings.Split(d.Id(), "/")
if len(parts) != 2 {
return nil, fmt.Errorf("Invalid compute instance group specifier. Expecting {zone}/{name}")
}

d.Set("zone", parts[0])
d.Set("name", parts[1])

return []*schema.ResourceData{d}, nil
}
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,11 @@ 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 is.Attributes[k] != v {
t.Fatalf(
"bad: %s\n\n expected: %#v -> %#v\n got: %#v -> %#v\n in: %#v",
Expand All @@ -52,24 +75,41 @@ 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,
},
}

for tn, tc := range cases {
var is *terraform.InstanceState
var meta *Config

// should handle nil
is, err := resourceComputeInstanceGroupMigrateState(0, is, meta)
// should handle nil
is, err := resourceComputeInstanceGroupMigrateState(tc.StateVersion, is, meta)

if err != nil {
t.Fatalf("err: %#v", err)
}
if is != nil {
t.Fatalf("expected nil instancestate, got: %#v", is)
}
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)
}

// should handle non-nil but empty
is = &terraform.InstanceState{}
is, err = resourceComputeInstanceGroupMigrateState(0, is, meta)
// should handle non-nil but empty
is = &terraform.InstanceState{}
is, err = resourceComputeInstanceGroupMigrateState(tc.StateVersion, is, meta)

if err != nil {
t.Fatalf("err: %#v", err)
// should handle non-nil but empty
Copy link
Contributor

Choose a reason for hiding this comment

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

I think these few lines can be removed (they're the same as what's directly above them)

is = &terraform.InstanceState{}
is, err = resourceComputeInstanceGroupMigrateState(tc.StateVersion, is, meta)

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