Commit a6783627 authored by Jack Pearkes's avatar Jack Pearkes

post-processor/vagrant-cloud: add api items

parent 7d4efdc2
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
}
......@@ -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,32 +51,33 @@ func encodeBody(obj interface{}) (io.Reader, error) {
return buf, nil
}
func (v VagrantCloudClient) Box(tag string) (*Box, error) {
resp, err := v.Get(tag)
func (v VagrantCloudClient) Get(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())
if err != nil {
return nil, fmt.Errorf("Error retrieving box: %s", err)
}
// Scrub API key for logs
scrubbedUrl := strings.Replace(reqUrl, v.AccessToken, "ACCESS_TOKEN", -1)
log.Printf("Post-Processor Vagrant Cloud API GET: %s", scrubbedUrl)
box := &Box{}
req, err := http.NewRequest("GET", reqUrl, nil)
resp, err := v.client.Do(req)
if err = decodeBody(resp, box); err != nil {
return nil, fmt.Errorf("Error parsing box response: %s", err)
}
log.Printf("Post-Processor Vagrant Cloud API Response: \n\n%s", resp)
return box, nil
return resp, err
}
func (v VagrantCloudClient) Get(path string) (*http.Response, 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(reqUrl, v.AccessToken, "ACCESS_TOKEN", -1)
log.Printf("Post-Processor Vagrant Cloud API GET: %s", scrubbedUrl)
log.Printf("Post-Processor Vagrant Cloud API DELETE: %s", scrubbedUrl)
req, err := http.NewRequest("GET", reqUrl, 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)
......@@ -88,10 +85,26 @@ 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) 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(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 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
}
......@@ -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
......
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
}
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
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment