From 11e20cebc5adf3b8371807f8d486ef4ee422746c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B1=88=E8=BD=A9?= Date: Tue, 9 Oct 2018 20:33:49 +0800 Subject: [PATCH] =?UTF-8?q?=E8=8E=B7=E5=8F=96aliyun=20region=E7=BB=8F?= =?UTF-8?q?=E7=BA=AC=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/util/aliyun/aliyun.go | 35 ++++++++++++----------- pkg/util/aliyun/region.go | 59 +++++++++++++++++++++++++++++++++++++-- 2 files changed, 74 insertions(+), 20 deletions(-) diff --git a/pkg/util/aliyun/aliyun.go b/pkg/util/aliyun/aliyun.go index c8a334a57c..e9e8262a59 100644 --- a/pkg/util/aliyun/aliyun.go +++ b/pkg/util/aliyun/aliyun.go @@ -4,11 +4,12 @@ import ( "github.com/aliyun/alibaba-cloud-sdk-go/sdk" "github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests" + "time" + "yunion.io/x/jsonutils" "yunion.io/x/log" "yunion.io/x/onecloud/pkg/cloudprovider" "yunion.io/x/onecloud/pkg/compute/models" - "time" ) const ( @@ -102,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 @@ -205,11 +206,11 @@ func (self *SAliyunClient) GetIStoragecacheById(id string) (cloudprovider.ICloud } type SAccountBalance struct { - AvailableAmount float64 + AvailableAmount float64 AvailableCashAmount float64 - CreditAmount float64 - MybankCreditAmount float64 - Currency string + CreditAmount float64 + MybankCreditAmount float64 + Currency string } type SCashCoupon struct { @@ -226,15 +227,15 @@ type SCashCoupon struct { } type SPrepaidCard struct { - PrepaidCardId string - PrepaidCardNo string - GrantedTime time.Time - EffectiveTime time.Time - ExpiryTime time.Time - NominalValue float64 - Balance float64 - ApplicableProducts string - ApplicableScenarios string + PrepaidCardId string + PrepaidCardNo string + GrantedTime time.Time + EffectiveTime time.Time + ExpiryTime time.Time + NominalValue float64 + Balance float64 + ApplicableProducts string + ApplicableScenarios string } func (self *SAliyunClient) QueryAccountBalance() (*SAccountBalance, error) { @@ -272,7 +273,7 @@ func (self *SAliyunClient) QueryCashCoupons() ([]SCashCoupon, error) { func (self *SAliyunClient) QueryPrepaidCards() ([]SPrepaidCard, error) { params := make(map[string]string) params["EffectiveOrNot"] = "True" - body, err := self.businessRequest("QueryPrepaidCards", params) + body, err := self.businessRequest("QueryPrepaidCards", params) if err != nil { log.Errorf("QueryPrepaidCards fail %s", err) return nil, err @@ -284,4 +285,4 @@ func (self *SAliyunClient) QueryPrepaidCards() ([]SPrepaidCard, error) { return nil, err } return cards, nil -} \ No newline at end of file +} diff --git a/pkg/util/aliyun/region.go b/pkg/util/aliyun/region.go index 8dfa8f89db..a355c11338 100644 --- a/pkg/util/aliyun/region.go +++ b/pkg/util/aliyun/region.go @@ -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 { @@ -603,4 +656,4 @@ func (self *SRegion) GetIEipById(eipId string) (cloudprovider.ICloudEIP, error) return nil, cloudprovider.ErrDuplicateId } return &eips[0], nil -} \ No newline at end of file +}