From 151abe95eac0621578adacb22530ac71c17702ca Mon Sep 17 00:00:00 2001 From: zhukun <2019229048@tju.edu.cn> Date: Mon, 20 Nov 2023 20:44:05 +0800 Subject: [PATCH] feat(dcs): import dcs resource and add unit test and docs --- docs/data-sources/dcs_instances.md | 98 ++++++++++++ docs/data-sources/dcs_template_detail.md | 79 ++++++++++ docs/data-sources/dcs_templates.md | 77 ++++++++++ docs/resources/dcs_backup.md | 82 ++++++++++ docs/resources/dcs_custom_template.md | 119 ++++++++++++++ g42cloud/provider.go | 15 +- ...data_source_g42cloud_dcs_instances_test.go | 45 ++++++ ...ource_g42cloud_dcs_template_detail_test.go | 66 ++++++++ ...a_source_huaweicloud_dcs_templates_test.go | 135 ++++++++++++++++ .../dcs/resource_g42cloud_dcs_backup_test.go | 145 ++++++++++++++++++ ...ource_g42cloud_dcs_custom_template_test.go | 136 ++++++++++++++++ .../resource_g42cloud_dcs_instance_test.go | 6 + 12 files changed, 998 insertions(+), 5 deletions(-) create mode 100644 docs/data-sources/dcs_instances.md create mode 100644 docs/data-sources/dcs_template_detail.md create mode 100644 docs/data-sources/dcs_templates.md create mode 100644 docs/resources/dcs_backup.md create mode 100644 docs/resources/dcs_custom_template.md create mode 100644 g42cloud/services/acceptance/dcs/data_source_g42cloud_dcs_instances_test.go create mode 100644 g42cloud/services/acceptance/dcs/data_source_g42cloud_dcs_template_detail_test.go create mode 100644 g42cloud/services/acceptance/dcs/data_source_huaweicloud_dcs_templates_test.go create mode 100644 g42cloud/services/acceptance/dcs/resource_g42cloud_dcs_backup_test.go create mode 100644 g42cloud/services/acceptance/dcs/resource_g42cloud_dcs_custom_template_test.go diff --git a/docs/data-sources/dcs_instances.md b/docs/data-sources/dcs_instances.md new file mode 100644 index 00000000..5443555d --- /dev/null +++ b/docs/data-sources/dcs_instances.md @@ -0,0 +1,98 @@ +--- +subcategory: "Distributed Cache Service (DCS)" +--- + +# g42cloud_dcs_instances + +Use this data source to get the list of DCS instances. + +## Example Usage + +```hcl +data "g42cloud_dcs_instances" "test" { + name = "test_name" + status = "RUNNING" +} +``` + +## 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. + +* `name` - (Optional, String) Specifies the name of an instance. + +* `status` - (Optional, String) Specifies the cache instance status. The valid values are **RUNNING**, **ERROR**, + **RESTARTING**, **FROZEN**, **EXTENDING**, **RESTORING**, **FLUSHING**. + +* `private_ip` - (Optional, String) Specifies the subnet Network ID. + +* `capacity` - (Optional, Float) Specifies the cache capacity. Unit: GB. + +## Attribute Reference + +In addition to all arguments above, the following attributes are exported: + +* `id` - The resource ID. + +* `instances` - Indicates the list of DCS instances. + The [instances](#DcsInstance_Instance) structure is documented below. + + +The `instances` block supports: + +* `id` - Indicates the ID of the instance. + +* `name` - Indicates the name of an instance. + +* `engine` - Indicates a cache engine. + +* `engine_version` - Indicates the version of a cache engine. + +* `capacity` - Indicates the cache capacity. Unit: GB. + +* `flavor` - Indicates the flavor of the cache instance. + +* `availability_zones` - Specifies the code of the AZ where the cache node resides. + +* `vpc_id` - Indicates the ID of VPC which the instance belongs to. + +* `vpc_name` - Indicates the name of VPC which the instance belongs to. + +* `subnet_id` - Indicates the ID of subnet which the instance belongs to. + +* `subnet_name` - Indicates the name of subnet which the instance belongs to. + +* `security_group_id` - Indicates the ID of the security group which the instance belongs to. + +* `security_group_name` - Indicates the name of security group which the instance belongs to. + +* `enterprise_project_id` - Indicates the enterprise project id of the dcs instance. + +* `description` - Indicates the description of an instance. + +* `private_ip` - Indicates the IP address of the DCS instance. + +* `maintain_begin` - Indicates the time at which the maintenance time window starts. + +* `maintain_end` - Indicates the time at which the maintenance time window ends. + +* `charging_mode` - Indicates the charging mode of the cache instance. + +* `port` - Indicates the port of the cache instance. + +* `status` - Indicates the cache instance status. + +* `used_memory` - Indicates the size of the used memory. Unit: MB. + +* `max_memory` - Indicates the total memory size. Unit: MB. + +* `domain_name` - Indicates the domain name of the instance. + +* `access_user` - Indicates the username used for accessing a DCS Memcached instance. + +* `order_id` - Indicates the ID of the order that created the instance. + +* `tags` - Indicates The key/value pairs to associate with the DCS instance. diff --git a/docs/data-sources/dcs_template_detail.md b/docs/data-sources/dcs_template_detail.md new file mode 100644 index 00000000..48c4b974 --- /dev/null +++ b/docs/data-sources/dcs_template_detail.md @@ -0,0 +1,79 @@ +--- +subcategory: "Distributed Cache Service (DCS)" +--- + +# g42cloud_dcs_template_detail + +Use this data source to get the detail of DCS template. + +## Example Usage + +```hcl +variable "template_id" {} +data "g42cloud_dcs_template_detail" "test" { + template_id = var.template_id + type = "sys" +} +``` + +## 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` - (Required, String) Specifies the ID of the template. + +* `params` - (Optional, List) Specifies the list of the template params. +The [params](#TemplateDetail_Param) structure is documented below. + + +The `params` block supports: + +* `param_name` - (Optional, String) Specifies the name of the param. + +## Attribute Reference + +In addition to all arguments above, the following attributes are exported: + +* `id` - The resource ID. + +* `name` - Indicates the name of the template. + +* `type` - Indicates the type of the template. The value can be **sys**, **user**. + +* `engine` - Indicates the cache engine. Currently, only **Redis** is supported. + +* `engine_version` - Indicates the cache engine version. The value can be **4.0**, **5.0**, **6.0**. + +* `cache_mode` - Indicates the DCS instance type. The value can be **single**, **ha**, **cluster**, **proxy**, + **ha_rw_split**. + +* `product_type` - Indicates the product edition. The value can be **generic**, **enterprise**. + +* `storage_type` - Indicates the storage type. The value can be **DRAM**, **SSD**. + +* `description` - Indicates the description of the template. + +* `params` - Indicates the list of the template params. + The [params](#TemplateDetail_Param) structure is documented below. + + +The `params` block supports: + +* `param_id` - Indicates the ID of the param. + +* `default_value` - Indicates the default of the param. + +* `value_range` - Indicates the value range of the param. + +* `value_type` - Indicates the value type of the param. + +* `description` - Indicates the description of the param. + +* `need_restart` - Indicates whether the DCS instance need restart. diff --git a/docs/data-sources/dcs_templates.md b/docs/data-sources/dcs_templates.md new file mode 100644 index 00000000..627963b4 --- /dev/null +++ b/docs/data-sources/dcs_templates.md @@ -0,0 +1,77 @@ +--- +subcategory: "Distributed Cache Service (DCS)" +--- + +# g42cloud_dcs_templates + +Use this data source to get the list of DCS templates. + +## Example Usage + +```hcl +data "g42cloud_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**. + +* `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. + + **ha_rw_split**: read/write splitting. + +* `product_type` - (Optional, String) Specifies the product edition. Value options: + + **generic**: standard edition. + +* `storage_type` - (Optional, String) Specifies the storage type. Value options: **DRAM**. + +## 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/docs/resources/dcs_backup.md b/docs/resources/dcs_backup.md new file mode 100644 index 00000000..2a483702 --- /dev/null +++ b/docs/resources/dcs_backup.md @@ -0,0 +1,82 @@ +--- +subcategory: "Distributed Cache Service (DCS)" +--- + +# g42cloud_dcs_backup + +Manages a DCS backup resource within G42Cloud. + +## Example Usage + +```hcl +variable "dcs_instance_id" {} + +resource "g42cloud_dcs_backup" "test"{ + instance_id = var.dcs_instance_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. + +* `instance_id` - (Required, String, ForceNew) Specifies the ID of the DCS instance. + + Changing this parameter will create a new resource. + +* `description` - (Optional, String, ForceNew) Specifies the description of DCS instance backup. + + Changing this parameter will create a new resource. + +* `backup_format` - (Optional, String, ForceNew) Specifies the format of the DCS instance backup. + Value options: **aof**, **rdb**. Default to rdb. + + 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. + +* `name` - Indicates the backup name. + +* `size` - Indicates the size of the backup file (byte). + +* `type` - Indicates the backup type. Valid value: + + **manual**: indicates manual backup. + + **auto**: indicates automatic backup. + +* `begin_time` - Indicates the time when the backup task is created. The format is yyyy-mm-dd hh:mm:ss. + The value is in UTC format. + +* `end_time` - Indicates the time at which DCS instance backup is completed. The format is yyyy-mm-dd hh:mm:ss. + The value is in UTC format. + +* `status` - Indicates the backup status. Valid value: + + **waiting**: The task is waiting to begin. + + **backuping**: DCS instance backup is in progress. + + **succeed**: DCS instance backup succeeded. + + **failed**: DCS instance backup failed. + + **expired**: The backup file has expired. + + **deleted**: The backup file has been deleted manually. + +* `is_support_restore` - Indicates whether restoration is supported. Value Options: **TRUE**, **FALSE**. + +## Timeouts + +This resource provides the following timeouts configuration options: + +* `create` - Default is 30 minutes. +* `delete` - Default is 10 minutes. + +## Import + +The DCS backup can be imported using the DCS instance ID and backup ID separated by a slash, e.g.: + +```bash +$ terraform import g42cloud_dcs_backup.test / +``` diff --git a/docs/resources/dcs_custom_template.md b/docs/resources/dcs_custom_template.md new file mode 100644 index 00000000..ce635e9c --- /dev/null +++ b/docs/resources/dcs_custom_template.md @@ -0,0 +1,119 @@ +--- +subcategory: "Distributed Cache Service (DCS)" +--- + +# g42cloud_dcs_custom_template + +Manages a DCS custom template resource within G42Cloud. + +## Example Usage + +```hcl +variable "source_template_id" {} + +resource "g42cloud_dcs_custom_template" "test"{ + template_id = var.source_template_id + name = "template_name" + type = "sys" + + params { + param_name = "timeout" + param_value = "100" + } +} +``` + +## 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. + +* `template_id` - (Required, String, ForceNew) Specifies the ID of the source template. + + Changing this parameter will create a new resource. + +* `source_type` - (Required, String, ForceNew) Specifies the type of the source template. Value options: + + **sys**: system template. + + **user**: custom template. + + Changing this parameter will create a new resource. + +* `name` - (Required, String) Specifies the name of the template. + +* `params` - (Required, List) Specifies the list of the template params. +The [params](#CustomTemplate_Param) structure is documented below. + +* `description` - (Optional, String) Specifies the description of the template. + + +The `params` block supports: + +* `param_name` - (Required, String) Indicates the name of the param. You can find it through data source + `g42cloud_dcs_template_detail`. + +* `param_value` - (Required, String) Indicates the value of the param. + +## Attribute Reference + +In addition to all arguments above, the following attributes are exported: + +* `id` - The resource ID. + +* `type` - Indicates the type of the template. The value can be **sys**, **user**. + +* `engine` - Indicates the cache engine. Currently, only **Redis** is supported. + +* `engine_version` - Indicates the cache engine version. The value can be **4.0**, **5.0.**, **6.0.**. + +* `cache_mode` - Indicates the DCS instance type. The value can be **single**, **ha**, **cluster**, **proxy**, + **ha_rw_split**. + +* `product_type` - Indicates the product edition. The value can be **generic**. + +* `storage_type` - Indicates the storage type. The value can be **DRAM**. + +* `params` - Indicates the list of the template params. + The [params](#CustomTemplate_Param) structure is documented below. + + +The `params` block supports: + +* `param_id` - (Optional, String) Indicates the ID of the param. + +* `default_value` - (Optional, String) Indicates the default value of the param. + +* `value_range` - (Optional, String) Indicates the value range of the param. + +* `value_type` - (Optional, String) Indicates the value type of the param. + +* `description` - (Optional, String) Indicates the description of the param. + +* `need_restart` - (Optional, Bool) Indicates whether the DCS instance need restart. + +## Import + +The dcs custom template can be imported using the `id`, e.g. + +```bash +$ terraform import g42cloud_dcs_custom_template.test +``` + +Note that the imported state may not be identical to your resource definition, due to some attributes missing from the +API response, security or some other reason. The missing attributes include: `template_id`, `source_type`, `params`. It +is generally recommended running `terraform plan` after importing a custom template. You can then decide if changes +should be applied to the custom template, or the resource definition should be updated to align with the DCS custom +template. Also you can ignore changes as below. + +```hcl +resource "g42cloud_dcs_custom_template" "test" { + ... + + lifecycle { + ignore_changes = [ + template_id, source_type, params, + ] + } +} +``` diff --git a/g42cloud/provider.go b/g42cloud/provider.go index 04d75819..8b3b6bb4 100644 --- a/g42cloud/provider.go +++ b/g42cloud/provider.go @@ -208,10 +208,13 @@ func Provider() *schema.Provider { "g42cloud_css_flavors": css.DataSourceCssFlavors(), - "g42cloud_dcs_flavors": dcs.DataSourceDcsFlavorsV2(), - "g42cloud_dcs_maintainwindow": dcs.DataSourceDcsMaintainWindow(), - "g42cloud_dcs_product": deprecated.DataSourceDcsProductV1(), - "g42cloud_dcs_az": deprecated.DataSourceDcsAZV1(), + "g42cloud_dcs_flavors": dcs.DataSourceDcsFlavorsV2(), + "g42cloud_dcs_maintainwindow": dcs.DataSourceDcsMaintainWindow(), + "g42cloud_dcs_product": deprecated.DataSourceDcsProductV1(), + "g42cloud_dcs_az": deprecated.DataSourceDcsAZV1(), + "g42cloud_dcs_instances": dcs.DataSourceDcsInstance(), + "g42cloud_dcs_template_detail": dcs.DataSourceTemplateDetail(), + "g42cloud_dcs_templates": dcs.DataSourceTemplates(), "g42cloud_dds_flavors": dds.DataSourceDDSFlavorV3(), @@ -339,7 +342,9 @@ func Provider() *schema.Provider { "g42cloud_dc_virtual_gateway": dc.ResourceVirtualGateway(), "g42cloud_dc_virtual_interface": dc.ResourceVirtualInterface(), - "g42cloud_dcs_instance": dcs.ResourceDcsInstance(), + "g42cloud_dcs_instance": dcs.ResourceDcsInstance(), + "g42cloud_dcs_backup": dcs.ResourceDcsBackup(), + "g42cloud_dcs_custom_template": dcs.ResourceCustomTemplate(), "g42cloud_dds_instance": dds.ResourceDdsInstanceV3(), diff --git a/g42cloud/services/acceptance/dcs/data_source_g42cloud_dcs_instances_test.go b/g42cloud/services/acceptance/dcs/data_source_g42cloud_dcs_instances_test.go new file mode 100644 index 00000000..f91bcd5e --- /dev/null +++ b/g42cloud/services/acceptance/dcs/data_source_g42cloud_dcs_instances_test.go @@ -0,0 +1,45 @@ +package dcs + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + + "github.com/g42cloud-terraform/terraform-provider-g42cloud/g42cloud/services/acceptance" +) + +func TestAccDatasourceDcsInstance_basic(t *testing.T) { + rName := "data.g42cloud_dcs_instances.test" + name := acceptance.RandomAccResourceName() + dc := acceptance.InitDataSourceCheck(rName) + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acceptance.TestAccPreCheck(t) }, + ProviderFactories: acceptance.TestAccProviderFactories, + Steps: []resource.TestStep{ + { + Config: testAccDatasourceDcsInstance_basic(name), + Check: resource.ComposeTestCheckFunc( + dc.CheckResourceExists(), + resource.TestCheckResourceAttr(rName, "instances.0.name", name), + resource.TestCheckResourceAttr(rName, "instances.0.port", "6388"), + resource.TestCheckResourceAttr(rName, "instances.0.flavor", "redis.ha.xu1.tiny.r2.128"), + resource.TestCheckResourceAttrPair(rName, "instances.0.flavor", + "data.g42cloud_dcs_flavors.test", "flavors.0.name"), + ), + }, + }, + }) +} + +func testAccDatasourceDcsInstance_basic(name string) string { + return fmt.Sprintf(` +%s + +data "g42cloud_dcs_instances" "test" { + name = g42cloud_dcs_instance.instance_1.name + status = "RUNNING" +} +`, testAccDcsV1Instance_basic(name)) +} diff --git a/g42cloud/services/acceptance/dcs/data_source_g42cloud_dcs_template_detail_test.go b/g42cloud/services/acceptance/dcs/data_source_g42cloud_dcs_template_detail_test.go new file mode 100644 index 00000000..895db48a --- /dev/null +++ b/g42cloud/services/acceptance/dcs/data_source_g42cloud_dcs_template_detail_test.go @@ -0,0 +1,66 @@ +package dcs + +import ( + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + + "github.com/g42cloud-terraform/terraform-provider-g42cloud/g42cloud/services/acceptance" +) + +func TestAccDatasourceTemplateDetail_basic(t *testing.T) { + rName := "data.g42cloud_dcs_template_detail.test" + dc := acceptance.InitDataSourceCheck(rName) + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acceptance.TestAccPreCheck(t) }, + ProviderFactories: acceptance.TestAccProviderFactories, + Steps: []resource.TestStep{ + { + Config: testAccDatasourceTemplateDetail_basic(), + Check: resource.ComposeTestCheckFunc( + dc.CheckResourceExists(), + resource.TestCheckResourceAttrSet(rName, "params.0.param_id"), + resource.TestCheckResourceAttrSet(rName, "params.0.param_name"), + resource.TestCheckResourceAttrSet(rName, "params.0.default_value"), + resource.TestCheckResourceAttrSet(rName, "params.0.value_range"), + resource.TestCheckResourceAttrSet(rName, "params.0.value_type"), + resource.TestCheckResourceAttrSet(rName, "params.0.description"), + resource.TestCheckResourceAttrSet(rName, "params.0.need_restart"), + + resource.TestCheckOutput("params_filter_is_useful", "true"), + ), + }, + }, + }) +} + +func testAccDatasourceTemplateDetail_basic() string { + return ` +data "g42cloud_dcs_templates" "test" { + type = "sys" + name = "Default-Redis-6.0-ha-enterprise-SSD" +} + +data "g42cloud_dcs_template_detail" "test" { + template_id = "6" + type = "sys" +} + +data "g42cloud_dcs_template_detail" "params_filter" { + template_id = "6" + type = "sys" + + params { + param_name = "timeout" + } + + depends_on = [data.g42cloud_dcs_template_detail.test] +} +output "params_filter_is_useful" { + value = length(data.g42cloud_dcs_template_detail.params_filter.params) > 0 && alltrue( + [for v in data.g42cloud_dcs_template_detail.params_filter.params[*].param_name : v == "timeout"] + ) +} +` +} diff --git a/g42cloud/services/acceptance/dcs/data_source_huaweicloud_dcs_templates_test.go b/g42cloud/services/acceptance/dcs/data_source_huaweicloud_dcs_templates_test.go new file mode 100644 index 00000000..6acc0b02 --- /dev/null +++ b/g42cloud/services/acceptance/dcs/data_source_huaweicloud_dcs_templates_test.go @@ -0,0 +1,135 @@ +package dcs + +import ( + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + + "github.com/g42cloud-terraform/terraform-provider-g42cloud/g42cloud/services/acceptance" +) + +func TestAccDatasourceTemplates_basic(t *testing.T) { + rName := "data.g42cloud_dcs_templates.test" + dc := acceptance.InitDataSourceCheck(rName) + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acceptance.TestAccPreCheck(t) }, + ProviderFactories: acceptance.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("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 "g42cloud_dcs_templates" "test" { + type = "sys" +} + +data "g42cloud_dcs_templates" "template_id_filter" { + type = "sys" + template_id = "6" + + depends_on = [data.g42cloud_dcs_templates.test] +} +output "template_id_filter_is_useful" { + value = length(data.g42cloud_dcs_templates.template_id_filter.templates) > 0 && alltrue( + [for v in data.g42cloud_dcs_templates.template_id_filter.templates[*].template_id : v == "6"] + ) +} + +data "g42cloud_dcs_templates" "name_filter" { + type = "sys" + name = "Default-Redis-6.0-ha-enterprise-SSD" + + depends_on = [data.g42cloud_dcs_templates.test] +} + + +data "g42cloud_dcs_templates" "engine_filter" { + type = "sys" + engine = "Redis" + + depends_on = [data.g42cloud_dcs_templates.test] +} +output "engine_filter_is_useful" { + value = length(data.g42cloud_dcs_templates.engine_filter.templates) > 0 && alltrue( + [for v in data.g42cloud_dcs_templates.engine_filter.templates[*].engine : v == "Redis"] + ) +} + +data "g42cloud_dcs_templates" "engine_version_filter" { + type = "sys" + engine_version = "6.0" + + depends_on = [data.g42cloud_dcs_templates.test] +} +output "engine_version_filter_is_useful" { + value = length(data.g42cloud_dcs_templates.engine_version_filter.templates) > 0 && alltrue( + [for v in data.g42cloud_dcs_templates.engine_version_filter.templates[*].engine_version : v == "6.0"] + ) +} + +data "g42cloud_dcs_templates" "cache_mode_filter" { + type = "sys" + cache_mode = "ha_rw_split" + + depends_on = [data.g42cloud_dcs_templates.test] +} +output "cache_mode_filter_is_useful" { + value = length(data.g42cloud_dcs_templates.cache_mode_filter.templates) > 0 && alltrue( + [for v in data.g42cloud_dcs_templates.cache_mode_filter.templates[*].cache_mode : v == "ha_rw_split"] + ) +} + +data "g42cloud_dcs_templates" "product_type_filter" { + type = "sys" + product_type = "enterprise" + + depends_on = [data.g42cloud_dcs_templates.test] +} +output "product_type_filter_is_useful" { + value = length(data.g42cloud_dcs_templates.product_type_filter.templates) > 0 && alltrue( + [for v in data.g42cloud_dcs_templates.product_type_filter.templates[*].product_type : v == "enterprise"] + ) +} + +data "g42cloud_dcs_templates" "storage_type_filter" { + type = "user" + storage_type = "DRAM" + + depends_on = [data.g42cloud_dcs_templates.test] +} +output "storage_type_filter_is_useful" { + value = length(data.g42cloud_dcs_templates.storage_type_filter.templates) > 0 && alltrue( + [for v in data.g42cloud_dcs_templates.storage_type_filter.templates[*].storage_type : v == "DRAM"] + ) +} +` +} diff --git a/g42cloud/services/acceptance/dcs/resource_g42cloud_dcs_backup_test.go b/g42cloud/services/acceptance/dcs/resource_g42cloud_dcs_backup_test.go new file mode 100644 index 00000000..994d37d0 --- /dev/null +++ b/g42cloud/services/acceptance/dcs/resource_g42cloud_dcs_backup_test.go @@ -0,0 +1,145 @@ +package dcs + +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/g42cloud-terraform/terraform-provider-g42cloud/g42cloud/services/acceptance" + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config" + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils" +) + +func getDcsBackupResourceFunc(cfg *config.Config, state *terraform.ResourceState) (interface{}, error) { + region := acceptance.G42_REGION_NAME + // getBackup: Query DCS backup + var ( + getBackupHttpUrl = "v2/{project_id}/instances/{instance_id}/backups" + getBackupProduct = "dcs" + ) + getBackupClient, err := cfg.NewServiceClient(getBackupProduct, region) + if err != nil { + return nil, fmt.Errorf("error creating DCS Client: %s", err) + } + + parts := strings.SplitN(state.Primary.ID, "/", 2) + if len(parts) != 2 { + return nil, fmt.Errorf("invalid id format, must be /") + } + instanceID := parts[0] + backupId := parts[1] + getBackupPath := getBackupClient.Endpoint + getBackupHttpUrl + getBackupPath = strings.ReplaceAll(getBackupPath, "{project_id}", getBackupClient.ProjectID) + getBackupPath = strings.ReplaceAll(getBackupPath, "{instance_id}", instanceID) + + getDdmSchemasOpt := golangsdk.RequestOpts{ + KeepResponseBody: true, + OkCodes: []int{ + 200, + }, + } + + var currentTotal int + getBackupPath += buildGetDcsBackupQueryParams(currentTotal) + + for { + getBackupResp, err := getBackupClient.Request("GET", getBackupPath, &getDdmSchemasOpt) + if err != nil { + return nil, fmt.Errorf("error retrieving DcsBackup") + } + getBackupRespBody, err := utils.FlattenResponse(getBackupResp) + if err != nil { + return nil, err + } + backups := utils.PathSearch("backup_record_response", getBackupRespBody, make([]interface{}, 0)).([]interface{}) + total := utils.PathSearch("total_num", getBackupRespBody, 0) + for _, backup := range backups { + id := utils.PathSearch("backup_id", backup, "") + if id != backupId { + continue + } + status := utils.PathSearch("status", backup, "") + if status == "deleted" { + return nil, fmt.Errorf("error get DCS backup by backup_id (%s)", backupId) + } + return backup, nil + } + currentTotal += len(backups) + if currentTotal == total { + break + } + getBackupPath = updatePathOffset(getBackupPath, currentTotal) + } + return nil, fmt.Errorf("error get DCS backup by backup_id (%s)", state.Primary.ID) +} + +func buildGetDcsBackupQueryParams(offset int) string { + return fmt.Sprintf("?limit=10&offset=%v", offset) +} + +func updatePathOffset(path string, offset int) string { + index := strings.Index(path, "offset") + return fmt.Sprintf("%soffset=%v", path[:index], offset) +} + +func TestAccDcsBackup_basic(t *testing.T) { + var obj interface{} + + name := acceptance.RandomAccResourceName() + rName := "g42cloud_dcs_backup.test" + + rc := acceptance.InitResourceCheck( + rName, + &obj, + getDcsBackupResourceFunc, + ) + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acceptance.TestAccPreCheck(t) }, + ProviderFactories: acceptance.TestAccProviderFactories, + CheckDestroy: rc.CheckResourceDestroy(), + Steps: []resource.TestStep{ + { + Config: testDcsBackup_basic(name), + Check: resource.ComposeTestCheckFunc( + rc.CheckResourceExists(), + resource.TestCheckResourceAttrPair(rName, "instance_id", + "g42cloud_dcs_instance.instance_1", "id"), + resource.TestCheckResourceAttr(rName, "type", "manual"), + resource.TestCheckResourceAttr(rName, "status", "succeed"), + resource.TestCheckResourceAttr(rName, "description", "test DCS backup remark"), + resource.TestCheckResourceAttr(rName, "backup_format", "rdb"), + resource.TestCheckResourceAttrSet(rName, "name"), + resource.TestCheckResourceAttrSet(rName, "size"), + resource.TestCheckResourceAttrSet(rName, "type"), + resource.TestCheckResourceAttrSet(rName, "begin_time"), + resource.TestCheckResourceAttrSet(rName, "end_time"), + resource.TestCheckResourceAttrSet(rName, "status"), + resource.TestCheckResourceAttrSet(rName, "is_support_restore"), + ), + }, + { + ResourceName: rName, + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +func testDcsBackup_basic(name string) string { + return fmt.Sprintf(` +%s + +resource "g42cloud_dcs_backup" "test" { + instance_id = g42cloud_dcs_instance.instance_1.id + description = "test DCS backup remark" + backup_format = "rdb" +} +`, testAccDcsV1Instance_basic(name)) +} diff --git a/g42cloud/services/acceptance/dcs/resource_g42cloud_dcs_custom_template_test.go b/g42cloud/services/acceptance/dcs/resource_g42cloud_dcs_custom_template_test.go new file mode 100644 index 00000000..25b78694 --- /dev/null +++ b/g42cloud/services/acceptance/dcs/resource_g42cloud_dcs_custom_template_test.go @@ -0,0 +1,136 @@ +package dcs + +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/g42cloud-terraform/terraform-provider-g42cloud/g42cloud/services/acceptance" + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config" + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils" +) + +func getCustomTemplateResourceFunc(cfg *config.Config, state *terraform.ResourceState) (interface{}, error) { + region := acceptance.G42_REGION_NAME + // getCustomTemplate: Query DCS custom template + var ( + getCustomTemplateHttpUrl = "v2/{project_id}/config-templates/{template_id}?type=user" + getCustomTemplateProduct = "dcs" + ) + getCustomTemplateClient, err := cfg.NewServiceClient(getCustomTemplateProduct, region) + if err != nil { + return nil, fmt.Errorf("error creating DCS client: %s", err) + } + + getCustomTemplatePath := getCustomTemplateClient.Endpoint + getCustomTemplateHttpUrl + getCustomTemplatePath = strings.ReplaceAll(getCustomTemplatePath, "{project_id}", getCustomTemplateClient.ProjectID) + getCustomTemplatePath = strings.ReplaceAll(getCustomTemplatePath, "{template_id}", state.Primary.ID) + + getCustomTemplateOpt := golangsdk.RequestOpts{ + KeepResponseBody: true, + MoreHeaders: map[string]string{"Content-Type": "application/json"}, + } + + getCustomTemplateResp, err := getCustomTemplateClient.Request("GET", getCustomTemplatePath, &getCustomTemplateOpt) + if err != nil { + return nil, fmt.Errorf("error retrieving DCS custom template: %s", err) + } + + getCustomTemplateRespBody, err := utils.FlattenResponse(getCustomTemplateResp) + if err != nil { + return nil, fmt.Errorf("error retrieving DCS custom template: %s", err) + } + + return getCustomTemplateRespBody, nil +} + +func TestAccCustomTemplate_basic(t *testing.T) { + var obj interface{} + + name := acceptance.RandomAccResourceName() + updateName := acceptance.RandomAccResourceName() + rName := "g42cloud_dcs_custom_template.test" + + rc := acceptance.InitResourceCheck( + rName, + &obj, + getCustomTemplateResourceFunc, + ) + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acceptance.TestAccPreCheck(t) }, + ProviderFactories: acceptance.TestAccProviderFactories, + CheckDestroy: rc.CheckResourceDestroy(), + Steps: []resource.TestStep{ + { + Config: testCustomTemplate_basic(name), + Check: resource.ComposeTestCheckFunc( + rc.CheckResourceExists(), + resource.TestCheckResourceAttr(rName, "name", name), + resource.TestCheckResourceAttr(rName, "params.#", "1"), + resource.TestCheckResourceAttr(rName, "params.0.param_name", "timeout"), + resource.TestCheckResourceAttr(rName, "params.0.param_value", "200"), + resource.TestCheckResourceAttrSet(rName, "type"), + resource.TestCheckResourceAttrSet(rName, "engine"), + resource.TestCheckResourceAttrSet(rName, "engine_version"), + resource.TestCheckResourceAttrSet(rName, "cache_mode"), + resource.TestCheckResourceAttrSet(rName, "product_type"), + resource.TestCheckResourceAttrSet(rName, "storage_type"), + ), + }, + { + Config: testCustomTemplate_basic_update(updateName), + Check: resource.ComposeTestCheckFunc( + rc.CheckResourceExists(), + resource.TestCheckResourceAttr(rName, "name", updateName), + resource.TestCheckResourceAttr(rName, "params.#", "1"), + resource.TestCheckResourceAttr(rName, "params.0.param_name", "maxmemory-policy"), + resource.TestCheckResourceAttr(rName, "params.0.param_value", "allkeys-lru"), + ), + }, + { + ResourceName: rName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"template_id", "source_type", "params"}, + }, + }, + }) +} + +func testCustomTemplate_basic(name string) string { + return fmt.Sprintf(` +resource "g42cloud_dcs_custom_template" "test" { + template_id = "16" + source_type = "sys" + name = "%s" + description = "test custom policy" + + params { + param_name = "timeout" + param_value = "200" + } +} +`, name) +} + +func testCustomTemplate_basic_update(name string) string { + return fmt.Sprintf(` +resource "g42cloud_dcs_custom_template" "test" { + template_id = "16" + source_type = "sys" + name = "%s" + description = "test custom policy update" + + params { + param_name = "maxmemory-policy" + param_value = "allkeys-lru" + } +} +`, name) +} diff --git a/g42cloud/services/acceptance/dcs/resource_g42cloud_dcs_instance_test.go b/g42cloud/services/acceptance/dcs/resource_g42cloud_dcs_instance_test.go index fb07fcdc..9f4f267d 100644 --- a/g42cloud/services/acceptance/dcs/resource_g42cloud_dcs_instance_test.go +++ b/g42cloud/services/acceptance/dcs/resource_g42cloud_dcs_instance_test.go @@ -212,6 +212,12 @@ data "g42cloud_vpc_subnet" "test" { name = "subnet-default" } +data "g42cloud_dcs_flavors" "test" { + cache_mode = "ha" + capacity = 0.125 + engine_version = "5.0" +} + resource "g42cloud_dcs_instance" "instance_1" { name = "%s" engine_version = "5.0"