diff --git a/docs/resources/dds_parameter_template.md b/docs/resources/dds_parameter_template.md new file mode 100644 index 00000000..ad461a54 --- /dev/null +++ b/docs/resources/dds_parameter_template.md @@ -0,0 +1,93 @@ +--- +subcategory: "Document Database Service (DDS)" +--- + +# flexibleengine_dds_parameter_template + +Manages a DDS parameter template resource within FlexibleEngineCloud. + +## Example Usage + +```hcl +variable "name" {} +variable "parameter_values" {} +variable "node_type" {} +variable "node_version" {} + +resource "flexibleengine_dds_parameter_template" "test"{ + name = var.name + parameter_values = var.parameter_values + node_type = var.node_type + node_version = var.node_version +} +``` + +## 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. + +* `name` - (Required, String) Specifies the parameter template name. + The value must be 1 to 64 characters, which can contain only letters, digits, hyphens (-), + underscores (_), and periods (.). + +* `node_type` - (Required, String, ForceNew) Specifies the node type of parameter template node_type. Valid value: + + **mongos**: the mongos node type. + + **shard**: the shard node type. + + **config**: the config node type. + + **replica**: the replica node type. + + **single**: the single node type. + + Changing this parameter will create a new resource. + +* `node_version` - (Required, String, ForceNew) Specifies the database version. + The value can be **4.2**, **4.0** or **3.4**. + + Changing this parameter will create a new resource. + +* `parameter_values` - (Optional, Map) Specifies the mapping between parameter names and parameter values. + You can customize parameter values based on the parameters in the default parameter template. + +* `description` - (Optional, String) Specifies the parameter template description. + The description must consist of a maximum of 256 characters and cannot contain the carriage + return character or the following special characters: >!<"&'=. + +## Attribute Reference + +In addition to all arguments above, the following attributes are exported: + +* `id` - The resource ID. + +* `parameters` - Indicates the parameters defined by users based on the default parameter templates. + The [parameters](#DdsParameterTemplate_Parameter) structure is documented below. + + +The `parameters` block supports: + +* `name` - Indicates the parameter name. + +* `value` - Indicates the parameter value. + +* `description` - Indicates the parameter description. + +* `type` - Indicates the parameter type. The value can be integer, string, boolean, float, or list. + +* `value_range` - Indicates the value range. + +* `restart_required` - Indicates whether the instance needs to be restarted. + + If the value is **true**, restart is required. + + If the value is **false**, restart is not required. + +* `readonly` - Indicates whether the parameter is read-only. + + If the value is **true**, the parameter is read-only. + + If the value is **false**, the parameter is not read-only. + +## Import + +The DDS parameter template can be imported using the `id`, e.g. + +```shell +terraform import flexibleengine_dds_parameter_template.test +``` diff --git a/flexibleengine/acceptance/resource_flexibleengine_dds_parameter_template_test.go b/flexibleengine/acceptance/resource_flexibleengine_dds_parameter_template_test.go new file mode 100644 index 00000000..56dd153a --- /dev/null +++ b/flexibleengine/acceptance/resource_flexibleengine_dds_parameter_template_test.go @@ -0,0 +1,309 @@ +package acceptance + +import ( + "fmt" + "strings" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" + + "github.com/chnsz/golangsdk" + + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config" + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance" + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils" +) + +func getDdsParameterTemplateResourceFunc(cfg *config.Config, state *terraform.ResourceState) (interface{}, error) { + region := OS_REGION_NAME + // getParameterTemplate: Query DDS parameter template + var ( + getParameterTemplateHttpUrl = "v3/{project_id}/configurations/{config_id}" + getParameterTemplateProduct = "dds" + ) + getParameterTemplateClient, err := cfg.NewServiceClient(getParameterTemplateProduct, region) + if err != nil { + return nil, fmt.Errorf("error creating DDS Client: %s", err) + } + + getParameterTemplatePath := getParameterTemplateClient.Endpoint + getParameterTemplateHttpUrl + getParameterTemplatePath = strings.ReplaceAll(getParameterTemplatePath, "{project_id}", + getParameterTemplateClient.ProjectID) + getParameterTemplatePath = strings.ReplaceAll(getParameterTemplatePath, "{config_id}", state.Primary.ID) + + getParameterTemplateOpt := golangsdk.RequestOpts{ + KeepResponseBody: true, + OkCodes: []int{ + 200, + }, + MoreHeaders: map[string]string{ + "Content-Type": "application/json", + }, + } + getParameterTemplateResp, err := getParameterTemplateClient.Request("GET", + getParameterTemplatePath, &getParameterTemplateOpt) + if err != nil { + return nil, fmt.Errorf("error retrieving DDS parameter template: %s", err) + } + return utils.FlattenResponse(getParameterTemplateResp) +} + +func TestAccDdsParameterTemplate_basic(t *testing.T) { + var obj interface{} + + name := acceptance.RandomAccResourceName() + updateName := acceptance.RandomAccResourceName() + rName := "flexibleengine_dds_parameter_template.test" + + rc := acceptance.InitResourceCheck( + rName, + &obj, + getDdsParameterTemplateResourceFunc, + ) + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + ProviderFactories: TestAccProviderFactories, + CheckDestroy: rc.CheckResourceDestroy(), + Steps: []resource.TestStep{ + { + Config: testDdsParameterTemplate_basic(name), + Check: resource.ComposeTestCheckFunc( + rc.CheckResourceExists(), + resource.TestCheckResourceAttr(rName, "name", name), + resource.TestCheckResourceAttr(rName, "description", "test description"), + resource.TestCheckResourceAttr(rName, "node_version", "4.0"), + resource.TestCheckResourceAttr(rName, "parameters.0.name", + "connPoolMaxConnsPerHost"), + resource.TestCheckResourceAttr(rName, "parameters.0.value", "800"), + resource.TestCheckResourceAttr(rName, "parameters.1.name", + "connPoolMaxShardedConnsPerHost"), + resource.TestCheckResourceAttr(rName, "parameters.1.value", "800"), + ), + }, + { + Config: testDdsParameterTemplate_basic_update(updateName), + Check: resource.ComposeTestCheckFunc( + rc.CheckResourceExists(), + resource.TestCheckResourceAttr(rName, "name", updateName), + resource.TestCheckResourceAttr(rName, "description", "test description update"), + resource.TestCheckResourceAttr(rName, "node_type", "mongos"), + resource.TestCheckResourceAttr(rName, "node_version", "4.0"), + resource.TestCheckResourceAttr(rName, "parameters.0.name", + "connPoolMaxConnsPerHost"), + resource.TestCheckResourceAttr(rName, "parameters.0.value", "500"), + resource.TestCheckResourceAttr(rName, "parameters.1.name", + "connPoolMaxShardedConnsPerHost"), + resource.TestCheckResourceAttr(rName, "parameters.1.value", "500"), + ), + }, + { + ResourceName: rName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"node_type", "parameter_values"}, + }, + }, + }) +} + +func TestAccDdsParameterTemplate_shared_basic(t *testing.T) { + var obj interface{} + + name := acceptance.RandomAccResourceName() + rName := "flexibleengine_dds_parameter_template.test" + + rc := acceptance.InitResourceCheck( + rName, + &obj, + getDdsParameterTemplateResourceFunc, + ) + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + ProviderFactories: TestAccProviderFactories, + CheckDestroy: rc.CheckResourceDestroy(), + Steps: []resource.TestStep{ + { + Config: testDdsParameterTemplate_shared_basic(name), + Check: resource.ComposeTestCheckFunc( + rc.CheckResourceExists(), + resource.TestCheckResourceAttr(rName, "name", name), + resource.TestCheckResourceAttr(rName, "description", "test description shared node_type"), + resource.TestCheckResourceAttr(rName, "node_type", "shard"), + resource.TestCheckResourceAttr(rName, "node_version", "4.2"), + ), + }, + }, + }) +} + +func TestAccDdsParameterTemplate_config_basic(t *testing.T) { + var obj interface{} + + name := acceptance.RandomAccResourceName() + rName := "flexibleengine_dds_parameter_template.test" + + rc := acceptance.InitResourceCheck( + rName, + &obj, + getDdsParameterTemplateResourceFunc, + ) + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + ProviderFactories: TestAccProviderFactories, + CheckDestroy: rc.CheckResourceDestroy(), + Steps: []resource.TestStep{ + { + Config: testDdsParameterTemplate_config_basic(name), + Check: resource.ComposeTestCheckFunc( + rc.CheckResourceExists(), + resource.TestCheckResourceAttr(rName, "name", name), + resource.TestCheckResourceAttr(rName, "description", "test description config node_type"), + resource.TestCheckResourceAttr(rName, "node_type", "config"), + resource.TestCheckResourceAttr(rName, "node_version", "3.4"), + ), + }, + }, + }) +} + +func TestAccDdsParameterTemplate_replica_basic(t *testing.T) { + var obj interface{} + + name := acceptance.RandomAccResourceName() + rName := "flexibleengine_dds_parameter_template.test" + + rc := acceptance.InitResourceCheck( + rName, + &obj, + getDdsParameterTemplateResourceFunc, + ) + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + ProviderFactories: TestAccProviderFactories, + CheckDestroy: rc.CheckResourceDestroy(), + Steps: []resource.TestStep{ + { + Config: testDdsParameterTemplate_replica_basic(name), + Check: resource.ComposeTestCheckFunc( + rc.CheckResourceExists(), + resource.TestCheckResourceAttr(rName, "name", name), + resource.TestCheckResourceAttr(rName, "description", "test description replica node_type"), + resource.TestCheckResourceAttr(rName, "node_type", "replica"), + resource.TestCheckResourceAttr(rName, "node_version", "4.0"), + ), + }, + }, + }) +} + +func TestAccDdsParameterTemplate_single_basic(t *testing.T) { + var obj interface{} + + name := acceptance.RandomAccResourceName() + rName := "flexibleengine_dds_parameter_template.test" + + rc := acceptance.InitResourceCheck( + rName, + &obj, + getDdsParameterTemplateResourceFunc, + ) + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + ProviderFactories: TestAccProviderFactories, + CheckDestroy: rc.CheckResourceDestroy(), + Steps: []resource.TestStep{ + { + Config: testDdsParameterTemplate_single_basic(name), + Check: resource.ComposeTestCheckFunc( + rc.CheckResourceExists(), + resource.TestCheckResourceAttr(rName, "name", name), + resource.TestCheckResourceAttr(rName, "description", "test description single node_type"), + resource.TestCheckResourceAttr(rName, "node_type", "single"), + resource.TestCheckResourceAttr(rName, "node_version", "4.0"), + ), + }, + }, + }) +} + +func testDdsParameterTemplate_basic(name string) string { + return fmt.Sprintf(` +resource "flexibleengine_dds_parameter_template" "test" { + name = "%s" + description = "test description" + node_type = "mongos" + node_version = "4.0" + + parameter_values = { + connPoolMaxConnsPerHost = 800 + connPoolMaxShardedConnsPerHost = 800 + } +} +`, name) +} + +func testDdsParameterTemplate_basic_update(name string) string { + return fmt.Sprintf(` +resource "flexibleengine_dds_parameter_template" "test" { + name = "%s" + description = "test description update" + node_type = "mongos" + node_version = "4.0" + + parameter_values = { + connPoolMaxConnsPerHost = 500 + connPoolMaxShardedConnsPerHost = 500 + } +} +`, name) +} + +func testDdsParameterTemplate_shared_basic(name string) string { + return fmt.Sprintf(` +resource "flexibleengine_dds_parameter_template" "test" { + name = "%s" + description = "test description shared node_type" + node_type = "shard" + node_version = "4.2" +} +`, name) +} + +func testDdsParameterTemplate_config_basic(name string) string { + return fmt.Sprintf(` +resource "flexibleengine_dds_parameter_template" "test" { + name = "%s" + description = "test description config node_type" + node_type = "config" + node_version = "3.4" +} +`, name) +} + +func testDdsParameterTemplate_replica_basic(name string) string { + return fmt.Sprintf(` +resource "flexibleengine_dds_parameter_template" "test" { + name = "%s" + description = "test description replica node_type" + node_type = "replica" + node_version = "4.0" +} +`, name) +} + +func testDdsParameterTemplate_single_basic(name string) string { + return fmt.Sprintf(` +resource "flexibleengine_dds_parameter_template" "test" { + name = "%s" + description = "test description single node_type" + node_type = "single" + node_version = "4.0" +} +`, name) +} diff --git a/flexibleengine/provider.go b/flexibleengine/provider.go index 989b7a00..6b801189 100644 --- a/flexibleengine/provider.go +++ b/flexibleengine/provider.go @@ -479,8 +479,9 @@ func Provider() *schema.Provider { "flexibleengine_ddm_schema": ddm.ResourceDdmSchema(), "flexibleengine_ddm_account": ddm.ResourceDdmAccount(), - "flexibleengine_dds_database_role": dds.ResourceDatabaseRole(), - "flexibleengine_dds_database_user": dds.ResourceDatabaseUser(), + "flexibleengine_dds_database_role": dds.ResourceDatabaseRole(), + "flexibleengine_dds_database_user": dds.ResourceDatabaseUser(), + "flexibleengine_dds_parameter_template": dds.ResourceDdsParameterTemplate(), "flexibleengine_apig_vpc_channel": deprecated.ResourceApigVpcChannelV2(),