Skip to content

Commit

Permalink
Support/batch naming (#89)
Browse files Browse the repository at this point in the history
* support naming in batch on creating uhost
  • Loading branch information
wangrzneu authored Sep 20, 2024
1 parent e4efec8 commit af20f46
Show file tree
Hide file tree
Showing 15 changed files with 617 additions and 149 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.3.0 (2024-09-20)

* support naming in batch on creating uhost

## 0.2.0 (2024-06-12)

* ssh key pair and security group support for creating uhost
Expand Down
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export VERSION=0.2.0
export VERSION=0.3.0

GOFMT_FILES?=$$(find . -name '*.go' |grep -v vendor)

.PHONY : install
Expand Down
2 changes: 1 addition & 1 deletion base/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const DefaultBaseURL = "https://api.ucloud.cn/"
const DefaultProfile = "default"

// Version 版本号
const Version = "0.2.0"
const Version = "0.3.0"

var UserAgent = fmt.Sprintf("UCloud-CLI/%s", Version)

Expand Down
118 changes: 117 additions & 1 deletion cmd/uhost.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,8 +415,23 @@ func NewCmdUHostCreate() *cobra.Command {
wg := &sync.WaitGroup{}
tokens := make(chan struct{}, concurrent)
wg.Add(count)
batchRename, err := regexp.Match(`[%d,%d]`, []byte(*req.Name))
if err != nil || !batchRename {
batchRename = false
}
if batchRename {
var actualRequest uhost.CreateUHostInstanceRequest
actualRequest = *req
if len(bindEipIDs) > 0 {
if len(bindEipIDs) != count {
return fmt.Errorf("bind-eip count should be equal to uhost count")
}
actualRequest.NetworkInterface = nil
}
wg.Add(1 - count)
createMultipleUhostWrapper(&actualRequest, count, updateEIPReq, bindEipIDs, async, make(chan bool, 1), wg, tokens)

if count <= 5 {
} else if count <= 5 {
for i := 0; i < count; i++ {
bindEipID := ""
if len(bindEipIDs) > i {
Expand Down Expand Up @@ -560,6 +575,23 @@ func NewCmdUHostCreate() *cobra.Command {
return cmd
}

// createMultipleUhostWrapper 处理UI和并发控制
func createMultipleUhostWrapper(req *uhost.CreateUHostInstanceRequest, count int, updateEIPReq *unet.UpdateEIPAttributeRequest, bindEipIDs []string, async bool, retCh chan<- bool, wg *sync.WaitGroup, tokens chan struct{}) {
//控制并发数量
tokens <- struct{}{}
defer func() {
<-tokens
//设置延时,使报错能渲染出来
time.Sleep(time.Second / 5)
wg.Done()
}()

success, logs := createMultipleUhost(req, count, updateEIPReq, bindEipIDs, async)
retCh <- success
logs = append(logs, fmt.Sprintf("result:%t", success))
base.LogInfo(logs...)
}

// createUhostWrapper 处理UI和并发控制
func createUhostWrapper(req *uhost.CreateUHostInstanceRequest, updateEIPReq *unet.UpdateEIPAttributeRequest, bindEipID string, async bool, retCh chan<- bool, wg *sync.WaitGroup, tokens chan struct{}, idx int) {
//控制并发数量
Expand All @@ -577,6 +609,90 @@ func createUhostWrapper(req *uhost.CreateUHostInstanceRequest, updateEIPReq *une
base.LogInfo(logs...)
}

func createMultipleUhost(req *uhost.CreateUHostInstanceRequest, count int, updateEIPReq *unet.UpdateEIPAttributeRequest, bindEipIDs []string, async bool) (bool, []string) {
resp, err := base.BizClient.CreateUHostInstance(req)
block := ux.NewBlock()
ux.Doc.Append(block)
logs := []string{"=================================================="}
logs = append(logs, fmt.Sprintf("api:CreateUHostInstance, request:%v", base.ToQueryMap(req)))
if err != nil {
logs = append(logs, fmt.Sprintf("err:%v", err))
block.Append(base.ParseError(err))
return false, logs
}
if len(bindEipIDs) > 0 && len(bindEipIDs) != count {
block.Append(fmt.Sprintf("expect eip count %d, accept %d", count, len(bindEipIDs)))
return false, logs
}

logs = append(logs, fmt.Sprintf("resp:%#v", resp))
if req.MaxCount == nil {
req.MaxCount = sdk.Int(1)
}
req.MaxCount = sdk.Int(count)
if len(resp.UHostIds) != *req.MaxCount {
block.Append(fmt.Sprintf("expect uhost count %d, accept %d", count, len(resp.UHostIds)))
return false, logs
}
for i, uhostID := range resp.UHostIds {
text := fmt.Sprintf("the uhost[%s]", uhostID)
if len(req.Disks) > 1 {
text = fmt.Sprintf("%s which attached a data disk", text)
if len(req.NetworkInterface) > 0 {
text = fmt.Sprintf("%s and binded an eip", text)
}
} else if len(req.NetworkInterface) > 0 {
text = fmt.Sprintf("%s which binded an eip", text)
}
text = fmt.Sprintf("%s is initializing", text)

if async {
block.Append(text)
} else {
uhostSpoller.Sspoll(resp.UHostIds[0], text, []string{status.HOST_RUNNING, status.HOST_FAIL}, block, &req.CommonBase)
}
bindEipID := bindEipIDs[i]
if bindEipID != "" {
eip := base.PickResourceID(bindEipID)
logs = append(logs, fmt.Sprintf("bind eip: %s", eip))
eipLogs, err := sbindEIP(sdk.String(uhostID), sdk.String("uhost"), &eip, req.ProjectId, req.Region)
logs = append(logs, eipLogs...)
if err != nil {
block.Append(fmt.Sprintf("bind eip[%s] with uhost[%s] failed: %v", eip, uhostID, err))
return false, logs
}
block.Append(fmt.Sprintf("bind eip[%s] with uhost[%s] successfully", eip, uhostID))
} else if len(req.NetworkInterface) > 0 {
ipSet, err := getEIPByUHostId(uhostID)
if err != nil {
block.Append(err.Error())
return false, logs
}
block.Append(fmt.Sprintf("IP:%s Line:%s", ipSet.IP, ipSet.Type))
if *updateEIPReq.Name != "" || *updateEIPReq.Remark != "" {
var message string
if *updateEIPReq.Name != "" && *updateEIPReq.Remark != "" {
message = "name and remark"
} else if *updateEIPReq.Name != "" {
message = "name"
} else {
message = "remark"
}

logs = append(logs, fmt.Sprintf("update attribute %s of eip[%s] binded uhost[%s]", message, ipSet.IPId, uhostID))
updateEIPReq.EIPId = sdk.String(ipSet.IPId)
_, err = base.BizClient.UpdateEIPAttribute(updateEIPReq)
if err != nil {
block.Append(fmt.Sprintf("update attribute %s of eip[%s] binded uhost[%s] got err, %s", message, ipSet.IPId, uhostID, err))
return false, logs
}
block.Append(fmt.Sprintf("update attribute %s of eip[%s] binded uhost[%s] successfully", message, ipSet.IPId, uhostID))
}
}
}
return true, logs
}

func createUhost(req *uhost.CreateUHostInstanceRequest, updateEIPReq *unet.UpdateEIPAttributeRequest, bindEipID string, async bool) (bool, []string) {
resp, err := base.BizClient.CreateUHostInstance(req)
block := ux.NewBlock()
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/sirupsen/logrus v1.3.0
github.com/spf13/cobra v0.0.3
github.com/spf13/pflag v1.0.3
github.com/ucloud/ucloud-sdk-go v0.22.17
github.com/ucloud/ucloud-sdk-go v0.22.25
golang.org/x/sys v0.0.0-20220422013727-9388b58f7150
)

Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/ucloud/ucloud-sdk-go v0.22.17 h1:EFn+GxVKS5Tj8hIPie3qL6Zgk25fmWcHqJ06K8wl+Qo=
github.com/ucloud/ucloud-sdk-go v0.22.17/go.mod h1:dyLmFHmUfgb4RZKYQP9IArlvQ2pxzFthfhwxRzOEPIw=
github.com/ucloud/ucloud-sdk-go v0.22.25 h1:ceKeH7WFnpUt9nJSubn+mnxS1iKGrk/Q+HLwa0iYwmQ=
github.com/ucloud/ucloud-sdk-go v0.22.25/go.mod h1:dyLmFHmUfgb4RZKYQP9IArlvQ2pxzFthfhwxRzOEPIw=
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
Expand Down
69 changes: 67 additions & 2 deletions vendor/github.com/ucloud/ucloud-sdk-go/services/udb/apis.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 37 additions & 13 deletions vendor/github.com/ucloud/ucloud-sdk-go/services/udb/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit af20f46

Please sign in to comment.