Skip to content

Commit

Permalink
feat(coc): add COC script resource
Browse files Browse the repository at this point in the history
  • Loading branch information
ShiChangkuo committed Nov 18, 2023
1 parent 6ea21bd commit e3b29d6
Show file tree
Hide file tree
Showing 5 changed files with 563 additions and 0 deletions.
80 changes: 80 additions & 0 deletions docs/incubating/coc_script.md
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
params {
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.

* `params` - (Optional, List) Specifies the input parameters of the script.
Up to 20 script parameters can be added. The [params](#block--params) structure is documented below.

<a name="block--params"></a>
The `params` 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>
```
6 changes: 6 additions & 0 deletions huaweicloud/config/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,12 @@ var allServiceCatalog = map[string]ServiceCatalog{
Version: "svcstg/icmgr/v1",
Product: "AOM",
},
"coc": {
Name: "coc",
Version: "v1",
Scope: "global",
Product: "COC",
},
"fgs": {
Name: "functiongraph",
Version: "v2",
Expand Down
3 changes: 3 additions & 0 deletions huaweicloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/cloudtable"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/cmdb"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/cnad"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/coc"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/codearts"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/cph"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/cpts"
Expand Down Expand Up @@ -801,6 +802,8 @@ func Provider() *schema.Provider {
"huaweicloud_compute_eip_associate": ecs.ResourceComputeEIPAssociate(),
"huaweicloud_compute_volume_attach": ecs.ResourceComputeVolumeAttach(),

"huaweicloud_coc_script": coc.ResourceCocScript(),

"huaweicloud_cph_server": cph.ResourceCphServer(),

"huaweicloud_cse_microservice": cse.ResourceMicroservice(),
Expand Down
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, "params.#", "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, "params.#", "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
params {
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
params {
name = "name"
value = "world"
description = "the first parameter"
}
params {
name = "company"
value = "Huawei"
description = "the second parameter"
sensitive = true
}
}`, name)
}
Loading

0 comments on commit e3b29d6

Please sign in to comment.