github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/aws/resource_aws_vpc_dhcp_options_association.go (about) 1 package aws 2 3 import ( 4 "log" 5 6 "github.com/aws/aws-sdk-go/aws" 7 "github.com/aws/aws-sdk-go/service/ec2" 8 "github.com/hashicorp/terraform/helper/schema" 9 ) 10 11 func resourceAwsVpcDhcpOptionsAssociation() *schema.Resource { 12 return &schema.Resource{ 13 Create: resourceAwsVpcDhcpOptionsAssociationCreate, 14 Read: resourceAwsVpcDhcpOptionsAssociationRead, 15 Update: resourceAwsVpcDhcpOptionsAssociationUpdate, 16 Delete: resourceAwsVpcDhcpOptionsAssociationDelete, 17 18 Schema: map[string]*schema.Schema{ 19 "vpc_id": &schema.Schema{ 20 Type: schema.TypeString, 21 Required: true, 22 }, 23 24 "dhcp_options_id": &schema.Schema{ 25 Type: schema.TypeString, 26 Required: true, 27 }, 28 }, 29 } 30 } 31 32 func resourceAwsVpcDhcpOptionsAssociationCreate(d *schema.ResourceData, meta interface{}) error { 33 conn := meta.(*AWSClient).ec2conn 34 35 log.Printf( 36 "[INFO] Creating DHCP Options association: %s => %s", 37 d.Get("vpc_id").(string), 38 d.Get("dhcp_options_id").(string)) 39 40 optsID := aws.String(d.Get("dhcp_options_id").(string)) 41 vpcID := aws.String(d.Get("vpc_id").(string)) 42 43 if _, err := conn.AssociateDhcpOptions(&ec2.AssociateDhcpOptionsInput{ 44 DhcpOptionsId: optsID, 45 VpcId: vpcID, 46 }); err != nil { 47 return err 48 } 49 50 // Set the ID and return 51 d.SetId(*optsID + "-" + *vpcID) 52 log.Printf("[INFO] Association ID: %s", d.Id()) 53 54 return nil 55 } 56 57 func resourceAwsVpcDhcpOptionsAssociationRead(d *schema.ResourceData, meta interface{}) error { 58 conn := meta.(*AWSClient).ec2conn 59 // Get the VPC that this association belongs to 60 vpcRaw, _, err := VPCStateRefreshFunc(conn, d.Get("vpc_id").(string))() 61 62 if err != nil { 63 return err 64 } 65 66 if vpcRaw == nil { 67 return nil 68 } 69 70 vpc := vpcRaw.(*ec2.Vpc) 71 if *vpc.VpcId != d.Get("vpc_id") || *vpc.DhcpOptionsId != d.Get("dhcp_options_id") { 72 log.Printf("[INFO] It seems the DHCP Options association is gone. Deleting reference from Graph...") 73 d.SetId("") 74 } 75 76 return nil 77 } 78 79 // DHCP Options Asociations cannot be updated. 80 func resourceAwsVpcDhcpOptionsAssociationUpdate(d *schema.ResourceData, meta interface{}) error { 81 return resourceAwsVpcDhcpOptionsAssociationCreate(d, meta) 82 } 83 84 // AWS does not provide an API to disassociate a DHCP Options set from a VPC. 85 // So, we do this by setting the VPC to the default DHCP Options Set. 86 func resourceAwsVpcDhcpOptionsAssociationDelete(d *schema.ResourceData, meta interface{}) error { 87 conn := meta.(*AWSClient).ec2conn 88 89 log.Printf("[INFO] Disassociating DHCP Options Set %s from VPC %s...", d.Get("dhcp_options_id"), d.Get("vpc_id")) 90 if _, err := conn.AssociateDhcpOptions(&ec2.AssociateDhcpOptionsInput{ 91 DhcpOptionsId: aws.String("default"), 92 VpcId: aws.String(d.Get("vpc_id").(string)), 93 }); err != nil { 94 return err 95 } 96 97 d.SetId("") 98 return nil 99 }