From c5343a111b3bcd2201c4f895e835eeca459432ce Mon Sep 17 00:00:00 2001 From: Qiu Jian Date: Tue, 22 Jan 2019 02:46:35 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=EF=BC=9A=E7=94=B1?= =?UTF-8?q?=E4=BA=8E=E9=98=BF=E9=87=8C=E4=BA=91=E7=BD=91=E7=BB=9C=E4=B8=8E?= =?UTF-8?q?SNAT=E5=85=B3=E8=81=94=EF=BC=8C=E5=AF=BC=E8=87=B4=E6=97=A0?= =?UTF-8?q?=E6=B3=95=E5=88=A0=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/aliyuncli/main.go | 3 +- pkg/compute/models/networks.go | 1 + pkg/mcclient/modules/mod_networks.go | 2 +- pkg/util/aliyun/aliyun.go | 6 +- pkg/util/aliyun/natgateway.go | 166 +++++++++++++++++++++++++++ pkg/util/aliyun/provider/provider.go | 2 +- pkg/util/aliyun/region.go | 41 ++----- pkg/util/aliyun/routetable.go | 25 +++- pkg/util/aliyun/shell/natgateway.go | 36 ++++++ pkg/util/aliyun/shell/vswitch.go | 20 ++++ pkg/util/aliyun/vpc.go | 27 +++-- pkg/util/aliyun/vswitch.go | 134 ++++++++++++++++----- 12 files changed, 392 insertions(+), 71 deletions(-) create mode 100644 pkg/util/aliyun/natgateway.go create mode 100644 pkg/util/aliyun/shell/natgateway.go diff --git a/cmd/aliyuncli/main.go b/cmd/aliyuncli/main.go index f0252647de..d896e1610b 100644 --- a/cmd/aliyuncli/main.go +++ b/cmd/aliyuncli/main.go @@ -13,6 +13,7 @@ import ( ) type BaseOptions struct { + Debug bool `help:"debug mode"` Help bool `help:"Show help"` AccessKey string `help:"Access key" default:"$ALIYUN_ACCESS_KEY"` Secret string `help:"Secret" default:"$ALIYUN_SECRET"` @@ -69,7 +70,7 @@ func newClient(options *BaseOptions) (*aliyun.SRegion, error) { return nil, fmt.Errorf("Missing secret") } - cli, err := aliyun.NewAliyunClient("", "", options.AccessKey, options.Secret) + cli, err := aliyun.NewAliyunClient("", "", options.AccessKey, options.Secret, options.Debug) if err != nil { return nil, err } diff --git a/pkg/compute/models/networks.go b/pkg/compute/models/networks.go index de478e5cb1..9ce743936d 100644 --- a/pkg/compute/models/networks.go +++ b/pkg/compute/models/networks.go @@ -831,6 +831,7 @@ func (self *SNetwork) getMoreDetails(extra *jsonutils.JSONDict) *jsonutils.JSOND extra.Add(jsonutils.NewInt(int64(self.GetTotalNicCount())), "ports_used") extra.Add(jsonutils.NewInt(int64(self.GetGuestnicsCount())), "vnics") extra.Add(jsonutils.NewInt(int64(self.GetBaremetalNicsCount())), "bm_vnics") + extra.Add(jsonutils.NewInt(int64(self.GetLoadbalancerIpsCount())), "lb_vnics") extra.Add(jsonutils.NewInt(int64(self.GetGroupNicsCount())), "group_vnics") extra.Add(jsonutils.NewInt(int64(self.GetReservedNicsCount())), "reserve_vnics") diff --git a/pkg/mcclient/modules/mod_networks.go b/pkg/mcclient/modules/mod_networks.go index d80674e00d..935bae88b4 100644 --- a/pkg/mcclient/modules/mod_networks.go +++ b/pkg/mcclient/modules/mod_networks.go @@ -10,7 +10,7 @@ func init() { "Guest_ip_end", "Guest_ip_mask", "wire_id", "wire", "is_public", "exit", "Ports", "vnics", "guest_gateway", - "group_vnics", "bm_vnics", "reserve_vnics", "server_type", + "group_vnics", "bm_vnics", "reserve_vnics", "lb_vnics", "server_type", "Status"}, []string{}) diff --git a/pkg/util/aliyun/aliyun.go b/pkg/util/aliyun/aliyun.go index becd3bc054..6407a03530 100644 --- a/pkg/util/aliyun/aliyun.go +++ b/pkg/util/aliyun/aliyun.go @@ -37,10 +37,12 @@ type SAliyunClient struct { accessKey string secret string iregions []cloudprovider.ICloudRegion + + Debug bool } -func NewAliyunClient(providerId string, providerName string, accessKey string, secret string) (*SAliyunClient, error) { - client := SAliyunClient{providerId: providerId, providerName: providerName, accessKey: accessKey, secret: secret} +func NewAliyunClient(providerId string, providerName string, accessKey string, secret string, isDebug bool) (*SAliyunClient, error) { + client := SAliyunClient{providerId: providerId, providerName: providerName, accessKey: accessKey, secret: secret, Debug: isDebug} err := client.fetchRegions() if err != nil { return nil, err diff --git a/pkg/util/aliyun/natgateway.go b/pkg/util/aliyun/natgateway.go new file mode 100644 index 0000000000..48ba8d9799 --- /dev/null +++ b/pkg/util/aliyun/natgateway.go @@ -0,0 +1,166 @@ +package aliyun + +import ( + "fmt" + + "time" + "yunion.io/x/log" +) + +type SBandwidthPackageIds struct { + BandwidthPackageId []string +} + +type SForwardTableIds struct { + ForwardTableId []string +} + +type SSnatTableIds struct { + SnatTableId []string +} + +type SNatGetway struct { + vpc *SVpc + + BandwidthPackageIds SBandwidthPackageIds + BusinessStatus string + CreationTime time.Time + Description string + ForwardTableIds SForwardTableIds + SnatTableIds SSnatTableIds + InstanceChargeType string + Name string + NatGatewayId string + RegionId string + Spec string + Status string + VpcId string +} + +func (self *SRegion) GetNatGateways(vpcId string, natGwId string, offset, limit int) ([]SNatGetway, int, error) { + if limit > 50 || limit <= 0 { + limit = 50 + } + params := make(map[string]string) + params["RegionId"] = self.RegionId + params["PageSize"] = fmt.Sprintf("%d", limit) + params["PageNumber"] = fmt.Sprintf("%d", (offset/limit)+1) + if len(vpcId) > 0 { + params["VpcId"] = vpcId + } + if len(natGwId) > 0 { + params["NatGatewayId"] = natGwId + } + + body, err := self.vpcRequest("DescribeNatGateways", params) + if err != nil { + log.Errorf("GetVSwitches fail %s", err) + return nil, 0, err + } + + if self.client.Debug { + log.Debugf("%s", body.PrettyString()) + } + + gateways := make([]SNatGetway, 0) + err = body.Unmarshal(&gateways, "NatGateways", "NatGateway") + if err != nil { + log.Errorf("Unmarshal gateways fail %s", err) + return nil, 0, err + } + total, _ := body.Int("TotalCount") + return gateways, int(total), nil +} + +type SSNATTableEntry struct { + SnatEntryId string + SnatIp string + SnatTableId string `json:"snat_table_id"` + SourceCIDR string `json:"source_cidr"` + SourceVSwitchId string `json:"source_vswitch_id"` + Status string +} + +func (self *SRegion) GetSNATEntries(tableId string, offset, limit int) ([]SSNATTableEntry, int, error) { + if limit > 50 || limit <= 0 { + limit = 50 + } + params := make(map[string]string) + params["RegionId"] = self.RegionId + params["PageSize"] = fmt.Sprintf("%d", limit) + params["PageNumber"] = fmt.Sprintf("%d", (offset/limit)+1) + params["SnatTableId"] = tableId + + body, err := self.vpcRequest("DescribeSnatTableEntries", params) + if err != nil { + log.Errorf("DescribeSnatTableEntries fail %s", err) + return nil, 0, err + } + + if self.client.Debug { + log.Debugf("%s", body.PrettyString()) + } + + entries := make([]SSNATTableEntry, 0) + err = body.Unmarshal(&entries, "SnatTableEntries", "SnatTableEntry") + if err != nil { + log.Errorf("Unmarshal entries fail %s", err) + return nil, 0, err + } + total, _ := body.Int("TotalCount") + return entries, int(total), nil +} + +func (region *SRegion) DeleteSnatEntry(tableId string, entryId string) error { + params := make(map[string]string) + params["RegionId"] = region.RegionId + params["SnatTableId"] = tableId + params["SnatEntryId"] = entryId + _, err := region.vpcRequest("DeleteSnatEntry", params) + return err +} + +func (nat *SNatGetway) getSnatEntriesForTable(tblId string) ([]SSNATTableEntry, error) { + entries := make([]SSNATTableEntry, 0) + entryTotal := -1 + for entryTotal < 0 || len(entries) < entryTotal { + parts, total, err := nat.vpc.region.GetSNATEntries(tblId, len(entries), 50) + if err != nil { + return nil, err + } + if len(parts) > 0 { + entries = append(entries, parts...) + } + entryTotal = total + } + return entries, nil +} + +func (nat *SNatGetway) getSnatEntries() ([]SSNATTableEntry, error) { + entries := make([]SSNATTableEntry, 0) + for i := range nat.SnatTableIds.SnatTableId { + sentries, err := nat.getSnatEntriesForTable(nat.SnatTableIds.SnatTableId[i]) + if err != nil { + return nil, err + } + entries = append(entries, sentries...) + } + return entries, nil +} + +func (nat *SNatGetway) dissociateWithVswitch(vswitchId string) error { + entries, err := nat.getSnatEntries() + if err != nil { + return err + } + for i := range entries { + log.Debugf("%s", entries[i]) + if entries[i].SourceVSwitchId == vswitchId { + err := nat.vpc.region.DeleteSnatEntry(entries[i].SnatTableId, entries[i].SnatEntryId) + if err != nil { + return err + } + } + } + return nil +} diff --git a/pkg/util/aliyun/provider/provider.go b/pkg/util/aliyun/provider/provider.go index d62276a9c3..e88aaf6088 100644 --- a/pkg/util/aliyun/provider/provider.go +++ b/pkg/util/aliyun/provider/provider.go @@ -72,7 +72,7 @@ func (self *SAliyunProviderFactory) GetProvider(providerId, providerName, url, a return self.providerTable[providerId], nil */ - client, err := aliyun.NewAliyunClient(providerId, providerName, account, secret) + client, err := aliyun.NewAliyunClient(providerId, providerName, account, secret, false) if err != nil { return nil, err } diff --git a/pkg/util/aliyun/region.go b/pkg/util/aliyun/region.go index e8956fc397..e54eae5245 100644 --- a/pkg/util/aliyun/region.go +++ b/pkg/util/aliyun/region.go @@ -21,6 +21,8 @@ type SRegion struct { sdkClient *sdk.Client ossClient *oss.Client + Debug bool + RegionId string LocalName string @@ -87,6 +89,14 @@ func (self *SRegion) ecsRequest(apiName string, params map[string]string) (jsonu return jsonRequest(client, "ecs.aliyuncs.com", ALIYUN_API_VERSION, apiName, params) } +func (self *SRegion) vpcRequest(action string, params map[string]string) (jsonutils.JSONObject, error) { + client, err := self.getSdkClient() + if err != nil { + return nil, err + } + return jsonRequest(client, "vpc.aliyuncs.com", ALIYUN_API_VERSION_VPC, action, params) +} + func (self *SRegion) lbRequest(apiName string, params map[string]string) (jsonutils.JSONObject, error) { client, err := self.getSdkClient() if err != nil { @@ -365,37 +375,6 @@ func (self *SRegion) GetRouteTables(ids []string, offset int, limit int) ([]SRou return routetables, int(total), nil } -func (self *SRegion) GetVSwitches(ids []string, vpcId string, offset int, limit int) ([]SVSwitch, int, error) { - if limit > 50 || limit <= 0 { - limit = 50 - } - params := make(map[string]string) - params["RegionId"] = self.RegionId - params["PageSize"] = fmt.Sprintf("%d", limit) - params["PageNumber"] = fmt.Sprintf("%d", (offset/limit)+1) - if ids != nil && len(ids) > 0 { - params["VSwitchId"] = strings.Join(ids, ",") - } - if len(vpcId) > 0 { - params["VpcId"] = vpcId - } - - body, err := self.ecsRequest("DescribeVSwitches", params) - if err != nil { - log.Errorf("GetVSwitches fail %s", err) - return nil, 0, err - } - - switches := make([]SVSwitch, 0) - err = body.Unmarshal(&switches, "VSwitches", "VSwitch") - if err != nil { - log.Errorf("Unmarshal vswitches fail %s", err) - return nil, 0, err - } - total, _ := body.Int("TotalCount") - return switches, int(total), nil -} - func (self *SRegion) GetMatchInstanceTypes(cpu int, memMB int, gpu int, zoneId string) ([]SInstanceType, error) { if self.instanceTypes == nil { types, err := self.GetInstanceTypes() diff --git a/pkg/util/aliyun/routetable.go b/pkg/util/aliyun/routetable.go index 6f37698cb2..130ce85bb3 100644 --- a/pkg/util/aliyun/routetable.go +++ b/pkg/util/aliyun/routetable.go @@ -7,6 +7,7 @@ import ( "yunion.io/x/jsonutils" "yunion.io/x/log" + "strings" "yunion.io/x/onecloud/pkg/cloudprovider" ) @@ -195,7 +196,7 @@ func (self *SVpc) RemoteGetRouteTableList(offset int, limit int) ([]*SRouteTable params["PageSize"] = fmt.Sprintf("%d", limit) params["PageNumber"] = fmt.Sprintf("%d", (offset/limit)+1) - body, err := self.apiRequest("DescribeRouteTableList", params) + body, err := self.region.vpcRequest("DescribeRouteTableList", params) if err != nil { log.Errorf("RemoteGetRouteTableList fail %s", err) return nil, 0, err @@ -213,3 +214,25 @@ func (self *SVpc) RemoteGetRouteTableList(offset int, limit int) ([]*SRouteTable total, _ := body.Int("TotalCount") return routeTables, int(total), nil } + +func (region *SRegion) AssociateRouteTable(rtableId string, vswitchId string) error { + params := make(map[string]string) + params["RegionId"] = region.RegionId + params["RouteTableId"] = rtableId + params["VSwitchId"] = vswitchId + _, err := region.vpcRequest("AssociateRouteTable", params) + return err +} + +func (region *SRegion) UnassociateRouteTable(rtableId string, vswitchId string) error { + params := make(map[string]string) + params["RegionId"] = region.RegionId + params["RouteTableId"] = rtableId + params["VSwitchId"] = vswitchId + _, err := region.vpcRequest("UnassociateRouteTable", params) + return err +} + +func (routeTable *SRouteTable) IsSystem() bool { + return strings.ToLower(routeTable.RouteTableType) == "system" +} diff --git a/pkg/util/aliyun/shell/natgateway.go b/pkg/util/aliyun/shell/natgateway.go new file mode 100644 index 0000000000..12f50553ad --- /dev/null +++ b/pkg/util/aliyun/shell/natgateway.go @@ -0,0 +1,36 @@ +package shell + +import ( + "yunion.io/x/onecloud/pkg/util/aliyun" + "yunion.io/x/onecloud/pkg/util/shellutils" +) + +func init() { + type NatGatewayListOptions struct { + Limit int `help:"page size"` + Offset int `help:"page offset"` + } + shellutils.R(&NatGatewayListOptions{}, "natgateway-list", "List NAT gateways", func(cli *aliyun.SRegion, args *NatGatewayListOptions) error { + gws, total, e := cli.GetNatGateways("", "", args.Offset, args.Limit) + if e != nil { + return e + } + printList(gws, total, args.Offset, args.Limit, []string{}) + return nil + }) + + type SNatEntryListOptions struct { + ID string `help:"SNat Table ID"` + Limit int `help:"page size"` + Offset int `help:"page offset"` + } + shellutils.R(&SNatEntryListOptions{}, "snat-entry-list", "List SNAT entries", func(cli *aliyun.SRegion, args *SNatEntryListOptions) error { + entries, total, e := cli.GetSNATEntries(args.ID, args.Offset, args.Limit) + if e != nil { + return e + } + printList(entries, total, args.Offset, args.Limit, []string{}) + return nil + }) + +} diff --git a/pkg/util/aliyun/shell/vswitch.go b/pkg/util/aliyun/shell/vswitch.go index 568e14e663..b9a0733e42 100644 --- a/pkg/util/aliyun/shell/vswitch.go +++ b/pkg/util/aliyun/shell/vswitch.go @@ -18,4 +18,24 @@ func init() { printList(vswitches, total, args.Offset, args.Limit, []string{}) return nil }) + + type VSwitchShowOptions struct { + ID string `help:"show vswitch details"` + } + shellutils.R(&VSwitchShowOptions{}, "vswitch-show", "Show vswitch details", func(cli *aliyun.SRegion, args *VSwitchShowOptions) error { + vswitch, e := cli.GetVSwitchAttributes(args.ID) + if e != nil { + return e + } + printObject(vswitch) + return nil + }) + + shellutils.R(&VSwitchShowOptions{}, "vswitch-delete", "Show vswitch details", func(cli *aliyun.SRegion, args *VSwitchShowOptions) error { + e := cli.DeleteVSwitch(args.ID) + if e != nil { + return e + } + return nil + }) } diff --git a/pkg/util/aliyun/vpc.go b/pkg/util/aliyun/vpc.go index d8d70350ca..9615f817fe 100644 --- a/pkg/util/aliyun/vpc.go +++ b/pkg/util/aliyun/vpc.go @@ -46,14 +46,6 @@ type SVpc struct { VpcName string } -func (self *SVpc) apiRequest(action string, params map[string]string) (jsonutils.JSONObject, error) { - client, err := self.region.getSdkClient() - if err != nil { - return nil, err - } - return jsonRequest(client, "vpc.aliyuncs.com", ALIYUN_API_VERSION_VPC, action, params) -} - func (self *SVpc) GetMetadata() *jsonutils.JSONDict { return nil } @@ -242,3 +234,22 @@ func (self *SVpc) Delete() error { } return self.region.DeleteVpc(self.VpcId) } + +func (self *SVpc) getNatGateways() ([]SNatGetway, error) { + natgatways := make([]SNatGetway, 0) + gwTotal := -1 + for gwTotal < 0 || len(natgatways) < gwTotal { + parts, total, err := self.region.GetNatGateways(self.VpcId, "", len(natgatways), 50) + if err != nil { + return nil, err + } + if len(parts) > 0 { + natgatways = append(natgatways, parts...) + } + gwTotal = total + } + for i := 0; i < len(natgatways); i += 1 { + natgatways[i].vpc = self + } + return natgatways, nil +} diff --git a/pkg/util/aliyun/vswitch.go b/pkg/util/aliyun/vswitch.go index 944f9323b0..2836c7ba7f 100644 --- a/pkg/util/aliyun/vswitch.go +++ b/pkg/util/aliyun/vswitch.go @@ -9,6 +9,7 @@ import ( "yunion.io/x/pkg/util/netutils" "yunion.io/x/pkg/utils" + "fmt" "yunion.io/x/onecloud/pkg/cloudprovider" "yunion.io/x/onecloud/pkg/compute/models" ) @@ -20,19 +21,29 @@ const ( VSwitchAvailable = "Available" ) +type SCloudResources struct { + CloudResourceSetType []string +} + type SVSwitch struct { wire *SWire AvailableIpAddressCount int - CidrBlock string - CreationTime time.Time - Description string - IsDefault bool - Status string - VSwitchId string - VSwitchName string - VpcId string - ZoneId string + + CidrBlock string + Ipv6CidrBlock string + CreationTime time.Time + Description string + IsDefault bool + Status string + VSwitchId string + VSwitchName string + VpcId string + ZoneId string + + CloudResources SCloudResources + ResourceGroupId string + RouteTable SRouteTable } func (self *SVSwitch) GetMetadata() *jsonutils.JSONDict { @@ -64,7 +75,7 @@ func (self *SVSwitch) GetStatus() string { func (self *SVSwitch) Refresh() error { log.Debugf("vsiwtch refresh %s", self.VSwitchId) - new, err := self.wire.zone.region.getVSwitch(self.VSwitchId) + new, err := self.wire.zone.region.GetVSwitchAttributes(self.VSwitchId) if err != nil { return err } @@ -123,37 +134,108 @@ func (self *SRegion) createVSwitch(zoneId string, vpcId string, name string, cid } params["ClientToken"] = utils.GenRequestId(20) - body, err := self.ecsRequest("CreateVSwitch", params) + body, err := self.vpcRequest("CreateVSwitch", params) if err != nil { return "", err } return body.GetString("VSwitchId") } -func (self *SRegion) getVSwitch(vswitchId string) (*SVSwitch, error) { - vswitches, total, err := self.GetVSwitches([]string{vswitchId}, "", 0, 1) - log.Debugf("getVSwitch %d %d %s %s", len(vswitches), total, err, vswitchId) - if err != nil { - return nil, err - } - if total != 1 { - return nil, cloudprovider.ErrNotFound - } - return &vswitches[0], nil -} - -func (self *SRegion) deleteVSwitch(vswitchId string) error { +func (self *SRegion) DeleteVSwitch(vswitchId string) error { params := make(map[string]string) params["VSwitchId"] = vswitchId - _, err := self.ecsRequest("DeleteVSwitch", params) + _, err := self.vpcRequest("DeleteVSwitch", params) return err } func (self *SVSwitch) Delete() error { - return self.wire.zone.region.deleteVSwitch(self.VSwitchId) + err := self.Refresh() + if err != nil { + log.Errorf("refresh vswitch fail %s", err) + return err + } + if len(self.RouteTable.RouteTableId) > 0 && !self.RouteTable.IsSystem() { + err = self.wire.zone.region.UnassociateRouteTable(self.RouteTable.RouteTableId, self.VSwitchId) + if err != nil { + log.Errorf("unassociate routetable fail %s", err) + return err + } + } + err = self.dissociateWithSNAT() + if err != nil { + log.Errorf("fail to dissociateWithSNAT") + return err + } + return self.wire.zone.region.DeleteVSwitch(self.VSwitchId) } func (self *SVSwitch) GetAllocTimeoutSeconds() int { return 120 // 2 minutes } + +func (self *SRegion) GetVSwitches(ids []string, vpcId string, offset int, limit int) ([]SVSwitch, int, error) { + if limit > 50 || limit <= 0 { + limit = 50 + } + params := make(map[string]string) + params["RegionId"] = self.RegionId + params["PageSize"] = fmt.Sprintf("%d", limit) + params["PageNumber"] = fmt.Sprintf("%d", (offset/limit)+1) + if ids != nil && len(ids) > 0 { + params["VSwitchId"] = strings.Join(ids, ",") + } + if len(vpcId) > 0 { + params["VpcId"] = vpcId + } + + body, err := self.vpcRequest("DescribeVSwitches", params) + if err != nil { + log.Errorf("GetVSwitches fail %s", err) + return nil, 0, err + } + + switches := make([]SVSwitch, 0) + err = body.Unmarshal(&switches, "VSwitches", "VSwitch") + if err != nil { + log.Errorf("Unmarshal vswitches fail %s", err) + return nil, 0, err + } + total, _ := body.Int("TotalCount") + return switches, int(total), nil +} + +func (self *SRegion) GetVSwitchAttributes(idstr string) (*SVSwitch, error) { + params := make(map[string]string) + params["VSwitchId"] = idstr + + body, err := self.vpcRequest("DescribeVSwitchAttributes", params) + if err != nil { + log.Errorf("DescribeVSwitchAttributes fail %s", err) + return nil, err + } + if self.client.Debug { + log.Debugf("%s", body.PrettyString()) + } + switches := SVSwitch{} + err = body.Unmarshal(&switches) + if err != nil { + log.Errorf("Unmarshal vswitches fail %s", err) + return nil, err + } + return &switches, nil +} + +func (vsw *SVSwitch) dissociateWithSNAT() error { + natgatways, err := vsw.wire.vpc.getNatGateways() + if err != nil { + return err + } + for i := range natgatways { + err = natgatways[i].dissociateWithVswitch(vsw.VSwitchId) + if err != nil { + return err + } + } + return nil +} From 1accd79348c667e7880a62ca7e2c86310afbbc67 Mon Sep 17 00:00:00 2001 From: Qiu Jian Date: Wed, 23 Jan 2019 14:38:14 +0800 Subject: [PATCH 2/3] minor fixes --- pkg/mcclient/modules/mod_networks.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/mcclient/modules/mod_networks.go b/pkg/mcclient/modules/mod_networks.go index 935bae88b4..443c2ee34e 100644 --- a/pkg/mcclient/modules/mod_networks.go +++ b/pkg/mcclient/modules/mod_networks.go @@ -10,8 +10,8 @@ func init() { "Guest_ip_end", "Guest_ip_mask", "wire_id", "wire", "is_public", "exit", "Ports", "vnics", "guest_gateway", - "group_vnics", "bm_vnics", "reserve_vnics", "lb_vnics", "server_type", - "Status"}, + "group_vnics", "bm_vnics", "reserve_vnics", "lb_vnics", + "server_type", "Status"}, []string{}) registerCompute(&Networks) From cd0247f1a85f67e25e0081055cf43c9722cb8724 Mon Sep 17 00:00:00 2001 From: Qiu Jian Date: Wed, 23 Jan 2019 14:51:57 +0800 Subject: [PATCH 3/3] add cmd loadbalancer-network-list --- cmd/climc/shell/loadbalancernetworks.go | 46 +++++++++++++++++++ .../modules/mod_loadbalancernetworks.go | 17 +++++++ 2 files changed, 63 insertions(+) create mode 100644 cmd/climc/shell/loadbalancernetworks.go create mode 100644 pkg/mcclient/modules/mod_loadbalancernetworks.go diff --git a/cmd/climc/shell/loadbalancernetworks.go b/cmd/climc/shell/loadbalancernetworks.go new file mode 100644 index 0000000000..c11d3cf821 --- /dev/null +++ b/cmd/climc/shell/loadbalancernetworks.go @@ -0,0 +1,46 @@ +package shell + +import ( + "yunion.io/x/jsonutils" + "yunion.io/x/onecloud/pkg/mcclient" + "yunion.io/x/onecloud/pkg/mcclient/modules" + "yunion.io/x/onecloud/pkg/mcclient/options" +) + +func init() { + type LoadbalancerNetworkListOptions struct { + options.BaseListOptions + Loadbalancer string `help:"ID or Name of Loadbalancer"` + Network string `help:"ID or Name of network"` + Ip string `help:"search the IP address"` + } + R(&LoadbalancerNetworkListOptions{}, "loadbalancer-network-list", "List loadbalancer network pairs", func(s *mcclient.ClientSession, args *LoadbalancerNetworkListOptions) error { + var params *jsonutils.JSONDict + { + var err error + params, err = args.BaseListOptions.Params() + if err != nil { + return err + + } + } + if len(args.Ip) > 0 { + params.Add(jsonutils.NewString(args.Ip), "ip_addr") + } + var result *modules.ListResult + var err error + if len(args.Loadbalancer) > 0 { + result, err = modules.Loadbalancernetworks.ListDescendent(s, args.Loadbalancer, params) + } else if len(args.Network) > 0 { + result, err = modules.Loadbalancernetworks.ListDescendent2(s, args.Network, params) + } else { + result, err = modules.Loadbalancernetworks.List(s, params) + } + if err != nil { + return err + } + printList(result, modules.Loadbalancernetworks.GetColumns(s)) + return nil + }) + +} diff --git a/pkg/mcclient/modules/mod_loadbalancernetworks.go b/pkg/mcclient/modules/mod_loadbalancernetworks.go new file mode 100644 index 0000000000..45185c7cca --- /dev/null +++ b/pkg/mcclient/modules/mod_loadbalancernetworks.go @@ -0,0 +1,17 @@ +package modules + +var ( + Loadbalancernetworks JointResourceManager +) + +func init() { + Loadbalancernetworks = NewJointComputeManager( + "loadbalancernetwork", + "loadbalancernetworks", + []string{"Loadbalancer_ID", "Loadbalancer", + "Network_ID", "Network", "Ip_Addr"}, + []string{}, + &Loadbalancers, + &Networks) + registerCompute(&Loadbalancernetworks) +}