From ba83aa76d73a305d900db68a9cbf7ef838d30936 Mon Sep 17 00:00:00 2001 From: liwanting0517 Date: Mon, 25 Dec 2023 16:55:59 +0800 Subject: [PATCH] feat(DCS): add a data source to get the list of DCS templates --- docs/data-sources/dcs_templates.md | 77 ++++++++++ ...ource_flexibleengine_dcs_templates_test.go | 141 ++++++++++++++++++ flexibleengine/provider.go | 1 + 3 files changed, 219 insertions(+) create mode 100644 docs/data-sources/dcs_templates.md create mode 100644 flexibleengine/acceptance/data_source_flexibleengine_dcs_templates_test.go diff --git a/docs/data-sources/dcs_templates.md b/docs/data-sources/dcs_templates.md new file mode 100644 index 000000000..f23581529 --- /dev/null +++ b/docs/data-sources/dcs_templates.md @@ -0,0 +1,77 @@ +--- +subcategory: "Distributed Cache Service (DCS)" +--- + +# flexibleengine_dcs_templates + +Use this data source to get the list of DCS templates. + +## Example Usage + +```hcl +data "flexibleengine_dcs_templates" "test" { + type = "sys" + name = "test_template_name" +} +``` + +## Argument Reference + +The following arguments are supported: + +* `region` - (Optional, String) Specifies the region in which to query the data source. + If omitted, the provider-level region will be used. + +* `type` - (Required, String) Specifies the type of the template. Value options: + + **sys**: system template. + + **user**: custom template. + +* `template_id` - (Optional, String) Specifies the ID of the template. + +* `name` - (Optional, String) Specifies the name of the template. + +* `engine` - (Optional, String) Specifies the cache engine. Value options: **Redis**, **Memcached**. + +* `engine_version` - (Optional, String) Specifies the cache engine version. Value options: **4.0**, **5.0**, **6.0**. + +* `cache_mode` - (Optional, String) Specifies the DCS instance type. Value options: + + **single**: single-node. + + **ha**: master/standby. + + **cluster**: Redis Cluster. + + **proxy**: Proxy Cluster. + +* `product_type` - (Optional, String) Specifies the product edition. Value options: + + **generic**: standard edition. + + **enterprise**: professional edition. + +* `storage_type` - (Optional, String) Specifies the storage type. Value options: **DRAM**, **SSD**. + +## Attribute Reference + +In addition to all arguments above, the following attributes are exported: + +* `id` - The resource ID. + +* `templates` - Indicates the list of DCS templates. + The [templates](#Templates_Template) structure is documented below. + + +The `templates` block supports: + +* `template_id` - Indicates the ID of the template. + +* `name` - Indicates the name of the template. + +* `type` - Indicates the type of the template. + +* `engine` - Indicates the cache engine. + +* `engine_version` - Indicates the cache engine version. + +* `cache_mode` - Indicates the DCS instance type. + +* `product_type` - Indicates the product edition. + +* `storage_type` - Indicates the storage type. + +* `description` - Indicates the description of the template. diff --git a/flexibleengine/acceptance/data_source_flexibleengine_dcs_templates_test.go b/flexibleengine/acceptance/data_source_flexibleengine_dcs_templates_test.go new file mode 100644 index 000000000..da99344fb --- /dev/null +++ b/flexibleengine/acceptance/data_source_flexibleengine_dcs_templates_test.go @@ -0,0 +1,141 @@ +package acceptance + +import ( + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance" +) + +func TestAccDatasourceTemplates_basic(t *testing.T) { + rName := "data.flexibleengine_dcs_templates.test" + dc := acceptance.InitDataSourceCheck(rName) + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + ProviderFactories: TestAccProviderFactories, + Steps: []resource.TestStep{ + { + Config: testAccDatasourceTemplates_basic(), + Check: resource.ComposeTestCheckFunc( + dc.CheckResourceExists(), + resource.TestCheckResourceAttrSet(rName, "templates.0.template_id"), + resource.TestCheckResourceAttrSet(rName, "templates.0.name"), + resource.TestCheckResourceAttrSet(rName, "templates.0.type"), + resource.TestCheckResourceAttrSet(rName, "templates.0.engine"), + resource.TestCheckResourceAttrSet(rName, "templates.0.engine_version"), + resource.TestCheckResourceAttrSet(rName, "templates.0.cache_mode"), + resource.TestCheckResourceAttrSet(rName, "templates.0.product_type"), + resource.TestCheckResourceAttrSet(rName, "templates.0.storage_type"), + + resource.TestCheckOutput("template_id_filter_is_useful", "true"), + + resource.TestCheckOutput("name_filter_is_useful", "true"), + + resource.TestCheckOutput("engine_filter_is_useful", "true"), + + resource.TestCheckOutput("engine_version_filter_is_useful", "true"), + + resource.TestCheckOutput("cache_mode_filter_is_useful", "true"), + + resource.TestCheckOutput("product_type_filter_is_useful", "true"), + + resource.TestCheckOutput("storage_type_filter_is_useful", "true"), + ), + }, + }, + }) +} + +func testAccDatasourceTemplates_basic() string { + return ` +data "flexibleengine_dcs_templates" "test" { + type = "sys" +} + +data "flexibleengine_dcs_templates" "template_id_filter" { + type = "sys" + template_id = "6" + + depends_on = [data.flexibleengine_dcs_templates.test] +} +output "template_id_filter_is_useful" { + value = length(data.flexibleengine_dcs_templates.template_id_filter.templates) > 0 && alltrue( + [for v in data.flexibleengine_dcs_templates.template_id_filter.templates[*].template_id : v == "6"] + ) +} + +data "flexibleengine_dcs_templates" "name_filter" { + type = "sys" + name = "Default-Redis-6.0-ha-generic-DRAM" + + depends_on = [data.flexibleengine_dcs_templates.test] +} +output "name_filter_is_useful" { + value = length(data.flexibleengine_dcs_templates.name_filter.templates) > 0 && alltrue( + [for v in data.flexibleengine_dcs_templates.name_filter.templates[*].name : v == "Default-Redis-6.0-ha-generic-DRAM"] + ) +} + +data "flexibleengine_dcs_templates" "engine_filter" { + type = "sys" + engine = "Redis" + + depends_on = [data.flexibleengine_dcs_templates.test] +} +output "engine_filter_is_useful" { + value = length(data.flexibleengine_dcs_templates.engine_filter.templates) > 0 && alltrue( + [for v in data.flexibleengine_dcs_templates.engine_filter.templates[*].engine : v == "Redis"] + ) +} + +data "flexibleengine_dcs_templates" "engine_version_filter" { + type = "sys" + engine_version = "6.0" + + depends_on = [data.flexibleengine_dcs_templates.test] +} +output "engine_version_filter_is_useful" { + value = length(data.flexibleengine_dcs_templates.engine_version_filter.templates) > 0 && alltrue( + [for v in data.flexibleengine_dcs_templates.engine_version_filter.templates[*].engine_version : v == "6.0"] + ) +} + +data "flexibleengine_dcs_templates" "cache_mode_filter" { + type = "sys" + cache_mode = "single" + + depends_on = [data.flexibleengine_dcs_templates.test] +} +output "cache_mode_filter_is_useful" { + value = length(data.flexibleengine_dcs_templates.cache_mode_filter.templates) > 0 && alltrue( + [for v in data.flexibleengine_dcs_templates.cache_mode_filter.templates[*].cache_mode : v == "single"] + ) +} + +data "flexibleengine_dcs_templates" "product_type_filter" { + type = "sys" + product_type = "generic" + + depends_on = [data.flexibleengine_dcs_templates.test] +} +output "product_type_filter_is_useful" { + value = length(data.flexibleengine_dcs_templates.product_type_filter.templates) > 0 && alltrue( + [for v in data.flexibleengine_dcs_templates.product_type_filter.templates[*].product_type : v == "generic"] + ) +} + +data "flexibleengine_dcs_templates" "storage_type_filter" { + type = "sys" + storage_type = "DRAM" + + depends_on = [data.flexibleengine_dcs_templates.test] +} +output "storage_type_filter_is_useful" { + value = length(data.flexibleengine_dcs_templates.storage_type_filter.templates) > 0 && alltrue( + [for v in data.flexibleengine_dcs_templates.storage_type_filter.templates[*].storage_type : v == "DRAM"] + ) +} +` +} diff --git a/flexibleengine/provider.go b/flexibleengine/provider.go index 02461e66f..9d9a05729 100644 --- a/flexibleengine/provider.go +++ b/flexibleengine/provider.go @@ -299,6 +299,7 @@ func Provider() *schema.Provider { "flexibleengine_dcs_flavors": dcs.DataSourceDcsFlavorsV2(), "flexibleengine_dcs_instances": dcs.DataSourceDcsInstance(), + "flexibleengine_dcs_templates": dcs.DataSourceTemplates(), "flexibleengine_ddm_engines": ddm.DataSourceDdmEngines(), "flexibleengine_ddm_flavors": ddm.DataSourceDdmFlavors(),