2018-01-11 04:57:53 -05:00
|
|
|
package ncloud
|
|
|
|
|
|
|
|
import (
|
2018-01-29 08:41:22 -05:00
|
|
|
"context"
|
2018-01-11 04:57:53 -05:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
|
2020-02-03 08:55:01 -05:00
|
|
|
"github.com/NaverCloudPlatform/ncloud-sdk-go-v2/ncloud"
|
|
|
|
"github.com/NaverCloudPlatform/ncloud-sdk-go-v2/services/server"
|
2018-01-29 08:41:22 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2018-01-11 04:57:53 -05:00
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
)
|
|
|
|
|
|
|
|
// StepCreateBlockStorageInstance struct is for making extra block storage
|
|
|
|
type StepCreateBlockStorageInstance struct {
|
2020-02-03 08:55:01 -05:00
|
|
|
Conn *NcloudAPIClient
|
|
|
|
CreateBlockStorageInstance func(serverInstanceNo string) (*string, error)
|
2018-01-11 04:57:53 -05:00
|
|
|
Say func(message string)
|
|
|
|
Error func(e error)
|
|
|
|
Config *Config
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewStepCreateBlockStorageInstance make StepCreateBlockStorage struct to make extra block storage
|
2020-02-03 08:55:01 -05:00
|
|
|
func NewStepCreateBlockStorageInstance(conn *NcloudAPIClient, ui packer.Ui, config *Config) *StepCreateBlockStorageInstance {
|
2018-01-11 04:57:53 -05:00
|
|
|
var step = &StepCreateBlockStorageInstance{
|
|
|
|
Conn: conn,
|
|
|
|
Say: func(message string) { ui.Say(message) },
|
|
|
|
Error: func(e error) { ui.Error(e.Error()) },
|
|
|
|
Config: config,
|
|
|
|
}
|
|
|
|
|
|
|
|
step.CreateBlockStorageInstance = step.createBlockStorageInstance
|
|
|
|
|
|
|
|
return step
|
|
|
|
}
|
|
|
|
|
2020-02-03 08:55:01 -05:00
|
|
|
func (s *StepCreateBlockStorageInstance) createBlockStorageInstance(serverInstanceNo string) (*string, error) {
|
2018-01-11 04:57:53 -05:00
|
|
|
|
2020-02-03 08:55:01 -05:00
|
|
|
reqParams := new(server.CreateBlockStorageInstanceRequest)
|
|
|
|
reqParams.BlockStorageSize = ncloud.Int64(int64(s.Config.BlockStorageSize))
|
|
|
|
reqParams.ServerInstanceNo = &serverInstanceNo
|
2018-01-11 04:57:53 -05:00
|
|
|
|
2020-02-03 08:55:01 -05:00
|
|
|
resp, err := s.Conn.server.V2Api.CreateBlockStorageInstance(reqParams)
|
2018-01-11 04:57:53 -05:00
|
|
|
if err != nil {
|
2020-02-03 08:55:01 -05:00
|
|
|
return nil, err
|
2018-01-11 04:57:53 -05:00
|
|
|
}
|
|
|
|
|
2020-02-03 08:55:01 -05:00
|
|
|
blockStorageInstance := resp.BlockStorageInstanceList[0]
|
|
|
|
log.Println("Block Storage Instance information : ", blockStorageInstance.BlockStorageInstanceNo)
|
2018-01-11 04:57:53 -05:00
|
|
|
|
2020-02-03 08:55:01 -05:00
|
|
|
if err := waiterBlockStorageInstanceStatus(s.Conn, blockStorageInstance.BlockStorageInstanceNo, "ATTAC", 10*time.Minute); err != nil {
|
|
|
|
return nil, errors.New("TIMEOUT : Block Storage instance status is not attached")
|
2018-01-11 04:57:53 -05:00
|
|
|
}
|
|
|
|
|
2020-02-03 08:55:01 -05:00
|
|
|
return blockStorageInstance.BlockStorageInstanceNo, nil
|
2018-01-11 04:57:53 -05:00
|
|
|
}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *StepCreateBlockStorageInstance) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2018-01-11 04:57:53 -05:00
|
|
|
if s.Config.BlockStorageSize == 0 {
|
|
|
|
return processStepResult(nil, s.Error, state)
|
|
|
|
}
|
|
|
|
|
|
|
|
s.Say("Create extra block storage instance")
|
|
|
|
|
|
|
|
serverInstanceNo := state.Get("InstanceNo").(string)
|
|
|
|
|
|
|
|
blockStorageInstanceNo, err := s.CreateBlockStorageInstance(serverInstanceNo)
|
|
|
|
if err == nil {
|
2020-02-03 08:55:01 -05:00
|
|
|
state.Put("BlockStorageInstanceNo", *blockStorageInstanceNo)
|
2018-01-11 04:57:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return processStepResult(err, s.Error, state)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepCreateBlockStorageInstance) Cleanup(state multistep.StateBag) {
|
|
|
|
_, cancelled := state.GetOk(multistep.StateCancelled)
|
|
|
|
_, halted := state.GetOk(multistep.StateHalted)
|
|
|
|
|
|
|
|
if !cancelled && !halted {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.Config.BlockStorageSize == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if blockStorageInstanceNo, ok := state.GetOk("BlockStorageInstanceNo"); ok {
|
|
|
|
s.Say("Clean up Block Storage Instance")
|
2020-02-03 08:55:01 -05:00
|
|
|
reqParams := server.DeleteBlockStorageInstancesRequest{
|
|
|
|
BlockStorageInstanceNoList: []*string{blockStorageInstanceNo.(*string)},
|
|
|
|
}
|
|
|
|
blockStorageInstanceList, err := s.Conn.server.V2Api.DeleteBlockStorageInstances(&reqParams)
|
2018-01-11 04:57:53 -05:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-03 08:55:01 -05:00
|
|
|
s.Say(fmt.Sprintf("Block Storage Instance is deleted. Block Storage InstanceNo is %s", blockStorageInstanceNo.(string)))
|
|
|
|
log.Println("Block Storage Instance information : ", blockStorageInstanceList.BlockStorageInstanceList[0])
|
2018-01-11 04:57:53 -05:00
|
|
|
|
2020-02-03 08:55:01 -05:00
|
|
|
if err := waiterBlockStorageInstanceStatus(s.Conn, blockStorageInstanceNo.(*string), "DETAC", time.Minute); err != nil {
|
2018-01-11 04:57:53 -05:00
|
|
|
s.Say("TIMEOUT : Block Storage instance status is not deattached")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|