2014-06-23 15:48:51 -04:00
|
|
|
package vagrantcloud
|
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2014-06-24 15:58:45 -04:00
|
|
|
"fmt"
|
2018-01-22 18:32:33 -05:00
|
|
|
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2020-11-17 19:31:03 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/multistep"
|
2014-06-23 15:48:51 -04:00
|
|
|
)
|
|
|
|
|
2014-06-24 15:58:45 -04:00
|
|
|
type Provider struct {
|
|
|
|
Name string `json:"name"`
|
2014-08-01 13:31:10 -04:00
|
|
|
Url string `json:"url,omitempty"`
|
2014-06-24 15:58:45 -04:00
|
|
|
HostedToken string `json:"hosted_token,omitempty"`
|
|
|
|
UploadUrl string `json:"upload_url,omitempty"`
|
|
|
|
}
|
|
|
|
|
2014-06-23 15:48:51 -04:00
|
|
|
type stepCreateProvider struct {
|
2014-06-24 15:58:45 -04:00
|
|
|
name string // the name of the provider
|
2014-06-23 15:48:51 -04:00
|
|
|
}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *stepCreateProvider) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2014-06-24 15:58:45 -04:00
|
|
|
client := state.Get("client").(*VagrantCloudClient)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
box := state.Get("box").(*Box)
|
|
|
|
version := state.Get("version").(*Version)
|
|
|
|
providerName := state.Get("providerName").(string)
|
2014-08-01 13:31:10 -04:00
|
|
|
downloadUrl := state.Get("boxDownloadUrl").(string)
|
2014-06-24 15:58:45 -04:00
|
|
|
|
2014-12-23 20:19:42 -05:00
|
|
|
path := fmt.Sprintf("box/%s/version/%v/providers", box.Tag, version.Version)
|
2014-06-24 15:58:45 -04:00
|
|
|
|
|
|
|
provider := &Provider{Name: providerName}
|
|
|
|
|
2014-08-01 13:31:10 -04:00
|
|
|
if downloadUrl != "" {
|
|
|
|
provider.Url = downloadUrl
|
|
|
|
}
|
|
|
|
|
2014-06-24 15:58:45 -04:00
|
|
|
// Wrap the provider in a provider object for the API
|
|
|
|
wrapper := make(map[string]interface{})
|
|
|
|
wrapper["provider"] = provider
|
|
|
|
|
|
|
|
ui.Say(fmt.Sprintf("Creating provider: %s", providerName))
|
|
|
|
|
|
|
|
resp, err := client.Post(path, wrapper)
|
|
|
|
|
|
|
|
if err != nil || (resp.StatusCode != 200) {
|
|
|
|
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 provider: %s", cloudErrors.FormatErrors()))
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = decodeBody(resp, provider); err != nil {
|
|
|
|
state.Put("error", fmt.Errorf("Error parsing provider response: %s", err))
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save the name for cleanup
|
|
|
|
s.name = provider.Name
|
|
|
|
|
|
|
|
state.Put("provider", provider)
|
|
|
|
|
2014-06-23 15:48:51 -04:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stepCreateProvider) Cleanup(state multistep.StateBag) {
|
2014-06-25 12:46:25 -04:00
|
|
|
client := state.Get("client").(*VagrantCloudClient)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
box := state.Get("box").(*Box)
|
|
|
|
version := state.Get("version").(*Version)
|
|
|
|
|
2014-06-24 15:58:45 -04:00
|
|
|
// If we didn't save the provider name, it likely doesn't exist
|
|
|
|
if s.name == "" {
|
2014-06-25 12:46:25 -04:00
|
|
|
ui.Say("Cleaning up provider")
|
|
|
|
ui.Message("Provider was not created, not deleting")
|
2014-06-24 15:58:45 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, cancelled := state.GetOk(multistep.StateCancelled)
|
|
|
|
_, halted := state.GetOk(multistep.StateHalted)
|
|
|
|
|
|
|
|
// Return if we didn't cancel or halt, and thus need
|
|
|
|
// no cleanup
|
|
|
|
if !cancelled && !halted {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-06-25 12:46:25 -04:00
|
|
|
ui.Say("Cleaning up provider")
|
|
|
|
ui.Message(fmt.Sprintf("Deleting provider: %s", s.name))
|
2014-06-24 15:58:45 -04:00
|
|
|
|
2014-12-23 20:19:42 -05:00
|
|
|
path := fmt.Sprintf("box/%s/version/%v/provider/%s", box.Tag, version.Version, s.name)
|
2014-06-24 15:58:45 -04:00
|
|
|
|
|
|
|
// No need for resp from the cleanup DELETE
|
|
|
|
_, err := client.Delete(path)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Error destroying provider: %s", err))
|
|
|
|
}
|
2014-06-23 15:48:51 -04:00
|
|
|
}
|