diff --git a/solution/tech-solution/polardb-mysql-mcp/README.md b/solution/tech-solution/polardb-mysql-mcp/README.md new file mode 100644 index 000000000..d60b41ed5 --- /dev/null +++ b/solution/tech-solution/polardb-mysql-mcp/README.md @@ -0,0 +1,43 @@ +## Introduction + +本示例用于实现解决方案[MCP 赋能可视化 OLAP 智能体应用](https://www.aliyun.com/solution/tech-solution/polardb-mysql-mcp),涉及专有网络(VPC)、交换机(VSwitch)、PolarDB数据库(PolarDB)等资源的部署。 + + + +This example is used to implement solution [MCP empowers visual OLAP intelligent agent applications](https://www.aliyun.com/solution/tech-solution/polardb-mysql-mcp), which involves the creation and deployment of resources such as Virtual Private Cloud (VPC), Virtual Switch (VSwitch), PolarDB Database (PolarDB). + + + +## Providers + +| Name | Version | +|------|---------| +| [alicloud](#provider\_alicloud) | n/a | +| [random](#provider\_random) | n/a | + +## Modules + +No modules. + +## Resources + +| Name | Type | +|------|------| +| [alicloud_polardb_account.account](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/polardb_account) | resource | +| [alicloud_polardb_cluster.polardb_cluster](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/polardb_cluster) | resource | +| [alicloud_polardb_database.polardb_database](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/polardb_database) | resource | +| [alicloud_polardb_endpoint_address.dbcluster_endpoint_address](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/polardb_endpoint_address) | resource | +| [alicloud_vpc.vpc](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/vpc) | resource | +| [alicloud_vswitch.vswitch](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/vswitch) | resource | +| [random_id.suffix](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/id) | resource | +| [alicloud_polardb_endpoints.polardb_endpoints](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/data-sources/polardb_endpoints) | data source | +| [alicloud_polardb_node_classes.default](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/data-sources/polardb_node_classes) | data source | + +## Inputs + +| Name | Description | Type | Default | Required | +|------|-------------|------|---------|:--------:| +| [account\_name](#input\_account\_name) | account\_name | `string` | `"polar_ai"` | no | +| [db\_password](#input\_db\_password) | db\_password | `string` | n/a | yes | +| [dbname](#input\_dbname) | dbname | `string` | `"db-test"` | no | + \ No newline at end of file diff --git a/solution/tech-solution/polardb-mysql-mcp/main.tf b/solution/tech-solution/polardb-mysql-mcp/main.tf new file mode 100644 index 000000000..e1052cadc --- /dev/null +++ b/solution/tech-solution/polardb-mysql-mcp/main.tf @@ -0,0 +1,89 @@ +# ------------------------------------------------------------------------------ +# 核心资源定义 +# +# 本文件包含了模块的核心基础设施资源 +# 这里的代码负责根据输入变量来创建和配置所有云资源 +# ------------------------------------------------------------------------------ + +# 配置阿里云提供商 +provider "alicloud" { + region = "cn-hangzhou" +} + +# 创建一个随机ID +resource "random_id" "suffix" { + byte_length = 8 +} + +# 定义本地变量 +locals { + common_name = "polardb-${random_id.suffix.id}" +} + +# 查询可用区 +data "alicloud_polardb_node_classes" "default" { + db_type = "MySQL" + pay_type = "PostPaid" + category = "SENormal" +} + +# 创建专有网络VPC +resource "alicloud_vpc" "vpc" { + cidr_block = "192.168.0.0/16" + vpc_name = "VPC_${local.common_name}" +} + +# 创建交换机VSwitch +resource "alicloud_vswitch" "vswitch" { + vpc_id = alicloud_vpc.vpc.id + cidr_block = "192.168.0.0/24" + zone_id = data.alicloud_polardb_node_classes.default.classes[0].zone_id + vswitch_name = "vsw_001_${local.common_name}" +} + +# 创建PolarDB集群 +resource "alicloud_polardb_cluster" "polardb_cluster" { + vpc_id = alicloud_vpc.vpc.id + db_type = "MySQL" + zone_id = data.alicloud_polardb_node_classes.default.classes[0].zone_id + vswitch_id = alicloud_vswitch.vswitch.id + db_version = "8.0" + creation_category = "SENormal" + storage_space = 40 + db_node_class = "polar.mysql.g1.tiny.c" + pay_type = "PostPaid" + storage_type = "ESSDAUTOPL" + security_ips = ["0.0.0.0/0"] +} + +# 创建PolarDB账号 +resource "alicloud_polardb_account" "account" { + db_cluster_id = alicloud_polardb_cluster.polardb_cluster.id + account_name = var.account_name + account_password = var.db_password + account_type = "Super" +} + +# 获取PolarDB集群的端点信息 +data "alicloud_polardb_endpoints" "polardb_endpoints" { + db_cluster_id = alicloud_polardb_cluster.polardb_cluster.id + depends_on = [alicloud_polardb_cluster.polardb_cluster] +} + +# 创建PolarDB集群的公网连接地址 +resource "alicloud_polardb_endpoint_address" "dbcluster_endpoint_address" { + db_endpoint_id = data.alicloud_polardb_endpoints.polardb_endpoints.endpoints[0].db_endpoint_id + db_cluster_id = alicloud_polardb_cluster.polardb_cluster.id + net_type = "Public" + depends_on = [ + alicloud_polardb_account.account + ] +} + +# 创建PolarDB数据库 +resource "alicloud_polardb_database" "polardb_database" { + db_cluster_id = alicloud_polardb_cluster.polardb_cluster.id + db_name = var.dbname + character_set_name = "utf8" + account_name = var.account_name +} \ No newline at end of file diff --git a/solution/tech-solution/polardb-mysql-mcp/output.tf b/solution/tech-solution/polardb-mysql-mcp/output.tf new file mode 100644 index 000000000..85ab4a861 --- /dev/null +++ b/solution/tech-solution/polardb-mysql-mcp/output.tf @@ -0,0 +1,37 @@ +# ------------------------------------------------------------------------------ +# 模块输出值 +# +# 本文件定义了模块执行成功后返回给调用方的值 +# 这些输出可以被其他 Terraform 配置引用,或在 apply 命令结束后显示给用户 +# ------------------------------------------------------------------------------ + +# 输出PolarDB集群的访问地址 +output "polardb_cluster_address" { + value = format("https://polardb.console.aliyun.com/%s/cluster/%s/baseInfo", "cn-hangzhou", alicloud_polardb_cluster.polardb_cluster.id) + description = "PolarDB访问地址" +} + +# 输出PolarDB MySQL的用户名称 +output "db_username" { + value = var.account_name + description = "PolarDB MySQL的用户名称" +} + +# 输出PolarDB MySQL的用户密码 +output "db_password" { + value = var.db_password + sensitive = true + description = "PolarDB MySQL的用户密码" +} + +# 输出PolarDB 数据库名称 +output "db_name" { + value = var.dbname + description = "PolarDB 数据库名称" +} + +# 输出PolarDB 数据库的公网连接地址 +output "connection_string" { + value = format("%s", alicloud_polardb_endpoint_address.dbcluster_endpoint_address.connection_string) + description = "PolarDB 数据库公网连接地址" +} \ No newline at end of file diff --git a/solution/tech-solution/polardb-mysql-mcp/variable.tf b/solution/tech-solution/polardb-mysql-mcp/variable.tf new file mode 100644 index 000000000..0ca6a5abd --- /dev/null +++ b/solution/tech-solution/polardb-mysql-mcp/variable.tf @@ -0,0 +1,36 @@ +# ------------------------------------------------------------------------------ +# 模块输入变量 +# +# 本文件定义了该 Terraform 模块所有可配置的输入变量 +# 每个变量都包含了详细的说明,以帮助用户正确配置模块 +# ------------------------------------------------------------------------------ + +# PolarDB的用户名 +variable "account_name" { + type = string + default = "polar_ai" + description = "account_name" + validation { + condition = can(regex("^[a-z][a-z0-9_]{0,30}[a-z0-9]$", var.account_name)) + error_message = "数据库用户名必须以字母开头,以字母或数字结尾,只能包含字母、数字和下划线最多32个字符" + } +} + +# PolarDB的用户密码 +variable "db_password" { + type = string + sensitive = true + description = "db_password" + #default = "" + validation { + condition = can(regex("^[0-9A-Za-z_!@#$%^&*()_+\\-=\\+]+$", var.db_password)) && length(var.db_password) >= 8 && length(var.db_password) <= 30 + error_message = "长度8-30,必须包含三项(大写字母、小写字母、数字、 !@#$%^&*()_+-=中的特殊符号)" + } +} + +# PolarDB的数据库名称 +variable "dbname" { + type = string + default = "db-test" + description = "dbname" +} \ No newline at end of file