update
This commit is contained in:
1 parent
0690285147
commit
5618cd2738
154 files changed
+5021
-3161
No files matched your search
@@ -227,10 +227,28 @@ func (h *CloudSyncHandler) ImportInstances(c *gin.Context) {
|
||||
ip = inst.InstanceID
|
||||
}
|
||||
|
||||
// 检查是否已存在(按 IP 或 hostname)
|
||||
var count int64
|
||||
h.db.Model(&model.Server{}).Where("ip = ? OR hostname = ?", ip, inst.Hostname).Count(&count)
|
||||
if count > 0 {
|
||||
hostname := cloudImportHostname(inst)
|
||||
alias := cloudImportAlias(inst)
|
||||
|
||||
// 检查是否已存在(优先实例 ID,其次 IP/hostname),重复导入只补空别名,避免覆盖人工命名。 // Logan.Li
|
||||
var existing model.Server
|
||||
existingQuery := h.db.Where("ip = ? OR hostname = ?", ip, hostname)
|
||||
if inst.InstanceID != "" {
|
||||
existingQuery = h.db.Where("cloud_instance_id = ? OR ip = ? OR hostname = ?", inst.InstanceID, ip, hostname)
|
||||
}
|
||||
if err := existingQuery.Take(&existing).Error; err == nil {
|
||||
updates := map[string]interface{}{
|
||||
"cloud_instance_name": inst.InstanceName,
|
||||
"cloud_instance_id": inst.InstanceID,
|
||||
"description": "从 " + account.Provider + " 导入 (" + inst.InstanceID + ")",
|
||||
}
|
||||
if existing.Alias == "" && alias != "" {
|
||||
updates["alias"] = alias
|
||||
}
|
||||
h.db.Model(&existing).Updates(updates)
|
||||
skipped++
|
||||
continue
|
||||
} else if err != gorm.ErrRecordNotFound {
|
||||
skipped++
|
||||
continue
|
||||
}
|
||||
@@ -264,33 +282,28 @@ func (h *CloudSyncHandler) ImportInstances(c *gin.Context) {
|
||||
if ipAddr == "" {
|
||||
ipAddr = inst.InstanceID
|
||||
}
|
||||
hostname := inst.Hostname
|
||||
if hostname == "" {
|
||||
hostname = inst.InstanceName
|
||||
}
|
||||
if hostname == "" {
|
||||
hostname = inst.InstanceID
|
||||
}
|
||||
|
||||
// 注意:servers 表的 cloud_id 外键引用的是 clouds 表(非 cloud_accounts),
|
||||
// region_id 外键引用 regions 表。未找到关联时设为 NULL 避免外键约束失败。
|
||||
server := model.Server{
|
||||
Hostname: hostname,
|
||||
IP: ipAddr,
|
||||
PublicIP: inst.PublicIP,
|
||||
Port: defaultPort,
|
||||
Username: username,
|
||||
AuthType: "password",
|
||||
OSType: osType,
|
||||
Protocol: protocol,
|
||||
RDPPort: 3389,
|
||||
OS: osName,
|
||||
CPU: inst.CPU,
|
||||
Memory: int64(inst.Memory),
|
||||
Status: status,
|
||||
RegionID: regionID, // nil = NULL
|
||||
Description: "从 " + account.Provider + " 导入 (" + inst.InstanceID + ")",
|
||||
AgentStatus: "offline",
|
||||
Hostname: hostname,
|
||||
Alias: alias,
|
||||
CloudInstanceName: inst.InstanceName,
|
||||
CloudInstanceID: inst.InstanceID,
|
||||
IP: ipAddr,
|
||||
PublicIP: inst.PublicIP,
|
||||
Port: defaultPort,
|
||||
Username: username,
|
||||
AuthType: "password",
|
||||
OSType: osType,
|
||||
Protocol: protocol,
|
||||
RDPPort: 3389,
|
||||
OS: osName,
|
||||
CPU: inst.CPU,
|
||||
Memory: int64(inst.Memory),
|
||||
Status: status,
|
||||
RegionID: regionID, // nil = NULL
|
||||
Description: "从 " + account.Provider + " 导入 (" + inst.InstanceID + ")",
|
||||
AgentStatus: "offline",
|
||||
}
|
||||
if protocol == "rdp" && hasRDPCred {
|
||||
applyCredential(rdpCred, rdpPlain, &server)
|
||||
@@ -317,3 +330,31 @@ func (h *CloudSyncHandler) ImportInstances(c *gin.Context) {
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func cloudImportHostname(inst cloud.CloudInstance) string {
|
||||
// hostname 保留技术主机名;展示友好名称写入 alias/cloud_instance_name。 // Logan.Li
|
||||
if inst.Hostname != "" && inst.Hostname != inst.InstanceID {
|
||||
return inst.Hostname
|
||||
}
|
||||
if inst.InstanceName != "" && inst.InstanceName != inst.InstanceID {
|
||||
return inst.InstanceName
|
||||
}
|
||||
if inst.PrivateIP != "" {
|
||||
return inst.PrivateIP
|
||||
}
|
||||
if inst.PublicIP != "" {
|
||||
return inst.PublicIP
|
||||
}
|
||||
if inst.InstanceName != "" {
|
||||
return inst.InstanceName
|
||||
}
|
||||
return inst.InstanceID
|
||||
}
|
||||
|
||||
func cloudImportAlias(inst cloud.CloudInstance) string {
|
||||
// 云导入初始别名优先使用控制台实例名称,后续人工别名不被同步覆盖。 // Logan.Li
|
||||
if inst.InstanceName != "" && inst.InstanceName != inst.InstanceID {
|
||||
return inst.InstanceName
|
||||
}
|
||||
return ""
|
||||
}
|
||||
Reference in new issue
Block a user