mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-24 16:03:43 +08:00
fix(region): support volc balance and gcp api retry (#18947)
This commit is contained in:
@@ -88,7 +88,7 @@ require (
|
||||
k8s.io/client-go v0.19.3
|
||||
k8s.io/cluster-bootstrap v0.19.3
|
||||
moul.io/http2curl/v2 v2.3.0
|
||||
yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20231207080936-b8afa7e45b2e
|
||||
yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20231208091219-2d00e5a8c2dd
|
||||
yunion.io/x/executor v0.0.0-20230705125604-c5ac3141db32
|
||||
yunion.io/x/jsonutils v1.0.1-0.20230613121553-0f3b41e2ef19
|
||||
yunion.io/x/log v1.0.1-0.20230411060016-feb3f46ab361
|
||||
|
||||
@@ -1201,8 +1201,8 @@ sigs.k8s.io/structured-merge-diff/v4 v4.0.1/go.mod h1:bJZC9H9iH24zzfZ/41RGcq60oK
|
||||
sigs.k8s.io/yaml v1.1.0/go.mod h1:UJmg0vDUVViEyp3mgSv9WPwZCDxu4rQW1olrI1uml+o=
|
||||
sigs.k8s.io/yaml v1.2.0 h1:kr/MCeFWJWTwyaHoR9c8EjH9OumOmoF9YGiZd7lFm/Q=
|
||||
sigs.k8s.io/yaml v1.2.0/go.mod h1:yfXDCHCao9+ENCvLSE62v9VSji2MKu5jeNfTrofGhJc=
|
||||
yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20231207080936-b8afa7e45b2e h1:bhjZGbs9E3cQCgyllO0lGhJl/EmppnxU525Xr0eh70U=
|
||||
yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20231207080936-b8afa7e45b2e/go.mod h1:aj1gR9PPb6eqqKOwvANe26CoZFY8ydmXy0fuvgKYXH0=
|
||||
yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20231208091219-2d00e5a8c2dd h1:PagO8T+39RGpJ7SKoVo0hndHsuSC1sDQSnsSm/t/Hzc=
|
||||
yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20231208091219-2d00e5a8c2dd/go.mod h1:aj1gR9PPb6eqqKOwvANe26CoZFY8ydmXy0fuvgKYXH0=
|
||||
yunion.io/x/executor v0.0.0-20230705125604-c5ac3141db32 h1:v7POYkQwo1XzOxBoIoRVr/k0V9Y5JyjpshlIFa9raug=
|
||||
yunion.io/x/executor v0.0.0-20230705125604-c5ac3141db32/go.mod h1:Uxuou9WQIeJXNpy7t2fPLL0BYLvLiMvGQwY7Qc6aSws=
|
||||
yunion.io/x/jsonutils v0.0.0-20190625054549-a964e1e8a051/go.mod h1:4N0/RVzsYL3kH3WE/H1BjUQdFiWu50JGCFQuuy+Z634=
|
||||
|
||||
Vendored
+1
-1
@@ -1465,7 +1465,7 @@ sigs.k8s.io/structured-merge-diff/v4/value
|
||||
# sigs.k8s.io/yaml v1.2.0
|
||||
## explicit; go 1.12
|
||||
sigs.k8s.io/yaml
|
||||
# yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20231207080936-b8afa7e45b2e
|
||||
# yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20231208091219-2d00e5a8c2dd
|
||||
## explicit; go 1.18
|
||||
yunion.io/x/cloudmux/pkg/apis
|
||||
yunion.io/x/cloudmux/pkg/apis/billing
|
||||
|
||||
+3
-1
@@ -33,6 +33,7 @@ import (
|
||||
"yunion.io/x/log"
|
||||
"yunion.io/x/pkg/errors"
|
||||
"yunion.io/x/pkg/util/httputils"
|
||||
"yunion.io/x/pkg/util/stringutils"
|
||||
|
||||
api "yunion.io/x/cloudmux/pkg/apis/compute"
|
||||
"yunion.io/x/cloudmux/pkg/cloudprovider"
|
||||
@@ -518,7 +519,8 @@ func (self *SGoogleClient) ecsInsert(resource string, body jsonutils.JSONObject,
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "checkAndSetName")
|
||||
}
|
||||
resp, err := jsonRequest(self.client, "POST", GOOGLE_COMPUTE_DOMAIN, GOOGLE_API_VERSION, resource, nil, body, self.debug)
|
||||
params := map[string]string{"requestId": stringutils.UUID4()}
|
||||
resp, err := jsonRequest(self.client, "POST", GOOGLE_COMPUTE_DOMAIN, GOOGLE_API_VERSION, resource, params, body, self.debug)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
+15
-6
@@ -144,12 +144,21 @@ func (self *SVolcEngineProvider) GetSysInfo() (jsonutils.JSONObject, error) {
|
||||
}
|
||||
|
||||
func (self *SVolcEngineProvider) GetBalance() (*cloudprovider.SBalanceInfo, error) {
|
||||
// GetBalance is not currently open
|
||||
return &cloudprovider.SBalanceInfo{
|
||||
Amount: 0.0,
|
||||
Currency: "CNY",
|
||||
Status: api.CLOUD_PROVIDER_HEALTH_NORMAL,
|
||||
}, nil
|
||||
ret := &cloudprovider.SBalanceInfo{Currency: "CNY", Status: api.CLOUD_PROVIDER_HEALTH_UNKNOWN}
|
||||
balance, err := self.client.QueryBalance()
|
||||
if err != nil {
|
||||
return ret, err
|
||||
}
|
||||
|
||||
ret.Status = api.CLOUD_PROVIDER_HEALTH_NORMAL
|
||||
ret.Amount = balance.AvailableBalance
|
||||
|
||||
if ret.Amount < 0 {
|
||||
ret.Status = api.CLOUD_PROVIDER_HEALTH_ARREARS
|
||||
} else if ret.Amount < 100 {
|
||||
ret.Status = api.CLOUD_PROVIDER_HEALTH_INSUFFICIENT
|
||||
}
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
func (self *SVolcEngineProvider) GetBucketCannedAcls(regionId string) []string {
|
||||
|
||||
+43
-9
@@ -17,6 +17,7 @@ package volcengine
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
@@ -43,10 +44,12 @@ const (
|
||||
VOLCENGINE_API_VERSION = "2020-04-01"
|
||||
VOLCENGINE_IAM_API_VERSION = "2021-08-01"
|
||||
VOLCENGINE_OBSERVE_API_VERSION = "2018-01-01"
|
||||
VOLCENGINE_BILLING_API_VERSION = "2022-01-01"
|
||||
|
||||
VOLCENGINE_API = "open.volcengineapi.com"
|
||||
VOLCENGINE_IAM_API = "iam.volcengineapi.com"
|
||||
VOLCENGINE_TOS_API = "tos-cn-beijing.volces.com"
|
||||
VOLCENGINE_API = "open.volcengineapi.com"
|
||||
VOLCENGINE_IAM_API = "iam.volcengineapi.com"
|
||||
VOLCENGINE_TOS_API = "tos-cn-beijing.volces.com"
|
||||
VOLCENGINE_BILLING_API = "billing.volcengineapi.com"
|
||||
|
||||
VOLCENGINE_SERVICE_ECS = "ecs"
|
||||
VOLCENGINE_SERVICE_VPC = "vpc"
|
||||
@@ -55,7 +58,9 @@ const (
|
||||
VOLCENGINE_SERVICE_IAM = "iam"
|
||||
VOLCENGINE_SERVICE_TOS = "tos"
|
||||
VOLCENGINE_SERVICE_OBSERVICE = "Volc_Observe"
|
||||
VOLCENGINE_DEFAULT_REGION = "cn-beijing"
|
||||
VOLCENGINE_SERVICE_BILLING = "billing"
|
||||
|
||||
VOLCENGINE_DEFAULT_REGION = "cn-beijing"
|
||||
)
|
||||
|
||||
type VolcEngineClientConfig struct {
|
||||
@@ -291,11 +296,9 @@ func (client *SVolcEngineClient) jsonRequest(cred sdk.Credentials, domain string
|
||||
body = params
|
||||
}
|
||||
|
||||
u := url.URL{
|
||||
Scheme: "http",
|
||||
Host: domain,
|
||||
Path: "/",
|
||||
RawQuery: query.Encode(),
|
||||
u, err := url.Parse(fmt.Sprintf("http://%s?%s", domain, query.Encode()))
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "url.Parse")
|
||||
}
|
||||
method := httputils.GET
|
||||
for prefix, _method := range map[string]httputils.THttpMethod{
|
||||
@@ -401,6 +404,15 @@ func (client *SVolcEngineClient) getTosClient(regionId string) (*tos.ClientV2, e
|
||||
return tosClient, err
|
||||
}
|
||||
|
||||
func (client *SVolcEngineClient) billRequest(region string, apiName string, params map[string]string) (jsonutils.JSONObject, error) {
|
||||
cred := client.getDefaultCredential(region, VOLCENGINE_SERVICE_BILLING)
|
||||
domain := VOLCENGINE_API
|
||||
if apiName == "QueryBalanceAcct" {
|
||||
domain = VOLCENGINE_BILLING_API + "/open-apis/trade_balance"
|
||||
}
|
||||
return client.jsonRequest(cred, domain, VOLCENGINE_BILLING_API_VERSION, apiName, params)
|
||||
}
|
||||
|
||||
// Buckets
|
||||
func (client *SVolcEngineClient) invalidateIBuckets() {
|
||||
client.iBuckets = nil
|
||||
@@ -473,3 +485,25 @@ func (region *SVolcEngineClient) GetCapabilities() []string {
|
||||
func (client *SVolcEngineClient) GetAccessEnv() string {
|
||||
return api.CLOUD_ACCESS_ENV_VOLCENGINE_CHINA
|
||||
}
|
||||
|
||||
type SBalance struct {
|
||||
AccountId string
|
||||
ArrearsBalance float64
|
||||
AvailableBalance float64
|
||||
CashBalance float64
|
||||
CreditLimit float64
|
||||
FreezeAmount float64
|
||||
}
|
||||
|
||||
func (client *SVolcEngineClient) QueryBalance() (*SBalance, error) {
|
||||
resp, err := client.billRequest("", "QueryBalanceAcct", nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ret := &SBalance{}
|
||||
err = resp.Unmarshal(ret)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "Unmarshal")
|
||||
}
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user