Skip to content

Commit

Permalink
Add features in bcc, cdn and eni
Browse files Browse the repository at this point in the history
  • Loading branch information
duanliguo committed May 29, 2024
1 parent 665fb49 commit 42c662f
Show file tree
Hide file tree
Showing 24 changed files with 302 additions and 81 deletions.
2 changes: 1 addition & 1 deletion bce/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (

// Constants and default values for the package bce
const (
SDK_VERSION = "0.9.181"
SDK_VERSION = "0.9.182"
URI_PREFIX = "/" // now support uri without prefix "v1" so just set root path
DEFAULT_DOMAIN = "baidubce.com"
DEFAULT_PROTOCOL = "http"
Expand Down
20 changes: 19 additions & 1 deletion doc/BCC.md
Original file line number Diff line number Diff line change
Expand Up @@ -1623,7 +1623,7 @@ if err != nil {
> **提示:**
> - 变更子网后默认自动重启,用户选择是否执行该操作。
> - 变更子网的范围目前仅支持在同AZ下变更子网,不支持跨AZ或跨VPC变更子网,如果从普通子网变更至NAT专属子网请先手动解绑EIP。
> - 变更子网的范围目前仅支持在同AZ下变更子网,如果从普通子网变更至NAT专属子网请先手动解绑EIP。
### 实例变更VPC
Expand Down Expand Up @@ -3686,6 +3686,24 @@ if res, err := bccClient.GetInstanceCreateStock(args); err != nil {
}
```
### CDS询价
以下代码可以查询CDS价格
```go
args := &api.VolumePriceRequestArgs{
PurchaseLength: 1,
PaymentTiming: "Prepaid",
StorageType: "cloud_hp1",
CdsSizeInGB: 1000,
PurchaseCount: 1,
ZoneName: "cn-bj-a",
}

if res, err := bccClient.getCdsPrice(args); err != nil {
fmt.Println("getCdsPrice failed: ", err)
} else {
fmt.Println("getCdsPrice success: ", res)
}
```
### 实例扩缩容库存查询
实例变配余量查询
Expand Down
12 changes: 6 additions & 6 deletions doc/CDN-abroad.md
Original file line number Diff line number Diff line change
Expand Up @@ -373,12 +373,12 @@ fmt.Printf("SetHTTPSConfig disable HTTPS successfully\n")

上述例子中,`api.HTTPSConfig`的详细说明如下:

| 字段 | 类型 | 说明 |
|---------|--------|----------------------------------------------------------------------------------------------------------------------------|
| Enabled | bool | true 表示开启HTTPS,certId必须有效且真实存在 | |
| CertId | string | 证书ID,百度智能云统一通过证书中心进行证书管理,您必须先通过 [证书管理中心](https://console.bce.baidu.com/cas/#/cas/purchased/common/list) 进行证书上传/购买,才能获得证书ID。 |
| HttpRedirect | bool | 为true时将HTTP请求重定向到HTTPS(重定向状态码为301)。 |
| Http2Enabled | bool | true 表示开启HTTP2。 |
| 字段 | 类型 | 说明 |
|---------|--------|--------------------------------------------------------------------------------------------------------------------------|
| Enabled | bool | true 表示开启HTTPS,certId必须有效且真实存在 | |
| CertId | string | 证书ID,百度智能云统一通过`SSL证书服务`进行证书管理,您必须先通过 [SSL证书服务](https://console.bce.baidu.com/cas/#/cas/purchased/common/list) 进行证书上传/购买,才能获得证书ID。 |
| HttpRedirect | bool | 为true时将HTTP请求重定向到HTTPS(重定向状态码为301)。 |
| Http2Enabled | bool | true 表示开启HTTP2。 |



Expand Down
1 change: 1 addition & 0 deletions doc/CDN.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ fmt.Printf("err:%+v\n", err)
- CreateDomainWithForm:配置域名的form(类型、类别)
- CreateDomainWithOriginDefaultHost:配置域名的域名级别回源Host,优先级低于源站级别回源Host
- CreateDomainWithTags:绑定域名到标签
- CreateDomainAsDrcdnType:将域名创建为 DRCDN 类型,必须要先开通 DRCDN 产品,关于如何开通 DRCDN 请参考教程:https://cloud.baidu.com/doc/DRCDN/s/gkh08rmki。当配置此项时, dsa 配置不能为 nil。

下面通过代码展示如何调用 CreateDomainWithOptions。

Expand Down
7 changes: 7 additions & 0 deletions examples/endpoint/example_create_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package endpointexamples

import (
"fmt"
"github.com/baidubce/bce-sdk-go/model"

"github.com/baidubce/bce-sdk-go/services/endpoint"
)
Expand All @@ -33,6 +34,12 @@ func CreateEndpoint() {
Billing: &endpoint.Billing{
PaymentTiming: "Postpaid",
},
Tags: []model.TagModel{
{
TagKey: "tagKey",
TagValue: "tagValue",
},
},
}
result, err := client.CreateEndpoint(args) // 创建endpoint
if err != nil {
Expand Down
7 changes: 7 additions & 0 deletions examples/eni/example_create_eni.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package eniexamples

import (
"fmt"
"github.com/baidubce/bce-sdk-go/model"

"github.com/baidubce/bce-sdk-go/services/eni"
"github.com/baidubce/bce-sdk-go/util"
Expand Down Expand Up @@ -35,6 +36,12 @@ func CreateEni() {
PrivateIpAddress: "", // 弹性网卡内网IPv6
},
},
Tags: []model.TagModel{
{
TagKey: "tagKey",
TagValue: "tagValue",
},
},
Description: "go sdk test: create eni", // 弹性网卡描述
}

Expand Down
1 change: 0 additions & 1 deletion examples/eni/example_get_eni_detail.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ func getClientToken() string {
func GetEniDetail() {
// 初始化AK/SK/Endpoint
ak, sk, endpoint := "Your AK", "Your SK", "Your Endpoint"

ENI_CLIENT, _ := eni.NewClient(ak, sk, endpoint) // 初始化client

response, err := ENI_CLIENT.GetEniDetail("eni-477g9akswgjv") // 查询指定的弹性网卡
Expand Down
1 change: 1 addition & 0 deletions services/bbc/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ type InstanceModel struct {
Zone string `json:"zone"`
Region string `json:"region"`
HasAlive int `json:"hasAlive"`
AutoRenew bool `json:"autoRenew"`
Tags []model.TagModel `json:"tags"`
SwitchId string `json:"switchId"`
HostId string `json:"hostId"`
Expand Down
32 changes: 32 additions & 0 deletions services/bcc/api/cds.go
Original file line number Diff line number Diff line change
Expand Up @@ -902,3 +902,35 @@ func UntagVolume(cli bce.Client, volumeId string, args *TagVolumeArgs) error {
}
return nil
}

func GetCdsPrice(cli bce.Client, args *VolumePriceRequestArgs) (*VolumePriceResponse, error) {
// Build the request
req := &bce.BceRequest{}
req.SetUri(getCdsPriceUri())
req.SetMethod(http.POST)
jsonBytes, err := json.Marshal(args)
if err != nil {
return nil, err
}
body, err := bce.NewBodyFromBytes(jsonBytes)
if err != nil {
return nil, err
}
req.SetBody(body)

// Send request and get response
resp := &bce.BceResponse{}
if err := cli.SendRequest(req, resp); err != nil {
return nil, err
}
if resp.IsFail() {
return nil, resp.ServiceError()
}

jsonBody := &VolumePriceResponse{}
if err := resp.ParseJsonBody(jsonBody); err != nil {
return nil, err
}
return jsonBody, nil
}

31 changes: 27 additions & 4 deletions services/bcc/api/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -1102,10 +1102,12 @@ type InstanceChangeVpcArgs struct {
}

type InstanceChangeSubnetArgs struct {
InstanceId string `json:"instanceId"`
SubnetId string `json:"subnetId"`
InternalIp string `json:"internalIp"`
Reboot bool `json:"reboot"`
InstanceId string `json:"instanceId"`
SubnetId string `json:"subnetId"`
InternalIp string `json:"internalIp"`
Reboot bool `json:"reboot"`
SecurityGroupIds []string `json:"securityGroupIds"`
EnterpriseSecurityGroupIds []string `json:"enterpriseSecurityGroupIds"`
}

type BatchAddIpArgs struct {
Expand Down Expand Up @@ -2027,6 +2029,7 @@ type Eni struct {
VpcId string `json:"vpcId"`
SubnetId string `json:"subnetId"`
Status string `json:"status"`
CreatedTime string `json:"createdTime"`
PrivateIpSet []PrivateIP `json:"privateIpSet"`
}

Expand Down Expand Up @@ -2443,3 +2446,23 @@ type ImageArg []struct {
OsLang string `json:"osLang"`
MinSizeInGiB int `json:"minSizeInGiB"`
}

type VolumePriceRequestArgs struct {
PurchaseLength int `json:"purchaseLength"`
PaymentTiming string `json:"paymentTiming"`
StorageType string `json:"storageType"`
CdsSizeInGB int `json:"cdsSizeInGB"`
PurchaseCount int `json:"purchaseCount"`
ZoneName string `json:"zoneName"`
}

type VolumePriceResponse struct {
Price []CdsPrice `json:"price"`
}

type CdsPrice struct {
StorageType string `json:"storageType"`
CdsSizeInGB int `json:"cdsSizeInGB"`
Price float64 `json:"price"`
Unit string `json:"unit"`
}
5 changes: 5 additions & 0 deletions services/bcc/api/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ const (
REQUEST_TRANSFER_IN_URI = "/reserved/transfer/in/list"
REQUEST_TRANSFER_OUT_URI = "/reserved/transfer/out/list"
REQUEST_RELATED_DELETE_POLICY = "/modifyRelatedDeletePolicy"
REQUEST_VOLUME_PRICE_URI = "/volume/getPrice"
)

func getInstanceUri() string {
Expand Down Expand Up @@ -494,3 +495,7 @@ func getTransferInReservedInstanceOrdersUri() string {
func getTransferOutReservedInstanceOrdersUri() string {
return URI_PREFIXV2 + REQUEST_INSTANCE_URI + REQUEST_TRANSFER_OUT_URI
}

func getCdsPriceUri() string {
return URI_PREFIXV2 + REQUEST_VOLUME_PRICE_URI
}
4 changes: 4 additions & 0 deletions services/bcc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2487,3 +2487,7 @@ func (c *Client) TransferOutReservedInstanceOrders(args *api.DescribeTransferRes
}
return api.TransferOutReservedInstanceOrders(c, body)
}

func (c *Client) getCdsPrice(args *api.VolumePriceRequestArgs) (*api.VolumePriceResponse, error) {
return api.GetCdsPrice(c, args)
}
24 changes: 20 additions & 4 deletions services/bcc/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -806,10 +806,11 @@ func TestInstanceChangeVpc(t *testing.T) {

func TestInstanceChangeSubnet(t *testing.T) {
args := &api.InstanceChangeSubnetArgs{
InstanceId: "i-YRMFRB6Z",
SubnetId: "sbn-z1y9tcedqnh6",
InternalIp: "192.168.5.12",
Reboot: false,
InstanceId: "i-YRMFRB6Z",
SubnetId: "sbn-z1y9tcedqnh6",
InternalIp: "192.168.5.12",
Reboot: false,
SecurityGroupIds: []string{"g-i24fkh******"},
}

err := BCC_CLIENT.InstanceChangeSubnet(args)
Expand Down Expand Up @@ -1982,3 +1983,18 @@ func TestCreateVolumeWithResGroup(t *testing.T) {
res, _ := BCC_CLIENT.GetCDSVolumeDetail(BCC_TestCdsId)
fmt.Println(res.Volume)
}

func TestGetCDSPrice(t *testing.T) {
args := &api.VolumePriceRequestArgs{
PurchaseLength: 1,
PaymentTiming: "Prepaid",
StorageType: "cloud_hp1",
CdsSizeInGB: 1000,
PurchaseCount: 1,
ZoneName: "cn-bj-a",
}

result, err := BCC_CLIENT.getCdsPrice(args)
ExpectEqual(t.Errorf, err, nil)
fmt.Println(result)
}
14 changes: 12 additions & 2 deletions services/cdn/api/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,22 +151,32 @@ func IsValidDomain(cli bce.Client, domain string) (*DomainValidInfo, error) {
// - domain: the specified domain
// - originInit: initialized data for a CDN domain
// - tags: bind with the specified tags
// - dsa: DRCDN type domain config, the rules of speed up the dynamic asserts.
// RETURNS:
// - *DomainCreatedInfo: the details about created a CDN domain
// - error: nil if success otherwise the specific error
func CreateDomain(cli bce.Client, domain string, originInit *OriginInit, tags []model.TagModel) (*DomainCreatedInfo, error) {
func CreateDomain(cli bce.Client, domain string, originInit *OriginInit, tags []model.TagModel, dsa *DSAConfig) (*DomainCreatedInfo, error) {
urlPath := fmt.Sprintf("/v2/domain/%s", domain)
respObj := &DomainCreatedInfo{}

type Request struct {
*OriginInit
Tags []model.TagModel `json:"tags,omitempty"`
Tags []model.TagModel `json:"tags,omitempty"`
ProductType int `json:"productType,omitempty"`
Dsa *DSAConfig `json:"dsa,omitempty"`
}

requestObject := &Request{
OriginInit: originInit,
Tags: tags,
}

// Make a request body to create DRCDN domain.
if dsa != nil {
requestObject.ProductType = 1
requestObject.Dsa = dsa
}

err := httpRequest(cli, "PUT", urlPath, nil, requestObject, respObj)
if err != nil {
return nil, err
Expand Down
15 changes: 14 additions & 1 deletion services/cdn/api/dsa.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,20 @@ import (
"github.com/baidubce/bce-sdk-go/bce"
)

// DSARule defined a struct for DSA urls
// DSARule configure the rule of speed up which asserts.
// Valid rule types are:
// - suffix: the file suffix in the URL, such as http://test.com/1.php has the suffix .php, or you can call it as extension.
// - path: the directory about URL, such as http://test.com/web/1.php, located at two directories, "/" and the "/web".
// - exactPath: the absolute URL path.
// - method: the HTTP request method, now we only support 5 methods, they are GET, POST, PUT, DELETE and OPTIONS.
//
// The value is related to the rule type, for example, we can configure "method" type rule with 3 methods,
// the value of it are "GET;POST;PUT" which come from 3 method string connect to each other by semicolon.
// Here shows how to construct a DSARule object of above example:
// var rule = &DSARule{
// Type: "method",
// Value: "GET;POST;PUT",
// }
type DSARule struct {
Type string `json:"type"`
Value string `json:"value"`
Expand Down
30 changes: 28 additions & 2 deletions services/cdn/client.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cdn

import (
"fmt"

"github.com/baidubce/bce-sdk-go/auth"
"github.com/baidubce/bce-sdk-go/bce"
"github.com/baidubce/bce-sdk-go/model"
Expand Down Expand Up @@ -143,10 +145,31 @@ func CreateDomainWithForm(form string) CreateDomainOption {
}
}

// CreateDomainAsDrcdnType configure the target product type as `DRCDN`, posting DRCDN rules by
// dsa argument which can not be nil value.
// Please be sure you are activated DRCDN first, then you can create DRCDN domain successfully.
// Don't know what is DRCDN or don't know how to activate DRCDN? Please read this
// tutorial DOC: https://cloud.baidu.com/doc/DRCDN/s/gkh08rmki
func CreateDomainAsDrcdnType(dsa *api.DSAConfig) CreateDomainOption {
return func(o interface{}) {
cfg, ok := o.(*createDomainOption)
if ok {
if dsa == nil {
cfg.errMsg = "nil dsa argument at CreateDomainAsDrcdnType"
return
}
cfg.dsa = dsa
}
}
}

type createDomainOption struct {
tags []model.TagModel
defaultHost string
form string
dsa *api.DSAConfig

errMsg string
}

// CreateDomain - create a BCE CDN domain
Expand All @@ -159,7 +182,7 @@ type createDomainOption struct {
// - *DomainCreatedInfo: the details about created a CDN domain
// - error: nil if success otherwise the specific error
func (cli *Client) CreateDomain(domain string, originInit *api.OriginInit) (*api.DomainCreatedInfo, error) {
return api.CreateDomain(cli, domain, originInit, nil)
return api.CreateDomain(cli, domain, originInit, nil, nil)
}

// CreateDomainWithOptions - create a BCE CDN domain with optional configurations.
Expand All @@ -176,13 +199,16 @@ func (cli *Client) CreateDomainWithOptions(domain string, origins []api.OriginPe
var cfg createDomainOption
for _, opt := range opts {
opt(&cfg)
if cfg.errMsg != "" {
return nil, fmt.Errorf("input to SDK error: %s", cfg.errMsg)
}
}
originInit := &api.OriginInit{
Origin: origins,
DefaultHost: cfg.defaultHost,
Form: cfg.form,
}
return api.CreateDomain(cli, domain, originInit, cfg.tags)
return api.CreateDomain(cli, domain, originInit, cfg.tags, cfg.dsa)
}

// EnableDomain - enable a specified domain
Expand Down
Loading

0 comments on commit 42c662f

Please sign in to comment.