From aac4d5bdf54609440c2aae338a6c3fdde4610aeb Mon Sep 17 00:00:00 2001 From: ioito Date: Mon, 22 Jul 2019 15:06:43 +0800 Subject: [PATCH] networks details for networkinterface --- cmd/climc/shell/networkinterfaces.go | 55 +++++++++++++++++++ pkg/apis/compute/networkinterface_const.go | 2 + pkg/compute/models/networkinterfaces.go | 10 +++- pkg/mcclient/modules/mod_networkinterfaces.go | 27 +++++++++ pkg/util/huawei/port.go | 15 +++-- pkg/util/openstack/port.go | 2 +- 6 files changed, 104 insertions(+), 7 deletions(-) create mode 100644 cmd/climc/shell/networkinterfaces.go create mode 100644 pkg/mcclient/modules/mod_networkinterfaces.go diff --git a/cmd/climc/shell/networkinterfaces.go b/cmd/climc/shell/networkinterfaces.go new file mode 100644 index 0000000000..75fb21f832 --- /dev/null +++ b/cmd/climc/shell/networkinterfaces.go @@ -0,0 +1,55 @@ +// 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/mcclient" + "yunion.io/x/onecloud/pkg/mcclient/modules" + "yunion.io/x/onecloud/pkg/mcclient/options" +) + +func init() { + + type NetworkinterfaceListOptions struct { + options.BaseListOptions + } + + R(&NetworkinterfaceListOptions{}, "network-interface-list", "List networkinterfaces", func(s *mcclient.ClientSession, opts *NetworkinterfaceListOptions) error { + params, err := options.ListStructToParams(opts) + if err != nil { + return err + } + result, err := modules.Networkinterfaces.List(s, params) + if err != nil { + return err + } + printList(result, modules.Networkinterfaces.GetColumns(s)) + return nil + }) + + type NetworkinterfaceIdOptions struct { + ID string `help:"Id or name of networkinterface"` + } + + R(&NetworkinterfaceIdOptions{}, "network-interface-show", "Show networkinterface detail", func(s *mcclient.ClientSession, args *NetworkinterfaceIdOptions) error { + result, err := modules.Networkinterfaces.Get(s, args.ID, nil) + if err != nil { + return err + } + printObject(result) + return nil + }) + +} diff --git a/pkg/apis/compute/networkinterface_const.go b/pkg/apis/compute/networkinterface_const.go index 90b8f1d9b0..116cd6396a 100644 --- a/pkg/apis/compute/networkinterface_const.go +++ b/pkg/apis/compute/networkinterface_const.go @@ -18,6 +18,8 @@ const ( NETWORK_INTERFACE_ASSOCIATE_TYPE_SERVER = "server" NETWORK_INTERFACE_ASSOCIATE_TYPE_RESERVED = "reserved" NETWORK_INTERFACE_ASSOCIATE_TYPE_LOADBALANCER = "loadbalancer" + NETWORK_INTERFACE_ASSOCIATE_TYPE_VIP = "vip" + NETWORK_INTERFACE_ASSOCIATE_TYPE_DHCP = "dhcp" NETWORK_INTERFACE_STATUS_INIT = "init" NETWORK_INTERFACE_STATUS_CREATING = "creating" diff --git a/pkg/compute/models/networkinterfaces.go b/pkg/compute/models/networkinterfaces.go index 8668def3ca..4f35aaef01 100644 --- a/pkg/compute/models/networkinterfaces.go +++ b/pkg/compute/models/networkinterfaces.go @@ -53,8 +53,8 @@ type SNetworkInterface struct { SManagedResourceBase SCloudregionResourceBase - Mac string `width:"36" charset:"ascii"` - AssociateType string `width:"36" charset:"ascii" nullable:"true" create:"required"` + Mac string `width:"36" charset:"ascii" list:"user"` + AssociateType string `width:"36" charset:"ascii" list:"user" nullable:"true" create:"optional"` AssociateId string `width:"36" charset:"ascii" list:"user"` } @@ -109,6 +109,12 @@ func (self *SNetworkInterface) GetCustomizeColumns(ctx context.Context, userCred if regionInfo != nil { extra.Update(regionInfo) } + networks, err := self.GetNetworks() + if err != nil { + log.Errorf("failed to get network for networkinterface %s(%s) error: %v", self.Name, self.Id, err) + return extra + } + extra.Add(jsonutils.Marshal(networks), "networks") return extra } diff --git a/pkg/mcclient/modules/mod_networkinterfaces.go b/pkg/mcclient/modules/mod_networkinterfaces.go new file mode 100644 index 0000000000..26a1098173 --- /dev/null +++ b/pkg/mcclient/modules/mod_networkinterfaces.go @@ -0,0 +1,27 @@ +// 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 modules + +var ( + Networkinterfaces ResourceManager +) + +func init() { + Networkinterfaces = NewComputeManager("networkinterface", "networkinterfaces", + []string{"ID", "Name", "Status", "Mac", "Region", "Associate_Type", "Associate_Id"}, + []string{}) + + registerCompute(&Networkinterfaces) +} diff --git a/pkg/util/huawei/port.go b/pkg/util/huawei/port.go index f6218d0a0c..945deb5fe4 100644 --- a/pkg/util/huawei/port.go +++ b/pkg/util/huawei/port.go @@ -18,15 +18,17 @@ import ( api "yunion.io/x/onecloud/pkg/apis/compute" "yunion.io/x/onecloud/pkg/cloudprovider" "yunion.io/x/onecloud/pkg/multicloud" + "yunion.io/x/pkg/utils" ) type SFixedIP struct { IpAddress string SubnetID string + NetworkId string } func (fixip *SFixedIP) GetGlobalId() string { - return fixip.SubnetID + return fixip.IpAddress } func (fixip *SFixedIP) GetIP() string { @@ -34,7 +36,7 @@ func (fixip *SFixedIP) GetIP() string { } func (fixip *SFixedIP) GetINetworkId() string { - return fixip.SubnetID + return fixip.NetworkId } func (fixip *SFixedIP) IsPrimary() bool { @@ -81,10 +83,14 @@ func (port *Port) GetAssociateType() string { switch port.DeviceOwner { case "compute:nova": return api.NETWORK_INTERFACE_ASSOCIATE_TYPE_SERVER - case "network:router_gateway", "network:dhcp", "network:router_interface", "network:router_interface_distributed": + case "network:router_gateway", "network:router_interface", "network:router_interface_distributed": return api.NETWORK_INTERFACE_ASSOCIATE_TYPE_RESERVED + case "network:dhcp": + return api.NETWORK_INTERFACE_ASSOCIATE_TYPE_DHCP case "neutron:LOADBALANCERV2": return api.NETWORK_INTERFACE_ASSOCIATE_TYPE_LOADBALANCER + case "neutron:VIP_PORT": + return api.NETWORK_INTERFACE_ASSOCIATE_TYPE_VIP } return port.DeviceOwner } @@ -106,6 +112,7 @@ func (port *Port) GetStatus() string { func (port *Port) GetICloudInterfaceAddresses() ([]cloudprovider.ICloudInterfaceAddress, error) { address := []cloudprovider.ICloudInterfaceAddress{} for i := 0; i < len(port.FixedIps); i++ { + port.FixedIps[i].NetworkId = port.NetworkID address = append(address, &port.FixedIps[i]) } return address, nil @@ -118,7 +125,7 @@ func (region *SRegion) GetINetworkInterfaces() ([]cloudprovider.ICloudNetworkInt } ret := []cloudprovider.ICloudNetworkInterface{} for i := 0; i < len(ports); i++ { - if len(ports[i].DeviceID) == 0 || (ports[i].DeviceOwner != "compute:nova" && ports[i].DeviceOwner != "neutron:LOADBALANCERV2") { + if len(ports[i].DeviceID) == 0 || !utils.IsInStringArray(ports[i].DeviceOwner, []string{"compute:CCI", "compute:nova", "neutron:LOADBALANCERV2"}) { ports[i].region = region ret = append(ret, &ports[i]) } diff --git a/pkg/util/openstack/port.go b/pkg/util/openstack/port.go index 43a61f9b60..3fdcb7597d 100644 --- a/pkg/util/openstack/port.go +++ b/pkg/util/openstack/port.go @@ -43,7 +43,7 @@ type SFixedIP struct { } func (fixip *SFixedIP) GetGlobalId() string { - return fixip.SubnetID + return fixip.IpAddress } func (fixip *SFixedIP) GetIP() string {