From 61b56ff67ba0dca0e0509df40afe0c54c0ee7625 Mon Sep 17 00:00:00 2001 From: zhukun <2019229048@tju.edu.cn> Date: Fri, 24 Nov 2023 10:59:58 +0800 Subject: [PATCH] tmp --- docs/data-sources/dcs_flavors_v1.md | 73 +++++++++ docs/data-sources/dcs_instances_v1.md | 96 ++++++++++++ docs/resources/dcs_backup_v1.md | 82 ++++++++++ flexibleengine/acceptance/common.go | 59 +++++++ ...urce_flexibleengine_dcs_flavors_v1_test.go | 39 +++++ ...ce_flexibleengine_dcs_instances_v1_test.go | 43 ++++++ ...e_flexibleengine_obs_bucket_object_test.go | 1 - ...ource_flexibleengine_dcs_backup_v1_test.go | 145 ++++++++++++++++++ flexibleengine/provider.go | 7 + 9 files changed, 544 insertions(+), 1 deletion(-) create mode 100644 docs/data-sources/dcs_flavors_v1.md create mode 100644 docs/data-sources/dcs_instances_v1.md create mode 100644 docs/resources/dcs_backup_v1.md create mode 100644 flexibleengine/acceptance/data_source_flexibleengine_dcs_flavors_v1_test.go create mode 100644 flexibleengine/acceptance/data_source_flexibleengine_dcs_instances_v1_test.go create mode 100644 flexibleengine/acceptance/resource_flexibleengine_dcs_backup_v1_test.go diff --git a/docs/data-sources/dcs_flavors_v1.md b/docs/data-sources/dcs_flavors_v1.md new file mode 100644 index 00000000..1e8dc24a --- /dev/null +++ b/docs/data-sources/dcs_flavors_v1.md @@ -0,0 +1,73 @@ +--- +subcategory: "Distributed Cache Service (DCS)" +--- + +# flexibleengine_dcs_flavors_v1_v1 + +Use this data source to get a list of available DCS flavors. + +## Example Usage + +```hcl +data "flexibleengine_dcs_flavors_v1" "flavors" { + capacity = "4" +} +``` + +## Argument Reference + +The following arguments are supported: + +* `region` - (Optional, String) Specifies the region in which to obtain the DCS flavors. + If omitted, the provider-level region will be used. + +* `capacity` - (Required, Float) The total memory of the cache, in GB. + + **Redis4.0, Redis5.0 and Redis6.0**: Stand-alone and active/standby type instance values: + `0.125`, `0.25`, `0.5`, `1`, `2`, `4`, `8`, `16`, `32` and `64`. + Cluster instance specifications support `4`,`8`,`16`,`24`, `32`, `48`, `64`, `96`, `128`, `192`, + `256`, `384`, `512`, `768` and `1024`. + + **Redis3.0**: Stand-alone and active/standby type instance values: `2`, `4`, `8`, `16`, `32` and `64`. + Proxy cluster instance specifications support `64`, `128`, `256`, `512`, and `1024`. + + **Memcached**: Stand-alone and active/standby type instance values: `2`, `4`, `8`, `16`, `32` and `64`. + +* `engine` - (Optional, String) The engine of the cache instance. Valid values are *Redis* and *Memcached*. + Default value is *Redis*. + +* `engine_version` - (Optional, String) The version of a cache engine. + It is mandatory when the engine is *Redis*, the value can be `3.0`, `4.0`, `5.0`, or `6.0`. + +* `cache_mode` - (Optional, String) The mode of a cache engine. The valid values are as follows: + + `single` - Single-node. + + `ha` - Master/Standby. + + `cluster` - Redis Cluster. + + `proxy` - Proxy Cluster. Redis6.0 not support this mode. + + `ha_rw_split` - Read/Write splitting. Redis6.0 not support this mode. + +* `name` - (Optional, String) The flavor name of the cache instance. + +* `cpu_architecture` - (Optional, String) The CPU architecture of cache instance. + Valid values *x86_64* and *aarch64*. + +## Attribute Reference + +In addition to all arguments above, the following attributes are exported: + +* `flavors` - A list of DCS flavors. + +The `flavors` block supports: + +* `name` - The flavor name of the cache instance. + +* `cache_mode` - The mode of a cache instance. + +* `engine` - The engine of the cache instance. Value is *redis* or *memcached*. + +* `engine_versions` - Supported versions of the specification. + +* `cpu_architecture` - The CPU architecture of cache instance. Value is *x86_64* or *aarch64*. + +* `capacity` - The total memory of the cache, in GB. + +* `available_zones` - An array of available zones where the cache specification can be used. + +* `ip_count` - Number of IP addresses corresponding to the specifications. diff --git a/docs/data-sources/dcs_instances_v1.md b/docs/data-sources/dcs_instances_v1.md new file mode 100644 index 00000000..20951cca --- /dev/null +++ b/docs/data-sources/dcs_instances_v1.md @@ -0,0 +1,96 @@ +--- +subcategory: "Distributed Cache Service (DCS)" +--- + +# flexibleengine_dcs_instances_v1 + +Use this data source to get the list of DCS instances. + +## Example Usage + +```hcl +data "flexibleengine_dcs_instances_v1" "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 [Instance](#DcsInstance_Instance) structure is documented below. + + +The `Instance` 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. + +* `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/resources/dcs_backup_v1.md b/docs/resources/dcs_backup_v1.md new file mode 100644 index 00000000..428aca6c --- /dev/null +++ b/docs/resources/dcs_backup_v1.md @@ -0,0 +1,82 @@ +--- +subcategory: "Distributed Cache Service (DCS)" +--- + +# flexibleengine_dcs_backup_v1 + +Manages a DCS backup resource within FlexibleEngine. + +## Example Usage + +```hcl +variable "dcs_instance_id" {} + +resource "flexibleengine_dcs_backup_v1" "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 flexibleengine_dcs_backup_v1.test / +``` diff --git a/flexibleengine/acceptance/common.go b/flexibleengine/acceptance/common.go index 38eb2858..9e251089 100644 --- a/flexibleengine/acceptance/common.go +++ b/flexibleengine/acceptance/common.go @@ -60,3 +60,62 @@ data "flexibleengine_images_image" "test" { } `, testBaseNetwork(name)) } + +func testAccDcsV1Instance_network(rName string) string { + return fmt.Sprintf(` +resource "flexibleengine_vpc_v1" "vpc_1" { + name = "%[1]s" + cidr = "192.168.0.0/16" +} + +resource "flexibleengine_vpc_subnet_v1" "subnet_1" { + name = "%[1]s" + cidr = "192.168.0.0/24" + gateway_ip = "192.168.0.1" + vpc_id = flexibleengine_vpc_v1.vpc_1.id +} + +resource "flexibleengine_networking_secgroup_v2" "secgroup_1" { + name = "%[1]s" + description = "secgroup_1" +} +`, rName) +} + +func testAccDcsV1Instance_basic(rName string) string { + return fmt.Sprintf(` +%s + +data "flexibleengine_dcs_flavors" "test" { + cache_mode = "ha" + capacity = 0.125 + engine_version = "5.0" +} + +data "flexibleengine_dcs_product_v1" "product1" { + engine = "Redis" + engine_version = "4.0;5.0" + cache_mode = "cluster" + capacity = 8 + replica_count = 2 +} + +resource "flexibleengine_dcs_instance_v1" "instance_1" { + name = "%s" + engine = "Redis" + engine_version = "5.0" + password = "Huawei_test" + product_id = data.flexibleengine_dcs_product_v1.product1.id + capacity = 8 + vpc_id = flexibleengine_vpc_v1.vpc_1.id + network_id = flexibleengine_vpc_subnet_v1.subnet_1.id + available_zones = ["eu-west-0a", "eu-west-0b"] + + save_days = 1 + backup_type = "manual" + begin_at = "00:00-01:00" + period_type = "weekly" + backup_at = [1] +} +`, testAccDcsV1Instance_network(rName), rName) +} diff --git a/flexibleengine/acceptance/data_source_flexibleengine_dcs_flavors_v1_test.go b/flexibleengine/acceptance/data_source_flexibleengine_dcs_flavors_v1_test.go new file mode 100644 index 00000000..e447c9c2 --- /dev/null +++ b/flexibleengine/acceptance/data_source_flexibleengine_dcs_flavors_v1_test.go @@ -0,0 +1,39 @@ +package acceptance + +import ( + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance" +) + +func TestAccDataSourceDcsFlavorsV2_basic(t *testing.T) { + dataSourceName := "data.flexibleengine_dcs_flavors_v1.flavors" + dc := acceptance.InitDataSourceCheck(dataSourceName) + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + ProviderFactories: TestAccProviderFactories, + Steps: []resource.TestStep{ + { + Config: testAccDcsFlavorsV2_conf(), + Check: resource.ComposeTestCheckFunc( + dc.CheckResourceExists(), + resource.TestCheckResourceAttrSet(dataSourceName, "flavors.#"), + resource.TestCheckResourceAttr(dataSourceName, "flavors.0.engine", "redis"), + resource.TestCheckResourceAttr(dataSourceName, "flavors.0.capacity", "0.125"), + ), + }, + }, + }) +} + +func testAccDcsFlavorsV2_conf() string { + return ` +data "flexibleengine_dcs_flavors_v1" "flavors" { + engine = "Redis" + capacity = 0.125 +} +` +} diff --git a/flexibleengine/acceptance/data_source_flexibleengine_dcs_instances_v1_test.go b/flexibleengine/acceptance/data_source_flexibleengine_dcs_instances_v1_test.go new file mode 100644 index 00000000..c1b68806 --- /dev/null +++ b/flexibleengine/acceptance/data_source_flexibleengine_dcs_instances_v1_test.go @@ -0,0 +1,43 @@ +package acceptance + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance" +) + +func TestAccDatasourceDcsInstance_basic(t *testing.T) { + rName := "data.flexibleengine_dcs_instances_v1.test" + name := acceptance.RandomAccResourceName() + dc := acceptance.InitDataSourceCheck(rName) + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + ProviderFactories: 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", "6379"), + resource.TestCheckResourceAttr(rName, "instances.0.flavor", "redis.cluster.xu1.large.r2.s1.8"), + ), + }, + }, + }) +} + +func testAccDatasourceDcsInstance_basic(name string) string { + return fmt.Sprintf(` +%s + +data "flexibleengine_dcs_instances_v1" "test" { + name = flexibleengine_dcs_instance_v1.instance_1.name + status = "RUNNING" +} +`, testAccDcsV1Instance_basic(name)) +} diff --git a/flexibleengine/acceptance/data_source_flexibleengine_obs_bucket_object_test.go b/flexibleengine/acceptance/data_source_flexibleengine_obs_bucket_object_test.go index 10b1e789..b9002dee 100644 --- a/flexibleengine/acceptance/data_source_flexibleengine_obs_bucket_object_test.go +++ b/flexibleengine/acceptance/data_source_flexibleengine_obs_bucket_object_test.go @@ -163,7 +163,6 @@ func testAccCheckObsBucketObjectExists(n string) resource.TestCheckFunc { } } - func testAccCheckObsObjectDataSourceExists(n string) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[n] diff --git a/flexibleengine/acceptance/resource_flexibleengine_dcs_backup_v1_test.go b/flexibleengine/acceptance/resource_flexibleengine_dcs_backup_v1_test.go new file mode 100644 index 00000000..4c74db00 --- /dev/null +++ b/flexibleengine/acceptance/resource_flexibleengine_dcs_backup_v1_test.go @@ -0,0 +1,145 @@ +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 getDcsBackupResourceFunc(cfg *config.Config, state *terraform.ResourceState) (interface{}, error) { + region := OS_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 := "flexibleengine_dcs_backup_v1.test" + + rc := acceptance.InitResourceCheck( + rName, + &obj, + getDcsBackupResourceFunc, + ) + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + ProviderFactories: TestAccProviderFactories, + CheckDestroy: rc.CheckResourceDestroy(), + Steps: []resource.TestStep{ + { + Config: testDcsBackup_basic(name), + Check: resource.ComposeTestCheckFunc( + rc.CheckResourceExists(), + resource.TestCheckResourceAttrPair(rName, "instance_id", + "flexibleengine_dcs_instance_v1.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 "flexibleengine_dcs_backup_v1" "test" { + instance_id = flexibleengine_dcs_instance_v1.instance_1.id + description = "test DCS backup remark" + backup_format = "rdb" +} +`, testAccDcsV1Instance_basic(name)) +} diff --git a/flexibleengine/provider.go b/flexibleengine/provider.go index 27b082b1..1ff39e3f 100644 --- a/flexibleengine/provider.go +++ b/flexibleengine/provider.go @@ -18,6 +18,7 @@ import ( "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/cbr" "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/cce" "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/cse" + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/dcs" "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/ddm" "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/dds" "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/deprecated" @@ -291,6 +292,9 @@ func Provider() *schema.Provider { "flexibleengine_compute_servergroups": ecs.DataSourceComputeServerGroups(), + "flexibleengine_dcs_flavors_v1": dcs.DataSourceDcsFlavorsV2(), + "flexibleengine_dcs_instances_v1": dcs.DataSourceDcsInstance(), + "flexibleengine_ddm_engines": ddm.DataSourceDdmEngines(), "flexibleengine_ddm_flavors": ddm.DataSourceDdmFlavors(), "flexibleengine_ddm_instance_nodes": ddm.DataSourceDdmInstanceNodes(), @@ -496,6 +500,9 @@ func Provider() *schema.Provider { "flexibleengine_cse_microservice_engine": cse.ResourceMicroserviceEngine(), "flexibleengine_cse_microservice_instance": cse.ResourceMicroserviceInstance(), + "flexibleengine_dcs_backup_v1": dcs.ResourceDcsBackup(), + "flexibleengine_dcs_instance": dcs.ResourceDcsInstance(), + "flexibleengine_ddm_instance": ddm.ResourceDdmInstance(), "flexibleengine_ddm_schema": ddm.ResourceDdmSchema(), "flexibleengine_ddm_account": ddm.ResourceDdmAccount(),