providers/aws: cleaner detection of VPC [GH-122]

This commit is contained in:
Mitchell Hashimoto 2014-08-19 17:22:25 -07:00
parent 87d3f76336
commit d25747ba5c
2 changed files with 41 additions and 19 deletions

View File

@ -21,12 +21,15 @@ IMPROVEMENTS:
* core: "~/.terraformrc" (Unix) or "%APPDATA%/terraform.rc" (Windows) * core: "~/.terraformrc" (Unix) or "%APPDATA%/terraform.rc" (Windows)
can be used to configure custom providers and provisioners. [GH-192] can be used to configure custom providers and provisioners. [GH-192]
* providers/aws: EIPs now expose `allocation_id` and `public_ip`
attributes.
BUG FIXES: BUG FIXES:
* core: Variables are validated to not contain interpolations. [GH-180] * core: Variables are validated to not contain interpolations. [GH-180]
* core: Key files for provisioning can now contain `~` and will be expanded * core: Key files for provisioning can now contain `~` and will be expanded
to the user's home directory. [GH-179] to the user's home directory. [GH-179]
* providers/aws: Fix issues around failing to read EIPs. [GH-122]
* providers/heroku: If you delete the `config_vars` block, config vars * providers/heroku: If you delete the `config_vars` block, config vars
are properly nuked. are properly nuked.
* providers/heroku: Domains and drains are deleted before the app. * providers/heroku: Domains and drains are deleted before the app.

View File

