Skip to content

Commit

Permalink
fix(baidu): support baidu resource
Browse files Browse the repository at this point in the history
  • Loading branch information
ioito committed Nov 19, 2024
1 parent 313ba76 commit 4cafecc
Show file tree
Hide file tree
Showing 35 changed files with 3,137 additions and 374 deletions.
8 changes: 8 additions & 0 deletions pkg/apis/compute/storage_const.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,14 @@ const (

STORAGE_FULL = "full"
STORAGE_SYSTEM_FULL = "system_full"

// baidu storage type
STORAGE_BAIDU_SSD = "ssd" // 通用型SSD
STORAGE_BAIDU_PREMIUM_SSD = "premium_ssd" // 高性能云磁盘
STORAGE_BAIDU_HDD = "hdd" // 通用型HDD
STORAGE_BAIDU_ENHANCED_SSD_PL1 = "enhanced_ssd_pl1" // 增强型SSD_PL1
STORAGE_BAIDU_ENHANCED_SSD_PL2 = "enhanced_ssd_pl2" // 增强型SSD_PL2
STORAGE_BAIDU_ENHANCED_SSD_PL3 = "enhanced_ssd_pl3" // 增强型SSD_PL2
)

const (
Expand Down
130 changes: 93 additions & 37 deletions pkg/multicloud/baidu/baidu.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ const (
CLOUD_PROVIDER_BAIDU_CN = "百度云"
BAIDU_DEFAULT_REGION = "bj"
ISO8601 = "2006-01-02T15:04:05Z"

SERVICE_STS = "sts"
SERVICE_BBC = "bbc"
SERVICE_BCC = "bcc"
SERVICE_BOS = "bos"
SERVICE_EIP = "eip"
SERVICE_BILLING = "billing"
)

type BaiduClientConfig struct {
Expand Down Expand Up @@ -85,42 +92,52 @@ func NewBaiduClient(cfg *BaiduClientConfig) (*SBaiduClient, error) {
return client, err
}

func (cli *SBaiduClient) GetRegions() []SRegion {
func (cli *SBaiduClient) GetRegions() ([]SRegion, error) {
resp, err := cli.post(SERVICE_BCC, "", "v2/region/describeRegions", nil, nil)
if err != nil {
return nil, err
}
ret := []SRegion{}
for k, v := range regions {
ret = append(ret, SRegion{
client: cli,
Region: k,
RegionName: v,
})
err = resp.Unmarshal(&ret, "regions")
if err != nil {
return nil, err
}
return ret
for i := range ret {
ret[i].client = cli
}
return ret, nil
}

func (cli *SBaiduClient) GetRegion(id string) (*SRegion, error) {
regions := cli.GetRegions()
regions, err := cli.GetRegions()
if err != nil {
return nil, err
}
for i := range regions {
if regions[i].Region == id {
regions[i].client = cli
if regions[i].GetId() == id || regions[i].GetGlobalId() == id {
return &regions[i], nil
}
}
return nil, cloudprovider.ErrNotFound
return nil, errors.Wrapf(cloudprovider.ErrNotFound, id)
}

func (cli *SBaiduClient) getUrl(service, regionId, resource string) (string, error) {
if len(regionId) == 0 {
regionId = BAIDU_DEFAULT_REGION
}
switch service {
case "bbc":
case SERVICE_BBC:
return fmt.Sprintf("https://bbc.%s.baidubce.com/%s", regionId, strings.TrimPrefix(resource, "/")), nil
case "bcc":
case SERVICE_BCC:
return fmt.Sprintf("https://bcc.%s.baidubce.com/%s", regionId, strings.TrimPrefix(resource, "/")), nil
case "bos":
case SERVICE_BOS:
return fmt.Sprintf("https://%s.bcebos.com", regionId), nil
case "billing":
case SERVICE_BILLING:
return fmt.Sprintf("https://billing.baidubce.com/%s", strings.TrimPrefix(resource, "/")), nil
case SERVICE_STS:
return fmt.Sprintf("https://sts.bj.baidubce.com/v1/%s", strings.TrimPrefix(resource, "/")), nil
case SERVICE_EIP:
return fmt.Sprintf("https://eip.%s.baidubce.com/%s", regionId, strings.TrimPrefix(resource, "/")), nil
default:
return "", errors.Wrapf(cloudprovider.ErrNotSupported, service)
}
Expand Down Expand Up @@ -164,6 +181,9 @@ func (e *sBaiduError) ParseErrorFromJsonResponse(statusCode int, status string,
body.Unmarshal(e)
}
e.StatusCode = statusCode
if e.StatusCode == 404 {
return errors.Wrapf(cloudprovider.ErrNotFound, e.Error())
}
return e
}

Expand All @@ -183,34 +203,66 @@ func (cli *SBaiduClient) Do(req *http.Request) (*http.Response, error) {
return client.Do(req)
}

func (cli *SBaiduClient) bccList(regionId, resource string, params map[string]interface{}) (jsonutils.JSONObject, error) {
return cli.list("bcc", regionId, resource, params)
func (cli *SBaiduClient) eipList(regionId, resource string, params url.Values) (jsonutils.JSONObject, error) {
return cli.list(SERVICE_EIP, regionId, resource, params)
}

func (cli *SBaiduClient) eipPost(regionId, resource string, params url.Values, body map[string]interface{}) (jsonutils.JSONObject, error) {
return cli.post(SERVICE_EIP, regionId, resource, params, body)
}

func (cli *SBaiduClient) eipDelete(regionId, resource string, params url.Values) (jsonutils.JSONObject, error) {
return cli.delete(SERVICE_EIP, regionId, resource, params)
}

func (cli *SBaiduClient) list(service, regionId, resource string, params map[string]interface{}) (jsonutils.JSONObject, error) {
return cli.request(httputils.GET, service, regionId, resource, params)
func (cli *SBaiduClient) eipUpdate(regionId, resource string, params url.Values, body map[string]interface{}) (jsonutils.JSONObject, error) {
return cli.update(SERVICE_EIP, regionId, resource, params, body)
}

func (cli *SBaiduClient) post(service, regionId, resource string, params map[string]interface{}) (jsonutils.JSONObject, error) {
return cli.request(httputils.POST, service, regionId, resource, params)
func (cli *SBaiduClient) bccList(regionId, resource string, params url.Values) (jsonutils.JSONObject, error) {
return cli.list(SERVICE_BCC, regionId, resource, params)
}

func (cli *SBaiduClient) request(method httputils.THttpMethod, service, regionId, resource string, params map[string]interface{}) (jsonutils.JSONObject, error) {
func (cli *SBaiduClient) bccPost(regionId, resource string, params url.Values, body map[string]interface{}) (jsonutils.JSONObject, error) {
return cli.post(SERVICE_BCC, regionId, resource, params, body)
}

func (cli *SBaiduClient) bccDelete(regionId, resource string, params url.Values) (jsonutils.JSONObject, error) {
return cli.delete(SERVICE_BCC, regionId, resource, params)
}

func (cli *SBaiduClient) bccUpdate(regionId, resource string, params url.Values, body map[string]interface{}) (jsonutils.JSONObject, error) {
return cli.update(SERVICE_BCC, regionId, resource, params, body)
}

func (cli *SBaiduClient) list(service, regionId, resource string, params url.Values) (jsonutils.JSONObject, error) {
return cli.request(httputils.GET, service, regionId, resource, params, nil)
}

func (cli *SBaiduClient) update(service, regionId, resource string, params url.Values, body map[string]interface{}) (jsonutils.JSONObject, error) {
return cli.request(httputils.PUT, service, regionId, resource, params, body)
}

func (cli *SBaiduClient) delete(service, regionId, resource string, params url.Values) (jsonutils.JSONObject, error) {
return cli.request(httputils.DELETE, service, regionId, resource, params, nil)
}

func (cli *SBaiduClient) post(service, regionId, resource string, params url.Values, body map[string]interface{}) (jsonutils.JSONObject, error) {
return cli.request(httputils.POST, service, regionId, resource, params, body)
}

func (cli *SBaiduClient) request(method httputils.THttpMethod, service, regionId, resource string, params url.Values, body map[string]interface{}) (jsonutils.JSONObject, error) {
uri, err := cli.getUrl(service, regionId, resource)
if err != nil {
return nil, err
}
if params == nil {
params = map[string]interface{}{}
if body == nil {
body = map[string]interface{}{}
}
values := url.Values{}
if method == httputils.GET && len(params) > 0 {
for k, v := range params {
values.Set(k, v.(string))
}
uri = fmt.Sprintf("%s?%s", uri, values.Encode())
if len(params) > 0 {
uri = fmt.Sprintf("%s?%s", uri, params.Encode())
}
req := httputils.NewJsonRequest(method, uri, params)
req := httputils.NewJsonRequest(method, uri, body)
bErr := &sBaiduError{}
client := httputils.NewJsonClient(cli)
_, resp, err := client.Send(cli.ctx, req, bErr, cli.debug)
Expand All @@ -230,12 +282,12 @@ func (cli *SBaiduClient) getOwnerId() (string, error) {
if len(cli.ownerId) > 0 {
return cli.ownerId, nil
}
resp, err := cli.list("bos", "bj", "/", nil)
session, err := cli.GetSessionToken()
if err != nil {
return "", err
}
cli.ownerId, err = resp.GetString("owner", "id")
return cli.ownerId, err
cli.ownerId = session.UserId
return cli.ownerId, nil
}

func (cli *SBaiduClient) GetAccountId() string {
Expand All @@ -248,7 +300,7 @@ type CashBalance struct {
}

func (cli *SBaiduClient) QueryBalance() (*CashBalance, error) {
resp, err := cli.post("billing", "", "/v1/finance/cash/balance", nil)
resp, err := cli.post("billing", "", "/v1/finance/cash/balance", nil, nil)
if err != nil {
return nil, err
}
Expand All @@ -262,7 +314,11 @@ func (cli *SBaiduClient) QueryBalance() (*CashBalance, error) {

func (cli *SBaiduClient) GetCapabilities() []string {
caps := []string{
cloudprovider.CLOUD_CAPABILITY_COMPUTE + cloudprovider.READ_ONLY_SUFFIX,
cloudprovider.CLOUD_CAPABILITY_COMPUTE,
cloudprovider.CLOUD_CAPABILITY_NETWORK,
cloudprovider.CLOUD_CAPABILITY_SECURITY_GROUP,
cloudprovider.CLOUD_CAPABILITY_EIP,
cloudprovider.CLOUD_CAPABILITY_SNAPSHOT_POLICY,
}
return caps
}
Loading

0 comments on commit 4cafecc

Please sign in to comment.