Skip to content

Commit 02e083d

Browse files
committed
chore(examples/coc): adjust script_order_operation script
1 parent 6c1291e commit 02e083d

File tree

4 files changed

+357
-37
lines changed

4 files changed

+357
-37
lines changed
Lines changed: 126 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,127 @@
1-
# Execute COC script
1+
# Execute COC script order operation
22

3-
In this example, we will operate a COC script.
3+
This example provides best practice code for using Terraform to create and operate a COC script in HuaweiCloud.
4+
5+
## Prerequisites
6+
7+
* A HuaweiCloud account
8+
* Terraform installed
9+
* HuaweiCloud access key and secret key (AK/SK)
10+
11+
## Required Variables
12+
13+
The following variables need to be configured:
14+
15+
### Authentication Variables
16+
17+
* `access_key` - HuaweiCloud access key
18+
* `secret_key` - HuaweiCloud secret key
19+
* `region_name` - The region where resources will be created
20+
21+
### Resource Variables
22+
23+
* `enterprise_project_id` - The ID of the enterprise project
24+
* `vpc_name` - The name of the VPC
25+
* `vpc_cidr` - The CIDR block of the VPC
26+
* `subnet_name` - The name of the subnet
27+
* `security_group_name` - The name of the security group
28+
* `ecs_instance_name` - The name of the ECS instance
29+
* `ecs_instance_user_data` - The user data for installing UniAgent on the ECS instance
30+
* `coc_script_name` - The name of the COC script
31+
* `coc_script_description` - The description of the script
32+
* `coc_script_risk_level` - The risk level of the script
33+
* `coc_script_version` - The version of the script
34+
* `coc_script_type` - The type of the script
35+
* `coc_script_content` - The content of the script
36+
* `coc_script_parameters` - The parameter list of the script
37+
- `name` - The name of the parameter
38+
- `value` - The value of the parameter
39+
- `description` - The description of the parameter
40+
- `sensitive` - Whether the parameter is sensitive
41+
* `coc_script_execute_timeout` - The maximum time to execute the script in seconds
42+
* `coc_script_execute_user` - The user to execute the script
43+
* `coc_script_execute_parameters` - The parameter list of the script execution
44+
- `name` - The name of the parameter
45+
- `value` - The value of the parameter
46+
* `coc_script_order_operation_batch_index` - The batch index for the script order
47+
* `coc_script_order_operation_instance_id` - The instance ID for the script order
48+
* `coc_script_order_operation_type` - The operation type for the script order
49+
50+
## Usage
51+
52+
* Create a working directory and create a `versions.tf` file, the content is as follows:
53+
54+
```hcl
55+
terraform {
56+
required_providers {
57+
huaweicloud = {
58+
source = "huaweicloud/huaweicloud"
59+
version = ">=1.75.0"
60+
}
61+
}
62+
}
63+
```
64+
65+
* Copy this example scripts (`main.tf` and `variables.tf`) to your working directory.
66+
67+
* Prepare the authentication (AK/SK and region) and configured in the TF script (versions.tf), also you can using
68+
environment variables.
69+
70+
```hcl
71+
provider "huaweicloud" {
72+
region = var.region_name
73+
access_key = var.access_key
74+
secret_key = var.secret_key
75+
}
76+
77+
variable "region_name" {
78+
type = string
79+
}
80+
81+
variable "access_key" {
82+
type = string
83+
}
84+
85+
variable "secret_key" {
86+
type = string
87+
}
88+
```
89+
90+
* Create a `terraform.tfvars` file and fill in the required variables.
91+
92+
* Initialize Terraform:
93+
94+
```bash
95+
$ terraform init
96+
```
97+
98+
* Review the Terraform plan:
99+
100+
```bash
101+
$ terraform plan
102+
```
103+
104+
* Apply the configuration:
105+
106+
```bash
107+
$ terraform apply
108+
```
109+
110+
* To clean up the resources:
111+
112+
```bash
113+
$ terraform destroy
114+
```
115+
116+
## Note
117+
118+
* Make sure to keep your credentials secure and never commit them to version control.
119+
* The ECS instance needs to have UniAgent installed for script execution.
120+
* All resources will be created in the specified region.
121+
122+
## Requirements
123+
124+
| Name | Version |
125+
|------|---------|
126+
| terraform | >= 0.12.0 |
127+
| huaweicloud | >= 1.75.0 |
Lines changed: 81 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,95 @@
11
resource "huaweicloud_coc_script" "test" {
2-
name = var.script_name
3-
description = "coc script description"
4-
risk_level = "LOW"
5-
version = "1.0.0"
6-
type = "SHELL"
7-
8-
content = <<EOF
9-
#! /bin/bash
10-
echo "hello world!"
11-
EOF
12-
13-
parameters {
14-
name = "name"
15-
value = "world"
16-
description = "the parameter"
2+
name = var.coc_script_name
3+
description = var.coc_script_description
4+
risk_level = var.coc_script_risk_level
5+
version = var.coc_script_version
6+
type = var.coc_script_type
7+
content = var.coc_script_content
8+
9+
dynamic "parameters" {
10+
for_each = var.coc_script_parameters
11+
12+
content {
13+
name = parameters.value.name
14+
value = parameters.value.value
15+
description = parameters.value.description
16+
sensitive = parameters.value.sensitive
17+
}
18+
}
19+
}
20+
21+
data "huaweicloud_availability_zones" "test" {}
22+
23+
data "huaweicloud_compute_flavors" "test" {
24+
availability_zone = try(data.huaweicloud_availability_zones.test.names[0], "")
25+
performance_type = "normal"
26+
cpu_core_count = 2
27+
memory_size = 4
28+
}
29+
30+
resource "huaweicloud_vpc" "test" {
31+
name = var.vpc_name
32+
cidr = var.vpc_cidr
33+
enterprise_project_id = var.enterprise_project_id
34+
}
35+
36+
resource "huaweicloud_vpc_subnet" "test" {
37+
vpc_id = huaweicloud_vpc.test.id
38+
availability_zone = try(data.huaweicloud_availability_zones.test.names[0], "")
39+
name = var.subnet_name
40+
cidr = cidrsubnet(huaweicloud_vpc.test.cidr, 8, 0)
41+
gateway_ip = cidrhost(cidrsubnet(huaweicloud_vpc.test.cidr, 8, 0), 1)
42+
}
43+
44+
data "huaweicloud_images_images" "test" {
45+
flavor_id = try(data.huaweicloud_compute_flavors.test.ids[0], "")
46+
os = "Ubuntu"
47+
visibility = "public"
48+
}
49+
50+
# The default security group rules cannot be deleted, otherwise the UniAgent installation will fail.
51+
resource "huaweicloud_networking_secgroup" "test" {
52+
name = var.security_group_name
53+
}
54+
55+
# Create an ECS instance and install UniAgent
56+
resource "huaweicloud_compute_instance" "test" {
57+
name = var.ecs_instance_name
58+
availability_zone = try(data.huaweicloud_availability_zones.test.names[0], "")
59+
flavor_id = try(data.huaweicloud_compute_flavors.test.flavors[0].id, "")
60+
image_id = try(data.huaweicloud_images_images.test.images[0].id, "")
61+
security_group_ids = [huaweicloud_networking_secgroup.test.id]
62+
user_data = var.ecs_instance_user_data
63+
enterprise_project_id = var.enterprise_project_id
64+
65+
network {
66+
uuid = huaweicloud_vpc_subnet.test.id
1767
}
1868
}
1969

2070
resource "huaweicloud_coc_script_execute" "test" {
2171
script_id = huaweicloud_coc_script.test.id
22-
instance_id = var.script_execute_name
23-
timeout = 600
24-
execute_user = "root"
72+
instance_id = huaweicloud_compute_instance.test.id
73+
timeout = var.coc_script_execute_timeout
74+
execute_user = var.coc_script_execute_user
2575

26-
parameters {
27-
name = "name"
28-
value = "somebody"
76+
dynamic "parameters" {
77+
for_each = var.coc_script_execute_parameters
78+
79+
content {
80+
name = parameters.value.name
81+
value = parameters.value.value
82+
}
2983
}
30-
parameters {
31-
name = "company"
32-
value = "HuaweiCloud"
84+
85+
timeouts {
86+
create = "1m"
3387
}
3488
}
3589

3690
resource "huaweicloud_coc_script_order_operation" "test" {
3791
execute_uuid = huaweicloud_coc_script_execute.test.id
38-
batch_index = 1
39-
instance_id = 1
40-
operation_type = var.operation_type
92+
batch_index = var.coc_script_order_operation_batch_index
93+
instance_id = var.coc_script_order_operation_instance_id
94+
operation_type = var.coc_script_order_operation_type
4195
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Enterprise Project
2+
enterprise_project_id = "0"
3+
4+
# VPC
5+
vpc_name = "tf_test_vpc"
6+
vpc_cidr = "192.168.0.0/16"
7+
subnet_name = "tf_test_subnet"
8+
security_group_name = "tf_test_secgroup"
9+
ecs_instance_name = "tf_test_coc_script_execute"
10+
ecs_instance_user_data = "your_user_data" # Please replace it with the command you actually used to install icagent
11+
12+
# COC
13+
coc_script_name = "tf_coc_script_demo"
14+
coc_script_description = "Created by terraform script"
15+
coc_script_risk_level = "LOW"
16+
coc_script_version = "1.0.0"
17+
coc_script_type = "SHELL"
18+
coc_script_content = <<EOF
19+
#! /bin/bash
20+
echo "hello $${name}!"
21+
sleep 2m
22+
EOF
23+
coc_script_parameters = [
24+
{
25+
name = "name"
26+
value = "world"
27+
description = "the parameter"
28+
},
29+
{
30+
name = "company"
31+
value = "Terraform"
32+
description = "the second parameter"
33+
sensitive = true
34+
}
35+
]
36+
coc_script_execute_timeout = 600
37+
coc_script_execute_user = "root"
38+
coc_script_execute_parameters = [
39+
{
40+
name = "name"
41+
value = "somebody"
42+
}
43+
]
44+
coc_script_order_operation_batch_index = 1
45+
coc_script_order_operation_instance_id = 1
46+
coc_script_order_operation_type = "CANCEL_ORDER"

0 commit comments

Comments
 (0)