File: step_stop_server_instance.go

package info (click to toggle)
packer 1.6.6%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 32,016 kB
  • sloc: sh: 1,154; python: 619; makefile: 251; ruby: 205; xml: 97
file content (65 lines) | stat: -rw-r--r-- 1,947 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package ncloud

import (
	"context"
	"fmt"
	"log"
	"time"

	"github.com/NaverCloudPlatform/ncloud-sdk-go-v2/services/server"
	"github.com/hashicorp/packer/packer-plugin-sdk/multistep"
	packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
)

type StepStopServerInstance struct {
	Conn               *NcloudAPIClient
	StopServerInstance func(serverInstanceNo string) error
	Say                func(message string)
	Error              func(e error)
}

func NewStepStopServerInstance(conn *NcloudAPIClient, ui packersdk.Ui) *StepStopServerInstance {
	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 {
	reqParams := new(server.StopServerInstancesRequest)
	reqParams.ServerInstanceNoList = []*string{&serverInstanceNo}

	serverInstanceList, err := s.Conn.server.V2Api.StopServerInstances(reqParams)
	if err != nil {
		return err
	}

	s.Say(fmt.Sprintf("Server Instance is stopping. Server InstanceNo is %s", *serverInstanceList.ServerInstanceList[0].ServerInstanceNo))
	log.Println("Server Instance information : ", serverInstanceList.ServerInstanceList[0])

	if err := waiterServerInstanceStatus(s.Conn, serverInstanceNo, "NSTOP", 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) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
	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) {
}