Skip to content

Commit

Permalink
Merge pull request #45 from gola/master
Browse files Browse the repository at this point in the history
Add AutoRenew for EIP
  • Loading branch information
duanliguo authored Apr 10, 2020
2 parents 968d5a8 + a548c0d commit 2e3ebef
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 6 deletions.
39 changes: 38 additions & 1 deletion doc/EIP.md
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,9 @@ args := &eip.CreateEipArgs{
PaymentTiming: "Postpaid",
BillingMethod: "ByTraffic",
},
// 预付费资源可以设置是否自动续费
AutoRenewTimeUnit: "month",
AutoRenewTime: 1,
// 指定eip的标签键值对列表
Tags: []model.TagModel{
{
Expand Down Expand Up @@ -446,7 +449,41 @@ fmt.Printf("renew eip success.")
```

> 注意: EIP扩缩容期间不能进行续费操作。
## EIP自动续费
使用以下代码可以为指定的EIP开启自动续费操作
```go
// import "github.com/baidubce/bce-sdk-go/services/eip"

args := &eip.StartAutoRenewArgs{
// 预付费资源可以设置是否自动续费
AutoRenewTimeUnit: "month",
AutoRenewTime: 1,
}
if err := client.StartAutoRenew(eip, args); err != nil {
fmt.Printf("start auto renew eip error: %+v\n", err)
return
}

fmt.Printf("start auto renew eip success.")
```

> 注意:
> - 仅预付费资源可以开通自动续费操作。
> - 设置续费单位若AutoRenewTimeUnit可以为"month"和"year"。
> - 若AutoRenewTimeUnit设置为month,AutoRenewTime支持1-9;若AutoRenewTimeUnit设置为year,AutoRenewTime支持1-3
## EIP停止自动续费
使用以下代码可以为指定的EIP停止自动续费操作
```go
// import "github.com/baidubce/bce-sdk-go/services/eip"
if err := client.StopAutoRenew(eip, ""); err != nil {
fmt.Printf("stop auto renew eip error: %+v\n", err)
return
}

fmt.Printf("stop auto renew eip success.")
```

# 错误处理

Expand Down
37 changes: 37 additions & 0 deletions services/eip/eip.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,40 @@ func (c *Client) PurchaseReservedEip(eip string, args *PurchaseReservedEipArgs)
WithBody(args).
Do()
}

// StartAutoRenew - start auto renew an eip
//
// PARAMS:
// - eip: the specific EIP
// - args: the arguments to start auto renew an eip
// RETURNS:
// - error: nil if success otherwise the specific error
func (c *Client) StartAutoRenew(eip string, args *StartAutoRenewArgs) error {
if args == nil {
return fmt.Errorf("please set eip auto renew argments")
}

return bce.NewRequestBuilder(c).
WithMethod(http.PUT).
WithURL(getEipUriWithEip(eip)).
WithQueryParam("startAutoRenew", "").
WithQueryParamFilter("clientToken", args.ClientToken).
WithBody(args).
Do()
}

// StopAutoRenew - stop eip auto renew
//
// PARAMS:
// - eip: the specific EIP
// - clientToken: optional parameter, an Idempotent Token
// RETURNS:
// - error: nil if success otherwise the specific error
func (c *Client) StopAutoRenew(eip string, clientToken string) error {
return bce.NewRequestBuilder(c).
WithMethod(http.PUT).
WithURL(getEipUriWithEip(eip)).
WithQueryParam("stopAutoRenew", "").
WithQueryParamFilter("clientToken", clientToken).
Do()
}
18 changes: 13 additions & 5 deletions services/eip/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@ type Billing struct {
}

type CreateEipArgs struct {
Name string `json:"name,omitempty"`
BandWidthInMbps int `json:"bandwidthInMbps"`
Billing *Billing `json:"billing"`
Tags []model.TagModel `json:"tags"`
ClientToken string `json:"-"`
Name string `json:"name,omitempty"`
BandWidthInMbps int `json:"bandwidthInMbps"`
Billing *Billing `json:"billing"`
Tags []model.TagModel `json:"tags"`
AutoRenewTimeUnit string `json:"autoRenewTimeUnit,omitempty"`
AutoRenewTime int `json:"autoRenewTime,omitempty"`
ClientToken string `json:"-"`
}

type CreateEipResult struct {
Expand Down Expand Up @@ -90,3 +92,9 @@ type PurchaseReservedEipArgs struct {
Billing *Billing `json:"billing"`
ClientToken string `json:"clientToken"`
}

type StartAutoRenewArgs struct {
AutoRenewTimeUnit string `json:"autoRenewTimeUnit,omitempty"`
AutoRenewTime int `json:"autoRenewTime,omitempty"`
ClientToken string `json:"-"`
}

0 comments on commit 2e3ebef

Please sign in to comment.