/
resource_aviatrix_aws_tgw_directconnect.go
228 lines (196 loc) · 7.5 KB
/
resource_aviatrix_aws_tgw_directconnect.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package aviatrix
import (
"fmt"
"log"
"strings"
"github.com/AviatrixSystems/terraform-provider-aviatrix/v2/goaviatrix"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func resourceAviatrixAWSTgwDirectConnect() *schema.Resource {
return &schema.Resource{
Create: resourceAviatrixAWSTgwDirectConnectCreate,
Read: resourceAviatrixAWSTgwDirectConnectRead,
Update: resourceAviatrixAWSTgwDirectConnectUpdate,
Delete: resourceAviatrixAWSTgwDirectConnectDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"tgw_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "This parameter represents the name of an AWS TGW.",
},
"directconnect_account_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "This parameter represents the name of an Account in Aviatrix controller.",
},
"dx_gateway_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "This parameter represents the name of a Direct Connect Gateway ID.",
},
"security_domain_name": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Deprecated: "Please use network_domain_name instead.",
ConflictsWith: []string{"network_domain_name"},
Description: "The name of an Aviatrix security domain, to which the direct connect gateway will be attached.",
},
"network_domain_name": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ConflictsWith: []string{"security_domain_name"},
Description: "The name of an Aviatrix network domain, to which the direct connect gateway will be attached.",
},
"allowed_prefix": {
Type: schema.TypeString,
Required: true,
Description: "Public IP address. Example: '40.0.0.0'.",
},
"enable_learned_cidrs_approval": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Switch to enable/disable encrypted transit approval for direct connection. Valid values: true, false.",
},
},
}
}
func resourceAviatrixAWSTgwDirectConnectCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*goaviatrix.Client)
awsTgwDirectConnect := &goaviatrix.AwsTgwDirectConnect{
TgwName: d.Get("tgw_name").(string),
DirectConnectAccountName: d.Get("directconnect_account_name").(string),
DxGatewayID: d.Get("dx_gateway_id").(string),
AllowedPrefix: d.Get("allowed_prefix").(string),
}
securityDomainName, securityDomainNameOk := d.GetOk("security_domain_name")
networkDomainName, networkDomainNameOk := d.GetOk("network_domain_name")
if !securityDomainNameOk && !networkDomainNameOk {
return fmt.Errorf("either security_domain_name or network_domain_name must be configured")
}
if securityDomainNameOk {
awsTgwDirectConnect.SecurityDomainName = securityDomainName.(string)
} else {
awsTgwDirectConnect.SecurityDomainName = networkDomainName.(string)
}
learnedCidrsApproval := d.Get("enable_learned_cidrs_approval").(bool)
if learnedCidrsApproval {
awsTgwDirectConnect.LearnedCidrsApproval = "yes"
} else {
awsTgwDirectConnect.LearnedCidrsApproval = "no"
}
d.SetId(awsTgwDirectConnect.TgwName + "~" + awsTgwDirectConnect.DxGatewayID)
flag := false
defer resourceAviatrixAWSTgwDirectConnectReadIfRequired(d, meta, &flag)
err := client.CreateAwsTgwDirectConnect(awsTgwDirectConnect)
if err != nil {
return fmt.Errorf("failed to create Aviatrix AWS TGW Direct Connect: %s", err)
}
return resourceAviatrixAWSTgwDirectConnectReadIfRequired(d, meta, &flag)
}
func resourceAviatrixAWSTgwDirectConnectReadIfRequired(d *schema.ResourceData, meta interface{}, flag *bool) error {
if !(*flag) {
*flag = true
return resourceAviatrixAWSTgwDirectConnectRead(d, meta)
}
return nil
}
func resourceAviatrixAWSTgwDirectConnectRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*goaviatrix.Client)
tgwName := d.Get("tgw_name").(string)
directConnectGatewayID := d.Get("dx_gateway_id").(string)
if tgwName == "" || directConnectGatewayID == "" {
id := d.Id()
log.Printf("[DEBUG] Looks like an import. Import Id is %s", id)
if !strings.Contains(id, "~") {
log.Printf("[DEBUG] Import Id: %s is invalid", id)
}
d.Set("tgw_name", strings.Split(id, "~")[0])
d.Set("dx_gateway_id", strings.Split(id, "~")[1])
d.SetId(id)
}
awsTgwDirectConnect := &goaviatrix.AwsTgwDirectConnect{
TgwName: d.Get("tgw_name").(string),
DxGatewayID: d.Get("dx_gateway_id").(string),
}
directConnect, err := client.GetAwsTgwDirectConnect(awsTgwDirectConnect)
if err != nil {
if err == goaviatrix.ErrNotFound {
d.SetId("")
return nil
}
return fmt.Errorf("couldn't find Aviatrix Aws Tgw Direct Connect: %s", err)
}
log.Printf("[INFO] Found Aviatrix Aws Tgw Direct Connect: %#v", directConnect)
d.Set("tgw_name", directConnect.TgwName)
d.Set("directconnect_account_name", directConnect.DirectConnectAccountName)
d.Set("dx_gateway_id", directConnect.DxGatewayID)
d.Set("allowed_prefix", directConnect.AllowedPrefix)
if _, ok := d.GetOk("security_domain_name"); ok {
d.Set("security_domain_name", directConnect.SecurityDomainName)
} else {
d.Set("network_domain_name", directConnect.SecurityDomainName)
}
if directConnect.LearnedCidrsApproval == "yes" {
d.Set("enable_learned_cidrs_approval", true)
} else {
d.Set("enable_learned_cidrs_approval", false)
}
d.SetId(directConnect.TgwName + "~" + directConnect.DxGatewayID)
return nil
}
func resourceAviatrixAWSTgwDirectConnectUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*goaviatrix.Client)
awsTgwDirectConnect := &goaviatrix.AwsTgwDirectConnect{
TgwName: d.Get("tgw_name").(string),
DxGatewayName: d.Get("dx_gateway_id").(string),
}
d.Partial(true)
log.Printf("[INFO] Updating Aviatrix Site2Cloud: %#v", awsTgwDirectConnect)
if ok := d.HasChange("allowed_prefix"); ok {
awsTgwDirectConnect.AllowedPrefix = d.Get("allowed_prefix").(string)
err := client.UpdateDirectConnAllowedPrefix(awsTgwDirectConnect)
if err != nil {
return fmt.Errorf("failed to update Aws Tgw Direct Connect Allowed Prefix: %s", err)
}
}
if d.HasChange("enable_learned_cidrs_approval") {
learnedCidrsApproval := d.Get("enable_learned_cidrs_approval").(bool)
if learnedCidrsApproval {
awsTgwDirectConnect.LearnedCidrsApproval = "yes"
err := client.EnableDirectConnectLearnedCidrsApproval(awsTgwDirectConnect)
if err != nil {
return fmt.Errorf("failed to enable learned cidrs approval: %s", err)
}
} else {
awsTgwDirectConnect.LearnedCidrsApproval = "no"
err := client.DisableDirectConnectLearnedCidrsApproval(awsTgwDirectConnect)
if err != nil {
return fmt.Errorf("failed to disable learned cidrs approval: %s", err)
}
}
}
d.Partial(false)
return resourceAviatrixAWSTgwDirectConnectRead(d, meta)
}
func resourceAviatrixAWSTgwDirectConnectDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*goaviatrix.Client)
awsTgwDirectConnect := &goaviatrix.AwsTgwDirectConnect{
TgwName: d.Get("tgw_name").(string),
DirectConnectID: d.Get("dx_gateway_id").(string),
}
log.Printf("[INFO] Deleting Aviatrix AWS TGW Direct Connect: %#v", awsTgwDirectConnect)
err := client.DeleteAwsTgwDirectConnect(awsTgwDirectConnect)
if err != nil {
return fmt.Errorf("failed to delete Aviatrix AWS TGW Direct Connect: %s", err)
}
return nil
}