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-12-17 16:29:25 -05:00
|
|
|
"github.com/hashicorp/packer-plugin-sdk/multistep"
|
|
|
|
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
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 {
|
|
|
|
}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *stepCreateVersion) 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
|
|
|
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)
|
2019-09-25 19:58:26 -04:00
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("error decoding error response: %s", err))
|
|
|
|
}
|
2014-06-24 15:58:45 -04:00
|
|
|
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) {}
|