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/services/server"
|
2018-01-11 04:57:53 -05:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2020-11-17 19:31:03 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/multistep"
|
2018-01-11 04:57:53 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type StepDeleteBlockStorageInstance struct {
|
2020-02-03 08:55:01 -05:00
|
|
|
Conn *NcloudAPIClient
|
2018-01-11 04:57:53 -05:00
|
|
|
DeleteBlockStorageInstance func(blockStorageInstanceNo string) error
|
|
|
|
Say func(message string)
|
|
|
|
Error func(e error)
|
|
|
|
Config *Config
|
|
|
|
}
|
|
|
|
|
2020-02-03 08:55:01 -05:00
|
|
|
func NewStepDeleteBlockStorageInstance(conn *NcloudAPIClient, ui packer.Ui, config *Config) *StepDeleteBlockStorageInstance {
|
2018-01-11 04:57:53 -05:00
|
|
|
var step = &StepDeleteBlockStorageInstance{
|
|
|
|
Conn: conn,
|
|
|
|
Say: func(message string) { ui.Say(message) },
|
|
|
|
Error: func(e error) { ui.Error(e.Error()) },
|
|
|
|
Config: config,
|
|
|
|
}
|
|
|
|
|
|
|
|
step.DeleteBlockStorageInstance = step.deleteBlockStorageInstance
|
|
|
|
|
|
|
|
return step
|
|
|
|
}
|
|
|
|
|
2020-02-03 08:55:01 -05:00
|
|
|
func (s *StepDeleteBlockStorageInstance) getBlockInstanceList(serverInstanceNo string) []*string {
|
|
|
|
reqParams := new(server.GetBlockStorageInstanceListRequest)
|
|
|
|
reqParams.ServerInstanceNo = &serverInstanceNo
|
2018-01-11 04:57:53 -05:00
|
|
|
|
2020-02-03 08:55:01 -05:00
|
|
|
blockStorageInstanceList, err := s.Conn.server.V2Api.GetBlockStorageInstanceList(reqParams)
|
2018-01-11 04:57:53 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-03 08:55:01 -05:00
|
|
|
if *blockStorageInstanceList.TotalRows == 1 {
|
2018-01-11 04:57:53 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-03 08:55:01 -05:00
|
|
|
var instanceList []*string
|
2018-01-11 04:57:53 -05:00
|
|
|
|
2020-02-03 08:55:01 -05:00
|
|
|
for _, blockStorageInstance := range blockStorageInstanceList.BlockStorageInstanceList {
|
2018-01-11 04:57:53 -05:00
|
|
|
log.Println(blockStorageInstance)
|
2020-02-03 08:55:01 -05:00
|
|
|
if *blockStorageInstance.BlockStorageType.Code != "BASIC" {
|
2018-01-11 04:57:53 -05:00
|
|
|
instanceList = append(instanceList, blockStorageInstance.BlockStorageInstanceNo)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return instanceList
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepDeleteBlockStorageInstance) deleteBlockStorageInstance(serverInstanceNo string) error {
|
|
|
|
blockStorageInstanceList := s.getBlockInstanceList(serverInstanceNo)
|
|
|
|
if blockStorageInstanceList == nil || len(blockStorageInstanceList) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2020-02-03 08:55:01 -05:00
|
|
|
reqParams := server.DeleteBlockStorageInstancesRequest{
|
|
|
|
BlockStorageInstanceNoList: blockStorageInstanceList,
|
|
|
|
}
|
|
|
|
_, err := s.Conn.server.V2Api.DeleteBlockStorageInstances(&reqParams)
|
2018-01-11 04:57:53 -05:00
|
|
|
if err != nil {
|
2018-01-11 20:12:47 -05:00
|
|
|
return err
|
2018-01-11 04:57:53 -05:00
|
|
|
}
|
|
|
|
|
2020-02-03 08:55:01 -05:00
|
|
|
s.Say(fmt.Sprintf("Block Storage Instance is deleted. Block Storage Instance List is %v", blockStorageInstanceList))
|
2018-01-11 04:57:53 -05:00
|
|
|
|
|
|
|
if err := waiterDetachedBlockStorageInstance(s.Conn, serverInstanceNo, time.Minute); err != nil {
|
|
|
|
return errors.New("TIMEOUT : Block Storage instance status is not deattached")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *StepDeleteBlockStorageInstance) 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("Delete Block Storage Instance")
|
|
|
|
|
|
|
|
var serverInstanceNo = state.Get("InstanceNo").(string)
|
|
|
|
|
|
|
|
err := s.DeleteBlockStorageInstance(serverInstanceNo)
|
|
|
|
|
|
|
|
return processStepResult(err, s.Error, state)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*StepDeleteBlockStorageInstance) Cleanup(multistep.StateBag) {
|
|
|
|
}
|