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
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"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 StepStopServerInstance struct {
|
2020-02-03 08:55:01 -05:00
|
|
|
Conn *NcloudAPIClient
|
2018-01-11 04:57:53 -05:00
|
|
|
StopServerInstance func(serverInstanceNo string) error
|
|
|
|
Say func(message string)
|
|
|
|
Error func(e error)
|
|
|
|
}
|
|
|
|
|
2020-02-03 08:55:01 -05:00
|
|
|
func NewStepStopServerInstance(conn *NcloudAPIClient, ui packer.Ui) *StepStopServerInstance {
|
2018-01-11 04:57:53 -05:00
|
|
|
var step = &StepStopServerInstance{
|
|
|
|
Conn: conn,
|
|
|
|
Say: func(message string) { ui.Say(message) },
|
|
|
|
Error: func(e error) { ui.Error(e.Error()) },
|
|
|
|
}
|
|
|
|
|
|
|
|
step.StopServerInstance = step.stopServerInstance
|
|
|
|
|
|
|
|
return step
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepStopServerInstance) stopServerInstance(serverInstanceNo string) error {
|
2020-02-03 08:55:01 -05:00
|
|
|
reqParams := new(server.StopServerInstancesRequest)
|
|
|
|
reqParams.ServerInstanceNoList = []*string{&serverInstanceNo}
|
2018-01-11 04:57:53 -05:00
|
|
|
|
2020-02-03 08:55:01 -05:00
|
|
|
serverInstanceList, err := s.Conn.server.V2Api.StopServerInstances(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("Server Instance is stopping. Server InstanceNo is %s", *serverInstanceList.ServerInstanceList[0].ServerInstanceNo))
|
2018-01-11 04:57:53 -05:00
|
|
|
log.Println("Server Instance information : ", serverInstanceList.ServerInstanceList[0])
|
|
|
|
|
|
|
|
if err := waiterServerInstanceStatus(s.Conn, serverInstanceNo, "NSTOP", 5*time.Minute); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-02-03 08:55:01 -05:00
|
|
|
s.Say(fmt.Sprintf("Server Instance stopped. Server InstanceNo is %s", *serverInstanceList.ServerInstanceList[0].ServerInstanceNo))
|
2018-01-11 04:57:53 -05:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *StepStopServerInstance) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2018-01-11 04:57:53 -05:00
|
|
|
s.Say("Stop Server Instance")
|
|
|
|
|
|
|
|
var serverInstanceNo = state.Get("InstanceNo").(string)
|
|
|
|
|
|
|
|
err := s.StopServerInstance(serverInstanceNo)
|
|
|
|
|
|
|
|
return processStepResult(err, s.Error, state)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*StepStopServerInstance) Cleanup(multistep.StateBag) {
|
|
|
|
}
|