Skip to content

Commit 236dc1c

Browse files
committed
feat: Implement cloud account region management with CRUD operations and API integration
1 parent 3b8abce commit 236dc1c

20 files changed

Lines changed: 1522 additions & 110 deletions

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ swagger:
5050
@echo "✅ Swagger 文档生成完成!"
5151

5252
# 兼容旧的命令名
53-
openai: swagger
53+
docs: swagger
5454

5555
# 检查 Swagger 注解完整性
5656
swagger-check:

go.sum

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,6 @@ github.com/google/pprof v0.0.0-20200708004538-1a94d8640e99/go.mod h1:ZgVRPoUq/hf
334334
github.com/google/pprof v0.0.0-20241029153458-d1b30febd7db h1:097atOisP2aRj7vFgYQBbFN4U4JNXUNYpxael3UzMyo=
335335
github.com/google/pprof v0.0.0-20241029153458-d1b30febd7db/go.mod h1:vavhavw2zAxS5dIdcRluK6cSGGPlZynqzFM8NdvU144=
336336
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
337-
github.com/google/subcommands v1.2.0 h1:vWQspBTo2nEqTUFita5/KeEWlUL8kQObDFbub/EN9oE=
338337
github.com/google/subcommands v1.2.0/go.mod h1:ZjhPrFU+Olkh9WazFPsl27BQ4UPiG37m3yTrtFlrHVk=
339338
github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
340339
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2024 Bamboo
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*
24+
*/
25+
26+
package model
27+
28+
import "time"
29+
30+
// CloudAccountRegionStatus 云账号区域状态
31+
type CloudAccountRegionStatus int8
32+
33+
const (
34+
CloudAccountRegionEnabled CloudAccountRegionStatus = iota + 1 // 启用
35+
CloudAccountRegionDisabled // 禁用
36+
)
37+
38+
// CloudAccountRegion 云账号区域关联表(支持一个账号配置多个区域)
39+
type CloudAccountRegion struct {
40+
Model
41+
CloudAccountID int `json:"cloud_account_id" gorm:"not null;comment:云账户ID;index:idx_account_region,unique"`
42+
CloudAccount *CloudAccount `json:"cloud_account,omitempty" gorm:"foreignKey:CloudAccountID"`
43+
Region string `json:"region" gorm:"type:varchar(50);not null;comment:区域,如cn-hangzhou;index:idx_account_region,unique"`
44+
RegionName string `json:"region_name" gorm:"type:varchar(100);comment:区域名称,如华东1(杭州)"`
45+
Status CloudAccountRegionStatus `json:"status" gorm:"type:tinyint(1);not null;comment:区域状态,1:启用,2:禁用;default:1"`
46+
IsDefault bool `json:"is_default" gorm:"comment:是否为默认区域;default:false"`
47+
Description string `json:"description" gorm:"type:text;comment:区域描述"`
48+
LastSyncTime *time.Time `json:"last_sync_time" gorm:"type:datetime;comment:最后同步时间"`
49+
CreateUserID int `json:"create_user_id" gorm:"comment:创建者ID;default:0"`
50+
CreateUserName string `json:"create_user_name" gorm:"type:varchar(100);comment:创建者姓名"`
51+
}
52+
53+
func (c *CloudAccountRegion) TableName() string {
54+
return "cl_cloud_account_region"
55+
}
56+
57+
// GetCloudAccountRegionListReq 获取云账号区域列表请求
58+
type GetCloudAccountRegionListReq struct {
59+
ListReq
60+
CloudAccountID int `json:"cloud_account_id" form:"cloud_account_id" binding:"omitempty,gt=0"`
61+
Region string `json:"region" form:"region"`
62+
Status CloudAccountRegionStatus `json:"status" form:"status" binding:"omitempty,oneof=1 2"`
63+
}
64+
65+
// CreateCloudAccountRegionReq 创建云账号区域关联请求
66+
type CreateCloudAccountRegionReq struct {
67+
CloudAccountID int `json:"cloud_account_id" binding:"required,gt=0"`
68+
Region string `json:"region" binding:"required"`
69+
RegionName string `json:"region_name"`
70+
IsDefault bool `json:"is_default"`
71+
Description string `json:"description"`
72+
CreateUserID int `json:"create_user_id"`
73+
CreateUserName string `json:"create_user_name"`
74+
}
75+
76+
// UpdateCloudAccountRegionReq 更新云账号区域关联请求
77+
type UpdateCloudAccountRegionReq struct {
78+
ID int `json:"id" binding:"required,gt=0"`
79+
RegionName string `json:"region_name"`
80+
IsDefault bool `json:"is_default"`
81+
Description string `json:"description"`
82+
}
83+
84+
// DeleteCloudAccountRegionReq 删除云账号区域关联请求
85+
type DeleteCloudAccountRegionReq struct {
86+
ID int `json:"id" binding:"required,gt=0"`
87+
}
88+
89+
// UpdateCloudAccountRegionStatusReq 更新云账号区域状态请求
90+
type UpdateCloudAccountRegionStatusReq struct {
91+
ID int `json:"id" binding:"required,gt=0"`
92+
Status CloudAccountRegionStatus `json:"status" binding:"required,oneof=1 2"`
93+
}
94+
95+
// BatchCreateCloudAccountRegionReq 批量创建云账号区域关联请求
96+
type BatchCreateCloudAccountRegionReq struct {
97+
CloudAccountID int `json:"cloud_account_id" binding:"required,gt=0"`
98+
Regions []CreateCloudAccountRegionItem `json:"regions" binding:"required,min=1"`
99+
CreateUserID int `json:"create_user_id"`
100+
CreateUserName string `json:"create_user_name"`
101+
}
102+
103+
// CreateCloudAccountRegionItem 创建云账号区域项
104+
type CreateCloudAccountRegionItem struct {
105+
Region string `json:"region" binding:"required"`
106+
RegionName string `json:"region_name"`
107+
IsDefault bool `json:"is_default"`
108+
Description string `json:"description"`
109+
}
110+
111+
// GetAvailableRegionsReq 获取可用区域列表请求
112+
type GetAvailableRegionsReq struct {
113+
Provider CloudProvider `json:"provider" form:"provider" binding:"required,oneof=1 2 3 4 5 6"`
114+
AccessKey string `json:"access_key" form:"access_key"` // 可选,提供时会通过API动态获取
115+
SecretKey string `json:"secret_key" form:"secret_key"` // 可选,提供时会通过API动态获取
116+
}
117+
118+
// AvailableRegion 可用区域信息
119+
type AvailableRegion struct {
120+
Region string `json:"region"` // 区域代码,如cn-hangzhou
121+
RegionName string `json:"region_name"` // 区域名称,如华东1(杭州)
122+
Available bool `json:"available"` // 是否可用
123+
}
124+
125+
// GetAvailableRegionsResp 获取可用区域列表响应
126+
type GetAvailableRegionsResp struct {
127+
Regions []AvailableRegion `json:"regions"`
128+
}

