-
Notifications
You must be signed in to change notification settings - Fork 4
/
resource_aws_dedicated_conn.go
264 lines (253 loc) · 8.24 KB
/
resource_aws_dedicated_conn.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package provider
import (
"context"
"time"
"github.com/PacketFabric/terraform-provider-packetfabric/internal/packetfabric"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)
const cloudCidNotFoundDetailsMsg = "Please wait few minutes then run: terraform refresh"
func resourceAwsReqDedicatedConn() *schema.Resource {
return &schema.Resource{
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(10 * time.Minute),
Update: schema.DefaultTimeout(10 * time.Minute),
Read: schema.DefaultTimeout(10 * time.Minute),
Delete: schema.DefaultTimeout(10 * time.Minute),
},
CreateContext: resourceAwsReqDedicatedConnCreate,
UpdateContext: resourceAwsReqDedicatedConnUpdate,
ReadContext: resourceAwsReqDedicatedConnRead,
DeleteContext: resourceAwsServicesDelete,
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
},
"aws_region": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The region that the new connection will connect to.\n\n\tExample: us-west-1",
},
"account_uuid": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
DefaultFunc: schema.EnvDefaultFunc("PF_ACCOUNT_ID", nil),
ValidateFunc: validation.IsUUID,
Description: "The UUID for the billing account that should be billed. " +
"Can also be set with the PF_ACCOUNT_ID environment variable.",
},
"description": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "A brief description of this connection.",
},
"zone": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The desired availability zone of the connection.\n\n\tExample: \"A\"",
},
"pop": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The POP in which the dedicated port should be provisioned (the cloud on-ramp).\n\n\tExample: DAL1",
},
"subscription_term": {
Type: schema.TypeInt,
Required: true,
Description: "The billing term, in months, for this connection.\n\n\tEnum: [\"1\", \"12\", \"24\", \"36\"]",
},
"service_class": {
Type: schema.TypeString,
Required: true,
Description: "The service class for the given port, either long haul or metro. Specify metro if the cloud on-ramp (the `pop`) is in the same market as the source ports (the ports to which you will be building out virtual circuits).\n\n\tEnum: [\"longhaul\",\"metro\"]",
},
"autoneg": {
Type: schema.TypeBool,
Required: true,
ForceNew: true,
Description: "Whether the port auto-negotiates or not. This is currently only possible with 1Gbps ports and the request will fail if specified with 10Gbps.",
},
"speed": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The desired capacity of the port.\n\n\tEnum: [\"1Gps\", \"10Gbps\"]",
},
"should_create_lag": {
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
Default: true,
Description: "Create the dedicated connection as a LAG interface. ",
},
"loa": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: "A base64 encoded string of a PDF of the LOA that AWS provided.\n\n\tExample: SSBhbSBhIFBERg==",
},
"po_number": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringLenBetween(1, 32),
Description: "Purchase order number or identifier of a service.",
},
"labels": {
Type: schema.TypeSet,
Optional: true,
Description: "Label value linked to an object.",
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"etl": {
Type: schema.TypeFloat,
Computed: true,
Description: "Early Termination Liability (ETL) fees apply when terminating a service before its term ends. ETL is prorated to the remaining contract days.",
},
},
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
}
}
func resourceAwsReqDedicatedConnCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
c := m.(*packetfabric.PFClient)
c.Ctx = ctx
var diags diag.Diagnostics
dedicatedConn := extractAwsDedicatedConn(d)
expectedResp, err := c.CreateDedicadedAWSConn(dedicatedConn)
if err != nil {
return diag.FromErr(err)
}
createOk := make(chan bool)
defer close(createOk)
ticker := time.NewTicker(time.Duration(30+c.GetRandomSeconds()) * time.Second)
go func() {
for range ticker.C {
dedicatedConns, err := c.GetCurrentCustomersDedicated()
if dedicatedConns != nil && err == nil && len(dedicatedConns) > 0 {
for _, conn := range dedicatedConns {
if expectedResp.UUID == conn.UUID && conn.State == "active" {
expectedResp.CloudCircuitID = conn.CloudCircuitID
ticker.Stop()
createOk <- true
}
}
}
}
}()
<-createOk
if err != nil {
return diag.FromErr(err)
}
d.SetId(expectedResp.CloudCircuitID)
if labels, ok := d.GetOk("labels"); ok {
diagnostics, created := createLabels(c, d.Id(), labels)
if !created {
return diagnostics
}
}
return diags
}
func resourceAwsReqDedicatedConnRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
c := m.(*packetfabric.PFClient)
c.Ctx = ctx
var diags diag.Diagnostics
resp, err := c.GetCloudConnInfo(d.Id())
if err != nil {
return diag.FromErr(err)
}
if resp != nil {
_ = d.Set("account_uuid", resp.AccountUUID)
_ = d.Set("description", resp.Description)
_ = d.Set("pop", resp.Pop)
_ = d.Set("subscription_term", resp.SubscriptionTerm)
_ = d.Set("service_class", resp.ServiceClass)
_ = d.Set("speed", resp.Speed)
_ = d.Set("aws_region", resp.Settings.AwsRegion)
}
resp2, err2 := c.GetPortByCID(d.Id())
if err2 != nil {
return diag.FromErr(err2)
}
if resp2 != nil {
_ = d.Set("autoneg", resp2.Autoneg)
_ = d.Set("zone", resp2.Zone)
_ = d.Set("po_number", resp2.PONumber)
if resp2.IsLag {
_ = d.Set("should_create_lag", true)
} else {
_ = d.Set("should_create_lag", false)
}
}
// unsetFields: loa
if _, ok := d.GetOk("labels"); ok {
labels, err2 := getLabels(c, d.Id())
if err2 != nil {
return diag.FromErr(err2)
}
_ = d.Set("labels", labels)
}
etl, err3 := c.GetEarlyTerminationLiability(d.Id())
if err3 != nil {
return diag.FromErr(err3)
}
if etl > 0 {
_ = d.Set("etl", etl)
}
return diags
}
func resourceAwsReqDedicatedConnUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
return resourceServicesDedicatedUpdate(ctx, d, m)
}
func resourceAwsServicesDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
return resourceCloudSourceDelete(ctx, d, m, "AWS Service Delete")
}
func extractAwsDedicatedConn(d *schema.ResourceData) packetfabric.DedicatedAwsConn {
dedicatedConn := packetfabric.DedicatedAwsConn{}
if awsRegion, ok := d.GetOk("aws_region"); ok {
dedicatedConn.AwsRegion = awsRegion.(string)
}
if accountUUID, ok := d.GetOk("account_uuid"); ok {
dedicatedConn.AccountUUID = accountUUID.(string)
}
if description, ok := d.GetOk("description"); ok {
dedicatedConn.Description = description.(string)
}
if zone, ok := d.GetOk("zone"); ok {
dedicatedConn.Zone = zone.(string)
}
if pop, ok := d.GetOk("pop"); ok {
dedicatedConn.Pop = pop.(string)
}
if subTerm, ok := d.GetOk("subscription_term"); ok {
dedicatedConn.SubscriptionTerm = subTerm.(int)
}
if serviceClass, ok := d.GetOk("service_class"); ok {
dedicatedConn.ServiceClass = serviceClass.(string)
}
if autoneg, ok := d.GetOk("autoneg"); ok {
dedicatedConn.AutoNeg = autoneg.(bool)
}
if speed, ok := d.GetOk("speed"); ok {
dedicatedConn.Speed = speed.(string)
}
if shouldCreateLag, ok := d.GetOk("should_create_lag"); ok {
dedicatedConn.ShouldCreateLag = shouldCreateLag.(bool)
}
if loa, ok := d.GetOk("loa"); ok {
dedicatedConn.Loa = loa.(string)
}
if poNumber, ok := d.GetOk("po_number"); ok {
dedicatedConn.PONumber = poNumber.(string)
}
return dedicatedConn
}