Skip to content

Commit

Permalink
Add features in resmanager
Browse files Browse the repository at this point in the history
  • Loading branch information
duanliguo committed Dec 17, 2024
1 parent d079a7c commit 97e0b5d
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 2 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.206"
SDK_VERSION = "0.9.207"
URI_PREFIX = "/" // now support uri without prefix "v1" so just set root path
DEFAULT_DOMAIN = "baidubce.com"
DEFAULT_PROTOCOL = "http"
Expand Down
29 changes: 28 additions & 1 deletion doc/RES_MANAGER.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ ExpireSeconds | int | 签名字符串的有效期
- 变更资源绑定的资源分组
- 查询资源分组列表
- 资源ID查询资源资源分组
- 创建资源分组

### 资源加入资源分组

Expand Down Expand Up @@ -370,6 +371,29 @@ if err != nil {
> 2. 仅能查询当前账号资源,也就是根据AK, SK签名解析的账号下的资源。
> 3. 接口详细描述请参考资源管理[API文档](https://cloud.baidu.com/doc/ResManagement/s/ilth0vmb9)
### 创建资源分组

使用以下代码创建资源分组
```go
args := &CreateResourceGroupArgs{
// 资源组名称
Name: "资源组名称",
// 资源组的备注
Extra: "备注",
}

result, err := client.CreateResourceGroup(args)
if err != nil {
fmt.Println("CreateResourceGroup failed:", err)
} else {
fmt.Println("CreateResourceGroup success: ", result)
}
```

> **提示:**
> 1. 接口详细描述请参考资源管理[API文档](https://cloud.baidu.com/doc/ResManagement/s/ilth0vmb9)
> 2. 资源组的名称,同一用户下不能重复,支持中英文及常见符号-_ /.,1~20个字符,必填。
> 3. 资源组的备注,可以为空。最多存储 256 个字符的文本。
# 错误处理

Expand Down Expand Up @@ -409,4 +433,7 @@ if err != nil {

首次发布:
- 资源加入资源分组、资源从资源分组移除、变更资源绑定的资源分组、查询资源分组列表
- 资源ID查询资源资源分组
- 资源ID查询资源资源分组

## v0.9.207 [2024-12-16]
- 创建资源分组
4 changes: 4 additions & 0 deletions services/resmanager/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ func NewClient(ak, sk, endPoint string) (*Client, error) {
return &Client{client}, nil
}

func getCreateGroupUri() string {
return URI_PREFIX + REQUEST_QUERY_GROUP
}

func getChangeGroupUri() string {
return URI_PREFIX + REQUEST_CHANGE_GROUP
}
Expand Down
11 changes: 11 additions & 0 deletions services/resmanager/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,17 @@ func TestBindResourceToGroup(t *testing.T) {
t.Logf(string(jsonRes))
}

func TestCreateResourceGroup(t *testing.T) {
args := &CreateResourceGroupArgs{
// 资源组名称
Name: "资源组名称",
// 资源组的备注
Extra: "备注",
}
err := resClient.CreateResourceGroup(args)
ExpectEqual(t.Errorf, err, nil)
}

func TestChangeResourceGroup(t *testing.T) {
args := &ChangeResourceGroupArgs{
MoveResModels: []MoveResModel{
Expand Down
5 changes: 5 additions & 0 deletions services/resmanager/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ type BindResourceResult struct {
ResGroups []ResGroup `json:"resGroups"`
}

type CreateResourceGroupArgs struct {
Name string `json:"name"`
Extra string `json:"extra"`
}

type ChangeResourceGroupArgs struct {
MoveResModels []MoveResModel `json:"moveResModels"`
}
Expand Down
19 changes: 19 additions & 0 deletions services/resmanager/resmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,25 @@ func (c *Client) BindResourceToGroup(args *BindResourceToGroupArgs) (*BindResour
return result, err
}

func (c *Client) CreateResourceGroup(args *CreateResourceGroupArgs) error {
if args == nil {
return fmt.Errorf("unset args")
}

if len(args.Name) == 0 {
return fmt.Errorf("unset name")
}

err := bce.NewRequestBuilder(c).
WithMethod(http.POST).
WithURL(getCreateGroupUri()).
WithHeader(http.CONTENT_TYPE, bce.DEFAULT_CONTENT_TYPE).
WithBody(args).
Do()

return err
}

func (c *Client) ChangeResourceGroup(args *ChangeResourceGroupArgs) (*BindResourceResult, error) {
if args == nil {
return nil, fmt.Errorf("unset args")
Expand Down

0 comments on commit 97e0b5d

Please sign in to comment.