Skip to content

Commit

Permalink
Merge pull request #116 from moul/fix-115
Browse files Browse the repository at this point in the history
Fix #115
  • Loading branch information
aimxhaisse committed Aug 12, 2015
2 parents ec0792d + fd4fc92 commit 3bbe74d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1051,6 +1051,7 @@ $ scw inspect myserver | jq '.[0].public_ip.address'
* `scw inspect TYPE:xxx TYPE:yyy` will only refresh cache for `TYPE`
* Sorting cache search by Levenshtein distance ([#87](https://github.com/scaleway/scaleway-cli/issues/87))
* Allow set up api endpoint using the environment variable $scaleway_api_endpoint
* Use TLS and verify can now be disabled using `SCALEWAY_TLSVERIFY=0` env var ([#115](https://github.com/scaleway/scaleway-cli/issues/115))

#### Fixes

Expand Down
29 changes: 18 additions & 11 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package api

import (
"bytes"
"crypto/tls"
"encoding/json"
"fmt"
"net/http"
Expand Down Expand Up @@ -43,6 +44,7 @@ type ScalewayAPI struct {
// Cache is used to quickly resolve identifiers from names
Cache *ScalewayCache

client *http.Client
anonuuid anonuuid.AnonUUID
}

Expand Down Expand Up @@ -543,13 +545,23 @@ func NewScalewayAPI(apiEndPoint, accountEndPoint, organization, token string) (*
return nil, err
}
s := &ScalewayAPI{
// exposed
ComputeAPI: apiEndPoint,
AccountAPI: accountEndPoint,
APIUrl: apiEndPoint,
Organization: organization,
Token: token,
Cache: cache,
anonuuid: *anonuuid.New(),

// internal
anonuuid: *anonuuid.New(),
client: &http.Client{},
}

if os.Getenv("SCALEWAY_TLSVERIFY") == "0" {
s.client.Transport = &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
}

return s, nil
Expand All @@ -564,20 +576,18 @@ func (s *ScalewayAPI) Sync() {
func (s *ScalewayAPI) GetResponse(resource string) (*http.Response, error) {
uri := fmt.Sprintf("%s/%s", strings.TrimRight(s.APIUrl, "/"), resource)
log.Debugf("GET %s", uri)
client := &http.Client{}
req, err := http.NewRequest("GET", uri, nil)
if err != nil {
return nil, err
}
req.Header.Set("X-Auth-Token", s.Token)
req.Header.Set("Content-Type", "application/json")
return client.Do(req)
return s.client.Do(req)
}

// PostResponse returns an http.Response object for the updated resource
func (s *ScalewayAPI) PostResponse(resource string, data interface{}) (*http.Response, error) {
uri := fmt.Sprintf("%s/%s", strings.TrimRight(s.APIUrl, "/"), resource)
client := &http.Client{}
payload := new(bytes.Buffer)
encoder := json.NewEncoder(payload)
if err := encoder.Encode(data); err != nil {
Expand All @@ -596,13 +606,12 @@ func (s *ScalewayAPI) PostResponse(resource string, data interface{}) (*http.Res
}
req.Header.Set("X-Auth-Token", s.Token)
req.Header.Set("Content-Type", "application/json")
return client.Do(req)
return s.client.Do(req)
}

// PatchResponse returns an http.Response object for the updated resource
func (s *ScalewayAPI) PatchResponse(resource string, data interface{}) (*http.Response, error) {
uri := fmt.Sprintf("%s/%s", strings.TrimRight(s.APIUrl, "/"), resource)
client := &http.Client{}
payload := new(bytes.Buffer)
encoder := json.NewEncoder(payload)
if err := encoder.Encode(data); err != nil {
Expand All @@ -621,13 +630,12 @@ func (s *ScalewayAPI) PatchResponse(resource string, data interface{}) (*http.Re
}
req.Header.Set("X-Auth-Token", s.Token)
req.Header.Set("Content-Type", "application/json")
return client.Do(req)
return s.client.Do(req)
}

// PutResponse returns an http.Response object for the updated resource
func (s *ScalewayAPI) PutResponse(resource string, data interface{}) (*http.Response, error) {
uri := fmt.Sprintf("%s/%s", strings.TrimRight(s.APIUrl, "/"), resource)
client := &http.Client{}
payload := new(bytes.Buffer)
encoder := json.NewEncoder(payload)
if err := encoder.Encode(data); err != nil {
Expand All @@ -646,21 +654,20 @@ func (s *ScalewayAPI) PutResponse(resource string, data interface{}) (*http.Resp
}
req.Header.Set("X-Auth-Token", s.Token)
req.Header.Set("Content-Type", "application/json")
return client.Do(req)
return s.client.Do(req)
}

// DeleteResponse returns an http.Response object for the deleted resource
func (s *ScalewayAPI) DeleteResponse(resource string) (*http.Response, error) {
uri := fmt.Sprintf("%s/%s", strings.TrimRight(s.APIUrl, "/"), resource)
client := &http.Client{}
log.Debugf("DELETE %s", uri)
req, err := http.NewRequest("DELETE", uri, nil)
if err != nil {
return nil, err
}
req.Header.Set("X-Auth-Token", s.Token)
req.Header.Set("Content-Type", "application/json")
return client.Do(req)
return s.client.Do(req)
}

// GetServers gets the list of servers from the ScalewayAPI
Expand Down

0 comments on commit 3bbe74d

Please sign in to comment.