github.com/marksheahan/packer@v0.10.2-0.20160613200515-1acb2d6645a0/post-processor/vagrant-cloud/step_verify_box.go (about)

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