post-processor/vagrant-cloud: add api items

This commit is contained in:
Jack Pearkes 2014-06-20 11:20:27 -04:00
parent 7d4efdc236
commit a678362701
5 changed files with 194 additions and 26 deletions

View File

@ -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
}

View File

@ -11,10 +11,6 @@ import (
"strings" "strings"
) )
type Box struct {
Tag string `json:"tag"`
}
type VagrantCloudClient struct { type VagrantCloudClient struct {
// The http client for communicating // The http client for communicating
client *http.Client client *http.Client
@ -55,22 +51,6 @@ func encodeBody(obj interface{}) (io.Reader, error) {
return buf, nil 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) { func (v VagrantCloudClient) Get(path string) (*http.Response, error) {
params := url.Values{} params := url.Values{}
params.Set("access_token", v.AccessToken) params.Set("access_token", v.AccessToken)
@ -88,10 +68,43 @@ func (v VagrantCloudClient) Get(path string) (*http.Response, error) {
return resp, err 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 // Scrub API key for logs
scrubbedUrl := strings.Replace(path, v.AccessToken, "ACCESS_TOKEN", -1) scrubbedUrl := strings.Replace(reqUrl, v.AccessToken, "ACCESS_TOKEN", -1)
log.Printf("Post-Processor Vagrant Cloud API POST: %s. \n\n Body: %s", scrubbedUrl, body) log.Printf("Post-Processor Vagrant Cloud API DELETE: %s", scrubbedUrl)
return nil, nil
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
} }

View File

@ -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) { func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, error) {
config := p.config config := p.config
fmt.Println(artifact)
// Only accepts input from the vagrant post-processor // Only accepts input from the vagrant post-processor
if artifact.BuilderId() != "mitchellh.post-processor.vagrant" { if artifact.BuilderId() != "mitchellh.post-processor.vagrant" {
return nil, false, fmt.Errorf( 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 // The name of the provider for vagrant cloud, and vagrant
provider := providerFromBuilderName(artifact.Id()) provider := providerFromBuilderName(artifact.Id())
version := p.config.Version
tag := p.config.Tag tag := p.config.Tag
// create the HTTP client // create the HTTP client
@ -108,7 +109,14 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (pac
return nil, false, err 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("Creating Provider %s", version))
ui.Say(fmt.Sprintf("Uploading Box %s", version)) ui.Say(fmt.Sprintf("Uploading Box %s", version))
ui.Say(fmt.Sprintf("Verifying upload %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 // Runs a cleanup if the post processor fails to upload
func (p *PostProcessor) Cleanup() { func (p *PostProcessor) Cleanup() {
// Delete the version // Delete the version
} }
// converts a packer builder name to the corresponding vagrant // converts a packer builder name to the corresponding vagrant

View File

@ -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
}

View File

@ -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
}