internal/model/tree_cloud.go

Lines changed: 45 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -165,40 +165,42 @@ const (
165165
type TreeCloudResource struct {
166166
Model
167167

168-
Name string `json:"name" gorm:"type:varchar(100);not null;comment:资源名称"`
169-
ResourceType CloudResourceType `json:"resource_type" gorm:"type:tinyint(1);not null;comment:资源类型;default:1"`
170-
Status CloudResourceStatus `json:"status" gorm:"type:tinyint(1);not null;comment:资源状态;default:1"`
171-
Environment string `json:"environment" gorm:"type:varchar(50);comment:环境标识(dev/test/prod)"`
172-
Description string `json:"description" gorm:"type:text;comment:资源描述"`
173-
Tags KeyValueList `json:"tags" gorm:"type:text;serializer:json;comment:资源标签集合"`
174-
CreateUserID int `json:"create_user_id" gorm:"comment:创建者ID;default:0"`
175-
CreateUserName string `json:"create_user_name" gorm:"type:varchar(100);comment:创建者姓名"`
176-
CloudAccountID int `json:"cloud_account_id" gorm:"not null;comment:云账户ID"`
177-
CloudAccount *CloudAccount `json:"cloud_account,omitempty" gorm:"foreignKey:CloudAccountID"`
178-
Region string `json:"region" gorm:"type:varchar(50);comment:区域,如cn-hangzhou"`
179-
InstanceID string `json:"instance_id" gorm:"type:varchar(100);comment:云资源实例ID"`
180-
InstanceType string `json:"instance_type" gorm:"type:varchar(100);comment:实例规格(如ecs.g6.large)"`
181-
Cpu int `json:"cpu" gorm:"comment:CPU核数;default:0"`
182-
Memory int `json:"memory" gorm:"comment:内存大小(GiB);default:0"`
183-
Disk int `json:"disk" gorm:"comment:磁盘大小(GiB);default:0"`
184-
PublicIP string `json:"public_ip" gorm:"type:varchar(45);comment:公网IP"`
185-
PrivateIP string `json:"private_ip" gorm:"type:varchar(45);comment:私网IP"`
186-
VpcID string `json:"vpc_id" gorm:"type:varchar(100);comment:VPC ID"`
187-
ZoneID string `json:"zone_id" gorm:"type:varchar(50);comment:可用区ID"`
188-
ChargeType ChargeType `json:"charge_type" gorm:"type:varchar(50);comment:计费方式(PostPaid/PrePaid)"`
189-
ExpireTime *time.Time `json:"expire_time" gorm:"type:datetime;comment:到期时间"`
190-
MonthlyCost float64 `json:"monthly_cost" gorm:"type:decimal(10,2);comment:月度成本;default:0"`
191-
Currency Currency `json:"currency" gorm:"type:varchar(10);not null;comment:货币单位;default:'CNY'"`
192-
OSType string `json:"os_type" gorm:"type:varchar(50);comment:操作系统类型(linux/windows)"`
193-
OSName string `json:"os_name" gorm:"type:varchar(100);comment:操作系统名称"`
194-
ImageID string `json:"image_id" gorm:"type:varchar(100);comment:镜像ID"`
195-
ImageName string `json:"image_name" gorm:"type:varchar(100);comment:镜像名称"`
196-
Port int `json:"port" gorm:"comment:SSH端口号;default:22"`
197-
Username string `json:"username" gorm:"type:varchar(100);comment:SSH用户名"`
198-
Password string `json:"-" gorm:"type:varchar(500);comment:SSH密码(加密存储)"`
199-
Key string `json:"-" gorm:"type:text;comment:SSH密钥"`
200-
AuthMode AuthMode `json:"auth_mode" gorm:"type:tinyint(1);comment:SSH认证方式(1:密码,2:密钥);default:1"`
201-
TreeNodes []*TreeNode `json:"tree_nodes" gorm:"many2many:cl_tree_node_cloud"`
168+
Name string `json:"name" gorm:"type:varchar(100);not null;comment:资源名称"`
169+
ResourceType CloudResourceType `json:"resource_type" gorm:"type:tinyint(1);not null;comment:资源类型;default:1"`
170+
Status CloudResourceStatus `json:"status" gorm:"type:tinyint(1);not null;comment:资源状态;default:1"`
171+
Environment string `json:"environment" gorm:"type:varchar(50);comment:环境标识(dev/test/prod)"`
172+
Description string `json:"description" gorm:"type:text;comment:资源描述"`
173+
Tags KeyValueList `json:"tags" gorm:"type:text;serializer:json;comment:资源标签集合"`
174+
CreateUserID int `json:"create_user_id" gorm:"comment:创建者ID;default:0"`
175+
CreateUserName string `json:"create_user_name" gorm:"type:varchar(100);comment:创建者姓名"`
176+
CloudAccountID int `json:"cloud_account_id" gorm:"not null;comment:云账户ID"`
177+
CloudAccount *CloudAccount `json:"cloud_account,omitempty" gorm:"foreignKey:CloudAccountID"`
178+
CloudAccountRegionID int `json:"cloud_account_region_id" gorm:"not null;comment:云账户区域ID"`
179+
CloudAccountRegion *CloudAccountRegion `json:"cloud_account_region,omitempty" gorm:"foreignKey:CloudAccountRegionID"`
180+
Region string `json:"region" gorm:"type:varchar(50);comment:区域,如cn-hangzhou(冗余字段,便于查询)"`
181+
InstanceID string `json:"instance_id" gorm:"type:varchar(100);comment:云资源实例ID"`
182+
InstanceType string `json:"instance_type" gorm:"type:varchar(100);comment:实例规格(如ecs.g6.large)"`
183+
Cpu int `json:"cpu" gorm:"comment:CPU核数;default:0"`
184+
Memory int `json:"memory" gorm:"comment:内存大小(GiB);default:0"`
185+
Disk int `json:"disk" gorm:"comment:磁盘大小(GiB);default:0"`
186+
PublicIP string `json:"public_ip" gorm:"type:varchar(45);comment:公网IP"`
187+
PrivateIP string `json:"private_ip" gorm:"type:varchar(45);comment:私网IP"`
188+
VpcID string `json:"vpc_id" gorm:"type:varchar(100);comment:VPC ID"`
189+
ZoneID string `json:"zone_id" gorm:"type:varchar(50);comment:可用区ID"`
190+
ChargeType ChargeType `json:"charge_type" gorm:"type:varchar(50);comment:计费方式(PostPaid/PrePaid)"`
191+
ExpireTime *time.Time `json:"expire_time" gorm:"type:datetime;comment:到期时间"`
192+
MonthlyCost float64 `json:"monthly_cost" gorm:"type:decimal(10,2);comment:月度成本;default:0"`
193+
Currency Currency `json:"currency" gorm:"type:varchar(10);not null;comment:货币单位;default:'CNY'"`
194+
OSType string `json:"os_type" gorm:"type:varchar(50);comment:操作系统类型(linux/windows)"`
195+
OSName string `json:"os_name" gorm:"type:varchar(100);comment:操作系统名称"`
196+
ImageID string `json:"image_id" gorm:"type:varchar(100);comment:镜像ID"`
197+
ImageName string `json:"image_name" gorm:"type:varchar(100);comment:镜像名称"`
198+
Port int `json:"port" gorm:"comment:SSH端口号;default:22"`
199+
Username string `json:"username" gorm:"type:varchar(100);comment:SSH用户名"`
200+
Password string `json:"-" gorm:"type:varchar(500);comment:SSH密码(加密存储)"`
201+
Key string `json:"-" gorm:"type:text;comment:SSH密钥"`
202+
AuthMode AuthMode `json:"auth_mode" gorm:"type:tinyint(1);comment:SSH认证方式(1:密码,2:密钥);default:1"`
203+
TreeNodes []*TreeNode `json:"tree_nodes" gorm:"many2many:cl_tree_node_cloud"`
202204
}
203205

204206
func (t *TreeCloudResource) TableName() string {
@@ -243,15 +245,15 @@ type DeleteTreeCloudResourceReq struct {
243245

244246
// SyncTreeCloudResourceReq 从云厂商同步资源请求
245247
type SyncTreeCloudResourceReq struct {
246-
CloudAccountID int `json:"cloud_account_id" binding:"required,gt=0"`
247-
ResourceTypes []CloudResourceType `json:"resource_types" binding:"omitempty"` // 同步的资源类型列表,为空则同步所有
248-
Regions []string `json:"regions"` // 指定同步的区域列表,为空则同步账号配置的区域
249-
InstanceIDs []string `json:"instance_ids"` // 指定同步的实例ID列表,为空则同步所有
250-
SyncMode SyncMode `json:"sync_mode" binding:"omitempty,oneof=full incremental"` // 同步模式: full-全量, incremental-增量
251-
AutoBind bool `json:"auto_bind"` // 是否自动绑定到服务树节点
252-
BindNodeID int `json:"bind_node_id" binding:"omitempty,gt=0"` // 自动绑定的目标节点ID
253-
OperatorID int `json:"-"` // 操作人ID (不通过JSON传递)
254-
OperatorName string `json:"-"` // 操作人姓名 (不通过JSON传递)
248+
CloudAccountID int `json:"cloud_account_id" binding:"required,gt=0"`
249+
CloudAccountRegionIDs []int `json:"cloud_account_region_ids"` // 指定同步的账号区域ID列表,为空则同步账号的所有区域
250+
ResourceTypes []CloudResourceType `json:"resource_types" binding:"omitempty"` // 同步的资源类型列表,为空则同步所有
251+
InstanceIDs []string `json:"instance_ids"` // 指定同步的实例ID列表,为空则同步所有
252+
SyncMode SyncMode `json:"sync_mode" binding:"omitempty,oneof=full incremental"` // 同步模式: full-全量, incremental-增量
253+
AutoBind bool `json:"auto_bind"` // 是否自动绑定到服务树节点
254+
BindNodeID int `json:"bind_node_id" binding:"omitempty,gt=0"` // 自动绑定的目标节点ID
255+
OperatorID int `json:"-"` // 操作人ID (不通过JSON传递)
256+
OperatorName string `json:"-"` // 操作人姓名 (不通过JSON传递)
255257
}
256258

257259
// SyncCloudResourceResp 同步云资源响应

0 commit comments

Comments
 (0)