From ae5dbb19c66896bfab592c60ea44dc90e54c92d1 Mon Sep 17 00:00:00 2001 From: ioito Date: Wed, 22 Dec 2021 15:09:09 +0800 Subject: [PATCH] fix(region): add vpc topology --- cmd/climc/shell/compute/vpcs.go | 1 + pkg/apis/compute/vpc.go | 28 ++++++++++++ pkg/compute/models/purge.go | 5 ++- pkg/compute/models/vpcs.go | 76 ++++++++++++++++++++++++++++----- 4 files changed, 99 insertions(+), 11 deletions(-) diff --git a/cmd/climc/shell/compute/vpcs.go b/cmd/climc/shell/compute/vpcs.go index ad65eae99c..6d4a1882c3 100644 --- a/cmd/climc/shell/compute/vpcs.go +++ b/cmd/climc/shell/compute/vpcs.go @@ -36,6 +36,7 @@ func init() { cmd.Perform("public", &options.BasePublicOptions{}) cmd.Perform("change-owner", &options.VpcChangeOwnerOptions{}) cmd.Get("vpc-change-owner-candidate-domains", &options.VpcIdOptions{}) + cmd.Get("topology", &options.VpcIdOptions{}) R(&options.ResourceMetadataOptions{}, "vpc-set-user-metadata", "Set metadata of a vpc", func(s *mcclient.ClientSession, opts *options.ResourceMetadataOptions) error { params, err := opts.Params() diff --git a/pkg/apis/compute/vpc.go b/pkg/apis/compute/vpc.go index ab8d4b7794..0e8b14fbaa 100644 --- a/pkg/apis/compute/vpc.go +++ b/pkg/apis/compute/vpc.go @@ -117,3 +117,31 @@ type VpcFilterListInput struct { RegionalFilterListInput ManagedResourceListInput } + +type VpcTopologyInput struct { +} + +type NetworkTopologyOutput struct { + Name string `json:"name"` + Status string `json:"status"` + GuestIpStart string `json:"guest_ip_start"` + GuestIpEnd string `json:"guest_ip_end"` + GuestIpMask int8 `json:"guest_ip_mask"` + ServerType string `json:"server_type"` + VlanId int `json:"vlan_id"` + Address []SNetworkUsedAddress `json:"address"` +} + +type WireTopologyOutput struct { + Name string `json:"name"` + Status string `json:"status"` + Bandwidth int `json:"bandwidth"` + Zone string `json:"zone"` + Networks []NetworkTopologyOutput `json:"networks"` +} + +type VpcTopologyOutput struct { + Name string `json:"name"` + Status string `json:"status"` + Wires []WireTopologyOutput `json:"wires"` +} diff --git a/pkg/compute/models/purge.go b/pkg/compute/models/purge.go index 0819c4481a..73ca340033 100644 --- a/pkg/compute/models/purge.go +++ b/pkg/compute/models/purge.go @@ -1112,7 +1112,10 @@ func (wire *SWire) purge(ctx context.Context, userCred mcclient.TokenCredential) } func (vpc *SVpc) purgeWires(ctx context.Context, userCred mcclient.TokenCredential) error { - wires := vpc.GetWires() + wires, err := vpc.GetWires() + if err != nil { + return errors.Wrapf(err, "GetWres for vpc %s", vpc.Id) + } for i := range wires { err := wires[i].purge(ctx, userCred) if err != nil { diff --git a/pkg/compute/models/vpcs.go b/pkg/compute/models/vpcs.go index 0c7a06f385..5be1278e19 100644 --- a/pkg/compute/models/vpcs.go +++ b/pkg/compute/models/vpcs.go @@ -18,6 +18,7 @@ import ( "context" "database/sql" "fmt" + "sort" "strings" "yunion.io/x/jsonutils" @@ -259,15 +260,14 @@ func (self *SVpc) GetWireCount() (int, error) { return q.CountWithError() } -func (self *SVpc) GetWires() []SWire { +func (self *SVpc) GetWires() ([]SWire, error) { wires := make([]SWire, 0) q := self.getWireQuery() err := db.FetchModelObjects(WireManager, q, &wires) if err != nil { - log.Errorf("getWires fail %s", err) - return nil + return nil, errors.Wrapf(err, "db.FetchModelObjects") } - return wires + return wires, nil } func (manager *SVpcManager) getVpcExternalIdForClassicNetwork(regionId, cloudproviderId string) string { @@ -616,7 +616,7 @@ func (manager *SVpcManager) newFromCloudVpc(ctx context.Context, userCred mcclie } func (self *SVpc) markAllNetworksUnknown(userCred mcclient.TokenCredential) error { - wires := self.GetWires() + wires, _ := self.GetWires() if wires == nil || len(wires) == 0 { return nil } @@ -1377,7 +1377,7 @@ func (vpc *SVpc) GetChangeOwnerCandidateDomainIds() []string { func (vpc *SVpc) GetChangeOwnerRequiredDomainIds() []string { requires := stringutils2.SSortedStrings{} - wires := vpc.GetWires() + wires, _ := vpc.GetWires() for i := range wires { requires = stringutils2.Append(requires, wires[i].DomainId) } @@ -1385,7 +1385,7 @@ func (vpc *SVpc) GetChangeOwnerRequiredDomainIds() []string { } func (vpc *SVpc) GetRequiredSharedDomainIds() []string { - wires := vpc.GetWires() + wires, _ := vpc.GetWires() if len(wires) == 0 { return vpc.SEnabledStatusInfrasResourceBase.GetRequiredSharedDomainIds() } @@ -1454,7 +1454,7 @@ func (vpc *SVpc) PerformPublic(ctx context.Context, userCred mcclient.TokenCrede return nil, errors.Wrap(err, "SEnabledStatusInfrasResourceBase.PerformPublic") } // perform public for all emulated wires - wires := vpc.GetWires() + wires, _ := vpc.GetWires() for i := range wires { if wires[i].IsEmulated { _, err := wires[i].PerformPublic(ctx, userCred, query, input) @@ -1492,7 +1492,7 @@ func (vpc *SVpc) PerformPrivate(ctx context.Context, userCred mcclient.TokenCred } // perform private for all emulated wires emptyNets := true - wires := vpc.GetWires() + wires, _ := vpc.GetWires() for i := range wires { if wires[i].DomainId == vpc.DomainId { nets, _ := wires[i].getNetworks(nil, rbacutils.ScopeNone) @@ -1549,7 +1549,7 @@ func (vpc *SVpc) PerformChangeOwner(ctx context.Context, userCred mcclient.Token if err != nil { return nil, errors.Wrap(err, "SEnabledStatusInfrasResourceBase.PerformChangeOwner") } - wires := vpc.GetWires() + wires, _ := vpc.GetWires() for i := range wires { if wires[i].IsEmulated { _, err := wires[i].PerformChangeOwner(ctx, userCred, query, input) @@ -1721,3 +1721,59 @@ func (self *SVpc) IsSupportAssociateEip() bool { return false } + +func (self *SVpc) GetDetailsTopology(ctx context.Context, userCred mcclient.TokenCredential, input *api.VpcTopologyInput) (*api.VpcTopologyOutput, error) { + ret := &api.VpcTopologyOutput{ + Name: self.Name, + Status: self.Status, + Wires: []api.WireTopologyOutput{}, + } + wires, err := self.GetWires() + if err != nil { + return ret, errors.Wrapf(err, "GetWires") + } + for i := range wires { + wire := api.WireTopologyOutput{ + Name: wires[i].Name, + Status: wires[i].Status, + Bandwidth: wires[i].Bandwidth, + Networks: []api.NetworkTopologyOutput{}, + } + if len(wires[i].ZoneId) > 0 { + zone, _ := wires[i].GetZone() + if zone != nil { + wire.Zone = zone.Name + } + } + networks, err := wires[i].GetNetworks(nil, rbacutils.ScopeSystem) + if err != nil { + return nil, errors.Wrapf(err, "GetNetworks") + } + for j := range networks { + network := api.NetworkTopologyOutput{ + Name: networks[j].Name, + Status: networks[j].Status, + GuestIpStart: networks[j].GuestIpStart, + GuestIpEnd: networks[j].GuestIpEnd, + GuestIpMask: networks[j].GuestIpMask, + ServerType: networks[j].ServerType, + VlanId: networks[j].VlanId, + Address: []api.SNetworkUsedAddress{}, + } + + netAddrs := make([]api.SNetworkUsedAddress, 0) + + q := networks[j].getUsedAddressQuery(userCred, rbacutils.ScopeDomain, false) + err = q.All(&netAddrs) + if err != nil { + return nil, errors.Wrapf(err, "q.All") + } + + sort.Sort(SNetworkUsedAddressList(netAddrs)) + network.Address = netAddrs + wire.Networks = append(wire.Networks, network) + } + ret.Wires = append(ret.Wires, wire) + } + return ret, nil +}