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"
|
|
|
|
"time"
|
|
|
|
|
2020-02-03 08:55:01 -05:00
|
|
|
"github.com/NaverCloudPlatform/ncloud-sdk-go-v2/services/server"
|
2021-04-05 22:40:41 -04:00
|
|
|
"github.com/NaverCloudPlatform/ncloud-sdk-go-v2/services/vserver"
|
2020-12-17 16:29:25 -05:00
|
|
|
"github.com/hashicorp/packer-plugin-sdk/multistep"
|
|
|
|
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
2018-01-11 04:57:53 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type StepStopServerInstance struct {
|
2021-03-23 03:35:36 -04:00
|
|
|
Conn *NcloudAPIClient
|
|
|
|
StopServerInstance func(serverInstanceNo string) error
|
|
|
|
WaiterServerInstanceStatus func(conn *NcloudAPIClient, serverInstanceNo string, status string, timeout time.Duration) error
|
|
|
|
Say func(message string)
|
|
|
|
Error func(e error)
|
|
|
|
Config *Config
|
2018-01-11 04:57:53 -05:00
|
|
|
}
|
|
|
|
|
2021-03-23 03:35:36 -04:00
|
|
|
func NewStepStopServerInstance(conn *NcloudAPIClient, ui packersdk.Ui, config *Config) *StepStopServerInstance {
|
2018-01-11 04:57:53 -05:00
|
|
|
var step = &StepStopServerInstance{
|
2021-03-23 03:35:36 -04:00
|
|
|
Conn: conn,
|
|
|
|
Say: func(message string) { ui.Say(message) },
|
|
|
|
Error: func(e error) { ui.Error(e.Error()) },
|
|
|
|
Config: config,
|
2018-01-11 04:57:53 -05:00
|
|
|
}
|
|
|
|
|
2021-03-23 03:35:36 -04:00
|
|
|
if config.SupportVPC {
|
|
|
|
step.StopServerInstance = step.stopVpcServerInstance
|
|
|
|
step.WaiterServerInstanceStatus = waiterVpcServerInstanceStatus
|
|
|
|
} else {
|
|
|
|
step.StopServerInstance = step.stopClassicServerInstance
|
|
|
|
step.WaiterServerInstanceStatus = waiterClassicServerInstanceStatus
|
|
|
|
}
|
2018-01-11 04:57:53 -05:00
|
|
|
|
|
|
|
return step
|
|
|
|
}
|
|
|
|
|
2021-03-23 03:35:36 -04:00
|
|
|
func (s *StepStopServerInstance) stopClassicServerInstance(serverInstanceNo string) error {
|
|
|
|
reqParams := &server.StopServerInstancesRequest{
|
|
|
|
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
|
|
|
|
2021-03-23 03:35:36 -04:00
|
|
|
if err := s.WaiterServerInstanceStatus(s.Conn, serverInstanceNo, ServerInstanceStatusStopped, 5*time.Minute); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
s.Say(fmt.Sprintf("Server Instance stopped. Server InstanceNo is %s", *serverInstanceList.ServerInstanceList[0].ServerInstanceNo))
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepStopServerInstance) stopVpcServerInstance(serverInstanceNo string) error {
|
|
|
|
reqParams := &vserver.StopServerInstancesRequest{
|
|
|
|
RegionCode: &s.Config.RegionCode,
|
|
|
|
ServerInstanceNoList: []*string{&serverInstanceNo},
|
|
|
|
}
|
|
|
|
|
|
|
|
serverInstanceList, err := s.Conn.vserver.V2Api.StopServerInstances(reqParams)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
s.Say(fmt.Sprintf("Server Instance is stopping. Server InstanceNo is %s", *serverInstanceList.ServerInstanceList[0].ServerInstanceNo))
|
|
|
|
|
|
|
|
if err := s.WaiterServerInstanceStatus(s.Conn, serverInstanceNo, ServerInstanceStatusStopped, 5*time.Minute); err != nil {
|
2018-01-11 04:57:53 -05:00
|
|
|
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")
|
|
|
|
|
2021-03-23 03:35:36 -04:00
|
|
|
var serverInstanceNo = state.Get("instance_no").(string)
|
2018-01-11 04:57:53 -05:00
|
|
|
|
|
|
|
err := s.StopServerInstance(serverInstanceNo)
|
|
|
|
|
|
|
|
return processStepResult(err, s.Error, state)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*StepStopServerInstance) Cleanup(multistep.StateBag) {
|
|
|
|
}
|