/
resource_huaweicloud_cfw_service_group.go
248 lines (213 loc) · 7.79 KB
/
resource_huaweicloud_cfw_service_group.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
// ---------------------------------------------------------------
// *** AUTO GENERATED CODE ***
// @Product CFW
// ---------------------------------------------------------------
package cfw
import (
"context"
"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 CFW POST /v1/{project_id}/service-set
// @API CFW DELETE /v1/{project_id}/service-sets/{id}
// @API CFW GET /v1/{project_id}/service-sets/{id}
// @API CFW PUT /v1/{project_id}/service-sets/{id}
func ResourceServiceGroup() *schema.Resource {
return &schema.Resource{
CreateContext: resourceServiceGroupCreate,
UpdateContext: resourceServiceGroupUpdate,
ReadContext: resourceServiceGroupRead,
DeleteContext: resourceServiceGroupDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"object_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: `Specifies the protected object ID.`,
},
"name": {
Type: schema.TypeString,
Required: true,
Description: `Specifies the service group name.`,
},
"description": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: `Specifies the service group description.`,
},
},
}
}
func resourceServiceGroupCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := meta.(*config.Config)
region := cfg.GetRegion(d)
// createServiceGroup: Create a CFW service group.
var (
createServiceGroupHttpUrl = "v1/{project_id}/service-set"
createServiceGroupProduct = "cfw"
)
createServiceGroupClient, err := cfg.NewServiceClient(createServiceGroupProduct, region)
if err != nil {
return diag.Errorf("error creating CFW Client: %s", err)
}
createServiceGroupPath := createServiceGroupClient.Endpoint + createServiceGroupHttpUrl
createServiceGroupPath = strings.ReplaceAll(createServiceGroupPath, "{project_id}",
createServiceGroupClient.ProjectID)
createServiceGroupOpt := golangsdk.RequestOpts{
KeepResponseBody: true,
OkCodes: []int{
200,
},
}
createServiceGroupOpt.JSONBody = utils.RemoveNil(buildCreateServiceGroupBodyParams(d))
createServiceGroupResp, err := createServiceGroupClient.Request("POST", createServiceGroupPath,
&createServiceGroupOpt)
if err != nil {
return diag.Errorf("error creating ServiceGroup: %s", err)
}
createServiceGroupRespBody, err := utils.FlattenResponse(createServiceGroupResp)
if err != nil {
return diag.FromErr(err)
}
id, err := jmespath.Search("data.id", createServiceGroupRespBody)
if err != nil {
return diag.Errorf("error creating ServiceGroup: ID is not found in API response")
}
d.SetId(id.(string))
return resourceServiceGroupRead(ctx, d, meta)
}
func buildCreateServiceGroupBodyParams(d *schema.ResourceData) map[string]interface{} {
bodyParams := map[string]interface{}{
"object_id": utils.ValueIngoreEmpty(d.Get("object_id")),
"name": utils.ValueIngoreEmpty(d.Get("name")),
"description": utils.ValueIngoreEmpty(d.Get("description")),
}
return bodyParams
}
func resourceServiceGroupRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := meta.(*config.Config)
region := cfg.GetRegion(d)
var mErr *multierror.Error
// getServiceGroup: Query the CFW service group detail
var (
getServiceGroupHttpUrl = "v1/{project_id}/service-sets/{id}"
getServiceGroupProduct = "cfw"
)
getServiceGroupClient, err := cfg.NewServiceClient(getServiceGroupProduct, region)
if err != nil {
return diag.Errorf("error creating CFW Client: %s", err)
}
getServiceGroupPath := getServiceGroupClient.Endpoint + getServiceGroupHttpUrl
getServiceGroupPath = strings.ReplaceAll(getServiceGroupPath, "{project_id}",
getServiceGroupClient.ProjectID)
getServiceGroupPath = strings.ReplaceAll(getServiceGroupPath, "{id}", d.Id())
getServiceGroupOpt := golangsdk.RequestOpts{
KeepResponseBody: true,
OkCodes: []int{
200,
},
}
getServiceGroupResp, err := getServiceGroupClient.Request("GET", getServiceGroupPath,
&getServiceGroupOpt)
if err != nil {
return common.CheckDeletedDiag(d, err, "error retrieving ServiceGroup")
}
getServiceGroupRespBody, err := utils.FlattenResponse(getServiceGroupResp)
if err != nil {
return diag.FromErr(err)
}
mErr = multierror.Append(
mErr,
d.Set("region", region),
d.Set("name", utils.PathSearch("data.name", getServiceGroupRespBody, nil)),
d.Set("description", utils.PathSearch("data.description", getServiceGroupRespBody, nil)),
)
return diag.FromErr(mErr.ErrorOrNil())
}
func resourceServiceGroupUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := meta.(*config.Config)
region := cfg.GetRegion(d)
updateServiceGroupChanges := []string{
"name",
"description",
}
if d.HasChanges(updateServiceGroupChanges...) {
// updateServiceGroup: Update the configuration of CFW service group
var (
updateServiceGroupHttpUrl = "v1/{project_id}/service-sets/{id}"
updateServiceGroupProduct = "cfw"
)
updateServiceGroupClient, err := cfg.NewServiceClient(updateServiceGroupProduct, region)
if err != nil {
return diag.Errorf("error creating CFW Client: %s", err)
}
updateServiceGroupPath := updateServiceGroupClient.Endpoint + updateServiceGroupHttpUrl
updateServiceGroupPath = strings.ReplaceAll(updateServiceGroupPath, "{project_id}",
updateServiceGroupClient.ProjectID)
updateServiceGroupPath = strings.ReplaceAll(updateServiceGroupPath, "{id}", d.Id())
updateServiceGroupOpt := golangsdk.RequestOpts{
KeepResponseBody: true,
OkCodes: []int{
200,
},
}
updateServiceGroupOpt.JSONBody = utils.RemoveNil(buildUpdateServiceGroupBodyParams(d))
_, err = updateServiceGroupClient.Request("PUT", updateServiceGroupPath, &updateServiceGroupOpt)
if err != nil {
return diag.Errorf("error updating ServiceGroup: %s", err)
}
}
return resourceServiceGroupRead(ctx, d, meta)
}
func buildUpdateServiceGroupBodyParams(d *schema.ResourceData) map[string]interface{} {
bodyParams := map[string]interface{}{
"name": utils.ValueIngoreEmpty(d.Get("name")),
"description": utils.ValueIngoreEmpty(d.Get("description")),
}
return bodyParams
}
func resourceServiceGroupDelete(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := meta.(*config.Config)
region := cfg.GetRegion(d)
// deleteServiceGroup: Delete an existing CFW service group
var (
deleteServiceGroupHttpUrl = "v1/{project_id}/service-sets/{id}"
deleteServiceGroupProduct = "cfw"
)
deleteServiceGroupClient, err := cfg.NewServiceClient(deleteServiceGroupProduct, region)
if err != nil {
return diag.Errorf("error creating CFW Client: %s", err)
}
deleteServiceGroupPath := deleteServiceGroupClient.Endpoint + deleteServiceGroupHttpUrl
deleteServiceGroupPath = strings.ReplaceAll(deleteServiceGroupPath, "{project_id}",
deleteServiceGroupClient.ProjectID)
deleteServiceGroupPath = strings.ReplaceAll(deleteServiceGroupPath, "{id}", d.Id())
deleteServiceGroupOpt := golangsdk.RequestOpts{
KeepResponseBody: true,
OkCodes: []int{
200,
},
}
_, err = deleteServiceGroupClient.Request("DELETE", deleteServiceGroupPath, &deleteServiceGroupOpt)
if err != nil {
return diag.Errorf("error deleting ServiceGroup: %s", err)
}
return nil
}