From a5dc41a730eab0e361b5ba098457811630cddb6b Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Thu, 30 Oct 2014 22:16:21 +0300 Subject: [PATCH 1/3] fix digitalocean v2 api content-type when using json In case of using json we need to set content-type header. Signed-off-by: Vasiliy Tolstov --- builder/digitalocean/api.go | 2 +- builder/digitalocean/api_v2.go | 15 +++++++++++---- builder/digitalocean/builder.go | 2 +- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/builder/digitalocean/api.go b/builder/digitalocean/api.go index d2b5f9f61..45b03684b 100644 --- a/builder/digitalocean/api.go +++ b/builder/digitalocean/api.go @@ -24,7 +24,7 @@ type Size struct { Memory uint `json:"memory,omitempty"` //only in v2 api VCPUS uint `json:"vcpus,omitempty"` //only in v2 api Disk uint `json:"disk,omitempty"` //only in v2 api - Transfer uint `json:"transfer,omitempty"` //only in v2 api + Transfer float64 `json:"transfer,omitempty"` //only in v2 api PriceMonthly float64 `json:"price_monthly,omitempty"` //only in v2 api PriceHourly float64 `json:"price_hourly,omitempty"` //only in v2 api Regions []string `json:"regions,omitempty"` //only in v2 api diff --git a/builder/digitalocean/api_v2.go b/builder/digitalocean/api_v2.go index 23806df6f..a8c0e97ae 100644 --- a/builder/digitalocean/api_v2.go +++ b/builder/digitalocean/api_v2.go @@ -285,17 +285,21 @@ func NewRequestV2(d DigitalOceanClientV2, path string, method string, req interf enc.Encode(req) defer buf.Reset() request, err = http.NewRequest(method, url, buf) + request.Header.Add("Content-Type", "application/json") } else { request, err = http.NewRequest(method, url, nil) } if err != nil { return err } + // Add the authentication parameters request.Header.Add("Authorization", "Bearer "+d.APIToken) - - log.Printf("sending new request to digitalocean: %s", url) - + if buf != nil { + log.Printf("sending new request to digitalocean: %s buffer: %s", url, buf) + } else { + log.Printf("sending new request to digitalocean: %s", url) + } resp, err := client.Do(request) if err != nil { return err @@ -325,7 +329,10 @@ func NewRequestV2(d DigitalOceanClientV2, path string, method string, req interf return errors.New(fmt.Sprintf("Failed to decode JSON response %s (HTTP %v) from DigitalOcean: %s", err.Error(), resp.StatusCode, body)) } - + switch resp.StatusCode { + case 403, 429, 422, 404, 503, 500: + return errors.New(fmt.Sprintf("digitalocean request error: %+v", res)) + } return nil } diff --git a/builder/digitalocean/builder.go b/builder/digitalocean/builder.go index 5b84755ae..ad41ee547 100644 --- a/builder/digitalocean/builder.go +++ b/builder/digitalocean/builder.go @@ -17,7 +17,7 @@ import ( ) // see https://api.digitalocean.com/images/?client_id=[client_id]&api_key=[api_key] -// name="Ubuntu 12.04.4 x64", id=3101045, +// name="Ubuntu 12.04.4 x64", id=6374128, const DefaultImage = "ubuntu-12-04-x64" // see https://api.digitalocean.com/regions/?client_id=[client_id]&api_key=[api_key] From 8af85df822e7f06abdec26ff045810e767164cdc Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Sun, 2 Nov 2014 18:47:11 +0300 Subject: [PATCH 2/3] add error code for failed auth Signed-off-by: Vasiliy Tolstov --- builder/digitalocean/api_v2.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/builder/digitalocean/api_v2.go b/builder/digitalocean/api_v2.go index a8c0e97ae..30fca2be7 100644 --- a/builder/digitalocean/api_v2.go +++ b/builder/digitalocean/api_v2.go @@ -330,7 +330,7 @@ func NewRequestV2(d DigitalOceanClientV2, path string, method string, req interf resp.StatusCode, body)) } switch resp.StatusCode { - case 403, 429, 422, 404, 503, 500: + case 403, 401, 429, 422, 404, 503, 500: return errors.New(fmt.Sprintf("digitalocean request error: %+v", res)) } return nil From 1e87e796dc45599bb7bc76a253482b9698970f9f Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Wed, 12 Nov 2014 22:57:29 +0300 Subject: [PATCH 3/3] fix using private ip address in digitalocean builder Signed-off-by: Vasiliy Tolstov --- builder/digitalocean/api_v2.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/builder/digitalocean/api_v2.go b/builder/digitalocean/api_v2.go index 30fca2be7..b52aeaaaf 100644 --- a/builder/digitalocean/api_v2.go +++ b/builder/digitalocean/api_v2.go @@ -262,8 +262,10 @@ func (d DigitalOceanClientV2) DropletStatus(id uint) (string, string, error) { } var ip string - if len(res.Droplet.Networks.V4) > 0 { - ip = res.Droplet.Networks.V4[0].IPAddr + for _, n := range res.Droplet.Networks.V4 { + if n.Type == "public" { + ip = n.IPAddr + } } return ip, res.Droplet.Status, err