From 1466830595c84911708d860af38cbbb94d707c12 Mon Sep 17 00:00:00 2001 From: ioito Date: Wed, 23 Sep 2020 17:02:57 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E9=81=BF=E5=85=8D=E8=85=BE=E8=AE=AF?= =?UTF-8?q?=E4=BA=91=E5=90=8C=E6=AD=A5=E8=99=9A=E6=8B=9F=E6=9C=BAip?= =?UTF-8?q?=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/compute/models/initdb.go | 1 + pkg/compute/models/networkinterfacenetwork.go | 21 +++++++++++ pkg/compute/models/networkinterfaces.go | 15 +++++++- pkg/multicloud/qcloud/networkinterface.go | 18 +++++++-- .../qcloud/shell/networkinterface.go | 37 +++++++++++++++++++ 5 files changed, 87 insertions(+), 5 deletions(-) create mode 100644 pkg/multicloud/qcloud/shell/networkinterface.go diff --git a/pkg/compute/models/initdb.go b/pkg/compute/models/initdb.go index 587d3f7271..d08d988811 100644 --- a/pkg/compute/models/initdb.go +++ b/pkg/compute/models/initdb.go @@ -62,6 +62,7 @@ func InitDB() error { ExternalProjectManager, CachedimageManager, StoragecachedimageManager, + NetworkinterfacenetworkManager, } { err := manager.InitializeData() if err != nil { diff --git a/pkg/compute/models/networkinterfacenetwork.go b/pkg/compute/models/networkinterfacenetwork.go index bb5a828037..9dbb97da8e 100644 --- a/pkg/compute/models/networkinterfacenetwork.go +++ b/pkg/compute/models/networkinterfacenetwork.go @@ -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 +} diff --git a/pkg/compute/models/networkinterfaces.go b/pkg/compute/models/networkinterfaces.go index 40d1a9bd13..1b48de39cc 100644 --- a/pkg/compute/models/networkinterfaces.go +++ b/pkg/compute/models/networkinterfaces.go @@ -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) } diff --git a/pkg/multicloud/qcloud/networkinterface.go b/pkg/multicloud/qcloud/networkinterface.go index 999c399be2..3988036def 100644 --- a/pkg/multicloud/qcloud/networkinterface.go +++ b/pkg/multicloud/qcloud/networkinterface.go @@ -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 } diff --git a/pkg/multicloud/qcloud/shell/networkinterface.go b/pkg/multicloud/qcloud/shell/networkinterface.go new file mode 100644 index 0000000000..283a6ea538 --- /dev/null +++ b/pkg/multicloud/qcloud/shell/networkinterface.go @@ -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 + }) +}