Skip to content

docs(examples/coc): adjust examples under coc #6900

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 86 additions & 2 deletions examples/coc/script/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,87 @@
# Create COC script
# Create a COC script

In this example, we will create a COC script.
This example provides best practice code for using Terraform to create a COC script in HuaweiCloud.

## Prerequisites

* A HuaweiCloud account
* Terraform installed
* HuaweiCloud access key and secret key (AK/SK)

## Required Variables

The following variables need to be configured:

### Authentication Variables

* `region_name` - The region where the COC script is located
* `access_key` - The access key of the IAM user
* `secret_key` - The secret key of the IAM user

### Resource Variables

#### Required Variables

* `coc_script_name` - The name of the script
* `coc_script_description` - The description of the script
* `coc_script_risk_level` - The risk level of the script
* `coc_script_version` - The version of the script
* `coc_script_type` - The type of the script
* `coc_script_content` - The content of the script
* `coc_script_parameters` - The parameter list of the script
+ `name` - The name of the parameter
+ `value` - The value of the parameter
+ `description` - The description of the parameter
+ `sensitive` - Whether the parameter is sensitive

## Usage

* Copy this example script to your `main.tf`.

* Create a `terraform.tfvars` file and fill in the required variables:

```hcl
coc_script_name = "your_coc_script_name"
coc_script_description = "your_coc_script_description"
coc_script_risk_level = "your_coc_script_risk_level"
coc_script_version = "your_coc_script_version"
coc_script_type = "your_coc_script_type"
coc_script_content = "your_coc_script_content"
coc_script_parameters = "your_coc_script_parameters"
```

* Initialize Terraform:

```bash
$ terraform init
```

* Review the Terraform plan:

```bash
$ terraform plan
```

* Apply the configuration:

```bash
$ terraform apply
```

* To clean up the resources:

```bash
$ terraform destroy
```

## Note

* Make sure to keep your credentials secure and never commit them to version control
* All resources will be created in the specified region

## Requirements

