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