mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-24 16:03:43 +08:00
获取aliyun region经纬度
This commit is contained in:
+18
-17
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user