diff --git a/pkg/apis/compute/loadbalancer.go b/pkg/apis/compute/loadbalancer.go index 406534da29..3c8dc73ebb 100644 --- a/pkg/apis/compute/loadbalancer.go +++ b/pkg/apis/compute/loadbalancer.go @@ -174,6 +174,7 @@ type LoadbalancerDetails struct { VpcResourceInfoBase ZoneResourceInfoBase + Zone1ResourceInfoBase NetworkResourceInfoBase SLoadbalancer diff --git a/pkg/apis/compute/zone.go b/pkg/apis/compute/zone.go index bc1ed0356a..47b727858b 100644 --- a/pkg/apis/compute/zone.go +++ b/pkg/apis/compute/zone.go @@ -88,13 +88,22 @@ type ZoneDetails struct { type ZoneResourceInfoBase struct { // 可用区名称 - // example: zone1 + // example: 北京一区 Zone string `json:"zone"` // 纳管云的zoneId ZoneExtId string `json:"zone_ext_id"` } +type Zone1ResourceInfoBase struct { + // 可用区名称 + // example: 北京2区 + Zone1Name string `json:"zone_1_name"` + + // 纳管云的zoneId + Zone1ExtId string `json:"zone_1_ext_id"` +} + type ZoneResourceInfo struct { ZoneResourceInfoBase diff --git a/pkg/cloudprovider/resources.go b/pkg/cloudprovider/resources.go index 43a5193612..d547846d8c 100644 --- a/pkg/cloudprovider/resources.go +++ b/pkg/cloudprovider/resources.go @@ -511,6 +511,7 @@ type ICloudLoadbalancer interface { GetNetworkIds() []string GetVpcId() string GetZoneId() string + GetZone1Id() string // first slave zone GetLoadbalancerSpec() string GetChargeType() string GetEgressMbps() int diff --git a/pkg/compute/models/loadbalancers.go b/pkg/compute/models/loadbalancers.go index 22ad7ed97d..834012671c 100644 --- a/pkg/compute/models/loadbalancers.go +++ b/pkg/compute/models/loadbalancers.go @@ -614,6 +614,7 @@ func (man *SLoadbalancerManager) FetchCustomizeColumns( regRows := man.SCloudregionResourceBaseManager.FetchCustomizeColumns(ctx, userCred, query, objs, fields, isList) vpcRows := man.SVpcResourceBaseManager.FetchCustomizeColumns(ctx, userCred, query, objs, fields, isList) zoneRows := man.SZoneResourceBaseManager.FetchCustomizeColumns(ctx, userCred, query, objs, fields, isList) + zone1Rows := man.FetchZone1ResourceInfos(ctx, userCred, query, objs) netRows := man.SNetworkResourceBaseManager.FetchCustomizeColumns(ctx, userCred, query, objs, fields, isList) for i := range rows { @@ -625,6 +626,7 @@ func (man *SLoadbalancerManager) FetchCustomizeColumns( CloudregionResourceInfo: regRows[i], VpcResourceInfoBase: vpcRows[i].VpcResourceInfoBase, ZoneResourceInfoBase: zoneRows[i].ZoneResourceInfoBase, + Zone1ResourceInfoBase: zone1Rows[i], NetworkResourceInfoBase: netRows[i].NetworkResourceInfoBase, } rows[i], _ = objs[i].(*SLoadbalancer).getMoreDetails(rows[i]) @@ -632,6 +634,37 @@ func (man *SLoadbalancerManager) FetchCustomizeColumns( return rows } + +func (lb *SLoadbalancerManager) FetchZone1ResourceInfos(ctx context.Context, + userCred mcclient.TokenCredential, + query jsonutils.JSONObject, + objs []interface{}) []api.Zone1ResourceInfoBase { + rows := make([]api.Zone1ResourceInfoBase, len(objs)) + zoneIds := []string{} + for i := range objs { + zone1 := objs[i].(*SLoadbalancer).Zone1 + if len(zone1) > 0 { + zoneIds = append(zoneIds, zone1) + } + } + + zones := make(map[string]SZone) + err := db.FetchStandaloneObjectsByIds(ZoneManager, zoneIds, &zones) + if err != nil { + log.Errorf("FetchStandaloneObjectsByIds fail %s", err) + return rows + } + + for i := range objs { + if zone, ok := zones[objs[i].(*SLoadbalancer).Zone1]; ok { + rows[i].Zone1Name = zone.GetName() + rows[i].Zone1ExtId = fetchExternalId(zone.GetExternalId()) + } + } + + return rows +} + func (lb *SLoadbalancer) GetExtraDetails( ctx context.Context, userCred mcclient.TokenCredential, @@ -867,6 +900,12 @@ func (man *SLoadbalancerManager) newFromCloudLoadbalancer(ctx context.Context, u } } + if zoneId := extLb.GetZone1Id(); len(zoneId) > 0 { + if zone, err := db.FetchByExternalId(ZoneManager, zoneId); err == nil && zone != nil { + lb.Zone1 = zone.GetId() + } + } + if extLb.GetMetadata() != nil { lb.LBInfo = extLb.GetMetadata() } @@ -1043,6 +1082,24 @@ func (lb *SLoadbalancer) SyncWithCloudLoadbalancer(ctx context.Context, userCred } } + if len(lb.ZoneId) == 0 { + extZoneId := extLb.GetZoneId() + if len(extZoneId) > 0 { + if zone, err := db.FetchByExternalId(ZoneManager, extZoneId); err == nil && zone != nil { + lb.ZoneId = zone.GetId() + } + } + } + + if len(lb.Zone1) == 0 { + extZoneId := extLb.GetZone1Id() + if len(extZoneId) > 0 { + if zone, err := db.FetchByExternalId(ZoneManager, extZoneId); err == nil && zone != nil { + lb.Zone1 = zone.GetId() + } + } + } + return nil }) diff --git a/pkg/multicloud/aliyun/loadbalancer.go b/pkg/multicloud/aliyun/loadbalancer.go index 3484ebb51d..c393e829e0 100644 --- a/pkg/multicloud/aliyun/loadbalancer.go +++ b/pkg/multicloud/aliyun/loadbalancer.go @@ -141,6 +141,10 @@ func (lb *SLoadbalancer) GetZoneId() string { return zone.GetGlobalId() } +func (self *SLoadbalancer) GetZone1Id() string { + return "" +} + func (lb *SLoadbalancer) IsEmulated() bool { return false } diff --git a/pkg/multicloud/aws/loadbalancer.go b/pkg/multicloud/aws/loadbalancer.go index 386669a085..16b6ef8126 100644 --- a/pkg/multicloud/aws/loadbalancer.go +++ b/pkg/multicloud/aws/loadbalancer.go @@ -170,7 +170,21 @@ func (self *SElb) GetZoneId() string { } sort.Strings(zones) - return zones[0] + if len(zones) > 0 { + z, err := self.region.getZoneById(zones[0]) + if err != nil { + log.Infof("getZoneById %s %s", zones[0], err) + return "" + } + + return z.GetGlobalId() + } + + return "" +} + +func (self *SElb) GetZone1Id() string { + return "" } func (self *SElb) GetLoadbalancerSpec() string { diff --git a/pkg/multicloud/huawei/loadbalancer.go b/pkg/multicloud/huawei/loadbalancer.go index 3c2bfcfbae..bb4cf03b06 100644 --- a/pkg/multicloud/huawei/loadbalancer.go +++ b/pkg/multicloud/huawei/loadbalancer.go @@ -201,12 +201,22 @@ func (self *SLoadbalancer) GetVpcId() string { func (self *SLoadbalancer) GetZoneId() string { net := self.GetNetwork() if net != nil { - return net.AvailabilityZone + z, err := self.region.getZoneById(net.AvailabilityZone) + if err != nil { + log.Infof("getZoneById %s %s", net.AvailabilityZone, err) + return "" + } + + return z.GetGlobalId() } return "" } +func (self *SLoadbalancer) GetZone1Id() string { + return "" +} + func (self *SLoadbalancer) GetLoadbalancerSpec() string { return "" } diff --git a/pkg/multicloud/openstack/loadbalbacer.go b/pkg/multicloud/openstack/loadbalbacer.go index 5b7c0f1a8f..5719ad1e66 100644 --- a/pkg/multicloud/openstack/loadbalbacer.go +++ b/pkg/multicloud/openstack/loadbalbacer.go @@ -200,6 +200,10 @@ func (lb *SLoadbalancer) GetZoneId() string { return lb.AvailabilityZone } +func (self *SLoadbalancer) GetZone1Id() string { + return "" +} + func (lb *SLoadbalancer) IsEmulated() bool { return false } diff --git a/pkg/multicloud/qcloud/loadbalancer.go b/pkg/multicloud/qcloud/loadbalancer.go index c4c09d74a9..b9048b3e4f 100644 --- a/pkg/multicloud/qcloud/loadbalancer.go +++ b/pkg/multicloud/qcloud/loadbalancer.go @@ -71,6 +71,14 @@ type SLoadbalancer struct { CreateTime time.Time `json:"CreateTime"` Isolation int64 `json:"Isolation"` // 0:表示未被隔离,1:表示被隔离。 SubnetId string `json:"SubnetId"` + BackupZoneSet []ZoneSet `json:"BackupZoneSet"` + MasterZone ZoneSet `json:"MasterZone"` +} + +type ZoneSet struct { + Zone string `json:"Zone"` + ZoneID int64 `json:"ZoneId"` + ZoneName string `json:"ZoneName"` } func (self *SLoadbalancer) GetLoadbalancerSpec() string { @@ -316,6 +324,47 @@ func (self *SLoadbalancer) GetVpcId() string { } func (self *SLoadbalancer) GetZoneId() string { + zoneId := "" + if len(self.MasterZone.Zone) > 0 { + zoneId = self.MasterZone.Zone + } else if len(self.SubnetId) > 0 { + net, err := self.region.GetNetwork(self.SubnetId) + if err != nil { + log.Warningf("GetNetwork %s %s", self.SubnetId, err) + return "" + } + + zoneId = net.Zone + } + + if len(zoneId) > 0 { + z, err := self.region.getZoneById(zoneId) + if err != nil { + log.Warningf("getZoneById %s %s", zoneId, err) + return "" + } + + return z.GetGlobalId() + } + + return "" +} + +func (self *SLoadbalancer) GetZone1Id() string { + if self.BackupZoneSet == nil { + return "" + } + + if len(self.BackupZoneSet) > 0 { + z, err := self.region.getZoneById(self.BackupZoneSet[0].Zone) + if err != nil { + log.Warningf("getZoneById %s %s", self.BackupZoneSet[0].Zone, err) + return "" + } + + return z.GetGlobalId() + } + return "" } diff --git a/pkg/multicloud/qcloud/region.go b/pkg/multicloud/qcloud/region.go index 2ae14b9293..f375aaba55 100644 --- a/pkg/multicloud/qcloud/region.go +++ b/pkg/multicloud/qcloud/region.go @@ -195,8 +195,6 @@ func (self *SRegion) CreateILoadBalancer(loadbalancer *cloudprovider.SLoadbalanc if len(loadbalancer.ZoneID) > 0 { if len(loadbalancer.SlaveZoneID) > 0 { params["MasterZoneId"] = loadbalancer.ZoneID - } else { - params["ZoneId"] = loadbalancer.ZoneID } } } diff --git a/pkg/multicloud/qcloud/shell/loadbalancer.go b/pkg/multicloud/qcloud/shell/loadbalancer.go new file mode 100644 index 0000000000..2e3510c903 --- /dev/null +++ b/pkg/multicloud/qcloud/shell/loadbalancer.go @@ -0,0 +1,34 @@ +// 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 LbListOptions struct { + } + shellutils.R(&LbListOptions{}, "lb-list", "List loadbalancers", func(cli *qcloud.SRegion, args *LbListOptions) error { + lbs, err := cli.GetILoadBalancers() + if err != nil { + return err + } + + printList(lbs, 0, 0, 0, []string{}) + return nil + }) +}