mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-08-29 03:51:54 +08:00
fix: 支持创建时指定多个安全组
This commit is contained in:
@@ -666,6 +666,13 @@ InstanceCreate:
|
||||
items:
|
||||
type: string
|
||||
description: 主机组的ID或者Name
|
||||
secgroups:
|
||||
type: array
|
||||
description: 安全组列表
|
||||
items:
|
||||
type: string
|
||||
example: default
|
||||
description: 安全组名称或ID,建议使用ID
|
||||
|
||||
|
||||
InstanceDisk:
|
||||
|
||||
@@ -188,6 +188,7 @@ type ServerCreateInput struct {
|
||||
EipChargeType string `json:"eip_charge_type,omitempty"`
|
||||
Eip string `json:"eip,omitempty"`
|
||||
InstanceSnapshotId string `json:"instance_snapshot_id,omitempty"`
|
||||
Secgroups []string `json:"secgroups"`
|
||||
|
||||
OsType string `json:"os_type"`
|
||||
// Fill by server
|
||||
|
||||
@@ -401,6 +401,7 @@ func FetchServerCreateInputByJSON(obj jsonutils.JSONObject) (*compute.ServerCrea
|
||||
if secgroup := jsonutils.GetAnyString(obj, []string{"secgroup", "secgroup_id", "secgrp_id"}); len(secgroup) != 0 {
|
||||
input.SecgroupId = secgroup
|
||||
}
|
||||
input.Secgroups, _ = jsonutils.GetStringArray(obj, "secgroups")
|
||||
|
||||
return input, nil
|
||||
}
|
||||
|
||||
@@ -398,6 +398,10 @@ func (self *SManagedVirtualizedGuestDriver) RemoteDeployGuestForCreate(ctx conte
|
||||
return nil, err
|
||||
}
|
||||
db.SetExternalId(guest, userCred, iVM.GetGlobalId())
|
||||
err = iVM.SetSecurityGroups(desc.ExternalSecgroupIds)
|
||||
if err != nil {
|
||||
log.Errorf("failed to set multi secgroup for instance %s error: %v", guest.Name, err)
|
||||
}
|
||||
|
||||
if hostId := iVM.GetIHostId(); len(hostId) > 0 {
|
||||
host, err := db.FetchByExternalId(models.HostManager, hostId)
|
||||
|
||||
@@ -1186,7 +1186,20 @@ func (manager *SGuestManager) validateCreateData(
|
||||
input.KeypairId = keypairObj.GetId()
|
||||
}
|
||||
|
||||
if input.SecgroupId != "" {
|
||||
secGrpIds := []string{}
|
||||
for _, secgroup := range input.Secgroups {
|
||||
secGrpObj, err := SecurityGroupManager.FetchByIdOrName(userCred, secgroup)
|
||||
if err != nil {
|
||||
return nil, httperrors.NewResourceNotFoundError("Secgroup %s not found", secgroup)
|
||||
}
|
||||
if !utils.IsInStringArray(secGrpObj.GetId(), secGrpIds) {
|
||||
secGrpIds = append(secGrpIds, secGrpObj.GetId())
|
||||
}
|
||||
}
|
||||
if len(secGrpIds) > 0 {
|
||||
input.SecgroupId = secGrpIds[0]
|
||||
input.Secgroups = secGrpIds[1:]
|
||||
} else if input.SecgroupId != "" {
|
||||
secGrpId := input.SecgroupId
|
||||
secGrpObj, err := SecurityGroupManager.FetchByIdOrName(userCred, secGrpId)
|
||||
if err != nil {
|
||||
@@ -1197,6 +1210,13 @@ func (manager *SGuestManager) validateCreateData(
|
||||
input.SecgroupId = "default"
|
||||
}
|
||||
|
||||
maxSecgrpCount := GetDriver(hypervisor).GetMaxSecurityGroupCount()
|
||||
if maxSecgrpCount == 0 { //esxi 不支持安全组
|
||||
input.Secgroups = []string{}
|
||||
} else if len(input.Secgroups)+1 > maxSecgrpCount {
|
||||
return nil, httperrors.NewInputParameterError("%s shall bind up to %d security groups", hypervisor, maxSecgrpCount)
|
||||
}
|
||||
|
||||
preferRegionId, _ := data.GetString("prefer_region_id")
|
||||
if err := manager.validateEip(userCred, input, preferRegionId); err != nil {
|
||||
return nil, err
|
||||
@@ -1419,6 +1439,12 @@ func (guest *SGuest) PostCreate(ctx context.Context, userCred mcclient.TokenCred
|
||||
if len(userData) > 0 {
|
||||
guest.setUserData(ctx, userCred, userData)
|
||||
}
|
||||
secgroups, _ := jsonutils.GetStringArray(data, "secgroups")
|
||||
for _, secgroup := range secgroups {
|
||||
gs := SGuestsecgroup{SecgroupId: secgroup}
|
||||
gs.GuestId = guest.Id
|
||||
GuestsecgroupManager.TableSpec().Insert(&gs)
|
||||
}
|
||||
}
|
||||
|
||||
func (guest *SGuest) setApptags(ctx context.Context, appTags []string, userCred mcclient.TokenCredential) {
|
||||
@@ -4553,6 +4579,11 @@ func (self *SGuest) ToCreateInput(userCred mcclient.TokenCredential) *api.Server
|
||||
userInput.EipBw = genInput.EipBw
|
||||
userInput.EipChargeType = genInput.EipChargeType
|
||||
userInput.Project = genInput.Project
|
||||
userInput.Secgroups = []string{}
|
||||
secgroups := self.GetSecgroups()
|
||||
for _, secgroup := range secgroups {
|
||||
userInput.Secgroups = append(userInput.Secgroups, secgroup.Id)
|
||||
}
|
||||
if genInput.ResourceType != "" {
|
||||
userInput.ResourceType = genInput.ResourceType
|
||||
}
|
||||
|
||||
@@ -272,6 +272,7 @@ type ServerCreateOptions struct {
|
||||
DryRun *bool `help:"Dry run to test scheduler" json:"-"`
|
||||
UserDataFile string `help:"user_data file path" json:"-"`
|
||||
InstanceSnapshot string `help:"instance snapshot" json:"instance_snapshot"`
|
||||
Secgroups []string `help:"secgroups" json:"secgroups"`
|
||||
|
||||
OsType string `help:"os type, e.g. Linux, Windows, etc."`
|
||||
|
||||
@@ -366,6 +367,7 @@ func (opts *ServerCreateOptions) Params() (*computeapi.ServerCreateInput, error)
|
||||
EnableCloudInit: opts.EnableCloudInit,
|
||||
OsType: opts.OsType,
|
||||
GuestImageID: opts.GuestImageID,
|
||||
Secgroups: opts.Secgroups,
|
||||
}
|
||||
|
||||
if opts.GenerateName {
|
||||
|
||||
Reference in New Issue
Block a user