Skip to content

Commit

Permalink
Add some features in bbc
Browse files Browse the repository at this point in the history
  • Loading branch information
duanliguo committed Sep 18, 2020
1 parent 8026d8c commit 4611989
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 0 deletions.
52 changes: 52 additions & 0 deletions doc/BBC.md
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,58 @@ if res, err := bbcClient.GetCustomImage(queryArgs); err != nil {
}
```

### 共享自定义镜像

- 该接口用于共享用户自己的指定的自定义镜像,仅限自定义镜像,系统镜像和服务集成镜像不能共享。
- imageId 所指定的镜像不存在,提示404错误。
- 镜像共享后,被共享的用户可以使用此镜像创建、重置实例。
- 请求参数中的account和accountId均为可选参数,但不能全部为空,当两个参数同时出现时,服务端会自动去重。

```go
// 待共享的用户id
accountId := "your-accountId"
//待共享的用户名
account := "your-account"
// 待共享的镜像ID
imageId := "your-imageId"

args := &api.SharedUser{
AccountId: accountId,
Account: account,
}
if err := bbcClient.ShareImage(imageId,args); err != nil {
fmt.Println("ShareImage failed: ", err)
} else {
fmt.Println("ShareImage success")
}
```

### 取消共享自定义镜像

- 该接口用于取消共享用户自己的指定的自定义镜像,仅限自定义镜像,系统镜像和服务集成镜像不能共享。
- imageId 所指定的镜像不存在,提示404错误。
- 镜像取消共享后,被取消共享的用户不能再使用此镜像创建、重置实例。
- 请求参数中的account和accountId均为可选参数,但不能全部为空,当两个参数同时出现时,服务端会自动去重。

```go
// 待共享的用户id
accountId := "your-accountId"
//待共享的用户名
account := "your-account"
// 待共享的镜像ID
imageId := "your-imageId"

args := &api.SharedUser{
AccountId: accountId,
Account: account,
}
if err := bbcClient.UnShareImage(imageId,args); err != nil {
fmt.Println("UnShareImage failed: ", err)
} else {
fmt.Println("UnShareImage success")
}
```

## 操作日志
### 查询操作日志
通过以下代码查询指定操作日志
Expand Down
22 changes: 22 additions & 0 deletions services/bbc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,28 @@ func (c *Client) GetCustomImage(args *GetFlavorImageArgs) (*GetImagesResult, err
return GetCustomImage(c, body)
}

// ShareImage - share an image
//
// PARAMS:
// - imageId: the specific image ID
// - args: the arguments to share an image
// RETURNS:
// - error: nil if success otherwise the specific error
func (c *Client) ShareImage(imageId string, args *SharedUser) error {
return ShareImage(c, imageId, args)
}

// UnShareImage - cancel share an image
//
// PARAMS:
// - imageId: the specific image ID
// - args: the arguments to cancel share an image
// RETURNS:
// - error: nil if success otherwise the specific error
func (c *Client) UnShareImage(imageId string, args *SharedUser) error {
return UnShareImage(c, imageId, args)
}

// GetInstanceEni - get the eni of the bbc instance
//
// PARAMS:
Expand Down
17 changes: 17 additions & 0 deletions services/bbc/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,23 @@ func TestGetCustomImage(t *testing.T) {
}
}

func TestShareImage(t *testing.T) {
args := &SharedUser{
AccountId: "id",
}
err := BBC_CLIENT.ShareImage(BBC_TestImageId, args)
ExpectEqual(t.Errorf, err, nil)
}

func TestUnShareImage(t *testing.T) {
args := &SharedUser{
AccountId: "id",
}
err := BBC_CLIENT.UnShareImage(BBC_TestImageId, args)
ExpectEqual(t.Errorf, err, nil)
}


func TestGetInstanceEni(t *testing.T) {
instanceId := "instanceId"
if res, err := BBC_CLIENT.GetInstanceEni(instanceId); err != nil {
Expand Down
79 changes: 79 additions & 0 deletions services/bbc/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package bbc

import (
"encoding/json"
"strconv"

"github.com/baidubce/bce-sdk-go/bce"
Expand Down Expand Up @@ -225,6 +226,84 @@ func GetCustomImage(cli bce.Client, reqBody *bce.Body) (*GetImagesResult, error)
return jsonBody, nil
}

// ShareImage - share a specified custom image
//
// PARAMS:
// - cli: the client agent which can perform sending request
// - imageId: id of the image to be shared
// - args: the arguments to share image
// RETURNS:
// - error: nil if success otherwise the specific error
func ShareImage(cli bce.Client, imageId string, args *SharedUser) error {
// Build the request
req := &bce.BceRequest{}
req.SetUri(getImageUriWithId(imageId))
req.SetMethod(http.POST)

req.SetParam("share", "")

jsonBytes, err := json.Marshal(args)
if err != nil {
return err
}
body, err := bce.NewBodyFromBytes(jsonBytes)
if err != nil {
return err
}
req.SetBody(body)

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

defer func() { resp.Body().Close() }()
return nil
}

// UnShareImage - unshare a specified image
//
// PARAMS:
// - cli: the client agent which can perform sending request
// - imageId: id of the image to be unshared
// - args: the arguments to unshare image
// RETURNS:
// - error: nil if success otherwise the specific error
func UnShareImage(cli bce.Client, imageId string, args *SharedUser) error {
// Build the request
req := &bce.BceRequest{}
req.SetUri(getImageUriWithId(imageId))
req.SetMethod(http.POST)

req.SetParam("unshare", "")

jsonBytes, err := json.Marshal(args)
if err != nil {
return err
}
body, err := bce.NewBodyFromBytes(jsonBytes)
if err != nil {
return err
}
req.SetBody(body)

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

defer func() { resp.Body().Close() }()
return nil
}

func getImageUri() string {
return URI_PREFIX_V1 + REQUEST_IMAGE_URI
}
Expand Down
5 changes: 5 additions & 0 deletions services/bbc/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -637,3 +637,8 @@ type DeleteInstanceResult struct {
SuccessResources *DeleteInstanceModel `json:"successResources"`
FailResources *DeleteInstanceModel `json:"failResources"`
}

type SharedUser struct {
AccountId string `json:"accountId,omitempty"`
Account string `json:"account,omitempty"`
}

0 comments on commit 4611989

Please sign in to comment.