From 4611989d8af2a2204918a9f0ec35e90403022724 Mon Sep 17 00:00:00 2001 From: duanliguo Date: Fri, 18 Sep 2020 11:30:28 +0800 Subject: [PATCH] Add some features in bbc --- doc/BBC.md | 52 ++++++++++++++++++++++++ services/bbc/client.go | 22 +++++++++++ services/bbc/client_test.go | 17 ++++++++ services/bbc/image.go | 79 +++++++++++++++++++++++++++++++++++++ services/bbc/model.go | 5 +++ 5 files changed, 175 insertions(+) diff --git a/doc/BBC.md b/doc/BBC.md index f8711a04..ff1222ce 100644 --- a/doc/BBC.md +++ b/doc/BBC.md @@ -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") +} +``` + ## 操作日志 ### 查询操作日志 通过以下代码查询指定操作日志 diff --git a/services/bbc/client.go b/services/bbc/client.go index 935503ed..9ee2f1d4 100644 --- a/services/bbc/client.go +++ b/services/bbc/client.go @@ -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: diff --git a/services/bbc/client_test.go b/services/bbc/client_test.go index 4c489b10..12224590 100644 --- a/services/bbc/client_test.go +++ b/services/bbc/client_test.go @@ -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 { diff --git a/services/bbc/image.go b/services/bbc/image.go index d2d52f6e..dd61ff7f 100644 --- a/services/bbc/image.go +++ b/services/bbc/image.go @@ -18,6 +18,7 @@ package bbc import ( + "encoding/json" "strconv" "github.com/baidubce/bce-sdk-go/bce" @@ -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 } diff --git a/services/bbc/model.go b/services/bbc/model.go index 58097019..7c8af1a9 100644 --- a/services/bbc/model.go +++ b/services/bbc/model.go @@ -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"` +}