forked from hashicorp/terraform-provider-aws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_aws_vpn_gateway_route_propagation.go
102 lines (85 loc) · 2.66 KB
/
resource_aws_vpn_gateway_route_propagation.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
100
101
102
package aws
import (
"fmt"
"log"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceAwsVpnGatewayRoutePropagation() *schema.Resource {
return &schema.Resource{
Create: resourceAwsVpnGatewayRoutePropagationEnable,
Read: resourceAwsVpnGatewayRoutePropagationRead,
Delete: resourceAwsVpnGatewayRoutePropagationDisable,
Schema: map[string]*schema.Schema{
"vpn_gateway_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"route_table_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
}
func resourceAwsVpnGatewayRoutePropagationEnable(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn
gwID := d.Get("vpn_gateway_id").(string)
rtID := d.Get("route_table_id").(string)
log.Printf("[INFO] Enabling VGW propagation from %s to %s", gwID, rtID)
_, err := conn.EnableVgwRoutePropagation(&ec2.EnableVgwRoutePropagationInput{
GatewayId: aws.String(gwID),
RouteTableId: aws.String(rtID),
})
if err != nil {
return fmt.Errorf("error enabling VGW propagation: %s", err)
}
d.SetId(fmt.Sprintf("%s_%s", gwID, rtID))
return nil
}
func resourceAwsVpnGatewayRoutePropagationDisable(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn
gwID := d.Get("vpn_gateway_id").(string)
rtID := d.Get("route_table_id").(string)
log.Printf("[INFO] Disabling VGW propagation from %s to %s", gwID, rtID)
_, err := conn.DisableVgwRoutePropagation(&ec2.DisableVgwRoutePropagationInput{
GatewayId: aws.String(gwID),
RouteTableId: aws.String(rtID),
})
if err != nil {
return fmt.Errorf("error disabling VGW propagation: %s", err)
}
d.SetId("")
return nil
}
func resourceAwsVpnGatewayRoutePropagationRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn
gwID := d.Get("vpn_gateway_id").(string)
rtID := d.Get("route_table_id").(string)
log.Printf("[INFO] Reading route table %s to check for VPN gateway %s", rtID, gwID)
rtRaw, _, err := resourceAwsRouteTableStateRefreshFunc(conn, rtID)()
if err != nil {
return err
}
if rtRaw == nil {
log.Printf("[INFO] Route table %q doesn't exist, so dropping %q route propagation from state", rtID, gwID)
d.SetId("")
return nil
}
rt := rtRaw.(*ec2.RouteTable)
exists := false
for _, vgw := range rt.PropagatingVgws {
if *vgw.GatewayId == gwID {
exists = true
}
}
if !exists {
log.Printf("[INFO] %s is no longer propagating to %s, so dropping route propagation from state", rtID, gwID)
d.SetId("")
return nil
}
return nil
}