-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathstep_shutdown_instance.go
More file actions
50 lines (39 loc) · 1.46 KB
/
step_shutdown_instance.go
File metadata and controls
50 lines (39 loc) · 1.46 KB
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
// Copyright IBM Corp. 2016, 2025
// SPDX-License-Identifier: MPL-2.0
package cloudstack
import (
"context"
"fmt"
"github.com/apache/cloudstack-go/v2/cloudstack"
"github.com/hashicorp/packer-plugin-sdk/multistep"
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
)
type stepShutdownInstance struct{}
func (s *stepShutdownInstance) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
client := state.Get("client").(*cloudstack.CloudStackClient)
config := state.Get("config").(*Config)
ui := state.Get("ui").(packersdk.Ui)
ui.Say("Shutting down instance...")
// Retrieve the instance ID from the previously saved state.
instanceID, ok := state.Get("instance_id").(string)
if !ok || instanceID == "" {
state.Put("error", fmt.Errorf("Could not retrieve instance_id from state!"))
return multistep.ActionHalt
}
// Create a new parameter struct.
p := client.VirtualMachine.NewStopVirtualMachineParams(instanceID)
// Shutdown the virtual machine.
_, err := client.VirtualMachine.StopVirtualMachine(p)
if err != nil {
err := fmt.Errorf("Error shutting down instance %s: %s", config.InstanceName, err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
ui.Message("Instance has been shutdown!")
return multistep.ActionContinue
}
// Cleanup any resources that may have been created during the Run phase.
func (s *stepShutdownInstance) Cleanup(state multistep.StateBag) {
// Nothing to cleanup for this step.
}