/
resource_ibm_compute_ssl_certificate.go
218 lines (180 loc) · 5.78 KB
/
resource_ibm_compute_ssl_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
package ibm
import (
"fmt"
"log"
"strconv"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/softlayer/softlayer-go/datatypes"
"github.com/softlayer/softlayer-go/services"
"github.com/softlayer/softlayer-go/sl"
)
func resourceIBMComputeSSLCertificate() *schema.Resource {
return &schema.Resource{
Create: resourceIBMComputeSSLCertificateCreate,
Read: resourceIBMComputeSSLCertificateRead,
Update: resourceIBMComputeSSLCertificateUpdate,
Delete: resourceIBMComputeSSLCertificateDelete,
Exists: resourceIBMComputeSSLCertificateExists,
Importer: &schema.ResourceImporter{},
Schema: map[string]*schema.Schema{
"certificate": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
StateFunc: normalizeCert,
Description: "SSL Certifcate",
},
"intermediate_certificate": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
StateFunc: normalizeCert,
Description: "Intermediate certificate value",
},
"private_key": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
Sensitive: true,
StateFunc: normalizeCert,
Description: "SSL Private Key",
},
"common_name": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "Common name",
},
"organization_name": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "Organization name",
},
"validity_begin": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "Validity begins from",
},
"validity_days": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
Description: "Validity days",
},
"validity_end": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "Validity ends before",
},
"key_size": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
Description: "SSL key size",
},
"create_date": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "certificate creation date",
},
"modify_date": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "certificate modificatiob date",
},
"tags": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
Description: "Tags set for resource",
},
},
}
}
func resourceIBMComputeSSLCertificateCreate(d *schema.ResourceData, meta interface{}) error {
sess := meta.(ClientSession).SoftLayerSession()
service := services.GetSecurityCertificateService(sess.SetRetries(0))
template := datatypes.Security_Certificate{
Certificate: sl.String(d.Get("certificate").(string)),
IntermediateCertificate: sl.String(d.Get("intermediate_certificate").(string)),
PrivateKey: sl.String(d.Get("private_key").(string)),
}
log.Printf("[INFO] Creating Security Certificate")
cert, err := service.CreateObject(&template)
if err != nil {
return fmt.Errorf("Error creating Security Certificate: %s", err)
}
d.SetId(fmt.Sprintf("%d", *cert.Id))
return resourceIBMComputeSSLCertificateRead(d, meta)
}
func resourceIBMComputeSSLCertificateRead(d *schema.ResourceData, meta interface{}) error {
sess := meta.(ClientSession).SoftLayerSession()
service := services.GetSecurityCertificateService(sess)
id, err := strconv.Atoi(d.Id())
if err != nil {
return fmt.Errorf("Not a valid ID, must be an integer: %s", err)
}
cert, err := service.Id(id).GetObject()
if err != nil {
return fmt.Errorf("Unable to get Security Certificate: %s", err)
}
d.SetId(fmt.Sprintf("%d", *cert.Id))
d.Set("certificate", *cert.Certificate)
if cert.IntermediateCertificate != nil {
d.Set("intermediate_certificate", *cert.IntermediateCertificate)
}
if cert.PrivateKey != nil {
d.Set("private_key", *cert.PrivateKey)
}
d.Set("common_name", *cert.CommonName)
d.Set("organization_name", *cert.OrganizationName)
d.Set("validity_begin", *cert.ValidityBegin)
d.Set("validity_days", *cert.ValidityDays)
d.Set("validity_end", *cert.ValidityEnd)
d.Set("key_size", *cert.KeySize)
d.Set("create_date", *cert.CreateDate)
d.Set("modify_date", *cert.ModifyDate)
return nil
}
func resourceIBMComputeSSLCertificateUpdate(d *schema.ResourceData, meta interface{}) error {
//Only tags are updated and that too locally hence nothing to validate and update in terms of real API at this point
return nil
}
func resourceIBMComputeSSLCertificateDelete(d *schema.ResourceData, meta interface{}) error {
sess := meta.(ClientSession).SoftLayerSession()
service := services.GetSecurityCertificateService(sess)
id, err := strconv.Atoi(d.Id())
_, err = service.Id(id).DeleteObject()
if err != nil {
return fmt.Errorf("Error deleting Security Certificate %d: %s", id, err)
}
return nil
}
func resourceIBMComputeSSLCertificateExists(d *schema.ResourceData, meta interface{}) (bool, error) {
sess := meta.(ClientSession).SoftLayerSession()
service := services.GetSecurityCertificateService(sess)
id, err := strconv.Atoi(d.Id())
if err != nil {
return false, fmt.Errorf("Not a valid ID, must be an integer: %s", err)
}
cert, err := service.Id(id).GetObject()
if err != nil {
if apiErr, ok := err.(sl.Error); ok {
if apiErr.StatusCode == 404 {
return false, nil
}
}
return false, fmt.Errorf("Error communicating with the API: %s", err)
}
return cert.Id != nil && *cert.Id == id, nil
}
func normalizeCert(cert interface{}) string {
if cert == nil || cert == (*string)(nil) {
return ""
}
switch cert.(type) {
case string:
return strings.TrimSpace(cert.(string))
default:
return ""
}
}