From a67836270166ba2402ead2b05622ec7f203bca8e Mon Sep 17 00:00:00 2001 From: Jack Pearkes Date: Fri, 20 Jun 2014 11:20:27 -0400 Subject: [PATCH] post-processor/vagrant-cloud: add api items --- post-processor/vagrant-cloud/box.go | 44 +++++++++++++ .../vagrant-cloud/{api.go => client.go} | 61 +++++++++++-------- .../vagrant-cloud/post-processor.go | 13 +++- post-processor/vagrant-cloud/provider.go | 44 +++++++++++++ post-processor/vagrant-cloud/version.go | 58 ++++++++++++++++++ 5 files changed, 194 insertions(+), 26 deletions(-) create mode 100644 post-processor/vagrant-cloud/box.go rename post-processor/vagrant-cloud/{api.go => client.go} (58%) create mode 100644 post-processor/vagrant-cloud/provider.go create mode 100644 post-processor/vagrant-cloud/version.go diff --git a/post-processor/vagrant-cloud/box.go b/post-processor/vagrant-cloud/box.go new file mode 100644 index 000000000..e8a67d44c --- /dev/null +++ b/post-processor/vagrant-cloud/box.go @@ -0,0 +1,44 @@ +package vagrantcloud + +import ( + "fmt" +) + +type Box struct { + client *VagrantCloudClient + Tag string `json:"tag"` +} + +// https://vagrantcloud.com/docs/boxes +func (v VagrantCloudClient) Box(tag string) (*Box, error) { + resp, err := v.Get(tag) + + if err != nil { + return nil, fmt.Errorf("Error retrieving box: %s", err) + } + + box := &Box{} + + if err = decodeBody(resp, box); err != nil { + return nil, fmt.Errorf("Error parsing box response: %s", err) + } + + return box, nil +} + +// Save persist the provider over HTTP to Vagrant Cloud +func (b Box) Save(tag string) (bool, error) { + resp, err := b.client.Get(tag) + + if err != nil { + return false, fmt.Errorf("Error retrieving box: %s", err) + } + + box := &Box{} + + if err = decodeBody(resp, box); err != nil { + return false, fmt.Errorf("Error parsing box response: %s", err) + } + + return true, nil +} diff --git a/post-processor/vagrant-cloud/api.go b/post-processor/vagrant-cloud/client.go similarity index 58% rename from post-processor/vagrant-cloud/api.go rename to post-processor/vagrant-cloud/client.go index ba809c358..1dba74a94 100644 --- a/post-processor/vagrant-cloud/api.go +++ b/post-processor/vagrant-cloud/client.go @@ -11,10 +11,6 @@ import ( "strings" ) -type Box struct { - Tag string `json:"tag"` -} - type VagrantCloudClient struct { // The http client for communicating client *http.Client @@ -55,22 +51,6 @@ func encodeBody(obj interface{}) (io.Reader, error) { return buf, nil } -func (v VagrantCloudClient) Box(tag string) (*Box, error) { - resp, err := v.Get(tag) - - if err != nil { - return nil, fmt.Errorf("Error retrieving box: %s", err) - } - - box := &Box{} - - if err = decodeBody(resp, box); err != nil { - return nil, fmt.Errorf("Error parsing box response: %s", err) - } - - return box, nil -} - func (v VagrantCloudClient) Get(path string) (*http.Response, error) { params := url.Values{} params.Set("access_token", v.AccessToken) @@ -88,10 +68,43 @@ func (v VagrantCloudClient) Get(path string) (*http.Response, error) { return resp, err } -func (v VagrantCloudClient) Post(path string, body map[string]interface{}) (map[string]interface{}, error) { +func (v VagrantCloudClient) Delete(path string) (*http.Response, error) { + params := url.Values{} + params.Set("access_token", v.AccessToken) + reqUrl := fmt.Sprintf("%s/%s%s", v.BaseURL, path, params.Encode()) // Scrub API key for logs - scrubbedUrl := strings.Replace(path, v.AccessToken, "ACCESS_TOKEN", -1) - log.Printf("Post-Processor Vagrant Cloud API POST: %s. \n\n Body: %s", scrubbedUrl, body) - return nil, nil + scrubbedUrl := strings.Replace(reqUrl, v.AccessToken, "ACCESS_TOKEN", -1) + log.Printf("Post-Processor Vagrant Cloud API DELETE: %s", scrubbedUrl) + + req, err := http.NewRequest("DELETE", reqUrl, nil) + resp, err := v.client.Do(req) + + log.Printf("Post-Processor Vagrant Cloud API Response: \n\n%s", resp) + + return resp, err +} + +func (v VagrantCloudClient) Post(path string, body interface{}) (*http.Response, error) { + params := url.Values{} + params.Set("access_token", v.AccessToken) + reqUrl := fmt.Sprintf("%s/%s%s", v.BaseURL, path, params.Encode()) + + encBody, err := encodeBody(body) + + if err != nil { + return nil, fmt.Errorf("Error encoding body for request: %s", err) + } + + // Scrub API key for logs + scrubbedUrl := strings.Replace(reqUrl, v.AccessToken, "ACCESS_TOKEN", -1) + log.Printf("Post-Processor Vagrant Cloud API POST: %s. \n\n Body: %s", scrubbedUrl, encBody) + + req, err := http.NewRequest("POST", reqUrl, encBody) + + resp, err := v.client.Do(req) + + log.Printf("Post-Processor Vagrant Cloud API Response: \n\n%s", resp) + + return resp, err } diff --git a/post-processor/vagrant-cloud/post-processor.go b/post-processor/vagrant-cloud/post-processor.go index b62542c6d..8b2eda710 100644 --- a/post-processor/vagrant-cloud/post-processor.go +++ b/post-processor/vagrant-cloud/post-processor.go @@ -81,6 +81,8 @@ func (p *PostProcessor) Configure(raws ...interface{}) error { func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, error) { config := p.config + fmt.Println(artifact) + // Only accepts input from the vagrant post-processor if artifact.BuilderId() != "mitchellh.post-processor.vagrant" { return nil, false, fmt.Errorf( @@ -89,7 +91,6 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (pac // The name of the provider for vagrant cloud, and vagrant provider := providerFromBuilderName(artifact.Id()) - version := p.config.Version tag := p.config.Tag // create the HTTP client @@ -108,7 +109,14 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (pac return nil, false, err } - ui.Say(fmt.Sprintf("Creating Version %s", version)) + ui.Say(fmt.Sprintf("Creating Version %s", p.config.Version)) + + // Create the new version for the box + version := Version{Version: p.config.Version} + if ok, err := version.Create(); !ok { + return nil, false, err + } + ui.Say(fmt.Sprintf("Creating Provider %s", version)) ui.Say(fmt.Sprintf("Uploading Box %s", version)) ui.Say(fmt.Sprintf("Verifying upload %s", version)) @@ -120,6 +128,7 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (pac // Runs a cleanup if the post processor fails to upload func (p *PostProcessor) Cleanup() { // Delete the version + } // converts a packer builder name to the corresponding vagrant diff --git a/post-processor/vagrant-cloud/provider.go b/post-processor/vagrant-cloud/provider.go new file mode 100644 index 000000000..d8c3d201e --- /dev/null +++ b/post-processor/vagrant-cloud/provider.go @@ -0,0 +1,44 @@ +package vagrantcloud + +import ( + "fmt" +) + +type Provider struct { + client *VagrantCloudClient + Name string `json:"name"` +} + +// https://vagrantcloud.com/docs/providers +func (v VagrantCloudClient) Provider(tag string) (*Box, error) { + resp, err := v.Get(tag) + + if err != nil { + return nil, fmt.Errorf("Error retrieving box: %s", err) + } + + box := &Box{} + + if err = decodeBody(resp, box); err != nil { + return nil, fmt.Errorf("Error parsing box response: %s", err) + } + + return box, nil +} + +// Save persist the box over HTTP to Vagrant Cloud +func (p Provider) Save(name string) (bool, error) { + resp, err := p.client.Get(name) + + if err != nil { + return false, fmt.Errorf("Error retrieving box: %s", err) + } + + provider := &Provider{} + + if err = decodeBody(resp, provider); err != nil { + return false, fmt.Errorf("Error parsing box response: %s", err) + } + + return true, nil +} diff --git a/post-processor/vagrant-cloud/version.go b/post-processor/vagrant-cloud/version.go new file mode 100644 index 000000000..5558f2967 --- /dev/null +++ b/post-processor/vagrant-cloud/version.go @@ -0,0 +1,58 @@ +package vagrantcloud + +import ( + "fmt" +) + +type Version struct { + client *VagrantCloudClient + Version string `json:"version"` + Number string `json:"number"` +} + +// https://vagrantcloud.com/docs/versions +func (v VagrantCloudClient) Version(number string) (*Version, error) { + resp, err := v.Get(number) + + if err != nil { + return nil, fmt.Errorf("Error retrieving version: %s", err) + } + + version := &Version{} + + if err = decodeBody(resp, version); err != nil { + return nil, fmt.Errorf("Error parsing version response: %s", err) + } + + return version, nil +} + +// Save persists the Version over HTTP to Vagrant Cloud +func (v Version) Create() (bool, error) { + resp, err := v.client.Post(v.Number, v) + + if err != nil { + return false, fmt.Errorf("Error retrieving box: %s", err) + } + + if err = decodeBody(resp, v); err != nil { + return false, fmt.Errorf("Error parsing box response: %s", err) + } + + return true, nil +} + +// Deletes the Version over HTTP to Vagrant Cloud +func (v Version) Destroy() (bool, error) { + resp, err := v.client.Delete(v.Number) + + if err != nil { + return false, fmt.Errorf("Error destroying version: %s", err) + } + + if err = decodeBody(resp, v); err != nil { + return false, fmt.Errorf("Error parsing box response: %s", err) + } + + return true, nil +}