Skip to content

Commit 8b6ae56

Browse files
committed
docs(coc/example): adjust examples under coc
1 parent 723b6fa commit 8b6ae56

File tree

8 files changed

+508
-57
lines changed

8 files changed

+508
-57
lines changed

examples/coc/script/README.md

Lines changed: 110 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,111 @@
1-
# Create COC script
1+
# Create a COC script
22

3-
In this example, we will create a COC script.
3+
This example provides best practice code for using Terraform to create 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+
* `coc_script_name` - The name of the script
24+
* `coc_script_description` - The description of the script
25+
* `coc_script_risk_level` - The risk level of the script
26+
* `coc_script_version` - The version of the script
27+
* `coc_script_type` - The type of the script
28+
* `coc_script_content` - The content of the script
29+
* `coc_script_parameters` - The parameter list of the script
30+
- `name` - The name of the parameter
31+
- `value` - The value of the parameter
32+
- `description` - The description of the parameter
33+
- `sensitive` - Whether the parameter is sensitive
34+
35+
## Usage
36+
37+
* Create a working directory and create a `versions.tf` file, the content is as follows:
38+
39+
```hcl
40+
terraform {
41+
required_providers {
42+
huaweicloud = {
43+
source = "huaweicloud/huaweicloud"
44+
version = ">= 1.58.0"
45+
}
46+
}
47+
}
48+
```
49+
50+
* Copy this example scripts (`main.tf` and `variables.tf`) to your working directory.
51+
52+
* Prepare the authentication (AK/SK and region) and configured in the TF script (versions.tf), also you can using
53+
environment variables.
54+
55+
```hcl
56+
provider "huaweicloud" {
57+
region = var.region_name
58+
access_key = var.access_key
59+
secret_key = var.secret_key
60+
}
61+
62+
variable "region_name" {
63+
type = string
64+
}
65+
66+
variable "access_key" {
67+
type = string
68+
}
69+
70+
variable "secret_key" {
71+
type = string
72+
}
73+
```
74+
75+
* Create a `terraform.tfvars` [file](./terraform.tfvars) and fill in the required variables.
76+
77+
* Initialize Terraform:
78+
79+
```bash
80+
$ terraform init
81+
```
82+
83+
* Review the Terraform plan:
84+
85+
```bash
86+
$ terraform plan
87+
```
88+
89+
* Apply the configuration:
90+
91+
```bash
92+
$ terraform apply
93+
```
94+
95+
* To clean up the resources:
96+
97+
```bash
98+
$ terraform destroy
99+
```
100+
101+
## Note
102+
103+
* Make sure to keep your credentials secure and never commit them to version control
104+
* All resources will be created in the specified region
105+
106+
## Requirements
107+
108+
| Name | Version |
109+
|---|---|
110+
| terraform | >= 0.12.0 |
111+
| huaweicloud | >= 1.58.0 |

examples/coc/script/main.tf

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,19 @@
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"
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
77

8-
content = <<EOF
9-
#! /bin/bash
10-
echo "hello world!"
11-
EOF
8+
content = var.coc_script_content
129

13-
parameters {
14-
name = "name"
15-
value = "world"
16-
description = "the first parameter"
17-
}
18-
parameters {
19-
name = "company"
20-
value = "Huawei"
21-
description = "the second parameter"
22-
sensitive = true
10+
dynamic "parameters" {
11+
for_each = var.coc_script_parameters
12+
content {
13+
name = parameters.value.name
14+
value = parameters.value.value
15+
description = parameters.value.description
16+
sensitive = parameters.value.sensitive
17+
}
2318
}
2419
}

examples/coc/script/terraform.tfvars

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
coc_script_name = "tf_coc_script"
2+
coc_script_description = "Created by terraform script"
3+
coc_script_risk_level = "LOW"
4+
coc_script_version = "1.0.0"
5+
coc_script_type = "SHELL"
6+
coc_script_content = <<EOF
7+
#! /bin/bash
8+
echo "hello world!"
9+
EOF
10+
coc_script_parameters = [
11+
{
12+
name = "name"
13+
value = "world"
14+
description = "the first parameter"
15+
},
16+
{
17+
name = "company"
18+
value = "Huawei"
19+
description = "the second parameter"
20+
sensitive = true
21+
}
22+
]

examples/coc/script/variables.tf

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,40 @@
1-
variable "script_name" {
2-
description = "The name of the COC script"
3-
default = "tf_coc_script_name"
1+
variable "coc_script_name" {
2+
description = "The name of the script"
3+
type = string
44
}
5+
6+
variable "coc_script_description" {
7+
description = "The description of the script"
8+
type = string
9+
}
10+
11+
variable "coc_script_risk_level" {
12+
description = "The description of the script"
13+
type = string
14+
}
15+
16+
variable "coc_script_version" {
17+
description = "The description of the script"
18+
type = string
19+
}
20+
21+
variable "coc_script_type" {
22+
description = "The risk level of the script"
23+
type = string
24+
}
25+
26+
variable "coc_script_content" {
27+
description = "The content of the script"
28+
type = string
29+
}
30+
31+
variable "coc_script_parameters" {
32+
description = "The parameter list of the script"
33+
type = list(object({
34+
name = string
35+
value = string
36+
description = string
37+
sensitive = optional(bool)
38+
}))
39+
}
40+

examples/coc/script_execute/README.md

Lines changed: 122 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,123 @@
1-
# Execute COC script
1+
# Execute a COC script
22

3-
In this example, we will execute a COC script.
3+
This example provides best practice code for using Terraform to execute 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 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_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+
47+
## Usage
48+
49+
* Create a working directory and create a `versions.tf` file, the content is as follows:
50+
51+
```hcl
52+
terraform {
53+
required_providers {
54+
huaweicloud = {
55+
source = "huaweicloud/huaweicloud"
56+
version = ">= 1.58.0"
57+
}
58+
}
59+
}
60+
```
61+
62+
* Copy this example scripts (`main.tf` and `variables.tf`) to your working directory.
63+
64+
* Prepare the authentication (AK/SK and region) and configured in the TF script (versions.tf), also you can using
65+
environment variables.
66+
67+
```hcl
68+
provider "huaweicloud" {
69+
region = var.region_name
70+
access_key = var.access_key
71+
secret_key = var.secret_key
72+
}
73+
74+
variable "region_name" {
75+
type = string
76+
}
77+
78+
variable "access_key" {
79+
type = string
80+
}
81+
82+
variable "secret_key" {
83+
type = string
84+
}
85+
```
86+
87+
* Create a `terraform.tfvars` [file](./terraform.tfvars) and fill in the required variables.
88+
89+
* Initialize Terraform:
90+
91+
```bash
92+
$ terraform init
93+
```
94+
95+
* Review the Terraform plan:
96+
97+
```bash
98+
$ terraform plan
99+
```
100+
101+
* Apply the configuration:
102+
103+
```bash
104+
$ terraform apply
105+
```
106+
107+
* To clean up the resources:
108+
109+
```bash
110+
$ terraform destroy
111+
```
112+
113+
## Note
114+
115+
* Make sure to keep your credentials secure and never commit them to version control
116+
* All resources will be created in the specified region
117+
118+
## Requirements
119+
120+
| Name | Version |
121+
|------|---------|
122+
| terraform | >= 0.12.0 |
123+
| huaweicloud | >= 1.58.0 |

0 commit comments

Comments
 (0)