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{
|
Mapping: map[string]resource.Resource{
|
||||||
"aws_instance": resource.Resource{
|
"aws_instance": resource.Resource{
|
||||||
Create: resource_aws_instance_create,
|
Create: resource_aws_instance_create,
|
||||||
|
Destroy: resource_aws_instance_destroy,
|
||||||
Refresh: resource_aws_instance_refresh,
|
Refresh: resource_aws_instance_refresh,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -56,7 +57,7 @@ func resource_aws_instance_create(
|
||||||
instanceRaw, err := WaitForState(&StateChangeConf{
|
instanceRaw, err := WaitForState(&StateChangeConf{
|
||||||
Pending: []string{"pending"},
|
Pending: []string{"pending"},
|
||||||
Target: "running",
|
Target: "running",
|
||||||
Refresh: InstanceStateRefreshFunc(ec2conn, instance),
|
Refresh: InstanceStateRefreshFunc(ec2conn, instance.InstanceId),
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return rs, fmt.Errorf(
|
return rs, fmt.Errorf(
|
||||||
|
@ -70,6 +71,34 @@ func resource_aws_instance_create(
|
||||||
return resource_aws_instance_update_state(rs, instance)
|
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(
|
func resource_aws_instance_refresh(
|
||||||
s *terraform.ResourceState,
|
s *terraform.ResourceState,
|
||||||
meta interface{}) (*terraform.ResourceState, error) {
|
meta interface{}) (*terraform.ResourceState, error) {
|
||||||
|
|
|
@ -30,9 +30,9 @@ type StateChangeConf struct {
|
||||||
|
|
||||||
// InstanceStateRefreshFunc returns a StateRefreshFunc that is used to watch
|
// InstanceStateRefreshFunc returns a StateRefreshFunc that is used to watch
|
||||||
// an EC2 instance.
|
// 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) {
|
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 err != nil {
|
||||||
if ec2err, ok := err.(*ec2.Error); ok && ec2err.Code == "InvalidInstanceID.NotFound" {
|
if ec2err, ok := err.(*ec2.Error); ok && ec2err.Code == "InvalidInstanceID.NotFound" {
|
||||||
// Set this to nil as if we didn't find anything.
|
// 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
|
return nil, "", nil
|
||||||
}
|
}
|
||||||
|
|
||||||
i = &resp.Reservations[0].Instances[0]
|
i := &resp.Reservations[0].Instances[0]
|
||||||
return i, i.State.Name, nil
|
return i, i.State.Name, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,6 +6,7 @@ import (
|
||||||
|
|
||||||
type Resource struct {
|
type Resource struct {
|
||||||
Create CreateFunc
|
Create CreateFunc
|
||||||
|
Destroy DestroyFunc
|
||||||
Diff DiffFunc
|
Diff DiffFunc
|
||||||
Refresh RefreshFunc
|
Refresh RefreshFunc
|
||||||
}
|
}
|
||||||
|
@ -17,6 +18,12 @@ type CreateFunc func(
|
||||||
*terraform.ResourceDiff,
|
*terraform.ResourceDiff,
|
||||||
interface{}) (*terraform.ResourceState, error)
|
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.
|
// DiffFunc is a function that performs a diff of a resource.
|
||||||
type DiffFunc func(
|
type DiffFunc func(
|
||||||
*terraform.ResourceState,
|
*terraform.ResourceState,
|
||||||
|
|
Loading…
Reference in New Issue