-
Notifications
You must be signed in to change notification settings - Fork 13
/
resource_ssl_offload_profile.go
313 lines (248 loc) · 10.1 KB
/
resource_ssl_offload_profile.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
package ddcloud
import (
"fmt"
"log"
"github.com/DimensionDataResearch/dd-cloud-compute-terraform/retry"
"github.com/DimensionDataResearch/go-dd-cloud-compute/compute"
"github.com/hashicorp/terraform/helper/schema"
)
const (
resourceKeySSLOffloadProfileNetworkDomainID = "networkdomain"
resourceKeySSLOffloadProfileName = "name"
resourceKeySSLOffloadProfileDescription = "description"
resourceKeySSLOffloadProfileCiphers = "ciphers"
resourceKeySSLOffloadProfileCertificateID = "certificate"
resourceKeySSLOffloadProfileChainID = "chain"
)
func resourceSSLOffloadProfile() *schema.Resource {
return &schema.Resource{
Exists: resourceSSLOffloadProfileExists,
Create: resourceSSLOffloadProfileCreate,
Read: resourceSSLOffloadProfileRead,
Update: resourceSSLOffloadProfileUpdate,
Delete: resourceSSLOffloadProfileDelete,
Importer: &schema.ResourceImporter{
State: resourceSSLOffloadProfileImport,
},
Schema: map[string]*schema.Schema{
resourceKeySSLOffloadProfileNetworkDomainID: &schema.Schema{
Type: schema.TypeString,
ForceNew: true,
Required: true,
Description: "The Id of the network domain in which the SSL-offload profile will be create.",
},
resourceKeySSLOffloadProfileName: &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "The name of the SSL-offload profile.",
},
resourceKeySSLOffloadProfileDescription: &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: nil,
Description: "A description of the SSL-offload profile.",
},
resourceKeySSLOffloadProfileCertificateID: &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "The Id of the SSL domain certificate to use.",
},
resourceKeySSLOffloadProfileChainID: &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "The Id of the SSL certificate chain to use.",
},
resourceKeySSLOffloadProfileCiphers: &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "The SSL ciphers use.",
},
},
}
}
// Check if a ddcloud_ssl_domain_certificate resource exists.
func resourceSSLOffloadProfileExists(data *schema.ResourceData, provider interface{}) (bool, error) {
id := data.Id()
log.Printf("Check if SSL-offload profile '%s' exists.", id)
providerState := provider.(*providerState)
apiClient := providerState.Client()
sslOffloadProfile, err := apiClient.GetSSLOffloadProfile(id)
if err != nil {
return false, err
}
exists := sslOffloadProfile != nil
log.Printf("SSL-offload profile '%s' exists: %t.", id, exists)
return exists, nil
}
// Create a ddcloud_ssl_domain_certificate resource.
func resourceSSLOffloadProfileCreate(data *schema.ResourceData, provider interface{}) error {
var err error
propertyHelper := propertyHelper(data)
networkDomainID := data.Get(resourceKeySSLOffloadProfileNetworkDomainID).(string)
name := data.Get(resourceKeySSLOffloadProfileName).(string)
description := data.Get(resourceKeySSLOffloadProfileDescription).(string)
certificateID := data.Get(resourceKeySSLOffloadProfileCertificateID).(string)
chainID := data.Get(resourceKeySSLOffloadProfileChainID).(string)
ciphers := propertyHelper.GetOptionalString(resourceKeySSLOffloadProfileCiphers, false)
log.Printf("Create SSL-offload profile '%s' in network domain '%s'.", name, networkDomainID)
providerState := provider.(*providerState)
apiClient := providerState.Client()
var (
sslOffloadProfileID string
createError error
)
operationDescription := fmt.Sprintf("Create SSL-offload profile '%s' in network domain '%s'.", name, networkDomainID)
err = providerState.RetryAction(operationDescription, func(context retry.Context) {
// CloudControl has issues if more than one asynchronous operation is initated at a time (returns UNEXPECTED_ERROR).
asyncLock := providerState.AcquireAsyncOperationLock(operationDescription)
defer asyncLock.Release() // Released at the end of the current attempt.
sslOffloadProfileID, createError = apiClient.CreateSSLOffloadProfile(networkDomainID, name, description, ciphers, certificateID, chainID)
if createError != nil {
if compute.IsResourceBusyError(createError) {
context.Retry()
} else {
context.Fail(createError)
}
}
})
if err != nil {
return err
}
data.SetId(sslOffloadProfileID)
log.Printf("Successfully created SSL-offload profile '%s'.", sslOffloadProfileID)
sslOffloadProfile, err := apiClient.GetSSLOffloadProfile(sslOffloadProfileID)
if err != nil {
return err
}
if sslOffloadProfile == nil {
return fmt.Errorf("cannot find newly-added SSL-offload profile '%s'", sslOffloadProfileID)
}
data.Set(resourceKeySSLOffloadProfileCiphers, sslOffloadProfile.Ciphers)
return nil
}
// Read a ddcloud_ssl_domain_certificate resource.
func resourceSSLOffloadProfileRead(data *schema.ResourceData, provider interface{}) error {
id := data.Id()
networkDomainID := data.Get(resourceKeySSLOffloadProfileNetworkDomainID).(string)
name := data.Get(resourceKeySSLOffloadProfileName).(string)
log.Printf("Read SSL-offload profile '%s' ('%s') in network domain '%s'.", name, id, networkDomainID)
apiClient := provider.(*providerState).Client()
sslOffloadProfile, err := apiClient.GetSSLOffloadProfile(id)
if err != nil {
return err
}
if sslOffloadProfile == nil {
data.SetId("") // SSL-offload profile has been deleted
return nil
}
data.Set(resourceKeySSLOffloadProfileCiphers, sslOffloadProfile.Ciphers)
return nil
}
// Update a ddcloud_ssl_domain_certificate resource.
func resourceSSLOffloadProfileUpdate(data *schema.ResourceData, provider interface{}) error {
id := data.Id()
networkDomainID := data.Get(resourceKeySSLOffloadProfileNetworkDomainID).(string)
name := data.Get(resourceKeySSLOffloadProfileName).(string)
log.Printf("Update SSL-offload profile '%s' ('%s') in network domain '%s'.", name, id, networkDomainID)
hasChange := false
providerState := provider.(*providerState)
apiClient := providerState.Client()
sslOffloadProfile, err := apiClient.GetSSLOffloadProfile(id)
if err != nil {
return err
}
if sslOffloadProfile == nil {
data.SetId("") // SSL-offload profile has been deleted
return nil
}
if data.HasChange(resourceKeySSLOffloadProfileName) {
sslOffloadProfile.Name = data.Get(resourceKeySSLOffloadProfileName).(string)
hasChange = true
}
if data.HasChange(resourceKeySSLOffloadProfileDescription) {
sslOffloadProfile.Description = data.Get(resourceKeySSLOffloadProfileDescription).(string)
hasChange = true
}
if data.HasChange(resourceKeySSLOffloadProfileCertificateID) {
sslOffloadProfile.SSLDomainCertificate.ID = data.Get(resourceKeySSLOffloadProfileCertificateID).(string)
hasChange = true
}
if data.HasChange(resourceKeySSLOffloadProfileChainID) {
sslOffloadProfile.SSLCertificateChain.ID = data.Get(resourceKeySSLOffloadProfileChainID).(string)
hasChange = true
}
if data.HasChange(resourceKeySSLOffloadProfileCiphers) {
sslOffloadProfile.Ciphers = data.Get(resourceKeySSLOffloadProfileCiphers).(string)
hasChange = true
}
if !hasChange {
return nil
}
var editError error
operationDescription := fmt.Sprintf("Create SSL-offload profile '%s' in network domain '%s'.", name, networkDomainID)
err = providerState.RetryAction(operationDescription, func(context retry.Context) {
// CloudControl has issues if more than one asynchronous operation is initated at a time (returns UNEXPECTED_ERROR).
asyncLock := providerState.AcquireAsyncOperationLock(operationDescription)
defer asyncLock.Release() // Released at the end of the current attempt.
editError = apiClient.EditSSLOffloadProfile(*sslOffloadProfile)
if editError != nil {
if compute.IsResourceBusyError(editError) {
context.Retry()
} else {
context.Fail(editError)
}
}
})
if err != nil {
return err
}
return nil
}
// Delete a ddcloud_ssl_domain_certificate resource.
func resourceSSLOffloadProfileDelete(data *schema.ResourceData, provider interface{}) error {
id := data.Id()
networkDomainID := data.Get(resourceKeySSLOffloadProfileNetworkDomainID).(string)
name := data.Get(resourceKeySSLOffloadProfileName).(string)
log.Printf("Delete SSL-offload profile '%s' ('%s') in network domain '%s' (nothing to do).", name, id, networkDomainID)
providerState := provider.(*providerState)
apiClient := providerState.Client()
operationDescription := fmt.Sprintf("Delete SSL-offload profile '%s", id)
return providerState.RetryAction(operationDescription, func(context retry.Context) {
// CloudControl has issues if more than one asynchronous operation is initated at a time (returns UNEXPECTED_ERROR).
asyncLock := providerState.AcquireAsyncOperationLock(operationDescription)
defer asyncLock.Release() // Released at the end of the current attempt.
err := apiClient.DeleteSSLOffloadProfile(id)
if err != nil {
if compute.IsResourceBusyError(err) {
context.Retry()
} else {
context.Fail(err)
}
}
})
}
// Import data for an existing SSL-offload profile.
func resourceSSLOffloadProfileImport(data *schema.ResourceData, provider interface{}) (importedData []*schema.ResourceData, err error) {
providerState := provider.(*providerState)
apiClient := providerState.Client()
id := data.Id()
log.Printf("Import SSL-offload profile '%s'.", id)
var sslOffloadProfile *compute.SSLOffloadProfile
sslOffloadProfile, err = apiClient.GetSSLOffloadProfile(id)
if err != nil {
return
}
if sslOffloadProfile == nil {
err = fmt.Errorf("SSL-offload profile '%s' not found", id)
return
}
data.Set(resourceKeySSLOffloadProfileNetworkDomainID, sslOffloadProfile.NetworkDomainID)
data.Set(resourceKeySSLOffloadProfileName, sslOffloadProfile.Name)
data.Set(resourceKeySSLOffloadProfileDescription, sslOffloadProfile.Description)
data.Set(resourceKeySSLOffloadProfileCiphers, sslOffloadProfile.Ciphers)
data.Set(resourceKeySSLOffloadProfileCertificateID, sslOffloadProfile.SSLDomainCertificate.ID)
data.Set(resourceKeySSLOffloadProfileChainID, sslOffloadProfile.SSLCertificateChain.ID)
importedData = []*schema.ResourceData{data}
return
}