From f901077eec72a29aa2e81945d104af424b5ff46b Mon Sep 17 00:00:00 2001 From: wuzhuanhong Date: Fri, 30 May 2025 16:10:23 +0800 Subject: [PATCH] docs(coc/example): adjust examples under coc --- examples/coc/script/README.md | 88 +++++++++- examples/coc/script/main.tf | 31 ++-- examples/coc/script/providers.tf | 14 ++ examples/coc/script/terraform.tfvars | 22 +++ examples/coc/script/variables.tf | 59 ++++++- examples/coc/script_execute/README.md | 120 ++++++++++++- examples/coc/script_execute/main.tf | 102 ++++++++--- examples/coc/script_execute/providers.tf | 14 ++ examples/coc/script_execute/terraform.tfvars | 29 ++++ examples/coc/script_execute/variables.tf | 170 ++++++++++++++++++- 10 files changed, 596 insertions(+), 53 deletions(-) create mode 100644 examples/coc/script/providers.tf create mode 100644 examples/coc/script/terraform.tfvars create mode 100644 examples/coc/script_execute/providers.tf create mode 100644 examples/coc/script_execute/terraform.tfvars diff --git a/examples/coc/script/README.md b/examples/coc/script/README.md index 6f51246a653..8b0bd6a6837 100644 --- a/examples/coc/script/README.md +++ b/examples/coc/script/README.md @@ -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 | diff --git a/examples/coc/script/main.tf b/examples/coc/script/main.tf index cc024d5dd7f..6792aef9c56 100644 --- a/examples/coc/script/main.tf +++ b/examples/coc/script/main.tf @@ -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 = <= 0.12.0 | +| huaweicloud | >= 1.58.0 | diff --git a/examples/coc/script_execute/main.tf b/examples/coc/script_execute/main.tf index be593222a1f..5615f9012f0 100644 --- a/examples/coc/script_execute/main.tf +++ b/examples/coc/script_execute/main.tf @@ -1,34 +1,88 @@ +data "huaweicloud_availability_zones" "test" { + count = var.availability_zone == "" ? 1 : 0 +} + +data "huaweicloud_compute_flavors" "test" { + count = var.instance_flavor_id == "" ? 1 : 0 + + availability_zone = var.availability_zone == "" ? try(data.huaweicloud_availability_zones.test[0].names[0], "") : var.availability_zone + performance_type = var.instance_flavor_performance_type + cpu_core_count = var.instance_flavor_cpu_core_count + memory_size = var.instance_flavor_memory_size +} + +data "huaweicloud_images_images" "test" { + count = var.instance_image_id == "" ? 1 : 0 + + flavor_id = var.instance_flavor_id == "" ? try(data.huaweicloud_compute_flavors.test[0].ids[0], "") : var.instance_flavor_id + os = var.instance_image_os_type + visibility = var.instance_image_visibility +} + +resource "huaweicloud_vpc" "test" { + name = var.vpc_name + cidr = var.vpc_cidr +} + +resource "huaweicloud_vpc_subnet" "test" { + vpc_id = huaweicloud_vpc.test.id + name = var.subnet_name + cidr = var.subnet_cidr == "" ? cidrsubnet(huaweicloud_vpc.test.cidr, 8, 0) : var.subnet_cidr + gateway_ip = var.subnet_gateway_ip == "" ? cidrhost(cidrsubnet(huaweicloud_vpc.test.cidr, 8, 0), 1) : var.subnet_gateway_ip + availability_zone = var.availability_zone == "" ? try(data.huaweicloud_availability_zones.test[0].names[0], null) : var.availability_zone +} + +# The default security group rules cannot be deleted, otherwise the UniAgent installation will fail. +resource "huaweicloud_networking_secgroup" "test" { + name = var.security_group_name +} + +# Create an ECS instance and install UniAgent +resource "huaweicloud_compute_instance" "test" { + name = var.instance_name + availability_zone = var.availability_zone == "" ? try(data.huaweicloud_availability_zones.test[0].names[0], "") : var.availability_zone + flavor_id = var.instance_flavor_id == "" ? try(data.huaweicloud_compute_flavors.test[0].flavors[0].id, "") : var.instance_flavor_id + image_id = var.instance_image_id == "" ? try(data.huaweicloud_images_images.test[0].images[0].id, "") : var.instance_image_id + security_group_ids = [huaweicloud_networking_secgroup.test.id] + user_data = var.instance_user_data + + network { + uuid = huaweicloud_vpc_subnet.test.id + } +} + resource "huaweicloud_coc_script" "test" { name = var.script_name - description = "coc script description" - risk_level = "LOW" - version = "1.0.0" - type = "SHELL" - - content = <