github.com/vijayrajah/packer@v1.3.2/post-processor/vagrant-cloud/client.go (about)

     1  package vagrantcloud
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"fmt"
     7  	"io"
     8  	"log"
     9  	"net/http"
    10  	"os"
    11  	"strings"
    12  )
    13  
    14  type VagrantCloudClient struct {
    15  	// The http client for communicating
    16  	client *http.Client
    17  
    18  	// The base URL of the API
    19  	BaseURL string
    20  
    21  	// Access token
    22  	AccessToken string
    23  }
    24  
    25  type VagrantCloudErrors struct {
    26  	Errors map[string][]string `json:"errors"`
    27  }
    28  
    29  func (v VagrantCloudErrors) FormatErrors() string {
    30  	errs := make([]string, 0)
    31  	for e := range v.Errors {
    32  		msg := fmt.Sprintf("%s %s", e, strings.Join(v.Errors[e], ","))
    33  		errs = append(errs, msg)
    34  	}
    35  	return strings.Join(errs, ". ")
    36  }
    37  
    38  func (v VagrantCloudClient) New(baseUrl string, token string) (*VagrantCloudClient, error) {
    39  	c := &VagrantCloudClient{
    40  		client: &http.Client{
    41  			Transport: &http.Transport{
    42  				Proxy: http.ProxyFromEnvironment,
    43  			},
    44  		},
    45  		BaseURL:     baseUrl,
    46  		AccessToken: token,
    47  	}
    48  
    49  	return c, c.ValidateAuthentication()
    50  }
    51  
    52  func decodeBody(resp *http.Response, out interface{}) error {
    53  	defer resp.Body.Close()
    54  	dec := json.NewDecoder(resp.Body)
    55  	return dec.Decode(out)
    56  }
    57  
    58  // encodeBody is used to encode a request body
    59  func encodeBody(obj interface{}) (io.Reader, error) {
    60  	buf := bytes.NewBuffer(nil)
    61  	enc := json.NewEncoder(buf)
    62  	if err := enc.Encode(obj); err != nil {
    63  		return nil, err
    64  	}
    65  	return buf, nil
    66  }
    67  
    68  func (v *VagrantCloudClient) ValidateAuthentication() error {
    69  	resp, err := v.Get("authenticate")
    70  	if err != nil {
    71  		return err
    72  	}
    73  	defer resp.Body.Close()
    74  	if resp.StatusCode != 200 {
    75  		return fmt.Errorf(resp.Status)
    76  	}
    77  	return nil
    78  }
    79  
    80  func (v *VagrantCloudClient) Get(path string) (*http.Response, error) {
    81  	reqUrl := fmt.Sprintf("%s/%s", v.BaseURL, path)
    82  
    83  	log.Printf("Post-Processor Vagrant Cloud API GET: %s", reqUrl)
    84  
    85  	req, err := v.newRequest("GET", reqUrl, nil)
    86  	if err != nil {
    87  		return nil, err
    88  	}
    89  	resp, err := v.client.Do(req)
    90  
    91  	log.Printf("Post-Processor Vagrant Cloud API Response: \n\n%+v", resp)
    92  
    93  	return resp, err
    94  }
    95  
    96  func (v *VagrantCloudClient) Delete(path string) (*http.Response, error) {
    97  	reqUrl := fmt.Sprintf("%s/%s", v.BaseURL, path)
    98  
    99  	// Scrub API key for logs
   100  	scrubbedUrl := strings.Replace(reqUrl, v.AccessToken, "ACCESS_TOKEN", -1)
   101  	log.Printf("Post-Processor Vagrant Cloud API DELETE: %s", scrubbedUrl)
   102  
   103  	req, err := http.NewRequest("DELETE", reqUrl, nil)
   104  	if err != nil {
   105  		return nil, err
   106  	}
   107  	req.Header.Add("Content-Type", "application/json")
   108  	req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", v.AccessToken))
   109  	resp, err := v.client.Do(req)
   110  
   111  	log.Printf("Post-Processor Vagrant Cloud API Response: \n\n%+v", resp)
   112  
   113  	return resp, err
   114  }
   115  
   116  func (v *VagrantCloudClient) Upload(path string, url string) (*http.Response, error) {
   117  	file, err := os.Open(path)
   118  
   119  	if err != nil {
   120  		return nil, fmt.Errorf("Error opening file for upload: %s", err)
   121  	}
   122  
   123  	fi, err := file.Stat()
   124  
   125  	if err != nil {
   126  		return nil, fmt.Errorf("Error stating file for upload: %s", err)
   127  	}
   128  
   129  	defer file.Close()
   130  
   131  	request, err := v.newRequest("PUT", url, file)
   132  
   133  	if err != nil {
   134  		return nil, fmt.Errorf("Error preparing upload request: %s", err)
   135  	}
   136  
   137  	log.Printf("Post-Processor Vagrant Cloud API Upload: %s %s", path, url)
   138  
   139  	request.ContentLength = fi.Size()
   140  	resp, err := v.client.Do(request)
   141  
   142  	log.Printf("Post-Processor Vagrant Cloud Upload Response: \n\n%+v", resp)
   143  
   144  	return resp, err
   145  }
   146  
   147  func (v *VagrantCloudClient) Post(path string, body interface{}) (*http.Response, error) {
   148  	reqUrl := fmt.Sprintf("%s/%s", v.BaseURL, path)
   149  
   150  	encBody, err := encodeBody(body)
   151  
   152  	if err != nil {
   153  		return nil, fmt.Errorf("Error encoding body for request: %s", err)
   154  	}
   155  
   156  	log.Printf("Post-Processor Vagrant Cloud API POST: %s. \n\n Body: %s", reqUrl, encBody)
   157  
   158  	req, err := v.newRequest("POST", reqUrl, encBody)
   159  	if err != nil {
   160  		return nil, err
   161  	}
   162  
   163  	resp, err := v.client.Do(req)
   164  
   165  	log.Printf("Post-Processor Vagrant Cloud API Response: \n\n%+v", resp)
   166  
   167  	return resp, err
   168  }
   169  
   170  func (v *VagrantCloudClient) Put(path string) (*http.Response, error) {
   171  	reqUrl := fmt.Sprintf("%s/%s", v.BaseURL, path)
   172  
   173  	log.Printf("Post-Processor Vagrant Cloud API PUT: %s", reqUrl)
   174  
   175  	req, err := v.newRequest("PUT", reqUrl, nil)
   176  	if err != nil {
   177  		return nil, err
   178  	}
   179  
   180  	resp, err := v.client.Do(req)
   181  
   182  	log.Printf("Post-Processor Vagrant Cloud API Response: \n\n%+v", resp)
   183  
   184  	return resp, err
   185  }
   186  
   187  func (v *VagrantCloudClient) newRequest(method, url string, body io.Reader) (*http.Request, error) {
   188  	req, err := http.NewRequest(method, url, body)
   189  	if err != nil {
   190  		return nil, err
   191  	}
   192  	req.Header.Add("Content-Type", "application/json")
   193  	req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", v.AccessToken))
   194  	return req, err
   195  }