/
check_target_image.go
54 lines (42 loc) · 1.61 KB
/
check_target_image.go
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
package steps
import (
"fmt"
"github.com/DimensionDataResearch/packer-plugins-ddcloud/builders/customerimage/config"
"github.com/DimensionDataResearch/packer-plugins-ddcloud/helpers"
"github.com/mitchellh/multistep"
)
// CheckTargetImage is the step that ensures the target image does not already exist in CloudControl.
type CheckTargetImage struct{}
// Run is called to perform the step's action.
//
// The return value determines whether multi-step sequences should continue or halt.
func (step *CheckTargetImage) Run(stateBag multistep.StateBag) multistep.StepAction {
state := helpers.ForStateBag(stateBag)
ui := state.GetUI()
settings := state.GetSettings().(*config.Settings)
client := state.GetClient()
targetImage, err := client.FindCustomerImage(settings.TargetImage, settings.DatacenterID)
if err != nil {
ui.Error(err.Error())
return multistep.ActionHalt
}
if targetImage != nil {
ui.Error(fmt.Sprintf(
"Target image '%s' already exists in datacenter '%s'.",
settings.TargetImage,
settings.DatacenterID,
))
return multistep.ActionHalt
}
return multistep.ActionContinue
}
// Cleanup is called in reverse order of the steps that have run
// and allow steps to clean up after themselves. Do not assume if this
// ran that the entire multi-step sequence completed successfully. This
// method can be ran in the face of errors and cancellations as well.
//
// The parameter is the same "state bag" as Run, and represents the
// state at the latest possible time prior to calling Cleanup.
func (step *CheckTargetImage) Cleanup(state multistep.StateBag) {
}
var _ multistep.Step = &CheckTargetImage{}