Skip to content

Commit

Permalink
Merge pull request #1650 from vtolstov/digitalocean
Browse files Browse the repository at this point in the history
fix digitalocean v2 api content-type when using json
  • Loading branch information
sethvargo committed Nov 26, 2014

Unverified

This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
2 parents 31a1aa4 + 1e87e79 commit a60039e
Showing 2 changed files with 16 additions and 7 deletions.
21 changes: 15 additions & 6 deletions builder/digitalocean/api_v2.go
Original file line number Diff line number Diff line change
@@ -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
@@ -285,17 +287,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 +331,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, 401, 429, 422, 404, 503, 500:
return errors.New(fmt.Sprintf("digitalocean request error: %+v", res))
}
return nil
}

2 changes: 1 addition & 1 deletion builder/digitalocean/builder.go
Original file line number Diff line number Diff line change
@@ -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]

0 comments on commit a60039e

Please sign in to comment.