packer-cn/builder/ncloud/waiter_block_storage_instan...

82 lines
1.9 KiB
Go
Raw Normal View History

2018-01-11 04:57:53 -05:00
package ncloud
import (
"fmt"
"log"
"time"
"github.com/NaverCloudPlatform/ncloud-sdk-go-v2/services/server"
2018-01-11 04:57:53 -05:00
)
func waiterBlockStorageInstanceStatus(conn *NcloudAPIClient, blockStorageInstanceNo *string, status string, timeout time.Duration) error {
reqParams := new(server.GetBlockStorageInstanceListRequest)
reqParams.BlockStorageInstanceNoList = []*string{blockStorageInstanceNo}
2018-01-11 04:57:53 -05:00
c1 := make(chan error, 1)
go func() {
for {
blockStorageInstanceList, err := conn.server.V2Api.GetBlockStorageInstanceList(reqParams)
2018-01-11 04:57:53 -05:00
if err != nil {
c1 <- err
return
}
if status == "DETAC" && len(blockStorageInstanceList.BlockStorageInstanceList) == 0 {
2018-01-11 04:57:53 -05:00
c1 <- nil
return
}
blockStorageInstance := blockStorageInstanceList.BlockStorageInstanceList[0]
code := blockStorageInstance.BlockStorageInstanceStatus.Code
operationCode := blockStorageInstance.BlockStorageInstanceOperation.Code
2018-01-11 04:57:53 -05:00
if *code == status && *operationCode == "NULL" {
2018-01-11 04:57:53 -05:00
c1 <- nil
return
}
log.Println(blockStorageInstance)
2018-01-11 04:57:53 -05:00
time.Sleep(time.Second * 5)
}
}()
select {
case res := <-c1:
return res
case <-time.After(timeout):
return fmt.Errorf("TIMEOUT : block storage instance status is not changed into status %s", status)
}
}
func waiterDetachedBlockStorageInstance(conn *NcloudAPIClient, serverInstanceNo string, timeout time.Duration) error {
reqParams := new(server.GetBlockStorageInstanceListRequest)
reqParams.ServerInstanceNo = &serverInstanceNo
2018-01-11 04:57:53 -05:00
c1 := make(chan error, 1)
go func() {
for {
blockStorageInstanceList, err := conn.server.V2Api.GetBlockStorageInstanceList(reqParams)
2018-01-11 04:57:53 -05:00
if err != nil {
c1 <- err
return
}
if *blockStorageInstanceList.TotalRows == 1 {
2018-01-11 04:57:53 -05:00
c1 <- nil
return
}
time.Sleep(time.Second * 5)
}
}()
select {
case res := <-c1:
return res
case <-time.After(timeout):
return fmt.Errorf("TIMEOUT : attached block storage instance is not detached")
}
}