@ -29,6 +29,16 @@ func resourceAwsEip() *schema.Resource {
Optional: true, Optional: true,
}, },
"allocation_id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"domain": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"public_ip": &schema.Schema{ "public_ip": &schema.Schema{
Type: schema.TypeString, Type: schema.TypeString,
Computed: true, Computed: true,
@ -47,10 +57,8 @@ func resourceAwsEipCreate(d *schema.ResourceData, meta interface{}) error {
ec2conn := p.ec2conn ec2conn := p.ec2conn
// By default, we're not in a VPC // By default, we're not in a VPC
vpc := false
domainOpt := "" domainOpt := ""
if v := d.Get("vpc"); v != nil && v.(bool) { if v := d.Get("vpc"); v != nil && v.(bool) {
vpc = true
domainOpt = "vpc" domainOpt = "vpc"
} }
@ -64,28 +72,29 @@ func resourceAwsEipCreate(d *schema.ResourceData, meta interface{}) error {
return fmt.Errorf("Error creating EIP: %s", err) return fmt.Errorf("Error creating EIP: %s", err)
} }
// The domain tells us if we're in a VPC or not
d.Set("domain", allocResp.Domain)
// Assign the eips (unique) allocation id for use later // Assign the eips (unique) allocation id for use later
// the EIP api has a conditional unique ID (really), so // the EIP api has a conditional unique ID (really), so
// if we're in a VPC we need to save the ID as such, otherwise // if we're in a VPC we need to save the ID as such, otherwise
// it defaults to using the public IP // it defaults to using the public IP
log.Printf("[DEBUG] EIP Allocate: %#v", allocResp) log.Printf("[DEBUG] EIP Allocate: %#v", allocResp)
if allocResp.AllocationId != "" { if d.Get("domain").(string) == "vpc" {
d.SetId(allocResp.AllocationId) d.SetId(allocResp.AllocationId)
d.Set("vpc", true)
} else { } else {
d.SetId(allocResp.PublicIp) d.SetId(allocResp.PublicIp)
d.Set("vpc", false)
} }
log.Printf("[INFO] EIP ID: %s (vpc: %v)", d.Id(), vpc) log.Printf("[INFO] EIP ID: %s (domain: %v)", d.Id(), allocResp.Domain)
return resourceAwsEipRead(d, meta) return resourceAwsEipUpdate(d, meta)
} }
func resourceAwsEipUpdate(d *schema.ResourceData, meta interface{}) error { func resourceAwsEipUpdate(d *schema.ResourceData, meta interface{}) error {
p := meta.(*ResourceProvider) p := meta.(*ResourceProvider)
ec2conn := p.ec2conn ec2conn := p.ec2conn
vpc := strings.Contains(d.Id(), "eipalloc") domain := resourceAwsEipDomain(d)
// Only register with an instance if we have one // Only register with an instance if we have one
if v := d.Get("instance"); v != nil { if v := d.Get("instance"); v != nil {
@ -97,7 +106,7 @@ func resourceAwsEipUpdate(d *schema.ResourceData, meta interface{}) error {
} }
// more unique ID conditionals // more unique ID conditionals
if vpc { if domain == "vpc" {
assocOpts = ec2.AssociateAddress{ assocOpts = ec2.AssociateAddress{
InstanceId: instanceId, InstanceId: instanceId,
AllocationId: d.Id(), AllocationId: d.Id(),
@ -105,7 +114,7 @@ func resourceAwsEipUpdate(d *schema.ResourceData, meta interface{}) error {
} }
} }
log.Printf("[DEBUG] EIP associate configuration: %#v (vpc: %v)", assocOpts, vpc) log.Printf("[DEBUG] EIP associate configuration: %#v (domain: %v)", assocOpts, domain)
_, err := ec2conn.AssociateAddress(&assocOpts) _, err := ec2conn.AssociateAddress(&assocOpts)
if err != nil { if err != nil {
return fmt.Errorf("Failure associating instances: %s", err) return fmt.Errorf("Failure associating instances: %s", err)
@ -119,8 +128,10 @@ func resourceAwsEipDelete(d *schema.ResourceData, meta interface{}) error {
p := meta.(*ResourceProvider) p := meta.(*ResourceProvider)
ec2conn := p.ec2conn ec2conn := p.ec2conn
domain := resourceAwsEipDomain(d)
var err error var err error
if strings.Contains(d.Id(), "eipalloc") { if domain == "vpc" {
log.Printf("[DEBUG] EIP release (destroy) address allocation: %v", d.Id()) log.Printf("[DEBUG] EIP release (destroy) address allocation: %v", d.Id())
_, err = ec2conn.ReleaseAddress(d.Id()) _, err = ec2conn.ReleaseAddress(d.Id())
return err return err
@ -137,24 +148,20 @@ func resourceAwsEipRead(d *schema.ResourceData, meta interface{}) error {
p := meta.(*ResourceProvider) p := meta.(*ResourceProvider)
ec2conn := p.ec2conn ec2conn := p.ec2conn
vpc := false domain := resourceAwsEipDomain(d)
if d.Get("vpc").(bool) {
vpc = true
}
id := d.Id() id := d.Id()
assocIds := []string{} assocIds := []string{}
publicIps := []string{} publicIps := []string{}
if vpc { if domain == "vpc" {
assocIds = []string{id} assocIds = []string{id}
} else { } else {
publicIps = []string{id} publicIps = []string{id}
} }
log.Printf( log.Printf(
"[DEBUG] EIP describe configuration: %#v, %#v (vpc: %v)", "[DEBUG] EIP describe configuration: %#v, %#v (domain: %s)",
assocIds, publicIps, vpc) assocIds, publicIps, domain)
describeAddresses, err := ec2conn.Addresses(publicIps, assocIds, nil) describeAddresses, err := ec2conn.Addresses(publicIps, assocIds, nil)
if err != nil { if err != nil {
@ -178,3 +185,15 @@ func resourceAwsEipRead(d *schema.ResourceData, meta interface{}) error {
return nil return nil
} }
func resourceAwsEipDomain(d *schema.ResourceData) string {
if v := d.Get("domain"); v != nil {
return v.(string)
} else if strings.Contains(d.Id(), "eipalloc") {
// We have to do this for backwards compatibility since TF 0.1
// didn't have the "domain" computed attribute.
return "vpc"
}
return "standard"
}