-
Notifications
You must be signed in to change notification settings - Fork 13
/
resource_ssl_domain_certificate.go
230 lines (186 loc) · 7.71 KB
/
resource_ssl_domain_certificate.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
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 (
resourceKeySSLDomainCertificateNetworkDomainID = "networkdomain"
resourceKeySSLDomainCertificateName = "name"
resourceKeySSLDomainCertificateDescription = "description"
resourceKeySSLDomainCertificateCertificate = "certificate"
resourceKeySSLDomainCertificatePrivateKey = "private_key"
)
func resourceSSLDomainCertificate() *schema.Resource {
return &schema.Resource{
Exists: resourceSSLDomainCertificateExists,
Create: resourceSSLDomainCertificateCreate,
Read: resourceSSLDomainCertificateRead,
Update: resourceSSLDomainCertificateUpdate,
Delete: resourceSSLDomainCertificateDelete,
Importer: &schema.ResourceImporter{
State: resourceSSLDomainCertificateImport,
},
Schema: map[string]*schema.Schema{
resourceKeySSLDomainCertificateNetworkDomainID: &schema.Schema{
Type: schema.TypeString,
ForceNew: true,
Required: true,
Description: "The Id of the network domain in which the SSL domain certificate will be used for SSL offload.",
},
resourceKeySSLDomainCertificateName: &schema.Schema{
Type: schema.TypeString,
ForceNew: true,
Required: true,
Description: "The name of the SSL domain certificate.",
},
resourceKeySSLDomainCertificateDescription: &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: nil,
Description: "A description of the SSL domain certificate.",
},
resourceKeySSLDomainCertificateCertificate: &schema.Schema{
Type: schema.TypeString,
ForceNew: true,
Required: true,
Description: "The certificate (in PEM format).",
},
resourceKeySSLDomainCertificatePrivateKey: &schema.Schema{
Type: schema.TypeString,
ForceNew: true,
Required: true,
Sensitive: true,
Description: "The certificate's private key (in PEM format).",
},
},
}
}
// Check if a ddcloud_ssl_domain_certificate resource exists.
func resourceSSLDomainCertificateExists(data *schema.ResourceData, provider interface{}) (bool, error) {
id := data.Id()
log.Printf("Check if SSL domain certificate '%s' exists.", id)
providerState := provider.(*providerState)
apiClient := providerState.Client()
SSLDomainCertificate, err := apiClient.GetSSLDomainCertificate(id)
if err != nil {
return false, err
}
exists := SSLDomainCertificate != nil
log.Printf("SSL domain certificate '%s' exists: %t.", id, exists)
return exists, nil
}
// Create a ddcloud_ssl_domain_certificate resource.
func resourceSSLDomainCertificateCreate(data *schema.ResourceData, provider interface{}) error {
var err error
networkDomainID := data.Get(resourceKeySSLDomainCertificateNetworkDomainID).(string)
name := data.Get(resourceKeySSLDomainCertificateName).(string)
description := data.Get(resourceKeySSLDomainCertificateDescription).(string)
certificatePEM := data.Get(resourceKeySSLDomainCertificateCertificate).(string)
privateKeyPEM := data.Get(resourceKeySSLDomainCertificatePrivateKey).(string)
log.Printf("Create SSL domain certificate '%s' in network domain '%s'.", name, networkDomainID)
providerState := provider.(*providerState)
apiClient := providerState.Client()
var (
domainCertificateID string
createError error
)
operationDescription := fmt.Sprintf("Create SSL domain certificate '%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.
domainCertificateID, createError = apiClient.ImportSSLDomainCertificate(networkDomainID, name, description, certificatePEM, privateKeyPEM)
if createError != nil {
if compute.IsResourceBusyError(createError) {
context.Retry()
} else {
context.Fail(createError)
}
}
})
if err != nil {
return err
}
data.SetId(domainCertificateID)
log.Printf("Successfully created SSL domain certificate '%s'.", domainCertificateID)
domainCertificate, err := apiClient.GetSSLDomainCertificate(domainCertificateID)
if err != nil {
return err
}
if domainCertificate == nil {
return fmt.Errorf("cannot find newly-added SSL domain certificate '%s'", domainCertificateID)
}
return nil
}
// Read a ddcloud_ssl_domain_certificate resource.
func resourceSSLDomainCertificateRead(data *schema.ResourceData, provider interface{}) error {
id := data.Id()
networkDomainID := data.Get(resourceKeySSLDomainCertificateNetworkDomainID).(string)
name := data.Get(resourceKeySSLDomainCertificateName).(string)
log.Printf("Read SSL domain certificate '%s' ('%s') in network domain '%s'.", name, id, networkDomainID)
apiClient := provider.(*providerState).Client()
SSLDomainCertificate, err := apiClient.GetSSLDomainCertificate(id)
if err != nil {
return err
}
if SSLDomainCertificate == nil {
data.SetId("") // SSL domain certificate has been deleted
return nil
}
return nil
}
// Update a ddcloud_ssl_domain_certificate resource.
func resourceSSLDomainCertificateUpdate(data *schema.ResourceData, provider interface{}) error {
id := data.Id()
networkDomainID := data.Get(resourceKeySSLDomainCertificateNetworkDomainID).(string)
name := data.Get(resourceKeySSLDomainCertificateName).(string)
log.Printf("Update SSL domain certificate '%s' ('%s') in network domain '%s' (nothing to do).", name, id, networkDomainID)
return nil
}
// Delete a ddcloud_ssl_domain_certificate resource.
func resourceSSLDomainCertificateDelete(data *schema.ResourceData, provider interface{}) error {
id := data.Id()
networkDomainID := data.Get(resourceKeySSLDomainCertificateNetworkDomainID).(string)
name := data.Get(resourceKeySSLDomainCertificateName).(string)
log.Printf("Delete SSL domain certificate '%s' ('%s') in network domain '%s' (nothing to do).", name, id, networkDomainID)
providerState := provider.(*providerState)
apiClient := providerState.Client()
operationDescription := fmt.Sprintf("Delete SSL domain certificate '%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.DeleteSSLDomainCertificate(id)
if err != nil {
if compute.IsResourceBusyError(err) {
context.Retry()
} else {
context.Fail(err)
}
}
})
}
// Import data for an existing SSL domain certificate.
func resourceSSLDomainCertificateImport(data *schema.ResourceData, provider interface{}) (importedData []*schema.ResourceData, err error) {
providerState := provider.(*providerState)
apiClient := providerState.Client()
id := data.Id()
log.Printf("Import SSL domain certificate '%s'.", id)
var domainCertificate *compute.SSLDomainCertificate
domainCertificate, err = apiClient.GetSSLDomainCertificate(id)
if err != nil {
return
}
if domainCertificate == nil {
err = fmt.Errorf("SSL domain certificate '%s' not found", id)
return
}
data.Set(resourceKeySSLDomainCertificateNetworkDomainID, domainCertificate.NetworkDomainID)
data.Set(resourceKeySSLDomainCertificateName, domainCertificate.Name)
data.Set(resourceKeySSLDomainCertificateDescription, domainCertificate.Description)
importedData = []*schema.ResourceData{data}
return
}