Refacto
This commit is contained in:
parent
dace64b70d
commit
2719fccce0
2 changed files with 68 additions and 38 deletions
|
@ -2,8 +2,22 @@ package croc
|
|||
|
||||
import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
"time"
|
||||
"crypto/hmac"
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
"strings"
|
||||
"sort"
|
||||
"bytes"
|
||||
"log"
|
||||
"io/ioutil"
|
||||
)
|
||||
|
||||
const SignatureMethod string = "HmacSHA256"
|
||||
const SignatureVersion string = "2"
|
||||
const Version string = "2013-02-01"
|
||||
|
||||
type Config struct {
|
||||
client *http.Client
|
||||
api_url string
|
||||
|
@ -13,6 +27,56 @@ type Config struct {
|
|||
}
|
||||
|
||||
|
||||
func (c Config) signRequest(query string) string {
|
||||
u, _ := url.Parse(c.api_url)
|
||||
var string_to_sign string = "GET\n" + strings.Split(u.Host, ":")[0] + "\n" + u.Path + "\n" + query
|
||||
mac := hmac.New(sha256.New, []byte(c.secret_key))
|
||||
mac.Write([]byte(string_to_sign))
|
||||
return base64.StdEncoding.EncodeToString(mac.Sum(nil))
|
||||
}
|
||||
|
||||
func (c Config) sendRequest(params map[string]string) {
|
||||
var buffer bytes.Buffer
|
||||
var i int = 0
|
||||
default_params := map[string]string{
|
||||
"AWSAccessKeyId" : c.project + ":" + c.access_key,
|
||||
"SignatureMethod" : SignatureMethod,
|
||||
"SignatureVersion" : SignatureVersion,
|
||||
"Timestamp" : time.Now().UTC().Format(time.RFC3339),
|
||||
"Version" : Version,
|
||||
}
|
||||
for k, v := range params {
|
||||
default_params[k] = v
|
||||
}
|
||||
mk := make([]string, len(default_params))
|
||||
for k, _ := range default_params {
|
||||
mk[i] = k
|
||||
i++
|
||||
}
|
||||
sort.Strings(mk)
|
||||
for _, e := range mk {
|
||||
buffer.WriteString(e + "=" + url.QueryEscape(default_params[e]) + "&")
|
||||
}
|
||||
query := buffer.String()
|
||||
hash := c.signRequest(query[0:len(query) - 1])
|
||||
query = query + "Signature=" + url.QueryEscape(hash)
|
||||
log.Println(query)
|
||||
req, err := http.NewRequest("GET", c.api_url + "?" + query, nil)
|
||||
|
||||
req.Header.Add("User-Agent", "Boto/2.12.0 Python/2.7.9 Linux/3.16.0-4-amd64")
|
||||
resp, err := c.client.Do(req)
|
||||
if err != nil {
|
||||
log.Println("Error")
|
||||
// handle error
|
||||
} else {
|
||||
defer resp.Body.Close()
|
||||
body, _ := ioutil.ReadAll(resp.Body)
|
||||
log.Printf("%s",body)
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
func newCrocClient(api_url, access_key, secret_key, project string) *Config{
|
||||
return &Config{
|
||||
client : &http.Client{
|
||||
|
|
|
@ -3,14 +3,6 @@ package croc
|
|||
import (
|
||||
"github.com/hashicorp/terraform/helper/schema"
|
||||
"log"
|
||||
"net/url"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"bytes"
|
||||
"time"
|
||||
"crypto/hmac"
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
)
|
||||
|
||||
func resourceCrocAddress() *schema.Resource {
|
||||
|
@ -49,36 +41,10 @@ func resourceCrocAddressExists(d *schema.ResourceData, meta interface{}) (b bool
|
|||
func resourceCrocAddressCreate(d *schema.ResourceData, meta interface{}) error {
|
||||
// http://docs.aws.amazon.com/general/latest/gr/signature-version-2.html
|
||||
config := meta.(*Config)
|
||||
var buffer bytes.Buffer
|
||||
u, _ := url.Parse(config.api_url)
|
||||
|
||||
timestamp := time.Now().UTC().Format(time.RFC3339)
|
||||
buffer.WriteString("GET\n")
|
||||
buffer.WriteString(u.Path + "\n")
|
||||
buffer.WriteString("AWSAccessKeyId=" + url.QueryEscape(config.project + ":" + config.access_key) + "\n")
|
||||
buffer.WriteString("Action=AllocateAddress\n")
|
||||
buffer.WriteString("SignatureMethod=HmacSHA256\n")
|
||||
buffer.WriteString("SignatureVersion=2\n")
|
||||
buffer.WriteString("Timestamp=" + timestamp + "\n")
|
||||
buffer.WriteString("Version=2013-02-01\n")
|
||||
log.Println(buffer.String())
|
||||
mac := hmac.New(sha256.New, []byte(config.secret_key))
|
||||
mac.Write([]byte(buffer.String()))
|
||||
log.Println(base64.StdEncoding.EncodeToString(mac.Sum(nil)))
|
||||
req, err := http.NewRequest("GET", config.api_url + "/?AWSAccessKeyId=" + url.QueryEscape(config.project + ":" + config.access_key) +
|
||||
"&Action=AllocateAddress&SignatureMethod=HmacSHA256&SignatureVersion=2&Timestamp=" + timestamp + "&Version=2013-02-01&Signature=" +
|
||||
base64.StdEncoding.EncodeToString(mac.Sum(nil)), nil)
|
||||
req.Header.Add("User-Agent", "Terraform croc plugin")
|
||||
resp, err := config.client.Do(req)
|
||||
if err != nil {
|
||||
log.Println("Error")
|
||||
// handle error
|
||||
} else {
|
||||
defer resp.Body.Close()
|
||||
body, _ := ioutil.ReadAll(resp.Body)
|
||||
log.Printf("%v",body)
|
||||
}
|
||||
log.Println("resourceCrocAddressCreate")
|
||||
log.Println("resourceCrocAddressCreate Start")
|
||||
param := map[string]string{"Action" : "AllocateAddress"}
|
||||
config.sendRequest(param)
|
||||
log.Println("resourceCrocAddressCreate Stop")
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue