-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(DLI): support global variable resource (FlexibleEngineCloud#1034)
- Loading branch information
1 parent
b7279eb
commit 4dcbbcd
Showing
3 changed files
with
179 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
121 changes: 121 additions & 0 deletions
121
flexibleengine/acceptance/resource_flexibleengine_dli_global_variable_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters