{
    "terraform": {
        "required_providers": [
            {
                "huaweicloud": {
                    "source": "huawei.com/provider/huaweicloud",
                    "version": ">=1.70.1"
                }
            }
        ]
    },
    "provider": {
        "huaweicloud": {
            "cloud": "myhuaweicloud.com",
            "endpoints": {
                "iam": "iam.cn-north-4.myhuaweicloud.com",
                "dns": "dns.cn-north-4.myhuaweicloud.com"
            },
            "insecure": true,
            "region": "cn-north-4",
            "auth_url": "https://iam.cn-north-4.myhuaweicloud.com/v3"
        }
    },
    "variable": {
        "vpc_name": {
            "default": "building-a-newapi-llm-gateway-demo",
            "description": "虚拟私有云名称，该模板使用新建VPC，不允许重名。取值范围：1-54个字符，支持中文、英文字母、数字、_（下划线）、-（中划线）、.（点）。默认：building-a-newapi-llm-gateway-demo。",
            "type": "string",
            "nullable": false
        },
        "security_group_name": {
            "default": "building-a-newapi-llm-gateway-demo",
            "description": "安全组名称，该模板新建安全组，安全组规则请参考部署指南进行配置。取值范围：1-64个字符，支持数字、字母、中文、_(下划线)、-（中划线）、.（点）。默认：building-a-newapi-llm-gateway-demo。",
            "type": "string",
            "nullable": false
        },
        "ecs_name": {
            "default": "building-a-newapi-llm-gateway-demo",
            "description": "云服务器实例名称，不支持重名。取值范围：1-64个字符，支持中文、英文字母、数字、_（下划线）、-（中划线）、.（点）。默认：building-a-newapi-llm-gateway-demo。",
            "type": "string",
            "nullable": false
        },
        "ecs_flavor": {
            "default": "x1.8u.16g",
            "description": "云服务器实例规格，支持弹性云服务器 ECS及华为云Flexus 云服务器X实例。Flexus 云服务器X实例规格ID命名规则为x1.?u.?g，例如2vCPUs4GiB规格ID为x1.2u.4g，具体华为云Flexus 云服务器X实例规格请参考控制台。弹性云服务器 ECS规格请参考部署指南配置。默认：x1.8u.16g。",
            "type": "string",
            "nullable": false,
            "validation": {
                "condition": "${length(regexall(\"^([a-z][a-z0-9]{0,3}\\\\.)(x||[1-9][0-9]{0,1}x)large\\\\.[1-9][0-9]{0,1}$|^x1.([1-9]|1[0-6])u.([1-9][0-9]{0,1}|1[0-2][0-8])g$\",var.ecs_flavor))>0}",
                "error_message": "Invalid input. Please re-enter."
            }
        },
        "ecs_password": {
            "default": "",
            "description": "云服务器密码，长度为8-26位，密码至少包含大写字母、小写字母、数字和特殊字符（!@$%^-_=+[{}]:,./?）中的三种。管理员账户默认root。",
            "type": "string",
            "nullable": false,
            "sensitive": true
        },
        "system_disk_size": {
            "default": 100,
            "description": "云服务器系统盘大小，磁盘类型默认为高IO，单位：GB，取值范围为40-1,024，不支持缩盘。默认：100",
            "type": "number",
            "nullable": false,
            "validation": {
                "condition": "${length(regexall(\"^([4-9][0-9]|[1-9][0-9]{2}|10[01][0-9]|102[0-4]|1024)$\",var.system_disk_size))>0}",
                "error_message": "Invalid input. Please re-enter."
            }
        },
        "bandwidth_size": {
            "default": 300,
            "description": "弹性公网带宽大小，该模板计费方式为按流量计费。单位：Mbit/s，取值范围：1-300Mbit/s。默认：300。",
            "type": "number",
            "nullable": false,
            "validation": {
                "condition": "${length(regexall(\"^([1-9][0-9]{0,1}|[1-2][0-9]{2}|300)$\",var.bandwidth_size))>0}",
                "error_message": "Invalid input. Please re-enter."
            }
        },
        "charging_mode": {
            "default": "postPaid",
            "description": "云服务器计费模式，默认自动扣费，可选值为：postPaid（按需计费）、prePaid（包年包月）。默认：postPaid。",
            "type": "string",
            "nullable": false,
            "validation": {
                "condition": "${contains([\"postPaid\",\"prePaid\"], var.charging_mode)}",
                "error_message": "Invalid input, please re-enter."
            }
        },
        "charging_unit": {
            "default": "month",
            "description": "云服务器订购周期类型，仅当charging_mode为prePaid（包年/包月）生效，此时该参数为必填参数。取值范围：month（月），year（年）。默认month。",
            "type": "string",
            "nullable": false,
            "validation": {
                "condition": "${contains([\"month\",\"year\"], var.charging_unit)}",
                "error_message": "Invalid input, please re-enter."
            }
        },
        "charging_period": {
            "default": 1,
            "description": "云服务器订购周期，仅当charging_mode为prePaid（包年/包月）生效，此时该参数为必填参数。取值范围：charging_unit=month（周期类型为月）时，取值为1-9；charging_unit=year（周期类型为年）时，取值为1-3。默认订购1月。",
            "type": "number",
            "nullable": false,
            "validation": {
                "condition": "${length(regexall(\"^[1-9]$\",var.charging_period))>0}",
                "error_message": "Invalid input, please re-enter."
            }
        }
    },
    "data": {
        "huaweicloud_images_image": {
            "Ubuntu": {
                "most_recent": true,
                "name": "Ubuntu 22.04 server 64bit",
                "visibility": "public"
            }
        }
    },
    "resource": {
        "huaweicloud_vpc": {
            "vpc": {
                "name": "${var.vpc_name}",
                "cidr": "172.16.0.0/16"
            }
        },
        "huaweicloud_vpc_subnet": {
            "subnet": {
                "name": "${var.vpc_name}-subnet",
                "cidr": "172.16.1.0/24",
                "gateway_ip": "172.16.1.1",
                "vpc_id": "${huaweicloud_vpc.vpc.id}"
            }
        },
        "huaweicloud_networking_secgroup": {
            "secgroup": {
                "name": "${var.security_group_name}"
            }
        },
        "huaweicloud_networking_secgroup_rule": {
            "allow_ping": {
                "security_group_id": "${huaweicloud_networking_secgroup.secgroup.id}",
                "description": "允许ping程序测试Flexus云服务器X实例的连通性",
                "direction": "ingress",
                "ethertype": "IPv4",
                "protocol": "icmp",
                "remote_ip_prefix": "0.0.0.0/0"
            },
            "newapi_HTTP": {
                "security_group_id": "${huaweicloud_networking_secgroup.secgroup.id}",
                "description": "HTTP协议网络端口，允许访问NewAPI应用",
                "direction": "ingress",
                "ethertype": "IPv4",
                "protocol": "tcp",
                "ports": 3000,
                "remote_ip_prefix": "0.0.0.0/0"
            },
            "cloud_shell": {
                "security_group_id": "${huaweicloud_networking_secgroup.secgroup.id}",
                "description": "Cloud Shell默认端口，通过CLoud shell登录服务器",
                "direction": "ingress",
                "ethertype": "IPv4",
                "protocol": "tcp",
                "ports": 22,
                "remote_ip_prefix": "121.36.59.153/32"
            }
        },
        "huaweicloud_vpc_eip": {
            "vpc_eip": {
                "name": "${var.vpc_name}-eip",
                "bandwidth": {
                    "name": "${var.vpc_name}-bandwidth",
                    "share_type": "PER",
                    "size": "${var.bandwidth_size}",
                    "charge_mode": "traffic"
                },
                "publicip": {
                    "type": "5_bgp"
                },
                "charging_mode": "postPaid"
            }
        },
        "huaweicloud_compute_instance": {
            "compute_instance": {
                "name": "${var.ecs_name}",
                "image_id": "${data.huaweicloud_images_image.Ubuntu.id}",
                "flavor_id": "${var.ecs_flavor}",
                "security_group_ids": [
                    "${huaweicloud_networking_secgroup.secgroup.id}"
                ],
                "system_disk_type": "SAS",
                "system_disk_size": "${var.system_disk_size}",
                "admin_pass": "${var.ecs_password}",
                "delete_disks_on_termination": true,
                "network": {
                    "uuid": "${huaweicloud_vpc_subnet.subnet.id}"
                },
                "agent_list": "hss,ces",
                "eip_id": "${huaweicloud_vpc_eip.vpc_eip.id}",
                "charging_mode": "${var.charging_mode}",
                "period_unit": "${var.charging_unit}",
                "period": "${var.charging_period}",
                "tags": {
                    "app": "NewAPI"
                },
                "user_data": "#!/bin/bash\necho 'root:${var.ecs_password}' | chpasswd\nwget -P /home/ https://documentation-samples.obs.cn-north-4.myhuaweicloud.com/solution-as-code-publicbucket/solution-as-code-moudle/building-a-newapi-llm-gateway/userdata/newapi_deploy.sh\nbash /home/newapi_deploy.sh\nrm -rf /home/newapi_deploy.sh"
            }
        }
    },
    "output": {
        "说明": {
            "depends_on": [
                "huaweicloud_vpc_eip.vpc_eip"
            ],
            "value": "等待应用下载及部署完毕（约10分钟）后，请在浏览器上输入网址：http://${huaweicloud_vpc_eip.vpc_eip.address}:3000/，即可访问平台。"
        }
    }
}