2014-06-23 15:48:51 -04:00
|
|
|
package vagrantcloud
|
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2014-06-23 15:48:51 -04:00
|
|
|
"fmt"
|
2018-01-22 18:32:33 -05:00
|
|
|
|
2020-11-17 19:31:03 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/multistep"
|
2020-11-19 14:54:31 -05:00
|
|
|
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
|
2014-06-23 15:48:51 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type Box struct {
|
2014-06-24 15:58:45 -04:00
|
|
|
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
|
2014-06-23 15:48:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type stepVerifyBox struct {
|
|
|
|
}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *stepVerifyBox) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2014-06-23 15:48:51 -04:00
|
|
|
client := state.Get("client").(*VagrantCloudClient)
|
2020-11-19 14:54:31 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2019-12-19 14:21:51 -05:00
|
|
|
config := state.Get("config").(*Config)
|
2014-06-23 15:48:51 -04:00
|
|
|
|
|
|
|
ui.Say(fmt.Sprintf("Verifying box is accessible: %s", config.Tag))
|
|
|
|
|
|
|
|
path := fmt.Sprintf("box/%s", config.Tag)
|
|
|
|
resp, err := client.Get(path)
|
|
|
|
|
2014-08-12 12:11:27 -04:00
|
|
|
if err != nil {
|
|
|
|
state.Put("error", fmt.Errorf("Error retrieving box: %s", err))
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode != 200 {
|
2014-06-25 11:29:25 -04:00
|
|
|
cloudErrors := &VagrantCloudErrors{}
|
|
|
|
err = decodeBody(resp, cloudErrors)
|
2019-09-25 19:58:26 -04:00
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("error decoding error response: %s", err))
|
|
|
|
}
|
2014-06-25 11:29:25 -04:00
|
|
|
state.Put("error", fmt.Errorf("Error retrieving box: %s", cloudErrors.FormatErrors()))
|
2014-06-23 15:48:51 -04:00
|
|
|
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 {
|
2014-06-25 11:29:25 -04:00
|
|
|
state.Put("error", fmt.Errorf("Could not verify box: %s", config.Tag))
|
2014-06-23 15:48:51 -04:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2014-06-25 12:46:25 -04:00
|
|
|
ui.Message("Box accessible and matches tag")
|
|
|
|
|
2014-06-23 15:48:51 -04:00
|
|
|
// 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
|
|
|
|
}
|