forked from hashicorp/packer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
step_verify_box.go
71 lines (55 loc) · 1.63 KB
/
step_verify_box.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package vagrantcloud
import (
"fmt"
"github.com/mitchellh/multistep"
"github.com/mitchellh/packer/packer"
)
type Box struct {
Tag string `json:"tag"`
Versions []*Version `json:"versions"`
}
func (b *Box) HasVersion(version string) (bool, *Version) {
for _, v := range b.Versions {
if v.Version == version {
return true, v
}
}
return false, nil
}
type stepVerifyBox struct {
}
func (s *stepVerifyBox) Run(state multistep.StateBag) multistep.StepAction {
client := state.Get("client").(*VagrantCloudClient)
ui := state.Get("ui").(packer.Ui)
config := state.Get("config").(Config)
ui.Say(fmt.Sprintf("Verifying box is accessible: %s", config.Tag))
path := fmt.Sprintf("box/%s", config.Tag)
resp, err := client.Get(path)
if err != nil {
state.Put("error", fmt.Errorf("Error retrieving box: %s", err))
return multistep.ActionHalt
}
if resp.StatusCode != 200 {
cloudErrors := &VagrantCloudErrors{}
err = decodeBody(resp, cloudErrors)
state.Put("error", fmt.Errorf("Error retrieving box: %s", cloudErrors.FormatErrors()))
return multistep.ActionHalt
}
box := &Box{}
if err = decodeBody(resp, box); err != nil {
state.Put("error", fmt.Errorf("Error parsing box response: %s", err))
return multistep.ActionHalt
}
if box.Tag != config.Tag {
state.Put("error", fmt.Errorf("Could not verify box: %s", config.Tag))
return multistep.ActionHalt
}
ui.Message("Box accessible and matches tag")
// Keep the box in state for later
state.Put("box", box)
// Box exists and is accessible
return multistep.ActionContinue
}
func (s *stepVerifyBox) Cleanup(state multistep.StateBag) {
// no cleanup needed
}