Skip to content

Commit

Permalink
Add TargetZone value to ConnectionInfo.RegionInfo.TargetZone for zone…
Browse files Browse the repository at this point in the history
…-based control of Subnet, VM, and Disk
  • Loading branch information
powerkimhub committed Sep 9, 2024
1 parent c1f4920 commit 5986075
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 50 deletions.
12 changes: 12 additions & 0 deletions api-runtime/common-runtime/DiskManager.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,15 @@ func RegisterDisk(connectionName string, zoneId string, userIID cres.IID) (*cres
return nil, err
}

if zoneId == "" {
// get defaultZoneId
_, zoneId, err = ccm.GetRegionNameByConnectionName(connectionName)
if err != nil {
cblog.Error(err)
return nil, err
}
}

cldConn, err := ccm.GetZoneLevelCloudConnection(connectionName, zoneId)
if err != nil {
cblog.Error(err)
Expand Down Expand Up @@ -116,6 +125,9 @@ func RegisterDisk(connectionName string, zoneId string, userIID cres.IID) (*cres
return nil, err
}

if getInfo.Zone == "" {
getInfo.Zone = zoneId
}
// set up Disk User IID for return info
getInfo.IId = userIID

Expand Down
99 changes: 59 additions & 40 deletions api-runtime/common-runtime/VMManager.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
// ====================================================================
// type for GORM

type VMIIDInfo FirstIIDInfo
type VMIIDInfo ZoneLevelIIDInfo

func (VMIIDInfo) TableName() string {
return "vm_iid_infos"
Expand Down Expand Up @@ -322,8 +322,18 @@ func RegisterVM(connectionName string, userIID cres.IID) (*cres.VMInfo, error) {
spiderIId := cres.IID{NameId: userIID.NameId, SystemId: systemId + ":" + getInfo.IId.SystemId}

// (4) insert spiderIID
if getInfo.Region.Zone == "" {
// get defaultZoneId
_, getInfo.Region.Zone, err = ccm.GetRegionNameByConnectionName(connectionName)
if err != nil {
cblog.Error(err)
return nil, err
}
}

// insert VM SpiderIID to metadb
err = infostore.Insert(&VMIIDInfo{ConnectionName: connectionName, NameId: spiderIId.NameId, SystemId: spiderIId.SystemId})
iidInfo := VMIIDInfo{ConnectionName: connectionName, ZoneId: getInfo.Region.Zone, NameId: spiderIId.NameId, SystemId: spiderIId.SystemId}
err = infostore.Insert(&iidInfo)
if err != nil {
cblog.Error(err)
return nil, err
Expand Down Expand Up @@ -374,20 +384,28 @@ func StartVM(connectionName string, rsType string, reqInfo cres.VMReqInfo, IDTra
return nil, err
}

cldConn, err := ccm.GetCloudConnection(connectionName)
vmSPLock.Lock(connectionName, reqInfo.IId.NameId)
defer vmSPLock.Unlock(connectionName, reqInfo.IId.NameId)

// Get ZoneId from input SubnetIID
var subnetIIDInfo SubnetIIDInfo
err = infostore.GetBy3Conditions(&subnetIIDInfo, CONNECTION_NAME_COLUMN, connectionName, NAME_ID_COLUMN, reqInfo.SubnetIID.NameId, OWNER_VPC_NAME_COLUMN, reqInfo.VpcIID.NameId)
if err != nil {
cblog.Error(err)
return nil, err
}

handler, err := cldConn.CreateVMHandler()
cldConn, err := ccm.GetZoneLevelCloudConnection(connectionName, subnetIIDInfo.ZoneId)
if err != nil {
cblog.Error(err)
return nil, err
}

vmSPLock.Lock(connectionName, reqInfo.IId.NameId)
defer vmSPLock.Unlock(connectionName, reqInfo.IId.NameId)
handler, err := cldConn.CreateVMHandler()
if err != nil {
cblog.Error(err)
return nil, err
}

// (1) check exist(NameID)
dockerTest := os.Getenv("DOCKER_POC_TEST") // For docker poc tests, this is currently the best method.
Expand Down Expand Up @@ -576,7 +594,7 @@ func StartVM(connectionName string, rsType string, reqInfo cres.VMReqInfo, IDTra
spiderIId := cres.IID{NameId: reqIId.NameId, SystemId: spUUID + ":" + info.IId.SystemId}

// (6) insert spiderIID
iidInfo := VMIIDInfo{ConnectionName: connectionName, NameId: spiderIId.NameId, SystemId: spiderIId.SystemId}
iidInfo := VMIIDInfo{ConnectionName: connectionName, ZoneId: subnetIIDInfo.ZoneId, NameId: spiderIId.NameId, SystemId: spiderIId.SystemId}
err = infostore.Insert(&iidInfo)
if err != nil {
cblog.Error(err)
Expand Down Expand Up @@ -1216,24 +1234,24 @@ func GetVM(connectionName string, rsType string, nameID string) (*cres.VMInfo, e
return nil, err
}

cldConn, err := ccm.GetCloudConnection(connectionName)
vmSPLock.RLock(connectionName, nameID)
defer vmSPLock.RUnlock(connectionName, nameID)

// (1) get IID(NameId)
var iidInfo VMIIDInfo
err = infostore.GetByConditions(&iidInfo, CONNECTION_NAME_COLUMN, connectionName, NAME_ID_COLUMN, nameID)
if err != nil {
cblog.Error(err)
return nil, err
}

handler, err := cldConn.CreateVMHandler()
cldConn, err := ccm.GetZoneLevelCloudConnection(connectionName, iidInfo.ZoneId)
if err != nil {
cblog.Error(err)
return nil, err
}

vmSPLock.RLock(connectionName, nameID)
defer vmSPLock.RUnlock(connectionName, nameID)

// (1) get IID(NameId)
var iidInfo VMIIDInfo
err = infostore.GetByConditions(&iidInfo, CONNECTION_NAME_COLUMN, connectionName, NAME_ID_COLUMN, nameID)
handler, err := cldConn.CreateVMHandler()
if err != nil {
cblog.Error(err)
return nil, err
Expand Down Expand Up @@ -1430,25 +1448,26 @@ func GetVMStatus(connectionName string, rsType string, nameID string) (cres.VMSt
return "", err
}

cldConn, err := ccm.GetCloudConnection(connectionName)
/* temporarily unlocked
vmSPLock.RLock(connectionName, nameID)
defer vmSPLock.RUnlock(connectionName, nameID)
*/

// (1) get IID(NameId)
var iidInfo VMIIDInfo
err = infostore.GetByConditions(&iidInfo, CONNECTION_NAME_COLUMN, connectionName, NAME_ID_COLUMN, nameID)
if err != nil {
cblog.Error(err)
return "", err
}

handler, err := cldConn.CreateVMHandler()
cldConn, err := ccm.GetZoneLevelCloudConnection(connectionName, iidInfo.ZoneId)
if err != nil {
cblog.Error(err)
return "", err
}
/* temporarily unlocked
vmSPLock.RLock(connectionName, nameID)
defer vmSPLock.RUnlock(connectionName, nameID)
*/

// (1) get IID(NameId)
var iidInfo VMIIDInfo
err = infostore.GetByConditions(&iidInfo, CONNECTION_NAME_COLUMN, connectionName, NAME_ID_COLUMN, nameID)
handler, err := cldConn.CreateVMHandler()
if err != nil {
cblog.Error(err)
return "", err
Expand Down Expand Up @@ -1489,24 +1508,24 @@ func GetVMStatus(connectionName string, rsType string, nameID string) (cres.VMSt
func ControlVM(connectionName string, rsType string, nameID string, action string) (cres.VMStatus, error) {
cblog.Info("call ControlVM()")

cldConn, err := ccm.GetCloudConnection(connectionName)
vmSPLock.RLock(connectionName, nameID)
defer vmSPLock.RUnlock(connectionName, nameID)

// (1) get IID(NameId)
var iidInfo VMIIDInfo
err := infostore.GetByConditions(&iidInfo, CONNECTION_NAME_COLUMN, connectionName, NAME_ID_COLUMN, nameID)
if err != nil {
cblog.Error(err)
return "", err
}

handler, err := cldConn.CreateVMHandler()
cldConn, err := ccm.GetZoneLevelCloudConnection(connectionName, iidInfo.ZoneId)
if err != nil {
cblog.Error(err)
return "", err
}

vmSPLock.RLock(connectionName, nameID)
defer vmSPLock.RUnlock(connectionName, nameID)

// (1) get IID(NameId)
var iidInfo VMIIDInfo
err = infostore.GetByConditions(&iidInfo, CONNECTION_NAME_COLUMN, connectionName, NAME_ID_COLUMN, nameID)
handler, err := cldConn.CreateVMHandler()
if err != nil {
cblog.Error(err)
return "", err
Expand Down Expand Up @@ -1552,24 +1571,24 @@ func DeleteVM(connectionName string, rsType string, nameID string, force string)
return false, "", err
}

cldConn, err := ccm.GetCloudConnection(connectionName)
vmSPLock.Lock(connectionName, nameID)
defer vmSPLock.Unlock(connectionName, nameID)

// (1) get spiderIID for creating driverIID
var iidInfo VMIIDInfo
err = infostore.GetByConditions(&iidInfo, CONNECTION_NAME_COLUMN, connectionName, NAME_ID_COLUMN, nameID)
if err != nil {
cblog.Error(err)
return false, "", err
}

handler, err := cldConn.CreateVMHandler()
cldConn, err := ccm.GetZoneLevelCloudConnection(connectionName, iidInfo.ZoneId)
if err != nil {
cblog.Error(err)
return false, "", err
}

vmSPLock.Lock(connectionName, nameID)
defer vmSPLock.Unlock(connectionName, nameID)

// (1) get spiderIID for creating driverIID
var iidInfo VMIIDInfo
err = infostore.GetByConditions(&iidInfo, CONNECTION_NAME_COLUMN, connectionName, NAME_ID_COLUMN, nameID)
handler, err := cldConn.CreateVMHandler()
if err != nil {
cblog.Error(err)
return false, "", err
Expand Down
30 changes: 22 additions & 8 deletions api-runtime/common-runtime/VPC-SubnetManager.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,13 @@ func RegisterVPC(connectionName string, userIID cres.IID) (*cres.VPCInfo, error)
return nil, err
}

// get defaultZoneId
_, defaultZoneId, err := ccm.GetRegionNameByConnectionName(connectionName)
if err != nil {
cblog.Error(err)
return nil, err
}

// insert subnet's spiderIIDs to metadb and setup subnet IID for return info
for count, subnetInfo := range getInfo.SubnetInfoList {
//---------- original code ----------------
Expand All @@ -147,8 +154,12 @@ func RegisterVPC(connectionName string, userIID cres.IID) (*cres.VPCInfo, error)
subnetUserId := systemId
---------- new version code ---------------- */

// check and set ZoneId
if subnetInfo.Zone == "" {
subnetInfo.Zone = defaultZoneId
}
subnetSpiderIId := cres.IID{NameId: subnetUserId, SystemId: systemId + ":" + subnetInfo.IId.SystemId}
err = infostore.Insert(&SubnetIIDInfo{ConnectionName: connectionName, NameId: subnetSpiderIId.NameId, SystemId: subnetSpiderIId.SystemId,
err = infostore.Insert(&SubnetIIDInfo{ConnectionName: connectionName, ZoneId: subnetInfo.Zone, NameId: subnetSpiderIId.NameId, SystemId: subnetSpiderIId.SystemId,
OwnerVPCName: userIID.NameId})
if err != nil {
cblog.Error(err)
Expand Down Expand Up @@ -195,6 +206,15 @@ func RegisterSubnet(connectionName string, zoneId string, vpcName string, userII
return nil, err
}

if zoneId == "" {
// get defaultZoneId
_, zoneId, err = ccm.GetRegionNameByConnectionName(connectionName)
if err != nil {
cblog.Error(err)
return nil, err
}
}

cldConn, err := ccm.GetZoneLevelCloudConnection(connectionName, zoneId)
if err != nil {
cblog.Error(err)
Expand Down Expand Up @@ -256,13 +276,7 @@ func RegisterSubnet(connectionName string, zoneId string, vpcName string, userII
return nil, err
}
if subnetInfo.Zone == "" { // GCP has no Zone info
var iidInfo SubnetIIDInfo
err = infostore.GetBy3Conditions(&iidInfo, CONNECTION_NAME_COLUMN, connectionName, NAME_ID_COLUMN, subnetInfo.IId.NameId, OWNER_VPC_NAME_COLUMN, vpcName)
if err != nil {
cblog.Info(err)
} else {
subnetInfo.Zone = iidInfo.ZoneId
}
subnetInfo.Zone = zoneId
}

// setup subnet IID for return info
Expand Down
2 changes: 1 addition & 1 deletion api-runtime/rest-runtime/DiskRest.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type DiskRegisterRequest struct {
ConnectionName string `json:"ConnectionName" validate:"required" example:"aws-connection"`
ReqInfo struct {
Name string `json:"Name" validate:"required" example:"disk-01"`
Zone string `json:"Zone" validate:"required" example:"us-east-1b"`
Zone string `json:"Zone" validate:"required" example:"us-east-1b"` // (default: defaultZone)
CSPId string `json:"CSPId" validate:"required" example:"csp-disk-1234"`
} `json:"ReqInfo" validate:"required"`
}
Expand Down
2 changes: 1 addition & 1 deletion api-runtime/rest-runtime/VPC-SubnetRest.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ type SubnetRegisterRequest struct {
ConnectionName string `json:"ConnectionName" validate:"required" example:"aws-connection"`
ReqInfo struct {
Name string `json:"Name" validate:"required" example:"subnet-01"`
Zone string `json:"Zone,omitempty" validate:"omitempty" example:"us-east-1a"`
Zone string `json:"Zone,omitempty" validate:"omitempty" example:"us-east-1a"` // (default: defaultZone)
VPCName string `json:"VPCName" validate:"required" example:"vpc-01"`
CSPId string `json:"CSPId" validate:"required" example:"csp-subnet-1234"`
} `json:"ReqInfo" validate:"required"`
Expand Down
2 changes: 2 additions & 0 deletions api/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -9988,6 +9988,7 @@ const docTemplate = `{
"example": "disk-01"
},
"Zone": {
"description": "(default: defaultZone)",
"type": "string",
"example": "us-east-1b"
}
Expand Down Expand Up @@ -10912,6 +10913,7 @@ const docTemplate = `{
"example": "vpc-01"
},
"Zone": {
"description": "(default: defaultZone)",
"type": "string",
"example": "us-east-1a"
}
Expand Down
2 changes: 2 additions & 0 deletions api/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -9985,6 +9985,7 @@
"example": "disk-01"
},
"Zone": {
"description": "(default: defaultZone)",
"type": "string",
"example": "us-east-1b"
}
Expand Down Expand Up @@ -10909,6 +10910,7 @@
"example": "vpc-01"
},
"Zone": {
"description": "(default: defaultZone)",
"type": "string",
"example": "us-east-1a"
}
Expand Down
2 changes: 2 additions & 0 deletions api/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1692,6 +1692,7 @@ definitions:
example: disk-01
type: string
Zone:
description: '(default: defaultZone)'
example: us-east-1b
type: string
required:
Expand Down Expand Up @@ -2334,6 +2335,7 @@ definitions:
example: vpc-01
type: string
Zone:
description: '(default: defaultZone)'
example: us-east-1a
type: string
required:
Expand Down

0 comments on commit 5986075

Please sign in to comment.