| Name | Version |
|---|---|
| terraform | >= 0.12.0 |
| huaweicloud | >= 1.58.0 |
31 changes: 13 additions & 18 deletions examples/coc/script/main.tf
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
resource "huaweicloud_coc_script" "test" {
name = var.script_name
description = "coc script description"
risk_level = "LOW"
version = "1.0.0"
type = "SHELL"
description = var.script_description
risk_level = var.script_risk_level
version = var.script_version
type = var.script_type
content = var.script_content

content = <<EOF
#! /bin/bash
echo "hello world!"
EOF
dynamic "parameters" {
for_each = var.script_parameters

parameters {
name = "name"
value = "world"
description = "the first parameter"
}
parameters {
name = "company"
value = "Huawei"
description = "the second parameter"
sensitive = true
content {
name = parameters.value.name
value = parameters.value.value
description = parameters.value.description
sensitive = parameters.value.sensitive
}
}
}
14 changes: 14 additions & 0 deletions examples/coc/script/providers.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
terraform {
required_providers {
huaweicloud = {
source = "huaweicloud/huaweicloud"
version = ">=1.58.0"
}
}
}

provider "huaweicloud" {
region = var.region_name
access_key = var.access_key
secret_key = var.secret_key
}
22 changes: 22 additions & 0 deletions examples/coc/script/terraform.tfvars
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
script_name = "tf_coc_script"
script_description = "Created by terraform script"
script_risk_level = "LOW"
script_version = "1.0.0"
script_type = "SHELL"
script_content = <<EOF
#! /bin/bash
echo "hello world!"
EOF
script_parameters = [
{
name = "name"
value = "world"
description = "the first parameter"
},
{
name = "company"
value = "Huawei"
description = "the second parameter"
sensitive = true
}
]
59 changes: 57 additions & 2 deletions examples/coc/script/variables.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,59 @@
# Variable definitions for authentication
variable "region_name" {
description = "The region where the COC script is located"
type = string
}

variable "access_key" {
description = "The access key of the IAM user"
type = string
}

variable "secret_key" {
description = "The secret key of the IAM user"
type = string
}

# Variable definitions for resources/data sources
variable "script_name" {
description = "The name of the COC script"
default = "tf_coc_script_name"
description = "The name of the script"
type = string
}

variable "script_description" {
description = "The description of the script"
type = string
}

variable "script_risk_level" {
description = "The risk level of the script"
type = string
}

variable "script_version" {
description = "The version of the script"
type = string
}

variable "script_type" {
description = "The type of the script"
type = string
}

variable "script_content" {
description = "The content of the script"
type = string
}

variable "script_parameters" {
description = "The parameter list of the script"
type = list(object({
name = string
value = string
description = string
sensitive = optional(bool)
}))

nullable = false
}

120 changes: 118 additions & 2 deletions examples/coc/script_execute/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,119 @@
# Execute COC script
# Create a COC script and execute it

In this example, we will execute a COC script.
This example provides best practice code for using Terraform to create a COC script and execute it in HuaweiCloud.

## Prerequisites

* A HuaweiCloud account
* Terraform installed
* HuaweiCloud access key and secret key (AK/SK)

## Required Variables

The following variables need to be configured:

### Authentication Variables

* `region_name` - The region where the COC script is located
* `access_key` - The access key of the IAM user
* `secret_key` - The secret key of the IAM user

### Resource Variables

#### Required Variables

* `vpc_name` - The name of the VPC
* `subnet_name` - The name of the subnet
* `security_group_name` - The name of the security group
* `instance_name` - The name of the ECS instance
* `instance_user_data` - The user data for installing UniAgent on the ECS instance
* `script_name` - The name of the script
* `script_description` - The description of the script
* `script_risk_level` - The risk level of the script
* `script_version` - The version of the script
* `script_type` - The type of the script
* `script_content` - The content of the script
* `script_parameters` - The parameter list of the script.
+ `name` - The name of the parameter
+ `value` - The value of the parameter
+ `description` - The description of the parameter
+ `sensitive` - Whether the parameter is sensitive
* `script_execute_timeout` - The maximum time to execute the script in seconds
* `script_execute_execute_user` - The user to execute the script
* `script_execute_parameters` - The parameter list of the script execution.
+ `name` - The name of the parameter
+ `value` - The value of the parameter

#### Optional Variables

* `availability_zone` - The availability zone to which the ECS instance and network belong (default: "")
* `instance_flavor_id` - The flavor ID of the ECS instance (default: "")
* `instance_flavor_performance_type` - The performance type of the ECS instance flavor (default: "normal")
* `instance_flavor_cpu_core_count` - The number of the ECS instance flavor CPU cores (default: 4)
* `instance_flavor_memory_size` - The memory size of the ECS instance flavor (default: 8)
* `instance_image_id` - The image ID of the ECS instance (default: "")
* `instance_image_os_type` - The OS type of the ECS instance flavor (default: "Ubuntu")
* `instance_image_visibility` - The visibility of the ECS instance flavor (default: "public")
* `vpc_cidr` - The CIDR block of the VPC (default: "192.168.0.0/16")
* `subnet_cidr` - The CIDR block of the subnet (default: "")
* `subnet_gateway_ip` - The gateway IP of the subnet (default: "")

## Usage

* Copy this example script to your `main.tf`.

* Create a `terraform.tfvars` file and fill in the required variables:

```hcl
vpc_name = "your_vpc_name"
subnet_name = "your_subnet_name"
security_group_name = "your_security_group_name"
instance_name = "your_instance_name"
instance_user_data = "your_user_data"
script_name = "your_script_name"
script_description = "your_script_description"
script_risk_level = "your_script_risk_level"
script_version = "your_script_version"
script_type = "your_script_type"
script_content = "your_script_content"
script_parameters = "your_script_parameters"
script_execute_timeout = "your_script_execute_timeout"
script_execute_execute_user = "your_script_execute_execute_user"
script_execute_parameters = "your_script_execute_parameters"
```

* Initialize Terraform:

```bash
$ terraform init
```

* Review the Terraform plan:

```bash
$ terraform plan
```

* Apply the configuration:

```bash
$ terraform apply
```

* To clean up the resources:

```bash
$ terraform destroy
```

## Note

* Make sure to keep your credentials secure and never commit them to version control
* All resources will be created in the specified region

## Requirements

| Name | Version |
|------|---------|
| terraform | >= 0.12.0 |
| huaweicloud | >= 1.58.0 |
Loading
Loading