Skip to content

Commit af20f46

Browse files
authored
Support/batch naming (#89)
* support naming in batch on creating uhost
1 parent e4efec8 commit af20f46

File tree

15 files changed

+617
-149
lines changed

15 files changed

+617
-149
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.3.0 (2024-09-20)
2+
3+
* support naming in batch on creating uhost
4+
15
## 0.2.0 (2024-06-12)
26

37
* ssh key pair and security group support for creating uhost

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
export VERSION=0.2.0
1+
export VERSION=0.3.0
2+
23
GOFMT_FILES?=$$(find . -name '*.go' |grep -v vendor)
34

45
.PHONY : install

base/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ const DefaultBaseURL = "https://api.ucloud.cn/"
3939
const DefaultProfile = "default"
4040

4141
// Version 版本号
42-
const Version = "0.2.0"
42+
const Version = "0.3.0"
4343

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

cmd/uhost.go

Lines changed: 117 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,8 +415,23 @@ func NewCmdUHostCreate() *cobra.Command {
415415
wg := &sync.WaitGroup{}
416416
tokens := make(chan struct{}, concurrent)
417417
wg.Add(count)
418+
batchRename, err := regexp.Match(`[%d,%d]`, []byte(*req.Name))
419+
if err != nil || !batchRename {
420+
batchRename = false
421+
}
422+
if batchRename {
423+
var actualRequest uhost.CreateUHostInstanceRequest
424+
actualRequest = *req
425+
if len(bindEipIDs) > 0 {
426+
if len(bindEipIDs) != count {
427+
return fmt.Errorf("bind-eip count should be equal to uhost count")
428+
}
429+
actualRequest.NetworkInterface = nil
430+
}
431+
wg.Add(1 - count)
432+
createMultipleUhostWrapper(&actualRequest, count, updateEIPReq, bindEipIDs, async, make(chan bool, 1), wg, tokens)
418433

419-
if count <= 5 {
434+
} else if count <= 5 {
420435
for i := 0; i < count; i++ {
421436
bindEipID := ""
422437
if len(bindEipIDs) > i {
@@ -560,6 +575,23 @@ func NewCmdUHostCreate() *cobra.Command {
560575
return cmd
561576
}
562577

578+
// createMultipleUhostWrapper 处理UI和并发控制
579+
func createMultipleUhostWrapper(req *uhost.CreateUHostInstanceRequest, count int, updateEIPReq *unet.UpdateEIPAttributeRequest, bindEipIDs []string, async bool, retCh chan<- bool, wg *sync.WaitGroup, tokens chan struct{}) {
580+
//控制并发数量
581+
tokens <- struct{}{}
582+
defer func() {
583+
<-tokens
584+
//设置延时,使报错能渲染出来
585+
time.Sleep(time.Second / 5)
586+
wg.Done()
587+
}()
588+
589+
success, logs := createMultipleUhost(req, count, updateEIPReq, bindEipIDs, async)
590+
retCh <- success
591+
logs = append(logs, fmt.Sprintf("result:%t", success))
592+
base.LogInfo(logs...)
593+
}
594+
563595
// createUhostWrapper 处理UI和并发控制
564596
func createUhostWrapper(req *uhost.CreateUHostInstanceRequest, updateEIPReq *unet.UpdateEIPAttributeRequest, bindEipID string, async bool, retCh chan<- bool, wg *sync.WaitGroup, tokens chan struct{}, idx int) {
565597
//控制并发数量
@@ -577,6 +609,90 @@ func createUhostWrapper(req *uhost.CreateUHostInstanceRequest, updateEIPReq *une
577609
base.LogInfo(logs...)
578610
}
579611

612+
func createMultipleUhost(req *uhost.CreateUHostInstanceRequest, count int, updateEIPReq *unet.UpdateEIPAttributeRequest, bindEipIDs []string, async bool) (bool, []string) {
613+
resp, err := base.BizClient.CreateUHostInstance(req)
614+
block := ux.NewBlock()
615+
ux.Doc.Append(block)
616+
logs := []string{"=================================================="}
617+
logs = append(logs, fmt.Sprintf("api:CreateUHostInstance, request:%v", base.ToQueryMap(req)))
618+
if err != nil {
619+
logs = append(logs, fmt.Sprintf("err:%v", err))
620+
block.Append(base.ParseError(err))
621+
return false, logs
622+
}
623+
if len(bindEipIDs) > 0 && len(bindEipIDs) != count {
624+
block.Append(fmt.Sprintf("expect eip count %d, accept %d", count, len(bindEipIDs)))
625+
return false, logs
626+
}
627+
628+
logs = append(logs, fmt.Sprintf("resp:%#v", resp))
629+
if req.MaxCount == nil {
630+
req.MaxCount = sdk.Int(1)
631+
}
632+
req.MaxCount = sdk.Int(count)
633+
if len(resp.UHostIds) != *req.MaxCount {
634+
block.Append(fmt.Sprintf("expect uhost count %d, accept %d", count, len(resp.UHostIds)))
635+
return false, logs
636+
}
637+
for i, uhostID := range resp.UHostIds {
638+
text := fmt.Sprintf("the uhost[%s]", uhostID)
639+
if len(req.Disks) > 1 {
640+
text = fmt.Sprintf("%s which attached a data disk", text)
641+
if len(req.NetworkInterface) > 0 {
642+
text = fmt.Sprintf("%s and binded an eip", text)
643+
}
644+
} else if len(req.NetworkInterface) > 0 {
645+
text = fmt.Sprintf("%s which binded an eip", text)
646+
}
647+
text = fmt.Sprintf("%s is initializing", text)
648+
649+
if async {
650+
block.Append(text)
651+
} else {
652+
uhostSpoller.Sspoll(resp.UHostIds[0], text, []string{status.HOST_RUNNING, status.HOST_FAIL}, block, &req.CommonBase)
653+
}
654+
bindEipID := bindEipIDs[i]
655+
if bindEipID != "" {
656+
eip := base.PickResourceID(bindEipID)
657+
logs = append(logs, fmt.Sprintf("bind eip: %s", eip))
658+
eipLogs, err := sbindEIP(sdk.String(uhostID), sdk.String("uhost"), &eip, req.ProjectId, req.Region)
659+
logs = append(logs, eipLogs...)
660+
if err != nil {
661+
block.Append(fmt.Sprintf("bind eip[%s] with uhost[%s] failed: %v", eip, uhostID, err))
662+
return false, logs
663+
}
664+
block.Append(fmt.Sprintf("bind eip[%s] with uhost[%s] successfully", eip, uhostID))
665+
} else if len(req.NetworkInterface) > 0 {
666+
ipSet, err := getEIPByUHostId(uhostID)
667+
if err != nil {
668+
block.Append(err.Error())
669+
return false, logs
670+
}
671+
block.Append(fmt.Sprintf("IP:%s Line:%s", ipSet.IP, ipSet.Type))
672+
if *updateEIPReq.Name != "" || *updateEIPReq.Remark != "" {
673+
var message string
674+
if *updateEIPReq.Name != "" && *updateEIPReq.Remark != "" {
675+
message = "name and remark"
676+
} else if *updateEIPReq.Name != "" {
677+
message = "name"
678+
} else {
679+
message = "remark"
680+
}
681+
682+
logs = append(logs, fmt.Sprintf("update attribute %s of eip[%s] binded uhost[%s]", message, ipSet.IPId, uhostID))
683+
updateEIPReq.EIPId = sdk.String(ipSet.IPId)
684+
_, err = base.BizClient.UpdateEIPAttribute(updateEIPReq)
685+
if err != nil {
686+
block.Append(fmt.Sprintf("update attribute %s of eip[%s] binded uhost[%s] got err, %s", message, ipSet.IPId, uhostID, err))
687+
return false, logs
688+
}
689+
block.Append(fmt.Sprintf("update attribute %s of eip[%s] binded uhost[%s] successfully", message, ipSet.IPId, uhostID))
690+
}
691+
}
692+
}
693+
return true, logs
694+
}
695+
580696
func createUhost(req *uhost.CreateUHostInstanceRequest, updateEIPReq *unet.UpdateEIPAttributeRequest, bindEipID string, async bool) (bool, []string) {
581697
resp, err := base.BizClient.CreateUHostInstance(req)
582698
block := ux.NewBlock()

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ require (
88
github.com/sirupsen/logrus v1.3.0
99
github.com/spf13/cobra v0.0.3
1010
github.com/spf13/pflag v1.0.3
11-
github.com/ucloud/ucloud-sdk-go v0.22.17
11+
github.com/ucloud/ucloud-sdk-go v0.22.25
1212
golang.org/x/sys v0.0.0-20220422013727-9388b58f7150
1313
)
1414

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1
5151
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
5252
github.com/ucloud/ucloud-sdk-go v0.22.17 h1:EFn+GxVKS5Tj8hIPie3qL6Zgk25fmWcHqJ06K8wl+Qo=
5353
github.com/ucloud/ucloud-sdk-go v0.22.17/go.mod h1:dyLmFHmUfgb4RZKYQP9IArlvQ2pxzFthfhwxRzOEPIw=
54+
github.com/ucloud/ucloud-sdk-go v0.22.25 h1:ceKeH7WFnpUt9nJSubn+mnxS1iKGrk/Q+HLwa0iYwmQ=
55+
github.com/ucloud/ucloud-sdk-go v0.22.25/go.mod h1:dyLmFHmUfgb4RZKYQP9IArlvQ2pxzFthfhwxRzOEPIw=
5456
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
5557
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
5658
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=

vendor/github.com/ucloud/ucloud-sdk-go/services/udb/apis.go

Lines changed: 67 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/ucloud/ucloud-sdk-go/services/udb/models.go

Lines changed: 37 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)