From 16879add04c989c5f9239933087605661b505980 Mon Sep 17 00:00:00 2001 From: Zippo-Wang <852420284@qq.com> Date: Wed, 11 Oct 2023 14:12:48 +0800 Subject: [PATCH] test(DLI): import DLI global variable resource and add unit test and document. --- docs/resources/dli_global_variable.md | 48 +++++++ ...flexibleengine_dli_global_variable_test.go | 121 ++++++++++++++++++ flexibleengine/provider.go | 19 +-- 3 files changed, 179 insertions(+), 9 deletions(-) create mode 100644 docs/resources/dli_global_variable.md create mode 100644 flexibleengine/acceptance/resource_flexibleengine_dli_global_variable_test.go diff --git a/docs/resources/dli_global_variable.md b/docs/resources/dli_global_variable.md new file mode 100644 index 00000000..cf928735 --- /dev/null +++ b/docs/resources/dli_global_variable.md @@ -0,0 +1,48 @@ +--- +subcategory: "Data Lake Insight (DLI)" +--- + +# flexibleengine_dli_global_variable + +Manages a DLI global variable resource within FlexibleEngineCloud. + +## Example Usage + +```hcl +resource "flexibleengine_dli_global_variable" "test" { + name = "demo" + value = "abc" +} +``` + +## 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, ForceNew) The name of a Global variable. + This parameter can contain a maximum of 128 characters, which may consist of digits, letters, and underscores (\_), + but cannot start with an underscore (\_) or contain only digits. + + Changing this parameter will create a new resource. + +* `value` - (Required, String) The value of Global variable. + +* `is_sensitive` - (Optional, Bool, ForceNew) Whether to set a variable as a sensitive variable. The default value is + **false**. 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 which equals the name. + +## Import + +The global variable can be imported using the `id` which equals the name, e.g. + +```shell +terraform import flexibleengine_dli_global_variable.test demo_name +``` diff --git a/flexibleengine/acceptance/resource_flexibleengine_dli_global_variable_test.go b/flexibleengine/acceptance/resource_flexibleengine_dli_global_variable_test.go new file mode 100644 index 00000000..fc7d65e2 --- /dev/null +++ b/flexibleengine/acceptance/resource_flexibleengine_dli_global_variable_test.go @@ -0,0 +1,121 @@ +package acceptance + +import ( + "encoding/json" + "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/chnsz/golangsdk/pagination" + + "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 getGlobalVariableResourceFunc(cfg *config.Config, state *terraform.ResourceState) (interface{}, error) { + region := OS_REGION_NAME + // getGlobalVariable: Query the Global variable. + var ( + getGlobalVariableHttpUrl = "v1.0/{project_id}/variables" + getGlobalVariableProduct = "dli" + ) + getGlobalVariableClient, err := cfg.NewServiceClient(getGlobalVariableProduct, region) + if err != nil { + return nil, fmt.Errorf("error creating DLI Client: %s", err) + } + + getGlobalVariablePath := getGlobalVariableClient.Endpoint + getGlobalVariableHttpUrl + getGlobalVariablePath = strings.ReplaceAll(getGlobalVariablePath, "{project_id}", getGlobalVariableClient.ProjectID) + + getGlobalVariableResp, err := pagination.ListAllItems( + getGlobalVariableClient, + "offset", + getGlobalVariablePath, + &pagination.QueryOpts{MarkerField: ""}) + if err != nil { + return nil, fmt.Errorf("error retrieving DLI global variable: %s", err) + } + + getGlobalVariableRespJson, err := json.Marshal(getGlobalVariableResp) + if err != nil { + return nil, fmt.Errorf("error retrieving DLI global variable: %s", err) + } + var getGlobalVariableRespBody interface{} + err = json.Unmarshal(getGlobalVariableRespJson, &getGlobalVariableRespBody) + if err != nil { + return nil, fmt.Errorf("error retrieving DLI global variable: %s", err) + } + + jsonPath := fmt.Sprintf("global_vars[?var_name=='%s']|[0]", state.Primary.ID) + globalVariable := utils.PathSearch(jsonPath, getGlobalVariableRespBody, nil) + if globalVariable == nil { + return nil, golangsdk.ErrDefault404{} + } + + return globalVariable, nil +} + +func TestAccGlobalVariable_basic(t *testing.T) { + var obj interface{} + + name := acceptance.RandomAccResourceName() + rName := "flexibleengine_dli_global_variable.test" + + rc := acceptance.InitResourceCheck( + rName, + &obj, + getGlobalVariableResourceFunc, + ) + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acceptance.TestAccPreCheck(t) }, + ProviderFactories: TestAccProviderFactories, + CheckDestroy: rc.CheckResourceDestroy(), + Steps: []resource.TestStep{ + { + Config: testGlobalVariable_basic(name), + Check: resource.ComposeTestCheckFunc( + rc.CheckResourceExists(), + resource.TestCheckResourceAttr(rName, "name", name), + resource.TestCheckResourceAttr(rName, "value", "abc"), + ), + }, + { + Config: testGlobalVariable_basic_update(name), + Check: resource.ComposeTestCheckFunc( + rc.CheckResourceExists(), + resource.TestCheckResourceAttr(rName, "name", name), + resource.TestCheckResourceAttr(rName, "value", "abcd"), + ), + }, + { + ResourceName: rName, + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +func testGlobalVariable_basic(name string) string { + return fmt.Sprintf(` +resource "flexibleengine_dli_global_variable" "test" { + name = "%s" + value = "abc" +} +`, name) +} + +func testGlobalVariable_basic_update(name string) string { + return fmt.Sprintf(` +resource "flexibleengine_dli_global_variable" "test" { + name = "%s" + value = "abcd" +} +`, name) +} diff --git a/flexibleengine/provider.go b/flexibleengine/provider.go index 989b7a00..7037ab6c 100644 --- a/flexibleengine/provider.go +++ b/flexibleengine/provider.go @@ -490,15 +490,16 @@ func Provider() *schema.Provider { "flexibleengine_dms_rocketmq_topic": dms.ResourceDmsRocketMQTopic(), "flexibleengine_dms_rocketmq_user": dms.ResourceDmsRocketMQUser(), - "flexibleengine_dli_database": dli.ResourceDliSqlDatabaseV1(), - "flexibleengine_dli_package": dli.ResourceDliPackageV2(), - "flexibleengine_dli_spark_job": dli.ResourceDliSparkJobV2(), - "flexibleengine_dli_table": dli.ResourceDliTable(), - "flexibleengine_dli_flinksql_job": dli.ResourceFlinkSqlJob(), - "flexibleengine_drs_job": drs.ResourceDrsJob(), - "flexibleengine_fgs_dependency": fgs.ResourceFgsDependency(), - "flexibleengine_fgs_function": fgs.ResourceFgsFunctionV2(), - "flexibleengine_fgs_trigger": fgs.ResourceFunctionGraphTrigger(), + "flexibleengine_dli_database": dli.ResourceDliSqlDatabaseV1(), + "flexibleengine_dli_global_variable": dli.ResourceGlobalVariable(), + "flexibleengine_dli_package": dli.ResourceDliPackageV2(), + "flexibleengine_dli_spark_job": dli.ResourceDliSparkJobV2(), + "flexibleengine_dli_table": dli.ResourceDliTable(), + "flexibleengine_dli_flinksql_job": dli.ResourceFlinkSqlJob(), + "flexibleengine_drs_job": drs.ResourceDrsJob(), + "flexibleengine_fgs_dependency": fgs.ResourceFgsDependency(), + "flexibleengine_fgs_function": fgs.ResourceFgsFunctionV2(), + "flexibleengine_fgs_trigger": fgs.ResourceFunctionGraphTrigger(), "flexibleengine_gaussdb_cassandra_instance": gaussdb.ResourceGeminiDBInstanceV3(), "flexibleengine_gaussdb_influx_instance": gaussdb.ResourceGaussDBInfluxInstanceV3(),