-
Notifications
You must be signed in to change notification settings - Fork 4
/
resource_oracle_hosted_conn.go
275 lines (262 loc) · 8.26 KB
/
resource_oracle_hosted_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
265
266
267
268
269
270
271
272
273
274
275
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"
)
func resourceOracleHostedConn() *schema.Resource {
return &schema.Resource{
CreateContext: resourceOracleHostedConnCreate,
ReadContext: resourceOracleHostedConnRead,
UpdateContext: resourceOracleHostedConnUpdate,
DeleteContext: resourceOracleHostedConnDelete,
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),
},
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
},
"vc_ocid": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringIsNotEmpty,
Description: "OCID of the FastConnect virtual circuit that you created from the Oracle side.",
},
"region": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringIsNotEmpty,
Description: "The region in which you created the FastConnect virtual circuit.",
},
"description": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
Description: "A brief description of this connection.",
},
"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.",
},
"pop": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringIsNotEmpty,
Description: "The POP in which the connection should be provisioned (the cloud on-ramp).",
},
"port": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringIsNotEmpty,
Description: "The circuit ID of the PacketFabric port you wish to connect to Oracle. This starts with \"PF-AP-\".",
},
"zone": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringIsNotEmpty,
Description: "The desired availability zone of the connection.\n\n\tExample: \"A\"",
},
"vlan": {
Type: schema.TypeInt,
Required: true,
ForceNew: true,
ValidateFunc: validation.IntBetween(4, 4094),
Description: "Valid VLAN range is from 4-4094, inclusive.",
},
"src_svlan": {
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
ValidateFunc: validation.IntBetween(4, 4094),
Description: "Valid S-VLAN range is from 4-4094, inclusive.",
},
"published_quote_line_uuid": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validation.IsUUID,
Description: "UUID of the published quote line with this connection should be associated.",
},
"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 resourceOracleHostedConnCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
c := m.(*packetfabric.PFClient)
c.Ctx = ctx
var diags diag.Diagnostics
hostedConn := extractOracleHostedConn(d)
expectedResp, err := c.RequestNewHostedOracleConn(hostedConn)
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.GetCurrentCustomersHosted()
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 resourceOracleHostedConnRead(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("vlan", resp.Settings.VlanIDCust)
_ = d.Set("pop", resp.CloudProvider.Pop)
_ = d.Set("vc_ocid", resp.Settings.VcOcid)
_ = d.Set("region", resp.Settings.OracleRegion)
_ = d.Set("po_number", resp.PONumber)
}
resp2, err2 := c.GetBackboneByVcCID(d.Id())
if err2 != nil {
return diag.FromErr(err2)
}
if resp2 != nil {
_ = d.Set("port", resp2.Interfaces[0].PortCircuitID) // Port A
if _, ok := d.GetOk("src_svlan"); ok {
if resp2.Interfaces[0].Svlan != 0 {
_ = d.Set("src_svlan", resp2.Interfaces[0].Svlan) // Port A if ENNI
}
}
_ = d.Set("zone", resp2.Interfaces[1].Zone) // Port Z
}
// unsetFields: published_quote_line_uuid
if _, ok := d.GetOk("labels"); ok {
labels, err3 := getLabels(c, d.Id())
if err3 != nil {
return diag.FromErr(err3)
}
_ = d.Set("labels", labels)
}
etl, err4 := c.GetEarlyTerminationLiability(d.Id())
if err4 != nil {
return diag.FromErr(err4)
}
if etl > 0 {
_ = d.Set("etl", etl)
}
return diags
}
func resourceOracleHostedConnUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
return resourceServicesHostedUpdate(ctx, d, m)
}
func resourceOracleHostedConnDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
c := m.(*packetfabric.PFClient)
c.Ctx = ctx
var diags diag.Diagnostics
vcRequestUUID, ok := d.GetOk("id")
if !ok {
return diag.Errorf("please provide a valid VC Request UUID to delete")
}
err := c.DeleteRequestedHostedMktService(vcRequestUUID.(string))
if err != nil {
return diag.FromErr(err)
}
return diags
}
func extractOracleHostedConn(d *schema.ResourceData) packetfabric.CloudServiceOracleConn {
oracleConn := packetfabric.CloudServiceOracleConn{}
if vcOcid, ok := d.GetOk("vc_ocid"); ok {
oracleConn.VcOcid = vcOcid.(string)
}
if region, ok := d.GetOk("region"); ok {
oracleConn.Region = region.(string)
}
if description, ok := d.GetOk("description"); ok {
oracleConn.Description = description.(string)
}
if accountUUID, ok := d.GetOk("account_uuid"); ok {
oracleConn.AccountUUID = accountUUID.(string)
}
if pop, ok := d.GetOk("pop"); ok {
oracleConn.Pop = pop.(string)
}
if port, ok := d.GetOk("port"); ok {
oracleConn.Port = port.(string)
}
if zone, ok := d.GetOk("zone"); ok {
oracleConn.Zone = zone.(string)
}
if vlan, ok := d.GetOk("vlan"); ok {
oracleConn.Vlan = vlan.(int)
}
if srcSvlan, ok := d.GetOk("src_svlan"); ok {
oracleConn.SrcSvlan = srcSvlan.(int)
}
if pubQuoteLineUUID, ok := d.GetOk("published_quote_line_uuid"); ok {
oracleConn.PublishedQuoteLineUUID = pubQuoteLineUUID.(string)
}
if poNumber, ok := d.GetOk("po_number"); ok {
oracleConn.PONumber = poNumber.(string)
}
return oracleConn
}