Skip to content

Commit

Permalink
feat(DLI): support global variable resource (FlexibleEngineCloud#1034)
Browse files Browse the repository at this point in the history
  • Loading branch information
Zippo-Wang authored and Zhukun-Huawei committed Nov 20, 2023
1 parent b7279eb commit 4dcbbcd
Show file tree
Hide file tree
Showing 3 changed files with 179 additions and 9 deletions.
48 changes: 48 additions & 0 deletions docs/resources/dli_global_variable.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
subcategory: "Data Lake Insight (DLI)"
---

# flexibleengine_dli_global_variable

Manages a DLI global variable resource within FlexibleEngine.

## 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
```
Original file line number Diff line number Diff line change
@@ -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)
}
19 changes: 10 additions & 9 deletions flexibleengine/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -494,15 +494,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(),
Expand Down

0 comments on commit 4dcbbcd

Please sign in to comment.