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