2014-06-23 15:48:51 -04:00
|
|
|
package vagrantcloud
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2014-06-24 15:58:45 -04:00
|
|
|
"github.com/mitchellh/multistep"
|
2014-06-23 15:48:51 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type Version struct {
|
2014-06-25 12:46:25 -04:00
|
|
|
Version string `json:"version"`
|
|
|
|
Description string `json:"description,omitempty"`
|
2014-06-23 15:48:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type stepCreateVersion struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stepCreateVersion) Run(state multistep.StateBag) multistep.StepAction {
|
|
|
|
client := state.Get("client").(*VagrantCloudClient)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
config := state.Get("config").(Config)
|
|
|
|
box := state.Get("box").(*Box)
|
|
|
|
|
2014-06-25 12:46:25 -04:00
|
|
|
ui.Say(fmt.Sprintf("Creating version: %s", config.Version))
|
|
|
|
|
2014-06-24 15:58:45 -04:00
|
|
|
if hasVersion, v := box.HasVersion(config.Version); hasVersion {
|
2014-06-25 12:46:25 -04:00
|
|
|
ui.Message(fmt.Sprintf("Version exists, skipping creation"))
|
2014-06-24 15:58:45 -04:00
|
|
|
state.Put("version", v)
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
path := fmt.Sprintf("box/%s/versions", box.Tag)
|
|
|
|
|
2014-06-25 10:32:17 -04:00
|
|
|
version := &Version{Version: config.Version, Description: config.VersionDescription}
|
2014-06-23 15:48:51 -04:00
|
|
|
|
|
|
|
// Wrap the version in a version object for the API
|
|
|
|
wrapper := make(map[string]interface{})
|
2014-06-24 15:58:45 -04:00
|
|
|
wrapper["version"] = version
|
2014-06-23 15:48:51 -04:00
|
|
|
|
|
|
|
resp, err := client.Post(path, wrapper)
|
|
|
|
|
|
|
|
if err != nil || (resp.StatusCode != 200) {
|
2014-06-24 15:58:45 -04:00
|
|
|
cloudErrors := &VagrantCloudErrors{}
|
|
|
|
err = decodeBody(resp, cloudErrors)
|
|
|
|
state.Put("error", fmt.Errorf("Error creating version: %s", cloudErrors.FormatErrors()))
|
2014-06-23 15:48:51 -04:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = decodeBody(resp, version); err != nil {
|
|
|
|
state.Put("error", fmt.Errorf("Error parsing version response: %s", err))
|
2014-06-24 15:58:45 -04:00
|
|
|
return multistep.ActionHalt
|
2014-06-23 15:48:51 -04:00
|
|
|
}
|
|
|
|
|
2014-06-24 15:58:45 -04:00
|
|
|
state.Put("version", version)
|
2014-06-23 15:48:51 -04:00
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2015-05-29 16:30:52 -04:00
|
|
|
func (s *stepCreateVersion) Cleanup(state multistep.StateBag) {}
|