/
resource_edgecenter_secret.go
276 lines (240 loc) · 7.68 KB
/
resource_edgecenter_secret.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
package edgecenter
import (
"context"
"log"
"net/http"
"time"
"github.com/hashicorp/go-cty/cty"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
edgecloudV2 "github.com/Edge-Center/edgecentercloud-go/v2"
utilV2 "github.com/Edge-Center/edgecentercloud-go/v2/util"
)
const (
SecretDeletingTimeout = 1200 * time.Second
SecretCreatingTimeout = 1200 * time.Second
SecretPoint = "secrets"
// RFC3339NoZ is the time format used in Heat (Orchestration).
RFC3339NoZ = "2006-01-02T15:04:05"
RFC3339WithTimeZone = "2006-01-02T15:04:05+00:00"
)
func resourceSecret() *schema.Resource {
return &schema.Resource{
CreateContext: resourceSecretCreate,
ReadContext: resourceSecretRead,
DeleteContext: resourceSecretDelete,
Description: "Represent secret",
Importer: &schema.ResourceImporter{
StateContext: func(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
projectID, regionID, secretID, err := ImportStringParser(d.Id())
if err != nil {
return nil, err
}
d.Set("project_id", projectID)
d.Set("region_id", regionID)
d.SetId(secretID)
return []*schema.ResourceData{d}, nil
},
},
Schema: map[string]*schema.Schema{
"project_id": {
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
Description: "The uuid of the project. Either 'project_id' or 'project_name' must be specified.",
ExactlyOneOf: []string{"project_id", "project_name"},
},
"project_name": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: "The name of the project. Either 'project_id' or 'project_name' must be specified.",
ExactlyOneOf: []string{"project_id", "project_name"},
},
"region_id": {
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
Description: "The uuid of the region. Either 'region_id' or 'region_name' must be specified.",
ExactlyOneOf: []string{"region_id", "region_name"},
},
"region_name": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: "The name of the region. Either 'region_id' or 'region_name' must be specified.",
ExactlyOneOf: []string{"region_id", "region_name"},
},
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The name of the secret.",
},
"private_key": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "SSL private key in PEM format",
},
"certificate_chain": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "SSL certificate chain of intermediates and root certificates in PEM format",
},
"certificate": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "SSL certificate in PEM format",
},
"algorithm": {
Type: schema.TypeString,
Computed: true,
Description: "The encryption algorithm used for the secret.",
},
"bit_length": {
Type: schema.TypeInt,
Computed: true,
Description: "The bit length of the encryption algorithm.",
},
"mode": {
Type: schema.TypeString,
Computed: true,
Description: "The mode of the encryption algorithm.",
},
"status": {
Type: schema.TypeString,
Computed: true,
Description: "The current status of the secret.",
},
"content_types": {
Type: schema.TypeMap,
Computed: true,
Description: "The content types associated with the secret's payload.",
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"expiration": {
Type: schema.TypeString,
Description: "Datetime when the secret will expire. The format is 2025-12-28T19:14:44",
Optional: true,
Computed: true,
StateFunc: func(val interface{}) string {
expTime, _ := time.Parse(RFC3339NoZ, val.(string))
return expTime.Format(RFC3339NoZ)
},
ValidateDiagFunc: func(i interface{}, path cty.Path) diag.Diagnostics {
rawTime := i.(string)
_, err := time.Parse(RFC3339NoZ, rawTime)
if err != nil {
return diag.FromErr(err)
}
return nil
},
},
"created": {
Type: schema.TypeString,
Description: "Datetime when the secret was created. The format is 2025-12-28T19:14:44.180394",
Computed: true,
},
},
}
}
func resourceSecretCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
log.Println("[DEBUG] Start Secret creating")
var diags diag.Diagnostics
config := m.(*Config)
clientV2 := config.CloudClient
regionID, projectID, err := GetRegionIDandProjectID(ctx, clientV2, d)
if err != nil {
return diag.FromErr(err)
}
clientV2.Region = regionID
clientV2.Project = projectID
opts := &edgecloudV2.SecretCreateRequestV2{
Name: d.Get("name").(string),
Payload: edgecloudV2.Payload{
CertificateChain: d.Get("certificate_chain").(string),
Certificate: d.Get("certificate").(string),
PrivateKey: d.Get("private_key").(string),
},
}
if rawTime := d.Get("expiration").(string); rawTime != "" {
opts.Expiration = &rawTime
}
taskResult, err := utilV2.ExecuteAndExtractTaskResult(ctx, clientV2.Secrets.CreateV2, opts, clientV2, SecretCreatingTimeout)
if err != nil {
return diag.FromErr(err)
}
secretID := taskResult.Secrets[0]
log.Printf("[DEBUG] Secret id (%s)", secretID)
d.SetId(secretID)
resourceSecretRead(ctx, d, m)
log.Printf("[DEBUG] Finish Secret creating (%s)", secretID)
return diags
}
func resourceSecretRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
log.Println("[DEBUG] Start secret reading")
var diags diag.Diagnostics
config := m.(*Config)
clientV2 := config.CloudClient
secretID := d.Id()
log.Printf("[DEBUG] Secret id = %s", secretID)
regionID, projectID, err := GetRegionIDandProjectID(ctx, clientV2, d)
if err != nil {
return diag.FromErr(err)
}
clientV2.Region = regionID
clientV2.Project = projectID
secret, _, err := clientV2.Secrets.Get(ctx, secretID)
if err != nil {
return diag.Errorf("cannot get secret with ID: %s. Error: %s", secretID, err.Error())
}
expTime, _ := time.Parse(RFC3339WithTimeZone, secret.Expiration)
d.Set("name", secret.Name)
d.Set("algorithm", secret.Algorithm)
d.Set("bit_length", secret.BitLength)
d.Set("mode", secret.Mode)
d.Set("status", secret.Status)
d.Set("expiration", expTime.Format(RFC3339NoZ))
d.Set("created", secret.Created)
if err := d.Set("content_types", secret.ContentTypes); err != nil {
return diag.FromErr(err)
}
log.Println("[DEBUG] Finish secret reading")
return diags
}
func resourceSecretDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
log.Println("[DEBUG] Start secret deleting")
var diags diag.Diagnostics
config := m.(*Config)
clientV2 := config.CloudClient
secretID := d.Id()
log.Printf("[DEBUG] Secret id = %s", secretID)
regionID, projectID, err := GetRegionIDandProjectID(ctx, clientV2, d)
if err != nil {
return diag.FromErr(err)
}
clientV2.Region = regionID
clientV2.Project = projectID
results, resp, err := clientV2.Secrets.Delete(ctx, secretID)
if err != nil {
if resp.StatusCode == http.StatusNotFound {
d.SetId("")
log.Printf("[DEBUG] Finish of Secret deleting")
return diags
}
return diag.FromErr(err)
}
taskID := results.Tasks[0]
err = utilV2.WaitForTaskComplete(ctx, clientV2, taskID, SecretDeletingTimeout)
if err != nil {
return diag.FromErr(err)
}
d.SetId("")
log.Printf("[DEBUG] Finish of secret deleting")
return diags
}