/
resource_huaweicloud_kms_dedicated_keystore.go
199 lines (174 loc) · 5.99 KB
/
resource_huaweicloud_kms_dedicated_keystore.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
// ---------------------------------------------------------------
// *** AUTO GENERATED CODE ***
// @Product DEW
// ---------------------------------------------------------------
package dew
import (
"context"
"encoding/json"
"errors"
"strings"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/jmespath/go-jmespath"
"github.com/chnsz/golangsdk"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/common"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils"
)
// @API DEW POST /v1.0/{project_id}/keystores
// @API DEW GET /v1.0/{project_id}/keystores/{keystore_id}
// @API DEW DELETE /v1.0/{project_id}/keystores/{keystore_id}
func ResourceKmsDedicatedKeystore() *schema.Resource {
return &schema.Resource{
CreateContext: resourceKmsDedicatedKeystoreCreate,
ReadContext: resourceKmsDedicatedKeystoreRead,
DeleteContext: resourceKmsDedicatedKeystoreDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"alias": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: `Specifies the alias of a dedicated keystore.`,
},
"hsm_cluster_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: `Specifies the ID of a dedicated HSM cluster that has no dedicated keystore.`,
},
"hsm_ca_cert": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: `Specifies the CA certificate of the dedicated HSM cluster.`,
},
},
}
}
func resourceKmsDedicatedKeystoreCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var (
cfg = meta.(*config.Config)
region = cfg.GetRegion(d)
httpUrl = "v1.0/{project_id}/keystores"
product = "kms"
)
client, err := cfg.NewServiceClient(product, region)
if err != nil {
return diag.Errorf("error creating KMS client: %s", err)
}
createPath := client.Endpoint + httpUrl
createPath = strings.ReplaceAll(createPath, "{project_id}", client.ProjectID)
createOpt := golangsdk.RequestOpts{
KeepResponseBody: true,
MoreHeaders: map[string]string{"Content-Type": "application/json;charset=UTF-8"},
JSONBody: buildCreateDedicatedKeystoreBodyParams(d),
}
createResp, err := client.Request("POST", createPath, &createOpt)
if err != nil {
return diag.Errorf("error creating KMS dedicated keystore: %s", err)
}
createRespBody, err := utils.FlattenResponse(createResp)
if err != nil {
return diag.FromErr(err)
}
id, err := jmespath.Search("keystore.keystore_id", createRespBody)
if err != nil || id == nil {
return diag.Errorf("error creating KMS dedicated keystore: ID is not found in API response")
}
d.SetId(id.(string))
return resourceKmsDedicatedKeystoreRead(ctx, d, meta)
}
func buildCreateDedicatedKeystoreBodyParams(d *schema.ResourceData) map[string]interface{} {
return map[string]interface{}{
"keystore_alias": d.Get("alias"),
"hsm_cluster_id": d.Get("hsm_cluster_id"),
"hsm_ca_cert": d.Get("hsm_ca_cert"),
}
}
func resourceKmsDedicatedKeystoreRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var (
cfg = meta.(*config.Config)
region = cfg.GetRegion(d)
mErr *multierror.Error
httpUrl = "v1.0/{project_id}/keystores/{keystore_id}"
product = "kms"
)
client, err := cfg.NewServiceClient(product, region)
if err != nil {
return diag.Errorf("error creating KMS client: %s", err)
}
getPath := client.Endpoint + httpUrl
getPath = strings.ReplaceAll(getPath, "{project_id}", client.ProjectID)
getPath = strings.ReplaceAll(getPath, "{keystore_id}", d.Id())
getOpt := golangsdk.RequestOpts{
KeepResponseBody: true,
MoreHeaders: map[string]string{"Content-Type": "application/json;charset=UTF-8"},
}
getResp, err := client.Request("GET", getPath, &getOpt)
if err != nil {
return common.CheckDeletedDiag(d, parseErrorToError404(err), "error retrieving KMS dedicated keystore")
}
getRespBody, err := utils.FlattenResponse(getResp)
if err != nil {
return diag.FromErr(err)
}
mErr = multierror.Append(
mErr,
d.Set("region", region),
d.Set("alias", utils.PathSearch("keystore.keystore_alias", getRespBody, nil)),
d.Set("hsm_cluster_id", utils.PathSearch("keystore.hsm_cluster_id", getRespBody, nil)),
)
return diag.FromErr(mErr.ErrorOrNil())
}
func parseErrorToError404(err error) error {
var errCode golangsdk.ErrDefault400
if errors.As(err, &errCode) {
var apiError interface{}
if jsonErr := json.Unmarshal(errCode.Body, &apiError); jsonErr != nil {
return err
}
errorCode, errorCodeErr := jmespath.Search("error.error_code", apiError)
if errorCodeErr != nil {
return err
}
if errorCode == "KMS.8003" {
return golangsdk.ErrDefault404(errCode)
}
}
return err
}
func resourceKmsDedicatedKeystoreDelete(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var (
cfg = meta.(*config.Config)
region = cfg.GetRegion(d)
httpUrl = "v1.0/{project_id}/keystores/{keystore_id}"
product = "kms"
)
client, err := cfg.NewServiceClient(product, region)
if err != nil {
return diag.Errorf("error creating KMS client: %s", err)
}
deletePath := client.Endpoint + httpUrl
deletePath = strings.ReplaceAll(deletePath, "{project_id}", client.ProjectID)
deletePath = strings.ReplaceAll(deletePath, "{keystore_id}", d.Id())
deleteOpt := golangsdk.RequestOpts{
KeepResponseBody: true,
MoreHeaders: map[string]string{"Content-Type": "application/json;charset=UTF-8"},
}
_, err = client.Request("DELETE", deletePath, &deleteOpt)
if err != nil {
return diag.Errorf("error deleting KMS dedicated keystore: %s", err)
}
return nil
}