From a7d97c74803b9ce6592a6277908adc9f61e56352 Mon Sep 17 00:00:00 2001 From: Zippo-Wang <852420284@qq.com> Date: Mon, 15 Jan 2024 19:48:26 +0800 Subject: [PATCH] WIP: feat(DLI): Synchronized DLI resources and data sources. --- docs/resources/dli_queue.md | 46 ++-- .../resource_flexibleengine_dli_queue_test.go | 209 ++++++++++++++++++ flexibleengine/provider.go | 3 +- 3 files changed, 243 insertions(+), 15 deletions(-) create mode 100644 flexibleengine/acceptance/resource_flexibleengine_dli_queue_test.go diff --git a/docs/resources/dli_queue.md b/docs/resources/dli_queue.md index 891f0fa50..9438405cc 100644 --- a/docs/resources/dli_queue.md +++ b/docs/resources/dli_queue.md @@ -28,33 +28,53 @@ resource "flexibleengine_dli_queue" "queue" { The following arguments are supported: +* `region` - (Optional, String, ForceNew) Specifies the region in which to create the dli queue resource. If omitted, + the provider-level region will be used. Changing this will create a new VPC channel resource. + * `cu_count` - (Required, Int) Minimum number of CUs that are bound to a queue. Initial value can be `16`, - `64`, or `256`. When scale_out or scale_in, the number must be a multiple of 16 + `64`, or `256`. When scale_out or scale_in, the number must be a multiple of 16. * `name` - (Required, String, ForceNew) Name of a queue. Name of a newly created resource queue. - The name can contain only digits, letters, and underscores (\_), - but cannot contain only digits or start with an underscore (_). - Length range: 1 to 128 characters. Changing this parameter will create a new resource. + The name can contain only digits, letters, and underscores (\_), + but cannot contain only digits or start with an underscore (_). + Length range: 1 to 128 characters. Changing this parameter will create a new resource. * `description` - (Optional, String, ForceNew) Description of a queue. - Changing this parameter will create a new resource. + Changing this parameter will create a new resource. * `queue_type` - (Optional, String, ForceNew) Indicates the queue type. - Changing this parameter will create a new resource. The options are as follows: - - sql, - - general + Changing this parameter will create a new resource. The options are as follows: + - **sql**. SQL queue type, used to run SQL jobs. + - **general**. Universal queue type for running Flink, Spark Jar jobs. The default value is `sql`. * `resource_mode` - (Optional, String, ForceNew) Queue resource mode. Changing this parameter will create a new resource. The options are as follows: - - 0: indicates the shared resource mode. - - 1: indicates the exclusive resource mode. + - **0**: indicates the shared resource mode. + - **1**: indicates the exclusive resource mode. + +* `feature` - (Optional, String, ForceNew)Indicates the queue feature. Changing this parameter will create a new + resource. The options are as follows: + - **basic**: basic type (default value) + +* `platform` - (Optional, String, ForceNew) CPU architecture of queue compute resources. Changing this parameter will + create a new resource. The options are as follows: + - **x86_64** : default value. + - **aarch64**. + +* `vpc_cidr` - (Optional, String) The CIDR block of a queue. If use DLI enhanced datasource connections, the CIDR block + cannot be the same as that of the data source. + The CIDR blocks supported by different CU specifications: + + - When `cu_count` is `16` or `64`: 10.0.0.0~10.255.0.0/8~24, 172.16.0.0~172.31.0.0/12~24, + 192.168.0.0~192.168.0.0/16~24. + - When `cu_count` is `256`: 10.0.0.0~10.255.0.0/8~22, 172.16.0.0~172.31.0.0/12~22, 192.168.0.0~192.168.0.0/16~22. * `tags` - (Optional, Map, ForceNew) Label of a queue. Changing this parameter will create a new resource. -## Attributes Reference +## Attribute Reference In addition to all arguments above, the following attributes are exported: @@ -66,12 +86,12 @@ In addition to all arguments above, the following attributes are exported: This resource provides the following timeouts configuration options: -* `update` - Default is 45 minute. +* `update` - Default is 45 minutes. ## Import DLI queue can be imported by `id`. For example, ```shell -terraform import flexibleengine_dli_queue.example abc123 +terraform import flexibleengine_dli_queue.example abc123 ``` diff --git a/flexibleengine/acceptance/resource_flexibleengine_dli_queue_test.go b/flexibleengine/acceptance/resource_flexibleengine_dli_queue_test.go new file mode 100644 index 000000000..184f6c2d6 --- /dev/null +++ b/flexibleengine/acceptance/resource_flexibleengine_dli_queue_test.go @@ -0,0 +1,209 @@ +package acceptance + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" + + "github.com/chnsz/golangsdk/openstack/dli/v1/queues" + + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config" + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance" +) + +const ( + CU16 = 16 + QueueTypeGeneral = "general" + QueueTypeSQL = "sql" +) + +func getDliQueueResourceFunc(cfg *config.Config, state *terraform.ResourceState) (interface{}, error) { + client, err := cfg.DliV1Client(OS_REGION_NAME) + if err != nil { + return nil, fmt.Errorf("error creating Dli v1 client, err=%s", err) + } + + result := queues.Get(client, state.Primary.Attributes["name"]) + return result.Body, result.Err +} + +func TestAccDliQueue_basic(t *testing.T) { + rName := acceptance.RandomAccResourceName() + resourceName := "flexibleengine_dli_queue.test" + + var obj queues.CreateOpts + rc := acceptance.InitResourceCheck( + resourceName, + &obj, + getDliQueueResourceFunc, + ) + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acceptance.TestAccPreCheck(t) }, + ProviderFactories: TestAccProviderFactories, + CheckDestroy: rc.CheckResourceDestroy(), + Steps: []resource.TestStep{ + { + Config: testAccDliQueue_basic(rName, CU16), + Check: resource.ComposeTestCheckFunc( + rc.CheckResourceExists(), + resource.TestCheckResourceAttr(resourceName, "name", rName), + resource.TestCheckResourceAttr(resourceName, "queue_type", QueueTypeSQL), + resource.TestCheckResourceAttr(resourceName, "cu_count", fmt.Sprintf("%d", CU16)), + resource.TestCheckResourceAttrSet(resourceName, "resource_mode"), + resource.TestCheckResourceAttrSet(resourceName, "create_time"), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateIdFunc: testAccQueueImportStateFunc(resourceName), + ImportStateVerifyIgnore: []string{ + "tags", + }, + }, + }, + }) +} + +func testAccQueueImportStateFunc(rName string) resource.ImportStateIdFunc { + return func(s *terraform.State) (string, error) { + rs, ok := s.RootModule().Resources[rName] + if !ok { + return "", fmt.Errorf("resource (%s) not found: %s", rName, rs) + } + name := rs.Primary.Attributes["name"] + if name == "" { + return "", fmt.Errorf("the queue name is incorrect, got '%s'", name) + } + return name, nil + } +} + +func TestAccDliQueue_withGeneral(t *testing.T) { + rName := acceptance.RandomAccResourceName() + resourceName := "flexibleengine_dli_queue.test" + + var obj queues.CreateOpts + rc := acceptance.InitResourceCheck( + resourceName, + &obj, + getDliQueueResourceFunc, + ) + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acceptance.TestAccPreCheck(t) }, + ProviderFactories: TestAccProviderFactories, + CheckDestroy: rc.CheckResourceDestroy(), + Steps: []resource.TestStep{ + { + Config: testAccDliQueue_withGeneral(rName, CU16), + Check: resource.ComposeTestCheckFunc( + rc.CheckResourceExists(), + resource.TestCheckResourceAttr(resourceName, "name", rName), + resource.TestCheckResourceAttr(resourceName, "queue_type", QueueTypeGeneral), + resource.TestCheckResourceAttr(resourceName, "cu_count", fmt.Sprintf("%d", CU16)), + resource.TestCheckResourceAttrSet(resourceName, "resource_mode"), + resource.TestCheckResourceAttrSet(resourceName, "create_time"), + ), + }, + }, + }) +} + +func testAccDliQueue_basic(rName string, cuCount int) string { + return fmt.Sprintf(` +resource "flexibleengine_dli_queue" "test" { + name = "%s" + cu_count = %d + + tags = { + foo = "bar" + } +} +`, rName, cuCount) +} + +func testAccDliQueue_withGeneral(rName string, cuCount int) string { + return fmt.Sprintf(` +resource "flexibleengine_dli_queue" "test" { + name = "%s" + cu_count = %d + queue_type = "general" + + tags = { + foo = "bar" + } +} +`, rName, cuCount) +} + +func TestAccDliQueue_cidr(t *testing.T) { + rName := acceptance.RandomAccResourceName() + resourceName := "flexibleengine_dli_queue.test" + + var obj queues.CreateOpts + rc := acceptance.InitResourceCheck( + resourceName, + &obj, + getDliQueueResourceFunc, + ) + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acceptance.TestAccPreCheck(t) }, + ProviderFactories: TestAccProviderFactories, + CheckDestroy: rc.CheckResourceDestroy(), + Steps: []resource.TestStep{ + { + Config: testAccDliQueue_cidr(rName, "172.16.0.0/21"), + Check: resource.ComposeTestCheckFunc( + rc.CheckResourceExists(), + resource.TestCheckResourceAttr(resourceName, "name", rName), + resource.TestCheckResourceAttr(resourceName, "queue_type", QueueTypeSQL), + resource.TestCheckResourceAttr(resourceName, "cu_count", "16"), + resource.TestCheckResourceAttr(resourceName, "resource_mode", "1"), + resource.TestCheckResourceAttr(resourceName, "vpc_cidr", "172.16.0.0/21"), + resource.TestCheckResourceAttrSet(resourceName, "create_time"), + ), + }, + { + + Config: testAccDliQueue_cidr(rName, "172.16.0.0/18"), + Check: resource.ComposeTestCheckFunc( + rc.CheckResourceExists(), + resource.TestCheckResourceAttr(resourceName, "name", rName), + resource.TestCheckResourceAttr(resourceName, "queue_type", QueueTypeSQL), + resource.TestCheckResourceAttr(resourceName, "cu_count", "16"), + resource.TestCheckResourceAttr(resourceName, "resource_mode", "1"), + resource.TestCheckResourceAttr(resourceName, "vpc_cidr", "172.16.0.0/18"), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateIdFunc: testAccQueueImportStateFunc(resourceName), + ImportStateVerifyIgnore: []string{ + "tags", + }, + }, + }, + }) +} + +func testAccDliQueue_cidr(rName string, cidr string) string { + return fmt.Sprintf(` +resource "flexibleengine_dli_queue" "test" { + name = "%s" + cu_count = 16 + resource_mode = 1 + vpc_cidr = "%s" + + tags = { + foo = "bar" + } +}`, rName, cidr) +} diff --git a/flexibleengine/provider.go b/flexibleengine/provider.go index 5a861c878..d2e740d34 100644 --- a/flexibleengine/provider.go +++ b/flexibleengine/provider.go @@ -547,8 +547,6 @@ func Provider() *schema.Provider { "flexibleengine_waf_rule_precise_protection": resourceWafRulePreciseProtection(), "flexibleengine_waf_rule_web_tamper_protection": resourceWafRuleWebTamperProtection(), - "flexibleengine_dli_queue": ResourceDliQueueV1(), - // importing new resource "flexibleengine_aom_service_discovery_rule": aom.ResourceServiceDiscoveryRule(), @@ -610,6 +608,7 @@ func Provider() *schema.Provider { "flexibleengine_dli_flinksql_job": dli.ResourceFlinkSqlJob(), "flexibleengine_dli_global_variable": dli.ResourceGlobalVariable(), "flexibleengine_dli_package": dli.ResourceDliPackageV2(), + "flexibleengine_dli_queue": dli.ResourceDliQueue(), "flexibleengine_dli_spark_job": dli.ResourceDliSparkJobV2(), "flexibleengine_dli_sql_job": dli.ResourceSqlJob(), "flexibleengine_dli_table": dli.ResourceDliTable(),