Automatic merge from release/2.1.0 -> release/2.2.0

* commit '1116fda18bf93abb172abed5ade2758b451779ec':
  获取aliyun region经纬度
This commit is contained in:
邱剑
2018-10-09 21:41:01 +08:00
2 changed files with 56 additions and 3 deletions
+1 -1
View File
@@ -103,7 +103,7 @@ func (self *SAliyunClient) businessRequest(apiName string, params map[string]str
}
func (self *SAliyunClient) fetchRegions() error {
body, err := self.jsonRequest("DescribeRegions", nil)
body, err := self.jsonRequest("DescribeRegions", map[string]string{"AcceptLanguage": "zh-CN"})
if err != nil {
log.Errorf("fetchRegions fail %s", err)
return err
+55 -2
View File
@@ -1,8 +1,11 @@
package aliyun
import (
"context"
"fmt"
"net/http"
"strings"
"time"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
@@ -13,8 +16,11 @@ import (
"yunion.io/x/onecloud/pkg/cloudprovider"
"yunion.io/x/onecloud/pkg/compute/models"
"yunion.io/x/onecloud/pkg/util/httputils"
)
const LOCATION_KEY = "AIzaSyDzERKiHXz3c3W0GFbzcINuJEk-gM47wjg"
type SRegion struct {
client *SAliyunClient
ecsClient *sdk.Client
@@ -30,6 +36,10 @@ type SRegion struct {
storageCache *SStoragecache
instanceTypes []SInstanceType
latitude float64
longitude float64
fetchLocation bool
}
func (self *SRegion) GetClient() *SAliyunClient {
@@ -103,12 +113,55 @@ func (self *SRegion) GetProvider() string {
return CLOUD_PROVIDER_ALIYUN
}
func (self *SRegion) fetchLatitudeLongtitude() {
city := self.LocalName
for key, value := range map[string]string{"": "", "(": ")"} {
if strings.Index(self.LocalName, key) > 0 {
_city := strings.Split(self.LocalName, key)[1]
city = strings.Replace(_city, value, "", -1)
}
}
if _, res, err := httputils.JSONRequest(
httputils.GetDefaultClient(),
context.Background(),
"GET", "https://maps.google.cn/maps/api/geocode/json?address="+city+"&key="+LOCATION_KEY,
http.Header{}, nil, false); err != nil {
log.Errorf("fetchLatitudeLongtitude %s error: %v", city, err)
} else if status, err := res.GetString("status"); err != nil || status != "OK" {
log.Errorf("fetchLatitudeLongtitude %s error: %v res: %s", city, err, res.PrettyString())
} else if results, err := res.GetArray("results"); err != nil {
log.Errorf("fetchLatitudeLongtitude %s error: %v res: %s", city, err, res.PrettyString())
} else if len(results) < 1 {
log.Errorf("fetchLatitudeLongtitude %s error: %v res: %s", city, err, res.PrettyString())
} else {
self.latitude, _ = results[0].Float("geometry", "location", "lat")
self.longitude, _ = results[0].Float("geometry", "location", "lng")
self.fetchLocation = true
return
}
time.Sleep(time.Second * 2)
}
func (self *SRegion) GetLatitude() float32 {
return 0.0
for i := 0; i < 3; i++ {
if !self.fetchLocation {
self.fetchLatitudeLongtitude()
} else {
break
}
}
return float32(self.latitude)
}
func (self *SRegion) GetLongitude() float32 {
return 0.0
for i := 0; i < 3; i++ {
if !self.fetchLocation {
self.fetchLatitudeLongtitude()
} else {
break
}
}
return float32(self.longitude)
}
func (self *SRegion) GetStatus() string {