From ae142efff74397d972a428162797c6d61a4613b7 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 24 Jun 2014 10:22:22 -0700 Subject: [PATCH] providers/aws: know how to destroy things --- builtin/providers/aws/resources.go | 31 +++++++++++++++++++++++++++++- builtin/providers/aws/state.go | 6 +++--- helper/resource/resource.go | 7 +++++++ 3 files changed, 40 insertions(+), 4 deletions(-) diff --git a/builtin/providers/aws/resources.go b/builtin/providers/aws/resources.go index df49ad005..794c84136 100644 --- a/builtin/providers/aws/resources.go +++ b/builtin/providers/aws/resources.go @@ -16,6 +16,7 @@ func init() { Mapping: map[string]resource.Resource{ "aws_instance": resource.Resource{ Create: resource_aws_instance_create, + Destroy: resource_aws_instance_destroy, Refresh: resource_aws_instance_refresh, }, }, @@ -56,7 +57,7 @@ func resource_aws_instance_create( instanceRaw, err := WaitForState(&StateChangeConf{ Pending: []string{"pending"}, Target: "running", - Refresh: InstanceStateRefreshFunc(ec2conn, instance), + Refresh: InstanceStateRefreshFunc(ec2conn, instance.InstanceId), }) if err != nil { return rs, fmt.Errorf( @@ -70,6 +71,34 @@ func resource_aws_instance_create( return resource_aws_instance_update_state(rs, instance) } +func resource_aws_instance_destroy( + s *terraform.ResourceState, + meta interface{}) error { + p := meta.(*ResourceProvider) + ec2conn := p.ec2conn + + log.Printf("[INFO] Terminating instance: %s", s.ID) + if _, err := ec2conn.TerminateInstances([]string{s.ID}); err != nil { + return fmt.Errorf("Error terminating instance: %s", err) + } + + log.Printf( + "[DEBUG] Waiting for instance (%s) to become terminated", + s.ID) + _, err := WaitForState(&StateChangeConf{ + Pending: []string{"pending", "running", "shutting-down", "stopped", "stopping"}, + Target: "terminated", + Refresh: InstanceStateRefreshFunc(ec2conn, s.ID), + }) + if err != nil { + return fmt.Errorf( + "Error waiting for instance (%s) to terminate: %s", + s.ID, err) + } + + return nil +} + func resource_aws_instance_refresh( s *terraform.ResourceState, meta interface{}) (*terraform.ResourceState, error) { diff --git a/builtin/providers/aws/state.go b/builtin/providers/aws/state.go index 7b0363d8b..cf076ddf4 100644 --- a/builtin/providers/aws/state.go +++ b/builtin/providers/aws/state.go @@ -30,9 +30,9 @@ type StateChangeConf struct { // InstanceStateRefreshFunc returns a StateRefreshFunc that is used to watch // an EC2 instance. -func InstanceStateRefreshFunc(conn *ec2.EC2, i *ec2.Instance) StateRefreshFunc { +func InstanceStateRefreshFunc(conn *ec2.EC2, instanceID string) StateRefreshFunc { return func() (interface{}, string, error) { - resp, err := conn.Instances([]string{i.InstanceId}, ec2.NewFilter()) + resp, err := conn.Instances([]string{instanceID}, ec2.NewFilter()) if err != nil { if ec2err, ok := err.(*ec2.Error); ok && ec2err.Code == "InvalidInstanceID.NotFound" { // Set this to nil as if we didn't find anything. @@ -49,7 +49,7 @@ func InstanceStateRefreshFunc(conn *ec2.EC2, i *ec2.Instance) StateRefreshFunc { return nil, "", nil } - i = &resp.Reservations[0].Instances[0] + i := &resp.Reservations[0].Instances[0] return i, i.State.Name, nil } } diff --git a/helper/resource/resource.go b/helper/resource/resource.go index f8620e8c2..3409228d1 100644 --- a/helper/resource/resource.go +++ b/helper/resource/resource.go @@ -6,6 +6,7 @@ import ( type Resource struct { Create CreateFunc + Destroy DestroyFunc Diff DiffFunc Refresh RefreshFunc } @@ -17,6 +18,12 @@ type CreateFunc func( *terraform.ResourceDiff, interface{}) (*terraform.ResourceState, error) +// DestroyFunc is a function that destroys a resource that previously +// exists using the state. +type DestroyFunc func( + *terraform.ResourceState, + interface{}) error + // DiffFunc is a function that performs a diff of a resource. type DiffFunc func( *terraform.ResourceState,