mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-01 15:07:17 +08:00
Merge pull request #237 in YUNIONIO/onecloud from ~QUXUAN/onecloud:hotfix/qx-secgroup-sync to release/2.1.0
* commit '3d64601d3978dd1571eba13f179396d33707c1a9': 避免同步出现重复安全组
This commit is contained in:
@@ -223,6 +223,15 @@ func (manager *SGuestManager) ListItemFilter(ctx context.Context, q *sqlchemy.SQ
|
||||
q = q.Equals("host_id", host.GetId())
|
||||
}
|
||||
|
||||
secgrpFilter, _ := queryDict.GetString("secgroup")
|
||||
if len(secgrpFilter) > 0 {
|
||||
secgrp, _ := SecurityGroupManager.FetchByIdOrName("", secgrpFilter)
|
||||
if secgrp == nil {
|
||||
return nil, httperrors.NewResourceNotFoundError("secgroup %s not found", secgrpFilter)
|
||||
}
|
||||
q = q.Equals("secgrp_id", secgrp.GetId())
|
||||
}
|
||||
|
||||
zoneFilter, _ := queryDict.GetString("zone")
|
||||
if len(zoneFilter) > 0 {
|
||||
zone, _ := ZoneManager.FetchByIdOrName("", zoneFilter)
|
||||
@@ -1311,6 +1320,7 @@ func (self *SGuest) GetIsolatedDevices() []SIsolatedDevice {
|
||||
}
|
||||
|
||||
func (self *SGuest) syncWithCloudVM(ctx context.Context, userCred mcclient.TokenCredential, host *SHost, extVM cloudprovider.ICloudVM) error {
|
||||
metaData := extVM.GetMetadata()
|
||||
diff, err := GuestManager.TableSpec().Update(self, func() error {
|
||||
extVM.Refresh()
|
||||
self.Name = extVM.GetName()
|
||||
@@ -1332,6 +1342,16 @@ func (self *SGuest) syncWithCloudVM(ctx context.Context, userCred mcclient.Token
|
||||
self.BillingType = extVM.GetBillingType()
|
||||
self.ExpiredAt = extVM.GetExpiredAt()
|
||||
|
||||
if metaData != nil && metaData.Contains("secgroupId") {
|
||||
if secgroupId, err := metaData.GetString("secgroupId"); err == nil && len(secgroupId) > 0 {
|
||||
if secgrp, err := SecurityGroupManager.FetchByExternalId(secgroupId); err == nil && secgrp != nil {
|
||||
self.SecgrpId = secgrp.GetId()
|
||||
} else {
|
||||
log.Errorf("Failed find secgroup %s for guest %s error: %v", secgroupId, self.Name, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
@@ -1344,9 +1364,9 @@ func (self *SGuest) syncWithCloudVM(ctx context.Context, userCred mcclient.Token
|
||||
db.OpsLog.LogEvent(self, db.ACT_UPDATE, diffStr, userCred)
|
||||
}
|
||||
}
|
||||
if metaData := extVM.GetMetadata(); metaData != nil {
|
||||
if metaData != nil {
|
||||
meta := make(map[string]string, 0)
|
||||
if metaData.Unmarshal(meta); err != nil {
|
||||
if err := metaData.Unmarshal(meta); err != nil {
|
||||
log.Errorf("Get VM Metadata error: %v", err)
|
||||
} else {
|
||||
for key, value := range meta {
|
||||
@@ -1385,12 +1405,24 @@ func (manager *SGuestManager) newCloudVM(ctx context.Context, userCred mcclient.
|
||||
guest.HostId = host.Id
|
||||
guest.ProjectId = userCred.GetProjectId()
|
||||
|
||||
metaData := extVM.GetMetadata()
|
||||
|
||||
if metaData != nil && metaData.Contains("secgroupId") {
|
||||
if secgroupId, err := metaData.GetString("secgroupId"); err == nil && len(secgroupId) > 0 {
|
||||
if secgrp, err := SecurityGroupManager.FetchByExternalId(secgroupId); err == nil && secgrp != nil {
|
||||
guest.SecgrpId = secgrp.GetId()
|
||||
} else {
|
||||
log.Errorf("Failed find secgroup %s for guest %s error: %v", secgroupId, guest.Name, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
err := manager.TableSpec().Insert(&guest)
|
||||
if err != nil {
|
||||
log.Errorf("Insert fail %s", err)
|
||||
}
|
||||
|
||||
if metaData := extVM.GetMetadata(); metaData != nil {
|
||||
if metaData != nil {
|
||||
meta := make(map[string]string, 0)
|
||||
if err := metaData.Unmarshal(meta); err != nil {
|
||||
log.Errorf("Get VM Metadata error: %v", err)
|
||||
|
||||
@@ -2,9 +2,9 @@ package models
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"strings"
|
||||
"time"
|
||||
"database/sql"
|
||||
|
||||
"yunion.io/x/jsonutils"
|
||||
"yunion.io/x/log"
|
||||
@@ -222,6 +222,12 @@ func (manager *SSecurityGroupManager) SyncSecgroups(ctx context.Context, userCre
|
||||
}
|
||||
|
||||
for i := 0; i < len(added); i += 1 {
|
||||
if metadata := added[i].GetMetadata(); metadata != nil && metadata.Contains("id") {
|
||||
secgroupId, _ := metadata.GetString("id")
|
||||
if secgrp, _ := manager.FetchById(secgroupId); secgrp != nil {
|
||||
continue
|
||||
}
|
||||
}
|
||||
if rules, err := added[i].GetRules(); err != nil {
|
||||
syncResult.AddError(err)
|
||||
} else if len(rules) > 0 {
|
||||
@@ -354,4 +360,4 @@ func (manager *SSecurityGroupManager) InitializeData() error {
|
||||
})
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
@@ -163,6 +163,12 @@ func (self *SInstance) GetMetadata() *jsonutils.JSONDict {
|
||||
data.Update(meta)
|
||||
}
|
||||
}
|
||||
for _, secgroupId := range self.SecurityGroupIds.SecurityGroupId {
|
||||
if len(secgroupId) > 0 {
|
||||
data.Add(jsonutils.NewString(secgroupId), "secgroupId")
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return data
|
||||
}
|
||||
@@ -314,8 +320,8 @@ func (self *SInstance) GetHypervisor() string {
|
||||
}
|
||||
|
||||
func (self *SInstance) StartVM() error {
|
||||
timeout := 300*time.Second
|
||||
interval := 15*time.Second
|
||||
timeout := 300 * time.Second
|
||||
interval := 15 * time.Second
|
||||
|
||||
startTime := time.Now()
|
||||
for time.Now().Sub(startTime) < timeout {
|
||||
@@ -781,4 +787,4 @@ func (self *SInstance) GetBillingType() string {
|
||||
|
||||
func (self *SInstance) GetExpiredAt() time.Time {
|
||||
return self.ExpiredTime
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,6 +46,15 @@ type SPermissions struct {
|
||||
Permission []SPermission
|
||||
}
|
||||
|
||||
type Tags struct {
|
||||
Tag []Tag
|
||||
}
|
||||
|
||||
type Tag struct {
|
||||
TagKey string
|
||||
TagValue string
|
||||
}
|
||||
|
||||
type SSecurityGroup struct {
|
||||
vpc *SVpc
|
||||
CreationTime time.Time
|
||||
@@ -56,6 +65,7 @@ type SSecurityGroup struct {
|
||||
InnerAccessPolicy string
|
||||
Permissions SPermissions
|
||||
RegionId string
|
||||
Tags Tags
|
||||
}
|
||||
|
||||
type PermissionSet []SPermission
|
||||
@@ -78,7 +88,14 @@ func (v PermissionSet) Less(i, j int) bool {
|
||||
}
|
||||
|
||||
func (self *SSecurityGroup) GetMetadata() *jsonutils.JSONDict {
|
||||
return nil
|
||||
if len(self.Tags.Tag) == 0 {
|
||||
return nil
|
||||
}
|
||||
data := jsonutils.NewDict()
|
||||
for _, value := range self.Tags.Tag {
|
||||
data.Add(jsonutils.NewString(value.TagValue), value.TagKey)
|
||||
}
|
||||
return data
|
||||
}
|
||||
|
||||
func (self *SSecurityGroup) GetId() string {
|
||||
|
||||
Reference in New Issue
Block a user