providers/aws/aws_eip: support update (reassociation)

/cc @pearkes - woot
This commit is contained in:
Mitchell Hashimoto 2014-07-26 15:53:26 -07:00
parent 332a33b640
commit b1fc87fe59
4 changed files with 70 additions and 17 deletions

View File

@ -3,6 +3,4 @@ This is just to keep track of what we need to do in the short term.
* Configuration schemas for providers and provisioners * Configuration schemas for providers and provisioners
* Helper improvements around complex types perhaps * Helper improvements around complex types perhaps
* Providers/AWS: `aws_security_group` needs an update func * Providers/AWS: `aws_security_group` needs an update func
* Providers/AWS: `aws_eip` needs an update func (re-assocation, I think this
is possible in the AWS API). `aws_eip` also needs improved tests
* Providers/AWS: `aws_rds` needs an update func (+goamz changes) * Providers/AWS: `aws_rds` needs an update func (+goamz changes)

View File

@ -51,6 +51,22 @@ func resource_aws_eip_create(
log.Printf("[INFO] EIP ID: %s (vpc: %v)", rs.ID, vpc) log.Printf("[INFO] EIP ID: %s (vpc: %v)", rs.ID, vpc)
return resource_aws_eip_update(rs, d, meta)
}
func resource_aws_eip_update(
s *terraform.ResourceState,
d *terraform.ResourceDiff,
meta interface{}) (*terraform.ResourceState, error) {
p := meta.(*ResourceProvider)
ec2conn := p.ec2conn
// Merge the diff into the state so that we have all the attributes
// properly.
rs := s.MergeDiff(d)
vpc := rs.Attributes["vpc"] == "true"
// If we have an instance to register, do it // If we have an instance to register, do it
instanceId := rs.Attributes["instance"] instanceId := rs.Attributes["instance"]
@ -60,6 +76,7 @@ func resource_aws_eip_create(
InstanceId: instanceId, InstanceId: instanceId,
PublicIp: rs.ID, PublicIp: rs.ID,
} }
// more unique ID conditionals // more unique ID conditionals
if vpc { if vpc {
assocOpts = ec2.AssociateAddress{ assocOpts = ec2.AssociateAddress{
@ -70,7 +87,6 @@ func resource_aws_eip_create(
log.Printf("[DEBUG] EIP associate configuration: %#v (vpc: %v)", assocOpts, vpc) log.Printf("[DEBUG] EIP associate configuration: %#v (vpc: %v)", assocOpts, vpc)
_, err := ec2conn.AssociateAddress(&assocOpts) _, err := ec2conn.AssociateAddress(&assocOpts)
if err != nil { if err != nil {
return rs, fmt.Errorf("Failure associating instances: %s", err) return rs, fmt.Errorf("Failure associating instances: %s", err)
} }
@ -84,19 +100,6 @@ func resource_aws_eip_create(
return resource_aws_eip_update_state(rs, address) return resource_aws_eip_update_state(rs, address)
} }
func resource_aws_eip_update(
s *terraform.ResourceState,
d *terraform.ResourceDiff,
meta interface{}) (*terraform.ResourceState, error) {
rs := s.MergeDiff(d)
log.Printf("ResourceDiff: %s", d)
log.Printf("ResourceState: %s", s)
log.Printf("Merged: %s", rs)
return nil, fmt.Errorf("Did not update")
}
func resource_aws_eip_destroy( func resource_aws_eip_destroy(
s *terraform.ResourceState, s *terraform.ResourceState,
meta interface{}) error { meta interface{}) error {

View File

@ -9,7 +9,7 @@ import (
"github.com/mitchellh/goamz/ec2" "github.com/mitchellh/goamz/ec2"
) )
func TestAccAWSEIP(t *testing.T) { func TestAccAWSEIP_normal(t *testing.T) {
var conf ec2.Address var conf ec2.Address
resource.Test(t, resource.TestCase{ resource.Test(t, resource.TestCase{
@ -28,6 +28,33 @@ func TestAccAWSEIP(t *testing.T) {
}) })
} }
func TestAccAWSEIP_instance(t *testing.T) {
var conf ec2.Address
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSEIPDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccAWSEIPInstanceConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSEIPExists("aws_eip.bar", &conf),
testAccCheckAWSEIPAttributes(&conf),
),
},
resource.TestStep{
Config: testAccAWSEIPInstanceConfig2,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSEIPExists("aws_eip.bar", &conf),
testAccCheckAWSEIPAttributes(&conf),
),
},
},
})
}
func testAccCheckAWSEIPDestroy(s *terraform.State) error { func testAccCheckAWSEIPDestroy(s *terraform.State) error {
conn := testAccProvider.ec2conn conn := testAccProvider.ec2conn
@ -107,3 +134,27 @@ const testAccAWSEIPConfig = `
resource "aws_eip" "bar" { resource "aws_eip" "bar" {
} }
` `
const testAccAWSEIPInstanceConfig = `
resource "aws_instance" "foo" {
# us-west-2
ami = "ami-4fccb37f"
instance_type = "m1.small"
}
resource "aws_eip" "bar" {
instance = "${aws_instance.foo.id}"
}
`
const testAccAWSEIPInstanceConfig2 = `
resource "aws_instance" "bar" {
# us-west-2
ami = "ami-4fccb37f"
instance_type = "m1.small"
}
resource "aws_eip" "bar" {
instance = "${aws_instance.bar.id}"
}
`

View File

@ -45,6 +45,7 @@ func init() {
Destroy: resource_aws_eip_destroy, Destroy: resource_aws_eip_destroy,
Diff: resource_aws_eip_diff, Diff: resource_aws_eip_diff,
Refresh: resource_aws_eip_refresh, Refresh: resource_aws_eip_refresh,
Update: resource_aws_eip_update,
}, },
"aws_elb": resource.Resource{ "aws_elb": resource.Resource{