mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-01 15:07:17 +08:00
缓存lb endpoint
This commit is contained in:
@@ -20,7 +20,7 @@ func init() {
|
||||
}
|
||||
|
||||
func (self *NetworkDeleteTask) taskFailed(ctx context.Context, network *models.SNetwork, err error) {
|
||||
log.Errorf("network create task fail: %s", err)
|
||||
log.Errorf("network delete task fail: %s", err)
|
||||
network.SetStatus(self.UserCred, models.NETWORK_STATUS_DELETE_FAILED, err.Error())
|
||||
db.OpsLog.LogEvent(network, db.ACT_ALLOCATE_FAIL, err.Error(), self.UserCred)
|
||||
self.SetStageFailed(ctx, err.Error())
|
||||
|
||||
+19
-14
@@ -30,6 +30,8 @@ type SRegion struct {
|
||||
|
||||
ivpcs []cloudprovider.ICloudVpc
|
||||
|
||||
lbEndpints map[string]string
|
||||
|
||||
storageCache *SStoragecache
|
||||
|
||||
instanceTypes []SInstanceType
|
||||
@@ -102,14 +104,24 @@ type LBRegion struct {
|
||||
RegionId string
|
||||
}
|
||||
|
||||
func (self *SRegion) getLBRegions(client *sdk.Client) ([]LBRegion, error) {
|
||||
func (self *SRegion) fetchLBRegions(client *sdk.Client) error {
|
||||
if len(self.lbEndpints) > 0 {
|
||||
return nil
|
||||
}
|
||||
params := map[string]string{}
|
||||
result, err := self._lbRequest(client, "DescribeRegions", "slb.aliyuncs.com", params)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return err
|
||||
}
|
||||
self.lbEndpints = map[string]string{}
|
||||
regions := []LBRegion{}
|
||||
return regions, result.Unmarshal(®ions, "Regions", "Region")
|
||||
if err := result.Unmarshal(®ions, "Regions", "Region"); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, region := range regions {
|
||||
self.lbEndpints[region.RegionId] = region.RegionEndpoint
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (self *SRegion) lbRequest(apiName string, params map[string]string) (jsonutils.JSONObject, error) {
|
||||
@@ -120,21 +132,14 @@ func (self *SRegion) lbRequest(apiName string, params map[string]string) (jsonut
|
||||
domain := "slb.aliyuncs.com"
|
||||
if !utils.IsInStringArray(apiName, []string{"DescribeRegions", "DescribeZones"}) {
|
||||
if regionId, ok := params["RegionId"]; ok {
|
||||
regions, err := self.getLBRegions(client)
|
||||
if err != nil {
|
||||
if err := self.fetchLBRegions(client); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
find := false
|
||||
for _, region := range regions {
|
||||
if regionId == region.RegionId {
|
||||
domain = region.RegionEndpoint
|
||||
find = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !find {
|
||||
endpoint, ok := self.lbEndpints[regionId]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("failed to find endpoint for lb region %s", regionId)
|
||||
}
|
||||
domain = endpoint
|
||||
}
|
||||
}
|
||||
return self._lbRequest(client, apiName, domain, params)
|
||||
|
||||
Reference in New Issue
Block a user