-
Notifications
You must be signed in to change notification settings - Fork 164
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(GaussDBforMySQL): add gaussdb mysql parameter template compare r…
…esource (#5526)
- Loading branch information
Showing
4 changed files
with
284 additions
and
13 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
docs/resources/gaussdb_mysql_parameter_template_compare.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
--- | ||
subcategory: "GaussDB(for MySQL)" | ||
layout: "huaweicloud" | ||
page_title: "HuaweiCloud: huaweicloud_gaussdb_mysql_parameter_template_compare" | ||
description: |- | ||
Manages a GaussDB MySQL parameter template apply resource within HuaweiCloud. | ||
--- | ||
|
||
# huaweicloud_gaussdb_mysql_parameter_template_compare | ||
|
||
Manages a GaussDB MySQL parameter template compare resource within HuaweiCloud. | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
variable "source_configuration_id" {} | ||
variable "target_configuration_id" {} | ||
resource "huaweicloud_gaussdb_mysql_parameter_template_compare" "test" { | ||
source_configuration_id = var.source_configuration_id | ||
target_configuration_id = var.target_configuration_id | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
The following arguments are supported: | ||
|
||
* `region` - (Optional, String, ForceNew) Specifies the region in which to create the resource. | ||
If omitted, the provider-level region will be used. Changing this parameter will create a new resource. | ||
|
||
* `source_configuration_id` - (Required, String, ForceNew) Specifies the ID of the source parameter template to be | ||
compared. Changing this parameter will create a new resource. | ||
|
||
* `target_configuration_id` - (Required, String, ForceNew) Specifies the ID of the destination parameter template to be | ||
compared. Changing this parameter will create a new resource. | ||
|
||
## Attribute Reference | ||
|
||
In addition to all arguments above, the following attributes are exported: | ||
|
||
* `id` - The resource ID in format of `<source_configuration_id>/<target_configuration_id>`. | ||
|
||
* `differences` - Indicates the differences between parameters. | ||
The [differences](#differences_struct) structure is documented below. | ||
|
||
<a name="differences_struct"></a> | ||
The `differences` block supports: | ||
|
||
* `parameter_name` - Indicates the parameter name. | ||
|
||
* `source_value` - Indicates the parameter value in the source parameter template. | ||
|
||
* `target_value` - Indicates the parameter value in the destination parameter template. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
.../acceptance/gaussdb/resource_huaweicloud_gaussdb_mysql_parameter_template_compare_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package gaussdb | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
|
||
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance" | ||
) | ||
|
||
func TestAccGaussDBMysqlTemplateCompare_basic(t *testing.T) { | ||
name := acceptance.RandomAccResourceName() | ||
rName := "huaweicloud_gaussdb_mysql_parameter_template_compare.test" | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { acceptance.TestAccPreCheck(t) }, | ||
ProviderFactories: acceptance.TestAccProviderFactories, | ||
CheckDestroy: nil, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testParameterTemplateCompare_basic(name), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr(rName, "differences.#", "1"), | ||
resource.TestCheckResourceAttr(rName, "differences.0.parameter_name", "auto_increment_increment"), | ||
resource.TestCheckResourceAttr(rName, "differences.0.source_value", "1"), | ||
resource.TestCheckResourceAttr(rName, "differences.0.target_value", "4"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testParameterTemplateCompare_basic(name string) string { | ||
return fmt.Sprintf(` | ||
resource "huaweicloud_gaussdb_mysql_parameter_template" "source" { | ||
name = "%[1]s_source" | ||
description = "test gaussdb mysql parameter template" | ||
datastore_engine = "gaussdb-mysql" | ||
datastore_version = "8.0" | ||
parameter_values = { | ||
auto_increment_increment = "1" | ||
} | ||
} | ||
resource "huaweicloud_gaussdb_mysql_parameter_template" "target" { | ||
name = "%[1]s_target" | ||
description = "test gaussdb mysql parameter template" | ||
datastore_engine = "gaussdb-mysql" | ||
datastore_version = "8.0" | ||
parameter_values = { | ||
auto_increment_increment = "4" | ||
} | ||
} | ||
resource "huaweicloud_gaussdb_mysql_parameter_template_compare" "test" { | ||
source_configuration_id = huaweicloud_gaussdb_mysql_parameter_template.source.id | ||
target_configuration_id = huaweicloud_gaussdb_mysql_parameter_template.target.id | ||
} | ||
`, name) | ||
} |
153 changes: 153 additions & 0 deletions
153
huaweicloud/services/gaussdb/huaweicloud_gaussdb_mysql_parameter_template_compare.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
package gaussdb | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
"time" | ||
|
||
"github.com/hashicorp/go-multierror" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
|
||
"github.com/chnsz/golangsdk" | ||
|
||
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config" | ||
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils" | ||
) | ||
|
||
// @API GaussDBforMySQL POST /v3/{project_id}/configurations/comparison | ||
func ResourceGaussDBMysqlTemplateCompare() *schema.Resource { | ||
return &schema.Resource{ | ||
CreateContext: resourceParameterTemplateCompareCreate, | ||
ReadContext: resourceParameterTemplateCompareRead, | ||
DeleteContext: resourceParameterTemplateCompareDelete, | ||
|
||
Timeouts: &schema.ResourceTimeout{ | ||
Create: schema.DefaultTimeout(60 * time.Minute), | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"region": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ForceNew: true, | ||
}, | ||
"source_configuration_id": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
"target_configuration_id": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
"differences": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Elem: gaussDBTemplateCompareDifferencesSchema(), | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func gaussDBTemplateCompareDifferencesSchema() *schema.Resource { | ||
sc := schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"parameter_name": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"source_value": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"target_value": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
return &sc | ||
} | ||
|
||
func resourceParameterTemplateCompareCreate(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
cfg := meta.(*config.Config) | ||
region := cfg.GetRegion(d) | ||
|
||
var ( | ||
httpUrl = "v3/{project_id}/configurations/comparison" | ||
product = "gaussdb" | ||
) | ||
client, err := cfg.NewServiceClient(product, region) | ||
if err != nil { | ||
return diag.Errorf("error creating GaussDB client: %s", err) | ||
} | ||
|
||
createPath := client.Endpoint + httpUrl | ||
createPath = strings.ReplaceAll(createPath, "{project_id}", client.ProjectID) | ||
|
||
createOpt := golangsdk.RequestOpts{KeepResponseBody: true} | ||
createOpt.JSONBody = utils.RemoveNil(buildCreateParameterTemplateCompareBodyParams(d)) | ||
|
||
createResp, err := client.Request("POST", createPath, &createOpt) | ||
if err != nil { | ||
return diag.Errorf("error creating GaussDB MySQL parameter template compare: %s", err) | ||
} | ||
|
||
sourceConfigurationId := d.Get("source_configuration_id").(string) | ||
targetConfigurationId := d.Get("target_configuration_id").(string) | ||
d.SetId(fmt.Sprintf("%s/%s", sourceConfigurationId, targetConfigurationId)) | ||
|
||
createRespBody, err := utils.FlattenResponse(createResp) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
mErr := multierror.Append( | ||
d.Set("differences", flattenGaussDBParameterTemplateCompareResponseBodyNodes(createRespBody)), | ||
) | ||
|
||
return diag.FromErr(mErr.ErrorOrNil()) | ||
} | ||
|
||
func buildCreateParameterTemplateCompareBodyParams(d *schema.ResourceData) map[string]interface{} { | ||
bodyParams := map[string]interface{}{ | ||
"source_configuration_id": d.Get("source_configuration_id"), | ||
"target_configuration_id": d.Get("target_configuration_id"), | ||
} | ||
return bodyParams | ||
} | ||
|
||
func flattenGaussDBParameterTemplateCompareResponseBodyNodes(resp interface{}) []interface{} { | ||
differencesJson := utils.PathSearch("differences", resp, make([]interface{}, 0)) | ||
differencesArray := differencesJson.([]interface{}) | ||
if len(differencesArray) < 1 { | ||
return nil | ||
} | ||
rst := make([]interface{}, 0, len(differencesArray)) | ||
for _, v := range differencesArray { | ||
rst = append(rst, map[string]interface{}{ | ||
"parameter_name": utils.PathSearch("parameter_name", v, nil), | ||
"source_value": utils.PathSearch("source_value", v, nil), | ||
"target_value": utils.PathSearch("target_value", v, nil), | ||
}) | ||
} | ||
return rst | ||
} | ||
|
||
func resourceParameterTemplateCompareRead(_ context.Context, _ *schema.ResourceData, _ interface{}) diag.Diagnostics { | ||
return nil | ||
} | ||
|
||
func resourceParameterTemplateCompareDelete(_ context.Context, _ *schema.ResourceData, _ interface{}) diag.Diagnostics { | ||
errorMsg := "Deleting parameter template compare resource is not supported. The resource is only removed from the" + | ||
"state, the GaussDB MySQL instance remains in the cloud." | ||
return diag.Diagnostics{ | ||
diag.Diagnostic{ | ||
Severity: diag.Warning, | ||
Summary: errorMsg, | ||
}, | ||
} | ||
} |