mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-24 16:03:43 +08:00
Merge pull request #8026 from ioito/hotfix/qx-qcloud-network-interface
fix: 避免腾讯云同步虚拟机ip异常
This commit is contained in:
@@ -62,6 +62,7 @@ func InitDB() error {
|
||||
ExternalProjectManager,
|
||||
CachedimageManager,
|
||||
StoragecachedimageManager,
|
||||
NetworkinterfacenetworkManager,
|
||||
} {
|
||||
err := manager.InitializeData()
|
||||
if err != nil {
|
||||
|
||||
@@ -19,6 +19,7 @@ import (
|
||||
"fmt"
|
||||
|
||||
"yunion.io/x/jsonutils"
|
||||
"yunion.io/x/log"
|
||||
"yunion.io/x/pkg/errors"
|
||||
"yunion.io/x/pkg/util/compare"
|
||||
"yunion.io/x/pkg/util/netutils"
|
||||
@@ -228,3 +229,23 @@ func (self *SNetworkinterfacenetwork) GetDetailJson() (jsonutils.JSONObject, err
|
||||
"network": network.Name,
|
||||
}), nil
|
||||
}
|
||||
|
||||
func (manager *SNetworkinterfacenetworkManager) InitializeData() error {
|
||||
sq := NetworkInterfaceManager.Query("id")
|
||||
q := manager.Query().NotIn("networkinterface_id", sq.SubQuery())
|
||||
networks := []SNetworkinterfacenetwork{}
|
||||
err := db.FetchModelObjects(manager, q, &networks)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "db.FetchModelObjects")
|
||||
}
|
||||
for i := range networks {
|
||||
_, err = db.Update(&networks[i], func() error {
|
||||
return networks[i].MarkDelete()
|
||||
})
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "Delete %d", networks[i].RowId)
|
||||
}
|
||||
}
|
||||
log.Debugf("SNetworkinterfacenetworkManager cleaned %d deprecated networkinterface ipAddrs.", len(networks))
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -286,8 +286,21 @@ func (self *SNetworkInterface) syncRemoveCloudNetworkInterface(ctx context.Conte
|
||||
|
||||
err := self.ValidateDeleteCondition(ctx)
|
||||
if err != nil {
|
||||
return self.SetStatus(userCred, api.NETWORK_INTERFACE_STATUS_UNKNOWN, "sync to delete")
|
||||
self.SetStatus(userCred, api.NETWORK_INTERFACE_STATUS_UNKNOWN, "sync to delete")
|
||||
return errors.Wrapf(err, "ValidateDeleteCondition")
|
||||
}
|
||||
|
||||
networks, err := self.GetNetworks()
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "GetNetworks")
|
||||
}
|
||||
for i := range networks {
|
||||
err = networks[i].Delete(ctx, userCred)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "Delete networkinterfacenetwork %d", networks[i].RowId)
|
||||
}
|
||||
}
|
||||
|
||||
return self.Delete(ctx, userCred)
|
||||
}
|
||||
|
||||
|
||||
@@ -16,6 +16,7 @@ package qcloud
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
@@ -51,6 +52,13 @@ func (ip *SPrivateIpAddress) IsPrimary() bool {
|
||||
return ip.Primary
|
||||
}
|
||||
|
||||
type SNetworkInterfaceAttachment struct {
|
||||
InstanceId string
|
||||
DeviceIndex int
|
||||
InstanceAccountId string
|
||||
AttachTime string
|
||||
}
|
||||
|
||||
type SNetworkInterface struct {
|
||||
multicloud.SNetworkInterfaceBase
|
||||
region *SRegion
|
||||
@@ -64,7 +72,7 @@ type SNetworkInterface struct {
|
||||
MacAddress string
|
||||
State string
|
||||
CreatedTime time.Time
|
||||
Attachment string
|
||||
Attachment SNetworkInterfaceAttachment
|
||||
Zone string
|
||||
PrivateIpAddressSet []SPrivateIpAddress
|
||||
}
|
||||
@@ -90,7 +98,7 @@ func (nic *SNetworkInterface) GetAssociateType() string {
|
||||
}
|
||||
|
||||
func (nic *SNetworkInterface) GetAssociateId() string {
|
||||
return nic.Attachment
|
||||
return nic.Attachment.InstanceId
|
||||
}
|
||||
|
||||
func (nic *SNetworkInterface) GetStatus() string {
|
||||
@@ -132,8 +140,10 @@ func (region *SRegion) GetINetworkInterfaces() ([]cloudprovider.ICloudNetworkInt
|
||||
}
|
||||
ret := []cloudprovider.ICloudNetworkInterface{}
|
||||
for i := 0; i < len(interfaces); i++ {
|
||||
interfaces[i].region = region
|
||||
ret = append(ret, &interfaces[i])
|
||||
if strings.HasPrefix(interfaces[i].Attachment.InstanceId, "ins-") { //弹性网卡有可能已绑定资源,若绑定资源则由资源进行同步
|
||||
interfaces[i].region = region
|
||||
ret = append(ret, &interfaces[i])
|
||||
}
|
||||
}
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
// Copyright 2019 Yunion
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package shell
|
||||
|
||||
import (
|
||||
"yunion.io/x/onecloud/pkg/multicloud/qcloud"
|
||||
"yunion.io/x/onecloud/pkg/util/shellutils"
|
||||
)
|
||||
|
||||
func init() {
|
||||
type NetworkInterfaceListOptions struct {
|
||||
Subnet string
|
||||
Ids []string
|
||||
Limit int `help:"page size"`
|
||||
Offset int `help:"page offset"`
|
||||
}
|
||||
shellutils.R(&NetworkInterfaceListOptions{}, "network-interface-list", "List network interfaces", func(cli *qcloud.SRegion, args *NetworkInterfaceListOptions) error {
|
||||
interfaces, total, err := cli.GetNetworkInterfaces(args.Ids, args.Subnet, args.Offset, args.Limit)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
printList(interfaces, total, args.Offset, args.Limit, []string{})
|
||||
return nil
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user