Skip to content

Commit

Permalink
Add features in bcc, bos and cdn
Browse files Browse the repository at this point in the history
  • Loading branch information
duanliguo committed Dec 9, 2024
1 parent 851e7b6 commit d079a7c
Show file tree
Hide file tree
Showing 12 changed files with 213 additions and 52 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.205"
SDK_VERSION = "0.9.206"
URI_PREFIX = "/" // now support uri without prefix "v1" so just set root path
DEFAULT_DOMAIN = "baidubce.com"
DEFAULT_PROTOCOL = "http"
Expand Down
20 changes: 20 additions & 0 deletions doc/BCC.md
Original file line number Diff line number Diff line change
Expand Up @@ -2582,6 +2582,26 @@ if err != nil {
}
```
### 修改自定义镜像名称
- 该接口用于修改用户自己的指定的自定义镜像的名称,仅限自定义镜像
- imageId 自定义镜像id。
- name 自定义镜像的新名称。
以下代码可以修改自定义镜像名称:
```go
args := &api.RenameImageArgs{
Name: "your-newName",
ImageId: "your-imageId",
}
err := client.RenameImage(args)
if err != nil {
fmt.Println("rename image failed:", err)
} else {
fmt.Println("rename image success")
}
```
### 跨区域复制自定义镜像
- 用于用户跨区域复制自定义镜像,仅限自定义镜像,系统镜像和服务集成镜像不能复制
- regions如北京"bj",广州"gz",苏州"su",可多选:
Expand Down
142 changes: 95 additions & 47 deletions services/bcc/api/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,22 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/baidubce/bce-sdk-go/bce"
"github.com/baidubce/bce-sdk-go/http"
"strconv"
"strings"

"github.com/baidubce/bce-sdk-go/bce"
"github.com/baidubce/bce-sdk-go/http"
)

// CreateImage - create an image
//
// PARAMS:
// - cli: the client agent which can perform sending request
// - args: the arguments to create image
// - cli: the client agent which can perform sending request
// - args: the arguments to create image
//
// RETURNS:
// - *CreateImageResult: the result of the image newly created
// - error: nil if success otherwise the specific error
// - *CreateImageResult: the result of the image newly created
// - error: nil if success otherwise the specific error
func CreateImage(cli bce.Client, args *CreateImageArgs) (*CreateImageResult, error) {
// Build the request
req := &bce.BceRequest{}
Expand Down Expand Up @@ -74,11 +76,12 @@ func CreateImage(cli bce.Client, args *CreateImageArgs) (*CreateImageResult, err
// ListImage - list all images with the specified parameters
//
// PARAMS:
// - cli: the client agent which can perform sending request
// - queryArgs: the arguments to list images
// - cli: the client agent which can perform sending request
// - queryArgs: the arguments to list images
//
// RETURNS:
// - *ListImageResult: result of the image list
// - error: nil if success otherwise the specific error
// - *ListImageResult: result of the image list
// - error: nil if success otherwise the specific error
func ListImage(cli bce.Client, queryArgs *ListImageArgs) (*ListImageResult, error) {
// Build the request
req := &bce.BceRequest{}
Expand Down Expand Up @@ -127,11 +130,12 @@ func ListImage(cli bce.Client, queryArgs *ListImageArgs) (*ListImageResult, erro
// GetImageDetail - get details of the specified image
//
// PARAMS:
// - cli: the client agent which can perform sending request
// - imageId: id of the image
// - cli: the client agent which can perform sending request
// - imageId: id of the image
//
// RETURNS:
// - *GetImageDetailResult: result of image details
// - error: nil if success otherwise the specific error
// - *GetImageDetailResult: result of image details
// - error: nil if success otherwise the specific error
func GetImageDetail(cli bce.Client, imageId string) (*GetImageDetailResult, error) {
// Build the request
req := &bce.BceRequest{}
Expand All @@ -157,10 +161,11 @@ func GetImageDetail(cli bce.Client, imageId string) (*GetImageDetailResult, erro
// DeleteImage - delete a specified image
//
// PARAMS:
// - cli: the client agent which can perform sending request
// - imageId: id of image to be deleted
// - cli: the client agent which can perform sending request
// - imageId: id of image to be deleted
//
// RETURNS:
// - error: nil if success otherwise the specific error
// - error: nil if success otherwise the specific error
func DeleteImage(cli bce.Client, imageId string) error {
// Build the request
req := &bce.BceRequest{}
Expand All @@ -180,15 +185,53 @@ func DeleteImage(cli bce.Client, imageId string) error {
return nil
}

// RenameImage - rename image
//
// PARAMS:
// - cli: the client agent which can perform sending request
// - args: the arguments to rename image
//
// RETURNS:
// - error: nil if success otherwise the specific error
func RenameImage(cli bce.Client, args *RenameImageArgs) error {
// Build the request
req := &bce.BceRequest{}
req.SetUri(getRenameImageUri())
req.SetMethod(http.PUT)

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
}

// RemoteCopyImage - copy custom images across regions, only custom images supported, the system \
// and service integration images cannot be copied.
//
// PARAMS:
// - cli: the client agent which can perform sending request
// - imageId: id of the image to be copied
// - args: the arguments to copy image
// - cli: the client agent which can perform sending request
// - imageId: id of the image to be copied
// - args: the arguments to copy image
//
// RETURNS:
// - error: nil if success otherwise the specific error
// - error: nil if success otherwise the specific error
func RemoteCopyImage(cli bce.Client, imageId string, args *RemoteCopyImageArgs) error {
// Build the request
req := &bce.BceRequest{}
Expand Down Expand Up @@ -224,11 +267,12 @@ func RemoteCopyImage(cli bce.Client, imageId string, args *RemoteCopyImageArgs)
// and service integration images cannot be copied.
//
// PARAMS:
// - cli: the client agent which can perform sending request
// - imageId: id of the image to be copied
// - args: the arguments to copy image
// - cli: the client agent which can perform sending request
// - imageId: id of the image to be copied
// - args: the arguments to copy image
//
// RETURNS:
// - imageIds of destination region if success otherwise the specific error
// - imageIds of destination region if success otherwise the specific error
func RemoteCopyImageReturnImageIds(cli bce.Client, imageId string, args *RemoteCopyImageArgs) (*RemoteCopyImageResult, error) {
// Build the request
req := &bce.BceRequest{}
Expand Down Expand Up @@ -266,10 +310,11 @@ func RemoteCopyImageReturnImageIds(cli bce.Client, imageId string, args *RemoteC
// CancelRemoteCopyImage - cancel the image copy across regions
//
// PARAMS:
// - cli: the client agent which can perform sending request
// - imageId: id of the image
// - cli: the client agent which can perform sending request
// - imageId: id of the image
//
// RETURNS:
// - error: nil if success otherwise the specific error
// - error: nil if success otherwise the specific error
func CancelRemoteCopyImage(cli bce.Client, imageId string) error {
// Build the request
req := &bce.BceRequest{}
Expand All @@ -294,11 +339,12 @@ func CancelRemoteCopyImage(cli bce.Client, imageId string) error {
// 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
// - 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
// - error: nil if success otherwise the specific error
func ShareImage(cli bce.Client, imageId string, args *SharedUser) error {
// Build the request
req := &bce.BceRequest{}
Expand Down Expand Up @@ -333,11 +379,12 @@ func ShareImage(cli bce.Client, imageId string, args *SharedUser) error {
// 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
// - 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
// - error: nil if success otherwise the specific error
func UnShareImage(cli bce.Client, imageId string, args *SharedUser) error {
// Build the request
req := &bce.BceRequest{}
Expand Down Expand Up @@ -372,11 +419,12 @@ func UnShareImage(cli bce.Client, imageId string, args *SharedUser) error {
// GetImageSharedUser - get the list of users that the image has been shared with
//
// PARAMS:
// - cli: the client agent which can perform sending request
// - imageId: id of the image
// - cli: the client agent which can perform sending request
// - imageId: id of the image
//
// RETURNS:
// - *GetImageSharedUserResult: result of the shared users
// - error: nil if success otherwise the specific error
// - *GetImageSharedUserResult: result of the shared users
// - error: nil if success otherwise the specific error
func GetImageSharedUser(cli bce.Client, imageId string) (*GetImageSharedUserResult, error) {
// Build the request
req := &bce.BceRequest{}
Expand All @@ -402,11 +450,12 @@ func GetImageSharedUser(cli bce.Client, imageId string) (*GetImageSharedUserResu
// GetImageOS - get the operating system information of the instance in batches according to the instance ids
//
// PARAMS:
// - cli: the client agent which can perform sending request
// - args: the arguments of instance ids
// - cli: the client agent which can perform sending request
// - args: the arguments of instance ids
//
// RETURNS:
// - *GetImageOsResult: result of the operating system information
// - error: nil if success otherwise the specific error
// - *GetImageOsResult: result of the operating system information
// - error: nil if success otherwise the specific error
func GetImageOS(cli bce.Client, args *GetImageOsArgs) (*GetImageOsResult, error) {
// Build the request
req := &bce.BceRequest{}
Expand Down Expand Up @@ -439,7 +488,6 @@ func GetImageOS(cli bce.Client, args *GetImageOsArgs) (*GetImageOsResult, error)
return jsonBody, nil
}


func BindImageToTags(cli bce.Client, imageId string, reqBody *bce.Body) error {
// Build the request
req := &bce.BceRequest{}
Expand Down Expand Up @@ -557,7 +605,7 @@ func GetAvailableImagesBySpec(cli bce.Client, args *GetAvailableImagesBySpecArg)
if len(args.Marker) > 0 {
req.SetParam("marker", args.Marker)
}
if args.MaxKeys > 0{
if args.MaxKeys > 0 {
req.SetParam("maxKeys", fmt.Sprint(args.MaxKeys))
}

Expand All @@ -575,4 +623,4 @@ func GetAvailableImagesBySpec(cli bce.Client, args *GetAvailableImagesBySpecArg)
return nil, err
}
return jsonBody, nil
}
}
5 changes: 5 additions & 0 deletions services/bcc/api/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -1699,6 +1699,11 @@ type RemoteCopyImageArgs struct {
DestRegion []string `json:"destRegion"`
}

type RenameImageArgs struct {
Name string `json:"name"`
ImageId string `json:"imageId"`
}

type RemoteCopyImageResult struct {
RemoteCopyImages []RemoteCopyImageModel `json:"result"`
}
Expand Down
4 changes: 4 additions & 0 deletions services/bcc/api/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,10 @@ func getImageUriWithId(id string) string {
return URI_PREFIXV2 + REQUEST_IMAGE_URI + "/" + id
}

func getRenameImageUri() string {
return URI_PREFIXV2 + REQUEST_IMAGE_URI + "/rename"
}

func getImageSharedUserUri(id string) string {
return URI_PREFIXV2 + REQUEST_IMAGE_URI + "/" + id + REQUEST_IMAGE_SHAREDUSER_URI
}
Expand Down
13 changes: 12 additions & 1 deletion services/bcc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package bcc
import (
"encoding/json"
"fmt"

"github.com/baidubce/bce-sdk-go/auth"
"github.com/baidubce/bce-sdk-go/bce"
"github.com/baidubce/bce-sdk-go/services/bcc/api"
Expand Down Expand Up @@ -1246,6 +1247,17 @@ func (c *Client) DeleteImage(imageId string) error {
return api.DeleteImage(c, imageId)
}

// RenameImage - rename image
//
// PARAMS:
// - args: the arguments to rename image
//
// RETURNS:
// - error: nil if success otherwise the specific error
func (c *Client) RenameImage(args *api.RenameImageArgs) error {
return api.RenameImage(c, args)
}

// RemoteCopyImage - copy an image from other region
//
// PARAMS:
Expand Down Expand Up @@ -2382,7 +2394,6 @@ func (c *Client) ImportCustomImage(args *api.ImportCustomImageArgs) (*api.Import

func (c *Client) BatchRefundResource(arg *api.BatchRefundResourceArg) (*api.BatchRefundResourceResult,
error) {

return api.BatchRefundResource(c, arg)
}

Expand Down
10 changes: 9 additions & 1 deletion services/bcc/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -934,6 +934,15 @@ func TestDeleteImage(t *testing.T) {
ExpectEqual(t.Errorf, err, nil)
}

func TestRenameImage(t *testing.T) {
args := &api.RenameImageArgs{
Name: "newName",
ImageId: "m-z6YPac1v",
}
err := BCC_CLIENT.RenameImage(args)
ExpectEqual(t.Errorf, err, nil)
}

func TestDeleteInstance(t *testing.T) {
err := BCC_CLIENT.DeleteInstance(BCC_TestBccId)
ExpectEqual(t.Errorf, err, nil)
Expand Down Expand Up @@ -2074,7 +2083,6 @@ func TestEhcClusterList(t *testing.T) {
}

func TestCreateReservedInstance(t *testing.T) {

args := &api.CreateReservedInstanceArgs{
ClientToken: "myClientToken1",
ReservedInstanceName: "myReservedInstance",
Expand Down
Loading

0 comments on commit d079a7c

Please sign in to comment.