Skip to content

Commit d423005

Browse files
committed
refactor(object/diff): using customize diff to refresh origin values
1 parent 56a4d22 commit d423005

9 files changed

+966
-223
lines changed

huaweicloud/services/evs/resource_huaweicloud_evs_snapshot.go

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ import (
1818
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils"
1919
)
2020

21+
var snapshotDiffParamKeys = []string{
22+
"metadata",
23+
}
24+
2125
// @API EVS DELETE /v2/{project_id}/cloudsnapshots/{snapshot_id}
2226
// @API EVS GET /v2/{project_id}/cloudsnapshots/{snapshot_id}
2327
// @API EVS PUT /v2/{project_id}/cloudsnapshots/{snapshot_id}
@@ -32,6 +36,10 @@ func ResourceEvsSnapshot() *schema.Resource {
3236
StateContext: schema.ImportStatePassthroughContext,
3337
},
3438

39+
CustomizeDiff: utils.ComposeAnyCustomDiffFunc(
40+
utils.RefreshObjectParamOriginValues(snapshotDiffParamKeys),
41+
),
42+
3543
Timeouts: &schema.ResourceTimeout{
3644
Create: schema.DefaultTimeout(10 * time.Minute),
3745
Delete: schema.DefaultTimeout(3 * time.Minute),
@@ -172,13 +180,10 @@ func waitingForSnapshotStatusAvailable(ctx context.Context, client *golangsdk.Se
172180

173181
func resourceEvsSnapshotCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
174182
var (
175-
cfg = meta.(*config.Config)
176-
region = cfg.GetRegion(d)
177-
httpUrl = "v2/{project_id}/cloudsnapshots"
178-
product = "evs"
179-
mapParamKeys = []string{
180-
"metadata",
181-
}
183+
cfg = meta.(*config.Config)
184+
region = cfg.GetRegion(d)
185+
httpUrl = "v2/{project_id}/cloudsnapshots"
186+
product = "evs"
182187
)
183188
client, err := cfg.NewServiceClient(product, region)
184189
if err != nil {
@@ -212,15 +217,6 @@ func resourceEvsSnapshotCreate(ctx context.Context, d *schema.ResourceData, meta
212217
return diag.Errorf("error waiting for EVS snapshot (%s) creation to available: %s", d.Id(), err)
213218
}
214219

215-
// If the request is successful, obtain the values of all JSON|object parameters first and save them to the
216-
// corresponding '_origin' attributes for subsequent determination and construction of the request body during
217-
// next updates.
218-
// And whether corresponding parameters are changed, the origin values must be refreshed.
219-
err = utils.RefreshObjectParamOriginValues(d, mapParamKeys)
220-
if err != nil {
221-
return diag.Errorf("unable to refresh the origin values: %s", err)
222-
}
223-
224220
return resourceEvsSnapshotRead(ctx, d, meta)
225221
}
226222

@@ -269,13 +265,10 @@ func buildUpdateSnapshotBodyParams(d *schema.ResourceData) map[string]interface{
269265

270266
func resourceEvsSnapshotUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
271267
var (
272-
cfg = meta.(*config.Config)
273-
region = cfg.GetRegion(d)
274-
httpUrl = "v2/{project_id}/cloudsnapshots/{snapshot_id}"
275-
product = "evs"
276-
mapParamKeys = []string{
277-
"metadata",
278-
}
268+
cfg = meta.(*config.Config)
269+
region = cfg.GetRegion(d)
270+
httpUrl = "v2/{project_id}/cloudsnapshots/{snapshot_id}"
271+
product = "evs"
279272
)
280273
client, err := cfg.NewServiceClient(product, region)
281274
if err != nil {
@@ -295,15 +288,6 @@ func resourceEvsSnapshotUpdate(ctx context.Context, d *schema.ResourceData, meta
295288
return diag.Errorf("error updating EVS snapshot: %s", err)
296289
}
297290

298-
// If the request is successful, obtain the values of all JSON|object parameters first and save them to the
299-
// corresponding '_origin' attributes for subsequent determination and construction of the request body during
300-
// next updates.
301-
// And whether corresponding parameters are changed, the origin values must be refreshed.
302-
err = utils.RefreshObjectParamOriginValues(d, mapParamKeys)
303-
if err != nil {
304-
return diag.Errorf("unable to refresh the origin values: %s", err)
305-
}
306-
307291
return resourceEvsSnapshotRead(ctx, d, meta)
308292
}
309293

huaweicloud/services/evs/resource_huaweicloud_evsv3_snapshot.go

Lines changed: 21 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,15 @@ import (
1919
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils"
2020
)
2121

22-
var v3SnapshotNonUpdatableParams = []string{"volume_id", "metadata"}
22+
var (
23+
v3SnapshotDiffParamKeys = []string{
24+
"metadata",
25+
}
26+
v3SnapshotNonUpdatableParams = []string{
27+
"volume_id",
28+
"metadata",
29+
}
30+
)
2331

2432
// @API EVS POST /v3/{project_id}/snapshots
2533
// @API EVS GET /v3/{project_id}/snapshots/{snapshot_id}
@@ -40,7 +48,10 @@ func ResourceV3Snapshot() *schema.Resource {
4048
Delete: schema.DefaultTimeout(3 * time.Minute),
4149
},
4250

43-
CustomizeDiff: config.FlexibleForceNew(v3SnapshotNonUpdatableParams),
51+
CustomizeDiff: utils.ComposeAnyCustomDiffFunc(
52+
utils.RefreshObjectParamOriginValues(v3SnapshotDiffParamKeys),
53+
config.FlexibleForceNew(v3SnapshotDiffParamKeys),
54+
),
4455

4556
Schema: map[string]*schema.Schema{
4657
"region": {
@@ -184,13 +195,10 @@ func waitingForV3SnapshotStatusAvailable(ctx context.Context, client *golangsdk.
184195

185196
func resourceV3SnapshotCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
186197
var (
187-
cfg = meta.(*config.Config)
188-
region = cfg.GetRegion(d)
189-
httpUrl = "v3/{project_id}/snapshots"
190-
product = "evs"
191-
mapParamKeys = []string{
192-
"metadata",
193-
}
198+
cfg = meta.(*config.Config)
199+
region = cfg.GetRegion(d)
200+
httpUrl = "v3/{project_id}/snapshots"
201+
product = "evs"
194202
)
195203

196204
client, err := cfg.NewServiceClient(product, region)
@@ -226,15 +234,6 @@ func resourceV3SnapshotCreate(ctx context.Context, d *schema.ResourceData, meta
226234
return diag.Errorf("error waiting for EVS v3 snapshot (%s) creation to available: %s", snapshotID, err)
227235
}
228236

229-
// If the request is successful, obtain the values of all JSON|object parameters first and save them to the
230-
// corresponding '_origin' attributes for subsequent determination and construction of the request body during
231-
// next updates.
232-
// And whether corresponding parameters are changed, the origin values must be refreshed.
233-
err = utils.RefreshObjectParamOriginValues(d, mapParamKeys)
234-
if err != nil {
235-
return diag.Errorf("unable to refresh the origin values: %s", err)
236-
}
237-
238237
return resourceV3SnapshotRead(ctx, d, meta)
239238
}
240239

@@ -284,13 +283,10 @@ func buildUpdateV3SnapshotBodyParams(d *schema.ResourceData) map[string]interfac
284283

285284
func resourceV3SnapshotUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
286285
var (
287-
cfg = meta.(*config.Config)
288-
region = cfg.GetRegion(d)
289-
httpUrl = "v3/{project_id}/snapshots/{snapshot_id}"
290-
product = "evs"
291-
mapParamKeys = []string{
292-
"metadata",
293-
}
286+
cfg = meta.(*config.Config)
287+
region = cfg.GetRegion(d)
288+
httpUrl = "v3/{project_id}/snapshots/{snapshot_id}"
289+
product = "evs"
294290
)
295291

296292
client, err := cfg.NewServiceClient(product, region)
@@ -311,15 +307,6 @@ func resourceV3SnapshotUpdate(ctx context.Context, d *schema.ResourceData, meta
311307
return diag.Errorf("error updating EVS v3 snapshot: %s", err)
312308
}
313309

314-
// If the request is successful, obtain the values of all JSON|object parameters first and save them to the
315-
// corresponding '_origin' attributes for subsequent determination and construction of the request body during
316-
// next updates.
317-
// And whether corresponding parameters are changed, the origin values must be refreshed.
318-
err = utils.RefreshObjectParamOriginValues(d, mapParamKeys)
319-
if err != nil {
320-
return diag.Errorf("unable to refresh the origin values: %s", err)
321-
}
322-
323310
return resourceV3SnapshotRead(ctx, d, meta)
324311
}
325312

huaweicloud/services/fgs/resource_huaweicloud_fgs_function.go

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ import (
2121
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils"
2222
)
2323

24+
var functionDiffObjectParamKeys = []string{
25+
"lts_custom_tag",
26+
}
27+
2428
// @API FunctionGraph POST /v2/{project_id}/fgs/functions
2529
// @API FunctionGraph PUT /v2/{project_id}/fgs/functions/{function_urn}/config
2630
// @API FunctionGraph PUT /v2/{project_id}/fgs/functions/{function_urn}/config-max-instance
@@ -43,6 +47,11 @@ func ResourceFgsFunction() *schema.Resource {
4347
ReadContext: resourceFunctionRead,
4448
UpdateContext: resourceFunctionUpdate,
4549
DeleteContext: resourceFunctionDelete,
50+
51+
CustomizeDiff: utils.ComposeAnyCustomDiffFunc(
52+
utils.RefreshObjectParamOriginValues(functionDiffObjectParamKeys),
53+
),
54+
4655
Importer: &schema.ResourceImporter{
4756
StateContext: schema.ImportStatePassthroughContext,
4857
},
@@ -1281,11 +1290,8 @@ func updateFunctionReservedInstances(client *golangsdk.ServiceClient, d *schema.
12811290

12821291
func resourceFunctionCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
12831292
var (
1284-
cfg = meta.(*config.Config)
1285-
region = cfg.GetRegion(d)
1286-
functionMetadataObjectParamKeys = []string{
1287-
"lts_custom_tag",
1288-
}
1293+
cfg = meta.(*config.Config)
1294+
region = cfg.GetRegion(d)
12891295
)
12901296

12911297
client, err := cfg.NewServiceClient("fgs", region)
@@ -1313,14 +1319,6 @@ func resourceFunctionCreate(ctx context.Context, d *schema.ResourceData, meta in
13131319
return diag.FromErr(err)
13141320
}
13151321
}
1316-
// If the request is successful, obtain the values of all JSON|object parameters first and save them to the
1317-
// corresponding '_origin' attributes for subsequent determination and construction of the request body during
1318-
// next updates.
1319-
// And whether corresponding parameters are changed, the origin values must be refreshed.
1320-
err = utils.RefreshObjectParamOriginValues(d, functionMetadataObjectParamKeys)
1321-
if err != nil {
1322-
return diag.Errorf("unable to refresh the origin values: %s", err)
1323-
}
13241322

13251323
if d.HasChange("depend_list") {
13261324
err := updateFunctionCode(client, d, funcUrnWithoutVersion)
@@ -1810,12 +1808,9 @@ func getFunctionLogConfiguration(client *golangsdk.ServiceClient, functionUrn st
18101808

18111809
func resourceFunctionUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
18121810
var (
1813-
cfg = meta.(*config.Config)
1814-
region = cfg.GetRegion(d)
1815-
funcUrnWithoutVersion = parseFunctionUrnWithoutVersion(d.Id())
1816-
functionMetadataObjectParamKeys = []string{
1817-
"lts_custom_tag",
1818-
}
1811+
cfg = meta.(*config.Config)
1812+
region = cfg.GetRegion(d)
1813+
funcUrnWithoutVersion = parseFunctionUrnWithoutVersion(d.Id())
18191814
)
18201815

18211816
client, err := cfg.NewServiceClient("fgs", region)
@@ -1848,14 +1843,6 @@ func resourceFunctionUpdate(ctx context.Context, d *schema.ResourceData, meta in
18481843
return diag.FromErr(err)
18491844
}
18501845
}
1851-
// If the request is successful, obtain the values ​​of all JSON|object parameters first and save them to the
1852-
// corresponding '_origin' attributes for subsequent determination and construction of the request body during
1853-
// next updates.
1854-
// And whether corresponding parameters are changed, the origin values must be refreshed.
1855-
err = utils.RefreshObjectParamOriginValues(d, functionMetadataObjectParamKeys)
1856-
if err != nil {
1857-
return diag.Errorf("unable to refresh the origin values: %s", err)
1858-
}
18591846

18601847
if d.HasChange("max_instance_num") {
18611848
// If the maximum number of instances is omitted (after type conversion, the value is zero), means this feature

huaweicloud/services/servicestage/resource_huaweicloud_servicestagev3_component.go

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,7 @@ import (
2121
)
2222

2323
var (
24-
v3ComponentNotFoundCodes = []string{
25-
"SVCSTG.00100401",
26-
}
27-
28-
componentJsonParamKeys = []string{
24+
v3ComponentDiffParamKeys = []string{
2925
"source",
3026
"build",
3127
"command",
@@ -35,6 +31,10 @@ var (
3531
"update_strategy",
3632
}
3733

34+
v3ComponentNotFoundCodes = []string{
35+
"SVCSTG.00100401",
36+
}
37+
3838
componentNonUpdatableParams = []string{
3939
"application_id",
4040
"environment_id",
@@ -64,7 +64,10 @@ func ResourceV3Component() *schema.Resource {
6464
StateContext: resourceV3ComponentImportState,
6565
},
6666

67-
CustomizeDiff: config.FlexibleForceNew(componentNonUpdatableParams),
67+
CustomizeDiff: utils.ComposeAnyCustomDiffFunc(
68+
utils.RefreshObjectParamOriginValues(v3ComponentDiffParamKeys),
69+
config.FlexibleForceNew(componentNonUpdatableParams),
70+
),
6871

6972
Timeouts: &schema.ResourceTimeout{
7073
Create: schema.DefaultTimeout(20 * time.Minute),
@@ -1126,14 +1129,6 @@ func resourceV3ComponentCreate(ctx context.Context, d *schema.ResourceData, meta
11261129
return diag.FromErr(err)
11271130
}
11281131

1129-
// If the request is successful, obtain the values ​​of all JSON parameters first and save them to the
1130-
// corresponding '_origin' attributes for subsequent determination and construction of the request body during
1131-
// next updates.
1132-
err = utils.RefreshObjectParamOriginValues(d, componentJsonParamKeys)
1133-
if err != nil {
1134-
return diag.Errorf("unable to refresh the origin values: %s", err)
1135-
}
1136-
11371132
return resourceV3ComponentRead(ctx, d, meta)
11381133
}
11391134

@@ -1593,14 +1588,6 @@ func resourceV3ComponentUpdate(ctx context.Context, d *schema.ResourceData, meta
15931588
return diag.FromErr(err)
15941589
}
15951590

1596-
// If the request is successful, obtain the values ​​of all JSON parameters first and save them to the
1597-
// corresponding '_origin' attributes for subsequent determination and construction of the request body during
1598-
// next updates.
1599-
err = utils.RefreshObjectParamOriginValues(d, componentJsonParamKeys)
1600-
if err != nil {
1601-
return diag.Errorf("unable to refresh the origin values: %s", err)
1602-
}
1603-
16041591
return resourceV3ComponentRead(ctx, d, meta)
16051592
}
16061593

huaweicloud/services/servicestage/resource_huaweicloud_servicestagev3_runtime_stack.go

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ import (
1818
)
1919

2020
var (
21-
runtimeStackJsonObjectParamKeys = []string{
21+
v3RuntimeStackDiffParamKeys = []string{
2222
"spec",
2323
}
24+
2425
runtimeStackNonUpdatableParams = []string{
2526
"deploy_mode",
2627
"type",
@@ -43,7 +44,10 @@ func ResourceV3RuntimeStack() *schema.Resource {
4344
StateContext: resourceV3RuntimeStackImportState,
4445
},
4546

46-
CustomizeDiff: config.FlexibleForceNew(runtimeStackNonUpdatableParams),
47+
CustomizeDiff: utils.ComposeAnyCustomDiffFunc(
48+
utils.RefreshObjectParamOriginValues(v3RuntimeStackDiffParamKeys),
49+
config.FlexibleForceNew(runtimeStackNonUpdatableParams),
50+
),
4751

4852
Schema: map[string]*schema.Schema{
4953
"region": {
@@ -181,14 +185,6 @@ func resourceV3RuntimeStackCreate(ctx context.Context, d *schema.ResourceData, m
181185
}
182186
d.SetId(runtimeStackId)
183187

184-
// If the request is successful, obtain the values ​​of all JSON parameters first and save them to the
185-
// corresponding '_origin' attributes for subsequent determination and construction of the request body during
186-
// next updates.
187-
err = utils.RefreshObjectParamOriginValues(d, runtimeStackJsonObjectParamKeys)
188-
if err != nil {
189-
return diag.Errorf("unable to refresh the origin values: %s", err)
190-
}
191-
192188
return resourceV3RuntimeStackRead(ctx, d, meta)
193189
}
194190

@@ -288,14 +284,6 @@ func resourceV3RuntimeStackUpdate(ctx context.Context, d *schema.ResourceData, m
288284
return diag.Errorf("error updating runtime stack (%s): %s", runtimeStackId, err)
289285
}
290286

291-
// If the request is successful, obtain the values ​​of all JSON parameters first and save them to the
292-
// corresponding '_origin' attributes for subsequent determination and construction of the request body during
293-
// next updates.
294-
err = utils.RefreshObjectParamOriginValues(d, runtimeStackJsonObjectParamKeys)
295-
if err != nil {
296-
return diag.Errorf("unable to refresh the origin values: %s", err)
297-
}
298-
299287
return resourceV3RuntimeStackRead(ctx, d, meta)
300288
}
301289

0 commit comments

Comments
 (0)