forked from huaweicloud/terraform-provider-huaweicloud
-
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.
- Loading branch information
1 parent
13c2f4b
commit b7fe7b5
Showing
5 changed files
with
588 additions
and
0 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,80 @@ | ||
--- | ||
subcategory: "Cloud Operations Center (COC)" | ||
--- | ||
|
||
# huaweicloud_coc_script | ||
|
||
Manages COC script resource within HuaweiCloud. | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
resource "huaweicloud_coc_script" "test" { | ||
name = "demo" | ||
description = "a demo script" | ||
risk_level = "LOW" | ||
version = "1.0.0" | ||
type = "SHELL" | ||
content = <<EOF | ||
#! /bin/bash | ||
echo "hello $${name}!" | ||
EOF | ||
parameters { | ||
name = "name" | ||
value = "world" | ||
description = "the first parameter" | ||
} | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
The following arguments are supported: | ||
|
||
* `name` - (Required, String, ForceNew) Specifies the name of the script. The value can contains 3 to 64 characters, | ||
including letters, digits, hyphens (-), and underscores (_). Changing this creates a new resource. | ||
|
||
* `description` - (Required, String) Specifies the description of the script. | ||
The value can consist of up to 256 characters. | ||
|
||
* `risk_level` - (Required, String) Specifies the risk level. The valid values are **LOW**, **MEDIUM** and **HIGH**. | ||
|
||
* `version` - (Required, String) Specifies the version of the script. For example, *1.0.0* or *1.1.0*. | ||
|
||
* `type` - (Required, String) Specifies the content type of the script. The valid values are **SHELL**, **PYTHON** and **BAT**. | ||
|
||
* `content` - (Required, String) Specifies the content of the script. | ||
The value can consist of up to 4096 characters. | ||
|
||
* `parameters` - (Optional, List) Specifies the input parameters of the script. | ||
Up to 20 script parameters can be added. The [parameters](#block--parameters) structure is documented below. | ||
|
||
<a name="block--parameters"></a> | ||
The `parameters` block supports: | ||
|
||
* `name` - (Required, String) Specifies the name of the parameter. | ||
|
||
* `value` - (Required, String) Specifies the **default** value of the parameter. | ||
|
||
* `description` - (Required, String) Specifies the description of the parameter. | ||
|
||
* `sensitive` - (Optional, Bool) Specifies whether the parameter is sensitive. | ||
|
||
## Attribute Reference | ||
|
||
In addition to all arguments above, the following attributes are exported: | ||
|
||
* `id` - The resource ID. | ||
* `status` - The status of the script. | ||
* `created_at` - The creation time of the script. | ||
* `updated_at` - The latest update time of the script. | ||
|
||
## Import | ||
|
||
The COC script can be imported using `id`, e.g. | ||
|
||
```bash | ||
$ terraform import huaweicloud_coc_script.test <id> | ||
``` |
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
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
146 changes: 146 additions & 0 deletions
146
huaweicloud/services/acceptance/coc/resource_huaweicloud_coc_script_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,146 @@ | ||
package coc | ||
|
||
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 getScriptResourceFunc(conf *config.Config, state *terraform.ResourceState) (interface{}, error) { | ||
client, err := conf.NewServiceClient("coc", acceptance.HW_REGION_NAME) | ||
if err != nil { | ||
return nil, fmt.Errorf("error creating COC client: %s", err) | ||
} | ||
|
||
getScriptHttpUrl := "v1/job/scripts/{id}" | ||
getScriptPath := client.Endpoint + getScriptHttpUrl | ||
getScriptPath = strings.ReplaceAll(getScriptPath, "{id}", state.Primary.ID) | ||
|
||
getScriptOpt := golangsdk.RequestOpts{ | ||
KeepResponseBody: true, | ||
MoreHeaders: map[string]string{"Content-Type": "application/json"}, | ||
} | ||
|
||
getScriptResp, err := client.Request("GET", getScriptPath, &getScriptOpt) | ||
if err != nil { | ||
return nil, fmt.Errorf("error retrieving COC script: %s", err) | ||
} | ||
|
||
getScriptRespBody, err := utils.FlattenResponse(getScriptResp) | ||
if err != nil { | ||
return nil, fmt.Errorf("error retrieving COC script: %s", err) | ||
} | ||
|
||
return getScriptRespBody, nil | ||
} | ||
|
||
func TestAccScript_basic(t *testing.T) { | ||
var obj interface{} | ||
rName := acceptance.RandomAccResourceName() | ||
resourceName := "huaweicloud_coc_script.test" | ||
|
||
rc := acceptance.InitResourceCheck( | ||
resourceName, | ||
&obj, | ||
getScriptResourceFunc, | ||
) | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { | ||
acceptance.TestAccPreCheck(t) | ||
acceptance.TestAccPreCheckInternal(t) | ||
}, | ||
ProviderFactories: acceptance.TestAccProviderFactories, | ||
CheckDestroy: rc.CheckResourceDestroy(), | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: tesScript_basic(rName), | ||
Check: resource.ComposeTestCheckFunc( | ||
rc.CheckResourceExists(), | ||
resource.TestCheckResourceAttr(resourceName, "name", rName), | ||
resource.TestCheckResourceAttr(resourceName, "description", "a demo script"), | ||
resource.TestCheckResourceAttr(resourceName, "risk_level", "LOW"), | ||
resource.TestCheckResourceAttr(resourceName, "version", "1.0.0"), | ||
resource.TestCheckResourceAttr(resourceName, "parameters.#", "1"), | ||
resource.TestCheckResourceAttrSet(resourceName, "created_at"), | ||
), | ||
}, | ||
{ | ||
ResourceName: resourceName, | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
}, | ||
{ | ||
Config: tesScript_updated(rName), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr(resourceName, "name", rName), | ||
resource.TestCheckResourceAttr(resourceName, "description", "a new demo script"), | ||
resource.TestCheckResourceAttr(resourceName, "risk_level", "MEDIUM"), | ||
resource.TestCheckResourceAttr(resourceName, "version", "1.0.1"), | ||
resource.TestCheckResourceAttr(resourceName, "parameters.#", "2"), | ||
resource.TestCheckResourceAttrSet(resourceName, "created_at"), | ||
resource.TestCheckResourceAttrSet(resourceName, "updated_at"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func tesScript_basic(name string) string { | ||
return fmt.Sprintf(` | ||
resource "huaweicloud_coc_script" "test" { | ||
name = "%s" | ||
description = "a demo script" | ||
risk_level = "LOW" | ||
version = "1.0.0" | ||
type = "SHELL" | ||
content = <<EOF | ||
#! /bin/bash | ||
echo "hello $${name}!" | ||
EOF | ||
parameters { | ||
name = "name" | ||
value = "world" | ||
description = "the first parameter" | ||
} | ||
}`, name) | ||
} | ||
|
||
func tesScript_updated(name string) string { | ||
return fmt.Sprintf(` | ||
resource "huaweicloud_coc_script" "test" { | ||
name = "%s" | ||
description = "a new demo script" | ||
risk_level = "MEDIUM" | ||
version = "1.0.1" | ||
type = "SHELL" | ||
content = <<EOF | ||
#! /bin/bash | ||
echo "hello $${name}@$${company}!" | ||
EOF | ||
parameters { | ||
name = "name" | ||
value = "world" | ||
description = "the first parameter" | ||
} | ||
parameters { | ||
name = "company" | ||
value = "Huawei" | ||
description = "the second parameter" | ||
sensitive = true | ||
} | ||
}`, name) | ||
} |
Oops, something went wrong.