github.phpd.cn/hashicorp/packer@v1.3.2/builder/ncloud/step_create_block_storage_instance.go (about) 1 package ncloud 2 3 import ( 4 "context" 5 "errors" 6 "fmt" 7 "log" 8 "time" 9 10 ncloud "github.com/NaverCloudPlatform/ncloud-sdk-go/sdk" 11 "github.com/hashicorp/packer/helper/multistep" 12 "github.com/hashicorp/packer/packer" 13 ) 14 15 // StepCreateBlockStorageInstance struct is for making extra block storage 16 type StepCreateBlockStorageInstance struct { 17 Conn *ncloud.Conn 18 CreateBlockStorageInstance func(serverInstanceNo string) (string, error) 19 Say func(message string) 20 Error func(e error) 21 Config *Config 22 } 23 24 // NewStepCreateBlockStorageInstance make StepCreateBlockStorage struct to make extra block storage 25 func NewStepCreateBlockStorageInstance(conn *ncloud.Conn, ui packer.Ui, config *Config) *StepCreateBlockStorageInstance { 26 var step = &StepCreateBlockStorageInstance{ 27 Conn: conn, 28 Say: func(message string) { ui.Say(message) }, 29 Error: func(e error) { ui.Error(e.Error()) }, 30 Config: config, 31 } 32 33 step.CreateBlockStorageInstance = step.createBlockStorageInstance 34 35 return step 36 } 37 38 func (s *StepCreateBlockStorageInstance) createBlockStorageInstance(serverInstanceNo string) (string, error) { 39 40 reqParams := new(ncloud.RequestBlockStorageInstance) 41 reqParams.BlockStorageSize = s.Config.BlockStorageSize 42 reqParams.ServerInstanceNo = serverInstanceNo 43 44 blockStorageInstanceList, err := s.Conn.CreateBlockStorageInstance(reqParams) 45 if err != nil { 46 return "", err 47 } 48 49 log.Println("Block Storage Instance information : ", blockStorageInstanceList.BlockStorageInstance[0]) 50 51 if err := waiterBlockStorageInstanceStatus(s.Conn, blockStorageInstanceList.BlockStorageInstance[0].BlockStorageInstanceNo, "ATTAC", 10*time.Minute); err != nil { 52 return "", errors.New("TIMEOUT : Block Storage instance status is not attached") 53 } 54 55 return blockStorageInstanceList.BlockStorageInstance[0].BlockStorageInstanceNo, nil 56 } 57 58 func (s *StepCreateBlockStorageInstance) Run(_ context.Context, state multistep.StateBag) multistep.StepAction { 59 if s.Config.BlockStorageSize == 0 { 60 return processStepResult(nil, s.Error, state) 61 } 62 63 s.Say("Create extra block storage instance") 64 65 serverInstanceNo := state.Get("InstanceNo").(string) 66 67 blockStorageInstanceNo, err := s.CreateBlockStorageInstance(serverInstanceNo) 68 if err == nil { 69 state.Put("BlockStorageInstanceNo", blockStorageInstanceNo) 70 } 71 72 return processStepResult(err, s.Error, state) 73 } 74 75 func (s *StepCreateBlockStorageInstance) Cleanup(state multistep.StateBag) { 76 _, cancelled := state.GetOk(multistep.StateCancelled) 77 _, halted := state.GetOk(multistep.StateHalted) 78 79 if !cancelled && !halted { 80 return 81 } 82 83 if s.Config.BlockStorageSize == 0 { 84 return 85 } 86 87 if blockStorageInstanceNo, ok := state.GetOk("BlockStorageInstanceNo"); ok { 88 s.Say("Clean up Block Storage Instance") 89 no := blockStorageInstanceNo.(string) 90 blockStorageInstanceList, err := s.Conn.DeleteBlockStorageInstances([]string{no}) 91 if err != nil { 92 return 93 } 94 95 s.Say(fmt.Sprintf("Block Storage Instance is deleted. Block Storage InstanceNo is %s", no)) 96 log.Println("Block Storage Instance information : ", blockStorageInstanceList.BlockStorageInstance[0]) 97 98 if err := waiterBlockStorageInstanceStatus(s.Conn, no, "DETAC", time.Minute); err != nil { 99 s.Say("TIMEOUT : Block Storage instance status is not deattached") 100 } 101 } 102 }