providers/aws: know how to destroy things
This commit is contained in:
parent
4084ed9234
commit
ae142efff7
|
@ -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) {
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Reference in New Issue