Skip to content

Commit

Permalink
Add some features in bbc, bec and ddc
Browse files Browse the repository at this point in the history
  • Loading branch information
duanliguo committed May 28, 2021
1 parent 113adf0 commit b283078
Show file tree
Hide file tree
Showing 19 changed files with 959 additions and 67 deletions.
21 changes: 21 additions & 0 deletions doc/BBC.md
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,27 @@ if err := bbcClient.ModifyInstanceDesc(instanceId, modifyInstanceDescArgs); err
}
```

### 实例变更子网

如下代码可以变更实例的子网
```go
instanceChangeVpcArgs := &api.InstanceChangeSubnetArgs{
InstanceId: instanceId,
SubnetId: subnetId,
Reboot: false,
}
err := bbcClient.InstanceChangeSubnet(args)
if err != nil {
fmt.Println("change instance subnet failed:", err)
} else {
fmt.Println("change instance subnet success")
}
```

> **提示:**
> - 变更子网后默认自动重启,用户选择是否执行该操作。
> - 变更子网的范围目前仅支持在同AZ下变更子网,不支持跨AZ或跨VPC变更子网。
### 修改实例VPC
使用以下代码可以修改指定BBC实例所在的VPC:
```go
Expand Down
104 changes: 104 additions & 0 deletions doc/DDCv2.md
Original file line number Diff line number Diff line change
Expand Up @@ -1358,6 +1358,110 @@ for _, database := range result.Databases {
}
```

## 获取数据表大小

使用以下代码可以获取指定库下满足条件的数据表的大小。
```go
// import ddcrds "github.com/baidubce/bce-sdk-go/services/ddc/v2"

args := &ddcrds.GetTableAmountArgs{
// 实例ID
InstanceId: instanceId,
// 指定数据库名称
DbName: dbName,
// 模糊搜索值, 可选
Pattern: search,
}
result, err := DDCRDS_CLIENT.GetTableAmount(args)
if err != nil {
fmt.Printf("get table amount error: %+v\n", err)
return
}
fmt.Printf("get table amount success.\n")
fmt.Println("ddc return amount ", result.ReturnAmount)
fmt.Println("ddc total amount ", result.TotalAmount)
for k, v := range result.Tables {
fmt.Println("ddc table ", k, " size: ", v)
}
```

## 获取数据库占用磁盘空间

使用以下代码可以获取指定实例下的数据库占用的磁盘空间大小以及剩余磁盘空间。
```go
// import ddcrds "github.com/baidubce/bce-sdk-go/services/ddc/v2"
// dbName可选,不指定数据库时传入空字符串即可
result, err := DDCRDS_CLIENT.GetDatabaseDiskUsage(instanceId, dbName)
if err != nil {
fmt.Printf("get database disk usage error: %+v\n", err)
return
}
fmt.Printf("get database disk usage success.\n")
fmt.Println("ddc rest disk size(byte) ", result.RestDisk)
fmt.Println("ddc used disk size(byte) ", result.UsedDisk)
for k, v := range result.Databases {
fmt.Println("ddc database ", k, " size(byte): ", v)
}
```

## 获取实例数据可恢复时间

使用以下代码可以获取实例数据所有的可恢复时间段。
```go
// import ddcrds "github.com/baidubce/bce-sdk-go/services/ddc/v2"
result, err := DDCRDS_CLIENT.GetRecoverableDateTime(instanceId)
if err != nil {
fmt.Printf("get recoverable datetimes error: %+v\n", err)
return
}
fmt.Printf("get recoverable datetimes success.\n")
for _, e := range result.RecoverableDateTimes {
fmt.Println("recover startTime: ", e.StartDateTime, " endTime: ", e.EndDateTime)
}
```

## 恢复数据库

使用以下代码可以按时间点恢复指定数据库或数据表到原有实例。
```go
// import ddcrds "github.com/baidubce/bce-sdk-go/services/ddc/v2"

