github.com/rothwerx/packer@v0.9.0/post-processor/vagrant-cloud/step_create_version.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 Version struct {
    10  	Version     string `json:"version"`
    11  	Description string `json:"description,omitempty"`
    12  }
    13  
    14  type stepCreateVersion struct {
    15  }
    16  
    17  func (s *stepCreateVersion) Run(state multistep.StateBag) multistep.StepAction {
    18  	client := state.Get("client").(*VagrantCloudClient)
    19  	ui := state.Get("ui").(packer.Ui)
    20  	config := state.Get("config").(Config)
    21  	box := state.Get("box").(*Box)
    22  
    23  	ui.Say(fmt.Sprintf("Creating version: %s", config.Version))
    24  
    25  	if hasVersion, v := box.HasVersion(config.Version); hasVersion {
    26  		ui.Message(fmt.Sprintf("Version exists, skipping creation"))
    27  		state.Put("version", v)
    28  		return multistep.ActionContinue
    29  	}
    30  
    31  	path := fmt.Sprintf("box/%s/versions", box.Tag)
    32  
    33  	version := &Version{Version: config.Version, Description: config.VersionDescription}
    34  
    35  	// Wrap the version in a version object for the API
    36  	wrapper := make(map[string]interface{})
    37  	wrapper["version"] = version
    38  
    39  	resp, err := client.Post(path, wrapper)
    40  
    41  	if err != nil || (resp.StatusCode != 200) {
    42  		cloudErrors := &VagrantCloudErrors{}
    43  		err = decodeBody(resp, cloudErrors)
    44  		state.Put("error", fmt.Errorf("Error creating version: %s", cloudErrors.FormatErrors()))
    45  		return multistep.ActionHalt
    46  	}
    47  
    48  	if err = decodeBody(resp, version); err != nil {
    49  		state.Put("error", fmt.Errorf("Error parsing version response: %s", err))
    50  		return multistep.ActionHalt
    51  	}
    52  
    53  	state.Put("version", version)
    54  
    55  	return multistep.ActionContinue
    56  }
    57  
    58  func (s *stepCreateVersion) Cleanup(state multistep.StateBag) {}