github.phpd.cn/hashicorp/packer@v1.3.2/builder/ncloud/step_delete_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 type StepDeleteBlockStorageInstance struct { 16 Conn *ncloud.Conn 17 DeleteBlockStorageInstance func(blockStorageInstanceNo string) error 18 Say func(message string) 19 Error func(e error) 20 Config *Config 21 } 22 23 func NewStepDeleteBlockStorageInstance(conn *ncloud.Conn, ui packer.Ui, config *Config) *StepDeleteBlockStorageInstance { 24 var step = &StepDeleteBlockStorageInstance{ 25 Conn: conn, 26 Say: func(message string) { ui.Say(message) }, 27 Error: func(e error) { ui.Error(e.Error()) }, 28 Config: config, 29 } 30 31 step.DeleteBlockStorageInstance = step.deleteBlockStorageInstance 32 33 return step 34 } 35 36 func (s *StepDeleteBlockStorageInstance) getBlockInstanceList(serverInstanceNo string) []string { 37 reqParams := new(ncloud.RequestBlockStorageInstanceList) 38 reqParams.ServerInstanceNo = serverInstanceNo 39 40 blockStorageInstanceList, err := s.Conn.GetBlockStorageInstance(reqParams) 41 if err != nil { 42 return nil 43 } 44 45 if blockStorageInstanceList.TotalRows == 1 { 46 return nil 47 } 48 49 var instanceList []string 50 51 for _, blockStorageInstance := range blockStorageInstanceList.BlockStorageInstance { 52 log.Println(blockStorageInstance) 53 if blockStorageInstance.BlockStorageType.Code != "BASIC" { 54 instanceList = append(instanceList, blockStorageInstance.BlockStorageInstanceNo) 55 } 56 } 57 58 return instanceList 59 } 60 61 func (s *StepDeleteBlockStorageInstance) deleteBlockStorageInstance(serverInstanceNo string) error { 62 blockStorageInstanceList := s.getBlockInstanceList(serverInstanceNo) 63 if blockStorageInstanceList == nil || len(blockStorageInstanceList) == 0 { 64 return nil 65 } 66 67 _, err := s.Conn.DeleteBlockStorageInstances(blockStorageInstanceList) 68 if err != nil { 69 return err 70 } 71 72 s.Say(fmt.Sprintf("Block Storage Instance is deleted. Block Storage InstanceNo is %s", blockStorageInstanceList)) 73 74 if err := waiterDetachedBlockStorageInstance(s.Conn, serverInstanceNo, time.Minute); err != nil { 75 return errors.New("TIMEOUT : Block Storage instance status is not deattached") 76 } 77 78 return nil 79 } 80 81 func (s *StepDeleteBlockStorageInstance) Run(_ context.Context, state multistep.StateBag) multistep.StepAction { 82 if s.Config.BlockStorageSize == 0 { 83 return processStepResult(nil, s.Error, state) 84 } 85 86 s.Say("Delete Block Storage Instance") 87 88 var serverInstanceNo = state.Get("InstanceNo").(string) 89 90 err := s.DeleteBlockStorageInstance(serverInstanceNo) 91 92 return processStepResult(err, s.Error, state) 93 } 94 95 func (*StepDeleteBlockStorageInstance) Cleanup(multistep.StateBag) { 96 }