args := &ddcrds.RecoverInstanceArgs{
// 可恢复时间点,从GetRecoverableDateTime()获取
Datetime: "2021-05-25T03:28:30Z",
// RestoreMode 为database或table
RecoverData: []ddcrds.RecoverData{
{
// 数据库名称
DbName: dbName,
// 新库名
NewDbName: newDbName,
RestoreMode: "database",
},
{
DbName: dbName,
NewDbName: newDbName,
RestoreMode: "table",
// RestoreMode为table时RecoverTables为必选
RecoverTables: []ddcrds.RecoverTable{
{
// 表名
TableName: tableName,
// 新表名
NewTableName: newTableName,
},
},
},
},
}
err := DDCRDS_CLIENT.RecoverToSourceInstanceByDatetime(instanceId, args)
if err != nil {
fmt.Printf("recover instance database error: %+v\n", err)
return
}
fmt.Printf("recover instance database success.\n")
```

## 删除特定数据库

使用以下代码可以删除特定数据库信息。
Expand Down
1 change: 1 addition & 0 deletions init.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
_ "github.com/baidubce/bce-sdk-go/services/appblb"
_ "github.com/baidubce/bce-sdk-go/services/bbc"
_ "github.com/baidubce/bce-sdk-go/services/bcc"
_ "github.com/baidubce/bce-sdk-go/services/bec"
_ "github.com/baidubce/bce-sdk-go/services/bie"
_ "github.com/baidubce/bce-sdk-go/services/blb"
_ "github.com/baidubce/bce-sdk-go/services/bos"
Expand Down
19 changes: 19 additions & 0 deletions services/bbc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,25 @@ func (c *Client) ModifyInstancePassword(instanceId string, args *ModifyInstanceP
return ModifyInstancePassword(c, instanceId, body)
}

// InstanceChangeSubnet - change an instance's subnet
//
// PARAMS:
// - args: the arguments to change an instance's subnet
// RETURNS:
// - error: nil if success otherwise the specific error
func (c *Client) InstanceChangeSubnet(args *InstanceChangeSubnetArgs) error {
jsonBytes, jsonErr := json.Marshal(args)
if jsonErr != nil {
return jsonErr
}
body, err := bce.NewBodyFromBytes(jsonBytes)
if err != nil {
return err
}

return InstanceChangeSubnet(c, body)
}

// InstanceChangeVpc - change an instance's vpc
//
// PARAMS:
Expand Down
11 changes: 11 additions & 0 deletions services/bbc/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,17 @@ func TestListRecycledInstances(t *testing.T) {
}
}

func TestInstanceChangeSubnet(t *testing.T) {
args := &InstanceChangeSubnetArgs{
InstanceId: "i-DFlNGqLf",
SubnetId: "sbn-z1y9tcedqnh6",
Reboot: true,
}

err := BBC_CLIENT.InstanceChangeSubnet(args)
ExpectEqual(t.Errorf, err, nil)
}

func TestInstanceChangeVpc(t *testing.T) {
args := &InstanceChangeVpcArgs{
InstanceId: "i-xxxxx",
Expand Down
31 changes: 31 additions & 0 deletions services/bbc/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -942,6 +942,33 @@ func BatchDeleteAutoRenewRules(cli bce.Client, reqBody *bce.Body) error {
return nil
}

// InstanceChangeVpc - change the subnet to which the instance belongs
//
// PARAMS:
// - cli: the client agent which can perform sending request
// - reqBody: request body to change subnet of instance
// RETURNS:
// - error: nil if success otherwise the specific error
func InstanceChangeSubnet(cli bce.Client, reqBody *bce.Body) error {
// Build the request
req := &bce.BceRequest{}
req.SetUri(getChangeSubnetUri())
req.SetMethod(http.PUT)
req.SetBody(reqBody)

// 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
}

// InstanceChangeVpc - change the vpc to which the instance belongs
//
// PARAMS:
Expand Down Expand Up @@ -1041,6 +1068,10 @@ func getRebuildBatchInstanceUri() string {
return URI_PREFIX_V1 + REQUEST_INSTANCE_URI + REQUEST_BATCH_REBUILD_INSTANCE_URI
}

func getChangeSubnetUri() string {
return URI_PREFIX_V1 + "/subnet" + "/changeSubnet"
}

func getChangeVpcUri() string {
return URI_PREFIX_V1 + REQUEST_VPC_URI + "/changeVpc"
}
6 changes: 6 additions & 0 deletions services/bbc/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,12 @@ type ModifyInstanceNameArgs struct {
Name string `json:"name"`
}

type InstanceChangeSubnetArgs struct {
InstanceId string `json:"instanceId"`
SubnetId string `json:"subnetId"`
Reboot bool `json:"reboot"`
}

type InstanceChangeVpcArgs struct {
InstanceId string `json:"instanceId"`
SubnetId string `json:"subnetId"`
Expand Down
60 changes: 59 additions & 1 deletion services/bec/api/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,10 +448,44 @@ type ImageDetail struct {
SharedToUserNum int `json:"sharedToUserNum"`
FpgaType string `json:"fpgaType"`
}

type VmInstanceIdVo struct {
VmId string `json:"vmId"`
VmName string `json:"vmName"`
Region string `json:"region"`
City string `json:"city"`
ServiceProvider string `json:"serviceProvider"`
}

type ResourceStatus string

const (
ResourceStatusStarting = "STARTING"
ResourceStatusRunning = "RUNNING"
ResourceStatusException = "EXCEPTION"
ResourceStatusFailed = "FAILED"
ResourceStatusUnknown = "UNKNOWN"
ResourceStatusTerminated = "TERMINATED"
ResourceStatusWaiting = "WAITING"
ResourceStatusStop = "STOP"
ResourceStatusStopping = "STOPPING"
ResourceStatusTerminating = "TERMINATING"
ResourceStatusNormal = "NORMAL"
// part of status for vm instant
ResourceStatusCreating = "CREATING"
ResourceStatusStopped = "STOPPED"
ResourceStatusRestarting = "RESTARTING"
ResourceStatusReinstalling = "REINSTALLING"
ResourceStatusImaging = "IMAGING"
// part of status for lb
ResourceStatusPending = "PENDING"
ResourceStatusBinding = "BINDING"
)

type VmServiceBriefVo struct {
ServiceId string `json:"serviceId"`
ServiceName string `json:"serviceName"`
Status string `json:"status"`
Status ResourceStatus `json:"status"`
TotalCpu int `json:"totalCpu"`
TotalMem int `json:"totalMem"`
TotalDisk int `json:"totalDisk"`
Expand All @@ -463,6 +497,7 @@ type VmServiceBriefVo struct {
OsImage ImageDetail `json:"osImage"`
CreateTime string `json:"createTime"`
TotalGpu int `json:"totalGpu"`
Instances []VmInstanceIdVo `json:"instances"`
}

type CreateVmServiceResult struct {
Expand Down Expand Up @@ -545,6 +580,7 @@ type GetVmServiceDetailArgs struct {
}

type VmServiceDetailsVo struct {
VmServiceBriefVo
Bandwidth string `json:"bandwidth"`
TotalBandwidth string `json:"totalBandwidth"`
DataVolumeList []VolumeConfig `json:"dataVolumeList"`
Expand Down Expand Up @@ -1077,3 +1113,25 @@ type VmPrivateIpResult struct {
Result IpamResultVo `json:"result"`
Success bool `json:"success"`
}

type ServiceProviderInfo struct {
ServiceProvider ServiceProvider `json:"serviceProvider"`
Name string `json:"name"`
Capability []string `json:"capability"`
}

type CityInfo struct {
City string `json:"city"`
Name string `json:"name"`
ServiceProviderList []ServiceProviderInfo `json:"serviceProviderList"`
}

type RegionInfo struct {
Region Region `json:"region"`
Name string `json:"name"`
CityList []CityInfo `json:"cityList"`
}

type GetBecAvailableNodeInfoVoResult struct {
RegionList []RegionInfo `json:"regionList"`
}
6 changes: 6 additions & 0 deletions services/bec/api/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ const (
REQUEST_LOADBALANCER_URL = URI_PREFIX + "/blb"

REQUEST_VM_INSTANCE_URL = URI_PREFIX + "/vm/instance"

REQUEST_NODE_URL = URI_PREFIX + "/node"
)

/*
Expand Down Expand Up @@ -116,3 +118,7 @@ func GetVmServiceMetricsURI(serviceId, metricsType string) string {
func GetVmInstanceURI() string {
return REQUEST_VM_INSTANCE_URL
}

func GetNodeInfoURI() string {
return REQUEST_NODE_URL
}
Loading

0 comments on commit b283078

Please sign in to comment.