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"
|
|
|
|
"time"
|
|
|
|
|
2020-02-03 08:55:01 -05:00
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
|
|
|
type StepTerminateServerInstance struct {
|
2020-02-03 08:55:01 -05:00
|
|
|
Conn *NcloudAPIClient
|
2018-01-11 04:57:53 -05:00
|
|
|
TerminateServerInstance func(serverInstanceNo string) error
|
|
|
|
Say func(message string)
|
|
|
|
Error func(e error)
|
|
|
|
}
|
|
|
|
|
2020-02-03 08:55:01 -05:00
|
|
|
func NewStepTerminateServerInstance(conn *NcloudAPIClient, ui packer.Ui) *StepTerminateServerInstance {
|
2018-01-11 04:57:53 -05:00
|
|
|
var step = &StepTerminateServerInstance{
|
|
|
|
Conn: conn,
|
|
|
|
Say: func(message string) { ui.Say(message) },
|
|
|
|
Error: func(e error) { ui.Error(e.Error()) },
|
|
|
|
}
|
|
|
|
|
|
|
|
step.TerminateServerInstance = step.terminateServerInstance
|
|
|
|
|
|
|
|
return step
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepTerminateServerInstance) terminateServerInstance(serverInstanceNo string) error {
|
2020-02-03 08:55:01 -05:00
|
|
|
reqParams := new(server.TerminateServerInstancesRequest)
|
|
|
|
reqParams.ServerInstanceNoList = []*string{&serverInstanceNo}
|
2018-01-11 04:57:53 -05:00
|
|
|
|
2020-02-03 08:55:01 -05:00
|
|
|
_, err := s.Conn.server.V2Api.TerminateServerInstances(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
|
|
|
}
|
|
|
|
|
|
|
|
c1 := make(chan error, 1)
|
|
|
|
|
|
|
|
go func() {
|
2020-02-03 08:55:01 -05:00
|
|
|
reqParams := new(server.GetServerInstanceListRequest)
|
|
|
|
reqParams.ServerInstanceNoList = []*string{&serverInstanceNo}
|
2018-01-11 04:57:53 -05:00
|
|
|
|
|
|
|
for {
|
|
|
|
|
2020-02-03 08:55:01 -05:00
|
|
|
serverInstanceList, err := s.Conn.server.V2Api.GetServerInstanceList(reqParams)
|
2018-01-11 04:57:53 -05:00
|
|
|
if err != nil {
|
2018-01-11 20:12:47 -05:00
|
|
|
c1 <- err
|
2018-01-11 04:57:53 -05:00
|
|
|
return
|
2020-02-03 08:55:01 -05:00
|
|
|
} else if *serverInstanceList.TotalRows == 0 {
|
2018-01-11 04:57:53 -05:00
|
|
|
c1 <- nil
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(time.Second * 3)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case res := <-c1:
|
|
|
|
return res
|
|
|
|
case <-time.After(time.Second * 60):
|
|
|
|
return errors.New("TIMEOUT : Can't terminate server instance")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *StepTerminateServerInstance) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2018-01-11 04:57:53 -05:00
|
|
|
s.Say("Terminate Server Instance")
|
|
|
|
|
|
|
|
var serverInstanceNo = state.Get("InstanceNo").(string)
|
|
|
|
|
|
|
|
err := s.TerminateServerInstance(serverInstanceNo)
|
|
|
|
|
|
|
|
return processStepResult(err, s.Error, state)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*StepTerminateServerInstance) Cleanup(multistep.StateBag) {
|
|
|
|
}
|