github.com/vijayrajah/packer@v1.3.2/post-processor/vagrant-cloud/step_verify_box.go (about) 1 package vagrantcloud 2 3 import ( 4 "context" 5 "fmt" 6 7 "github.com/hashicorp/packer/helper/multistep" 8 "github.com/hashicorp/packer/packer" 9 ) 10 11 type Box struct { 12 Tag string `json:"tag"` 13 Versions []*Version `json:"versions"` 14 } 15 16 func (b *Box) HasVersion(version string) (bool, *Version) { 17 for _, v := range b.Versions { 18 if v.Version == version { 19 return true, v 20 } 21 } 22 return false, nil 23 } 24 25 type stepVerifyBox struct { 26 } 27 28 func (s *stepVerifyBox) Run(_ context.Context, state multistep.StateBag) multistep.StepAction { 29 client := state.Get("client").(*VagrantCloudClient) 30 ui := state.Get("ui").(packer.Ui) 31 config := state.Get("config").(Config) 32 33 ui.Say(fmt.Sprintf("Verifying box is accessible: %s", config.Tag)) 34 35 path := fmt.Sprintf("box/%s", config.Tag) 36 resp, err := client.Get(path) 37 38 if err != nil { 39 state.Put("error", fmt.Errorf("Error retrieving box: %s", err)) 40 return multistep.ActionHalt 41 } 42 43 if resp.StatusCode != 200 { 44 cloudErrors := &VagrantCloudErrors{} 45 err = decodeBody(resp, cloudErrors) 46 state.Put("error", fmt.Errorf("Error retrieving box: %s", cloudErrors.FormatErrors())) 47 return multistep.ActionHalt 48 } 49 50 box := &Box{} 51 52 if err = decodeBody(resp, box); err != nil { 53 state.Put("error", fmt.Errorf("Error parsing box response: %s", err)) 54 return multistep.ActionHalt 55 } 56 57 if box.Tag != config.Tag { 58 state.Put("error", fmt.Errorf("Could not verify box: %s", config.Tag)) 59 return multistep.ActionHalt 60 } 61 62 ui.Message("Box accessible and matches tag") 63 64 // Keep the box in state for later 65 state.Put("box", box) 66 67 // Box exists and is accessible 68 return multistep.ActionContinue 69 } 70 71 func (s *stepVerifyBox) Cleanup(state multistep.StateBag) { 72 // no cleanup needed 73 }