Skip to content

Commit d8af992

Browse files
authored
feat(GaussDB): add gaussdb opengauss parameter template compare resource (#6126)
1 parent 24ec5ad commit d8af992

6 files changed

+283
-17
lines changed

docs/resources/gaussdb_mysql_parameter_template_compare.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ subcategory: "GaussDB(for MySQL)"
33
layout: "huaweicloud"
44
page_title: "HuaweiCloud: huaweicloud_gaussdb_mysql_parameter_template_compare"
55
description: |-
6-
Manages a GaussDB MySQL parameter template apply resource within HuaweiCloud.
6+
Manages a GaussDB MySQL parameter template compare resource within HuaweiCloud.
77
---
88

99
# huaweicloud_gaussdb_mysql_parameter_template_compare
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
---
2+
subcategory: "GaussDB"
3+
layout: "huaweicloud"
4+
page_title: "HuaweiCloud: huaweicloud_gaussdb_opengauss_parameter_template_compare"
5+
description: |-
6+
Manages a GaussDB OpenGauss parameter template compare resource within HuaweiCloud.
7+
---
8+
9+
# huaweicloud_gaussdb_opengauss_parameter_template_compare
10+
11+
Manages a GaussDB OpenGauss parameter template compare resource within HuaweiCloud.
12+
13+
## Example Usage
14+
15+
```hcl
16+
variable "source_id" {}
17+
variable "target_id" {}
18+
19+
resource "huaweicloud_gaussdb_opengauss_parameter_template_compare" "test" {
20+
source_id = var.source_id
21+
target_id = var.target_id
22+
}
23+
```
24+
25+
## Argument Reference
26+
27+
The following arguments are supported:
28+
29+
* `region` - (Optional, String, ForceNew) Specifies the region in which to create the resource.
30+
If omitted, the provider-level region will be used. Changing this parameter will create a new resource.
31+
32+
* `source_id` - (Required, String, ForceNew) Specifies the ID of the source parameter template to be compared. Changing
33+
this parameter will create a new resource.
34+
35+
* `target_id` - (Required, String, ForceNew) Specifies the ID of the destination parameter template to be compared.
36+
Changing this parameter will create a new resource.
37+
38+
## Attribute Reference
39+
40+
In addition to all arguments above, the following attributes are exported:
41+
42+
* `id` - The resource ID in format of `<source_id>/<target_id>`.
43+
44+
* `differences` - Indicates the differences between parameters.
45+
The [differences](#differences_struct) structure is documented below.
46+
47+
<a name="differences_struct"></a>
48+
The `differences` block supports:
49+
50+
* `name` - Indicates the parameter name.
51+
52+
* `source_value` - Indicates the parameter value in the source parameter template.
53+
54+
* `target_value` - Indicates the parameter value in the destination parameter template.

huaweicloud/provider.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1774,15 +1774,16 @@ func Provider() *schema.Provider {
17741774
"huaweicloud_gaussdb_mysql_scheduled_task_delete": taurusdb.ResourceGaussDBScheduledTaskDelete(),
17751775
"huaweicloud_gaussdb_mysql_instant_task_delete": taurusdb.ResourceGaussDBInstantTaskDelete(),
17761776

1777-
"huaweicloud_gaussdb_opengauss_instance": gaussdb.ResourceOpenGaussInstance(),
1778-
"huaweicloud_gaussdb_opengauss_instance_restart": gaussdb.ResourceOpenGaussInstanceRestart(),
1779-
"huaweicloud_gaussdb_opengauss_database": gaussdb.ResourceOpenGaussDatabase(),
1780-
"huaweicloud_gaussdb_opengauss_backup": gaussdb.ResourceGaussDBOpenGaussBackup(),
1781-
"huaweicloud_gaussdb_opengauss_backup_stop": gaussdb.ResourceOpenGaussBackupStop(),
1782-
"huaweicloud_gaussdb_opengauss_restore": gaussdb.ResourceOpenGaussRestore(),
1783-
"huaweicloud_gaussdb_opengauss_eip_associate": gaussdb.ResourceOpenGaussEipAssociate(),
1784-
"huaweicloud_gaussdb_opengauss_primary_standby_switch": gaussdb.ResourceOpenGaussPrimaryStandbySwitch(),
1785-
"huaweicloud_gaussdb_opengauss_parameter_template": gaussdb.ResourceOpenGaussParameterTemplate(),
1777+
"huaweicloud_gaussdb_opengauss_instance": gaussdb.ResourceOpenGaussInstance(),
1778+
"huaweicloud_gaussdb_opengauss_instance_restart": gaussdb.ResourceOpenGaussInstanceRestart(),
1779+
"huaweicloud_gaussdb_opengauss_database": gaussdb.ResourceOpenGaussDatabase(),
1780+
"huaweicloud_gaussdb_opengauss_backup": gaussdb.ResourceGaussDBOpenGaussBackup(),
1781+
"huaweicloud_gaussdb_opengauss_backup_stop": gaussdb.ResourceOpenGaussBackupStop(),
1782+
"huaweicloud_gaussdb_opengauss_restore": gaussdb.ResourceOpenGaussRestore(),
1783+
"huaweicloud_gaussdb_opengauss_eip_associate": gaussdb.ResourceOpenGaussEipAssociate(),
1784+
"huaweicloud_gaussdb_opengauss_primary_standby_switch": gaussdb.ResourceOpenGaussPrimaryStandbySwitch(),
1785+
"huaweicloud_gaussdb_opengauss_parameter_template": gaussdb.ResourceOpenGaussParameterTemplate(),
1786+
"huaweicloud_gaussdb_opengauss_parameter_template_compare": gaussdb.ResourceOpenGaussParameterTemplateCompare(),
17861787

17871788
"huaweicloud_ges_graph": ges.ResourceGesGraph(),
17881789
"huaweicloud_ges_metadata": ges.ResourceGesMetadata(),
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package gaussdb
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
8+
9+
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance"
10+
)
11+
12+
func TestAccOpenGaussParameterTemplateCompare_basic(t *testing.T) {
13+
name := acceptance.RandomAccResourceName()
14+
rName := "huaweicloud_gaussdb_opengauss_parameter_template_compare.test"
15+
16+
resource.ParallelTest(t, resource.TestCase{
17+
PreCheck: func() { acceptance.TestAccPreCheck(t) },
18+
ProviderFactories: acceptance.TestAccProviderFactories,
19+
CheckDestroy: nil,
20+
Steps: []resource.TestStep{
21+
{
22+
Config: testOpenGaussParameterrTemplateCompare_basic(name),
23+
Check: resource.ComposeTestCheckFunc(
24+
resource.TestCheckResourceAttr(rName, "differences.#", "1"),
25+
resource.TestCheckResourceAttr(rName, "differences.0.name", "cn:auto_explain_log_min_duration"),
26+
resource.TestCheckResourceAttr(rName, "differences.0.source_value", "1000"),
27+
resource.TestCheckResourceAttr(rName, "differences.0.target_value", "500"),
28+
),
29+
},
30+
},
31+
})
32+
}
33+
34+
func testOpenGaussParameterrTemplateCompare_basic(name string) string {
35+
return fmt.Sprintf(`
36+
resource "huaweicloud_gaussdb_opengauss_parameter_template" "source" {
37+
name = "%[1]s_source"
38+
engine_version = "8.201"
39+
instance_mode = "independent"
40+
41+
parameters {
42+
name = "cn:auto_explain_log_min_duration"
43+
value = "1000"
44+
}
45+
}
46+
47+
resource "huaweicloud_gaussdb_opengauss_parameter_template" "target" {
48+
name = "%[1]s_target"
49+
engine_version = "8.201"
50+
instance_mode = "independent"
51+
52+
parameters {
53+
name = "cn:auto_explain_log_min_duration"
54+
value = "500"
55+
}
56+
}
57+
58+
resource "huaweicloud_gaussdb_opengauss_parameter_template_compare" "test" {
59+
source_id = huaweicloud_gaussdb_opengauss_parameter_template.source.id
60+
target_id = huaweicloud_gaussdb_opengauss_parameter_template.target.id
61+
}
62+
`, name)
63+
}
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
package gaussdb
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"strings"
7+
"time"
8+
9+
"github.com/hashicorp/go-multierror"
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
11+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
12+
13+
"github.com/chnsz/golangsdk"
14+
15+
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config"
16+
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils"
17+
)
18+
19+
// @API GaussDB POST /v3/{project_id}/configurations/comparison
20+
func ResourceOpenGaussParameterTemplateCompare() *schema.Resource {
21+
return &schema.Resource{
22+
CreateContext: resourceOpenGaussParameterTemplateCompareCreate,
23+
ReadContext: resourceOpenGaussParameterTemplateCompareRead,
24+
DeleteContext: resourceOpenGaussParameterTemplateCompareDelete,
25+
26+
Timeouts: &schema.ResourceTimeout{
27+
Create: schema.DefaultTimeout(60 * time.Minute),
28+
},
29+
30+
Schema: map[string]*schema.Schema{
31+
"region": {
32+
Type: schema.TypeString,
33+
Optional: true,
34+
ForceNew: true,
35+
},
36+
"source_id": {
37+
Type: schema.TypeString,
38+
Required: true,
39+
ForceNew: true,
40+
},
41+
"target_id": {
42+
Type: schema.TypeString,
43+
Required: true,
44+
ForceNew: true,
45+
},
46+
"differences": {
47+
Type: schema.TypeList,
48+
Computed: true,
49+
Elem: gaussDBTemplateCompareDifferencesSchema(),
50+
},
51+
},
52+
}
53+
}
54+
55+
func gaussDBTemplateCompareDifferencesSchema() *schema.Resource {
56+
sc := schema.Resource{
57+
Schema: map[string]*schema.Schema{
58+
"name": {
59+
Type: schema.TypeString,
60+
Computed: true,
61+
},
62+
"source_value": {
63+
Type: schema.TypeString,
64+
Computed: true,
65+
},
66+
"target_value": {
67+
Type: schema.TypeString,
68+
Computed: true,
69+
},
70+
},
71+
}
72+
return &sc
73+
}
74+
75+
func resourceOpenGaussParameterTemplateCompareCreate(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
76+
cfg := meta.(*config.Config)
77+
region := cfg.GetRegion(d)
78+
79+
var (
80+
httpUrl = "v3/{project_id}/configurations/comparison"
81+
product = "opengauss"
82+
)
83+
client, err := cfg.NewServiceClient(product, region)
84+
if err != nil {
85+
return diag.Errorf("error creating GaussDB client: %s", err)
86+
}
87+
88+
createPath := client.Endpoint + httpUrl
89+
createPath = strings.ReplaceAll(createPath, "{project_id}", client.ProjectID)
90+
91+
createOpt := golangsdk.RequestOpts{KeepResponseBody: true}
92+
createOpt.JSONBody = utils.RemoveNil(buildCreateOpenGaussParameterTemplateCompareBodyParams(d))
93+
94+
createResp, err := client.Request("POST", createPath, &createOpt)
95+
if err != nil {
96+
return diag.Errorf("error creating GaussDB OpenGauss parameter template compare: %s", err)
97+
}
98+
99+
sourceId := d.Get("source_id").(string)
100+
targetId := d.Get("target_id").(string)
101+
d.SetId(fmt.Sprintf("%s/%s", sourceId, targetId))
102+
103+
createRespBody, err := utils.FlattenResponse(createResp)
104+
if err != nil {
105+
return diag.FromErr(err)
106+
}
107+
108+
mErr := multierror.Append(
109+
d.Set("differences", flattenOpenGaussParameterTemplateCompareResponseBody(createRespBody)),
110+
)
111+
112+
return diag.FromErr(mErr.ErrorOrNil())
113+
}
114+
115+
func buildCreateOpenGaussParameterTemplateCompareBodyParams(d *schema.ResourceData) map[string]interface{} {
116+
bodyParams := map[string]interface{}{
117+
"source_id": d.Get("source_id"),
118+
"target_id": d.Get("target_id"),
119+
}
120+
return bodyParams
121+
}
122+
123+
func flattenOpenGaussParameterTemplateCompareResponseBody(resp interface{}) []interface{} {
124+
differencesJson := utils.PathSearch("differences", resp, make([]interface{}, 0))
125+
differencesArray := differencesJson.([]interface{})
126+
if len(differencesArray) < 1 {
127+
return nil
128+
}
129+
rst := make([]interface{}, 0, len(differencesArray))
130+
for _, v := range differencesArray {
131+
rst = append(rst, map[string]interface{}{
132+
"name": utils.PathSearch("name", v, nil),
133+
"source_value": utils.PathSearch("source_value", v, nil),
134+
"target_value": utils.PathSearch("target_value", v, nil),
135+
})
136+
}
137+
return rst
138+
}
139+
140+
func resourceOpenGaussParameterTemplateCompareRead(_ context.Context, _ *schema.ResourceData, _ interface{}) diag.Diagnostics {
141+
return nil
142+
}
143+
144+
func resourceOpenGaussParameterTemplateCompareDelete(_ context.Context, _ *schema.ResourceData, _ interface{}) diag.Diagnostics {
145+
errorMsg := "Deleting parameter template compare resource is not supported. The resource is only removed from the" +
146+
"state, the GaussDB OpenGauss instance remains in the cloud."
147+
return diag.Diagnostics{
148+
diag.Diagnostic{
149+
Severity: diag.Warning,
150+
Summary: errorMsg,
151+
},
152+
}
153+
}

huaweicloud/services/taurusdb/resource_huaweicloud_gaussdb_mysql_parameter_template_compare.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"fmt"
66
"strings"
7-
"time"
87

98
"github.com/hashicorp/go-multierror"
109
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
@@ -23,10 +22,6 @@ func ResourceGaussDBMysqlTemplateCompare() *schema.Resource {
2322
ReadContext: resourceParameterTemplateCompareRead,
2423
DeleteContext: resourceParameterTemplateCompareDelete,
2524

26-
Timeouts: &schema.ResourceTimeout{
27-
Create: schema.DefaultTimeout(60 * time.Minute),
28-
},
29-
3025
Schema: map[string]*schema.Schema{
3126
"region": {
3227
Type: schema.TypeString,
@@ -106,7 +101,7 @@ func resourceParameterTemplateCompareCreate(_ context.Context, d *schema.Resourc
106101
}
107102

108103
mErr := multierror.Append(
109-
d.Set("differences", flattenGaussDBParameterTemplateCompareResponseBodyNodes(createRespBody)),
104+
d.Set("differences", flattenGaussDBParameterTemplateCompareResponseBody(createRespBody)),
110105
)
111106

112107
return diag.FromErr(mErr.ErrorOrNil())
@@ -120,7 +115,7 @@ func buildCreateParameterTemplateCompareBodyParams(d *schema.ResourceData) map[s
120115
return bodyParams
121116
}
122117

123-
func flattenGaussDBParameterTemplateCompareResponseBodyNodes(resp interface{}) []interface{} {
118+
func flattenGaussDBParameterTemplateCompareResponseBody(resp interface{}) []interface{} {
124119
differencesJson := utils.PathSearch("differences", resp, make([]interface{}, 0))
125120
differencesArray := differencesJson.([]interface{})
126121
if len(differencesArray) < 1 {

0 commit comments

Comments
 (0)