github.phpd.cn/hashicorp/packer@v1.3.2/builder/ncloud/step_stop_server_instance.go (about)

     1  package ncloud
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"log"
     7  	"time"
     8  
     9  	ncloud "github.com/NaverCloudPlatform/ncloud-sdk-go/sdk"
    10  	"github.com/hashicorp/packer/helper/multistep"
    11  	"github.com/hashicorp/packer/packer"
    12  )
    13  
    14  type StepStopServerInstance struct {
    15  	Conn               *ncloud.Conn
    16  	StopServerInstance func(serverInstanceNo string) error
    17  	Say                func(message string)
    18  	Error              func(e error)
    19  }
    20  
    21  func NewStepStopServerInstance(conn *ncloud.Conn, ui packer.Ui) *StepStopServerInstance {
    22  	var step = &StepStopServerInstance{
    23  		Conn:  conn,
    24  		Say:   func(message string) { ui.Say(message) },
    25  		Error: func(e error) { ui.Error(e.Error()) },
    26  	}
    27  
    28  	step.StopServerInstance = step.stopServerInstance
    29  
    30  	return step
    31  }
    32  
    33  func (s *StepStopServerInstance) stopServerInstance(serverInstanceNo string) error {
    34  	reqParams := new(ncloud.RequestStopServerInstances)
    35  	reqParams.ServerInstanceNoList = []string{serverInstanceNo}
    36  
    37  	serverInstanceList, err := s.Conn.StopServerInstances(reqParams)
    38  	if err != nil {
    39  		return err
    40  	}
    41  
    42  	s.Say(fmt.Sprintf("Server Instance is stopping. Server InstanceNo is %s", serverInstanceList.ServerInstanceList[0].ServerInstanceNo))
    43  	log.Println("Server Instance information : ", serverInstanceList.ServerInstanceList[0])
    44  
    45  	if err := waiterServerInstanceStatus(s.Conn, serverInstanceNo, "NSTOP", 5*time.Minute); err != nil {
    46  		return err
    47  	}
    48  
    49  	s.Say(fmt.Sprintf("Server Instance stopped. Server InstanceNo is %s", serverInstanceList.ServerInstanceList[0].ServerInstanceNo))
    50  
    51  	return nil
    52  }
    53  
    54  func (s *StepStopServerInstance) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
    55  	s.Say("Stop Server Instance")
    56  
    57  	var serverInstanceNo = state.Get("InstanceNo").(string)
    58  
    59  	err := s.StopServerInstance(serverInstanceNo)
    60  
    61  	return processStepResult(err, s.Error, state)
    62  }
    63  
    64  func (*StepStopServerInstance) Cleanup(multistep.StateBag) {
    65  }