github.com/marksheahan/packer@v0.10.2-0.20160613200515-1acb2d6645a0/post-processor/vagrant-cloud/step_create_provider.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 Provider struct { 10 Name string `json:"name"` 11 Url string `json:"url,omitempty"` 12 HostedToken string `json:"hosted_token,omitempty"` 13 UploadUrl string `json:"upload_url,omitempty"` 14 } 15 16 type stepCreateProvider struct { 17 name string // the name of the provider 18 } 19 20 func (s *stepCreateProvider) Run(state multistep.StateBag) multistep.StepAction { 21 client := state.Get("client").(*VagrantCloudClient) 22 ui := state.Get("ui").(packer.Ui) 23 box := state.Get("box").(*Box) 24 version := state.Get("version").(*Version) 25 providerName := state.Get("providerName").(string) 26 downloadUrl := state.Get("boxDownloadUrl").(string) 27 28 path := fmt.Sprintf("box/%s/version/%v/providers", box.Tag, version.Version) 29 30 provider := &Provider{Name: providerName} 31 32 if downloadUrl != "" { 33 provider.Url = downloadUrl 34 } 35 36 // Wrap the provider in a provider object for the API 37 wrapper := make(map[string]interface{}) 38 wrapper["provider"] = provider 39 40 ui.Say(fmt.Sprintf("Creating provider: %s", providerName)) 41 42 resp, err := client.Post(path, wrapper) 43 44 if err != nil || (resp.StatusCode != 200) { 45 cloudErrors := &VagrantCloudErrors{} 46 err = decodeBody(resp, cloudErrors) 47 state.Put("error", fmt.Errorf("Error creating provider: %s", cloudErrors.FormatErrors())) 48 return multistep.ActionHalt 49 } 50 51 if err = decodeBody(resp, provider); err != nil { 52 state.Put("error", fmt.Errorf("Error parsing provider response: %s", err)) 53 return multistep.ActionHalt 54 } 55 56 // Save the name for cleanup 57 s.name = provider.Name 58 59 state.Put("provider", provider) 60 61 return multistep.ActionContinue 62 } 63 64 func (s *stepCreateProvider) Cleanup(state multistep.StateBag) { 65 client := state.Get("client").(*VagrantCloudClient) 66 ui := state.Get("ui").(packer.Ui) 67 box := state.Get("box").(*Box) 68 version := state.Get("version").(*Version) 69 70 // If we didn't save the provider name, it likely doesn't exist 71 if s.name == "" { 72 ui.Say("Cleaning up provider") 73 ui.Message("Provider was not created, not deleting") 74 return 75 } 76 77 _, cancelled := state.GetOk(multistep.StateCancelled) 78 _, halted := state.GetOk(multistep.StateHalted) 79 80 // Return if we didn't cancel or halt, and thus need 81 // no cleanup 82 if !cancelled && !halted { 83 return 84 } 85 86 ui.Say("Cleaning up provider") 87 ui.Message(fmt.Sprintf("Deleting provider: %s", s.name)) 88 89 path := fmt.Sprintf("box/%s/version/%v/provider/%s", box.Tag, version.Version, s.name) 90 91 // No need for resp from the cleanup DELETE 92 _, err := client.Delete(path) 93 94 if err != nil { 95 ui.Error(fmt.Sprintf("Error destroying provider: %s", err)) 96 } 97 }