mirror of
https://github.com/letic/terraform-provider-google.git
synced 2024-09-06 00:56:43 +00:00
148 lines
3.3 KiB
Go
148 lines
3.3 KiB
Go
package google
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/url"
|
|
"reflect"
|
|
"regexp"
|
|
"strings"
|
|
|
|
"google.golang.org/api/googleapi"
|
|
)
|
|
|
|
func isEmptyValue(v reflect.Value) bool {
|
|
switch v.Kind() {
|
|
case reflect.Array, reflect.Map, reflect.Slice, reflect.String:
|
|
return v.Len() == 0
|
|
case reflect.Bool:
|
|
return !v.Bool()
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
|
return v.Int() == 0
|
|
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
|
|
return v.Uint() == 0
|
|
case reflect.Float32, reflect.Float64:
|
|
return v.Float() == 0
|
|
case reflect.Interface, reflect.Ptr:
|
|
return v.IsNil()
|
|
}
|
|
return false
|
|
}
|
|
|
|
func Post(config *Config, rawurl string, body map[string]interface{}) (map[string]interface{}, error) {
|
|
return sendRequest(config, "POST", rawurl, body)
|
|
}
|
|
|
|
func Get(config *Config, rawurl string) (map[string]interface{}, error) {
|
|
return sendRequest(config, "GET", rawurl, nil)
|
|
}
|
|
|
|
func Put(config *Config, rawurl string, body map[string]interface{}) (map[string]interface{}, error) {
|
|
return sendRequest(config, "PUT", rawurl, body)
|
|
}
|
|
|
|
func Delete(config *Config, rawurl string) (map[string]interface{}, error) {
|
|
return sendRequest(config, "DELETE", rawurl, nil)
|
|
}
|
|
|
|
func sendRequest(config *Config, method, rawurl string, body map[string]interface{}) (map[string]interface{}, error) {
|
|
reqHeaders := make(http.Header)
|
|
reqHeaders.Set("User-Agent", config.userAgent)
|
|
reqHeaders.Set("Content-Type", "application/json")
|
|
|
|
var buf bytes.Buffer
|
|
if body != nil {
|
|
err := json.NewEncoder(&buf).Encode(body)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
u, err := addQueryParams(rawurl, map[string]string{"alt": "json"})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
req, err := http.NewRequest(method, u, &buf)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
req.Header = reqHeaders
|
|
res, err := config.client.Do(req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer googleapi.CloseBody(res)
|
|
if err := googleapi.CheckResponse(res); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
result := make(map[string]interface{})
|
|
if err := json.NewDecoder(res.Body).Decode(&result); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return result, nil
|
|
}
|
|
|
|
func addQueryParams(rawurl string, params map[string]string) (string, error) {
|
|
u, err := url.Parse(rawurl)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
q := u.Query()
|
|
for k, v := range params {
|
|
q.Set(k, v)
|
|
}
|
|
u.RawQuery = q.Encode()
|
|
return u.String(), nil
|
|
}
|
|
|
|
func replaceVars(d TerraformResourceData, config *Config, linkTmpl string) (string, error) {
|
|
re := regexp.MustCompile("{{([[:word:]]+)}}")
|
|
var project, region, zone string
|
|
var err error
|
|
|
|
if strings.Contains(linkTmpl, "{{project}}") {
|
|
project, err = getProject(d, config)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
}
|
|
|
|
if strings.Contains(linkTmpl, "{{region}}") {
|
|
region, err = getRegion(d, config)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
}
|
|
|
|
if strings.Contains(linkTmpl, "{{zone}}") {
|
|
zone, err = getZone(d, config)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
}
|
|
|
|
replaceFunc := func(s string) string {
|
|
m := re.FindStringSubmatch(s)[1]
|
|
if m == "project" {
|
|
return project
|
|
}
|
|
if m == "region" {
|
|
return region
|
|
}
|
|
if m == "zone" {
|
|
return zone
|
|
}
|
|
v, ok := d.GetOk(m)
|
|
if ok {
|
|
return v.(string)
|
|
}
|
|
return ""
|
|
}
|
|
|
|
return re.ReplaceAllStringFunc(linkTmpl, replaceFunc), nil
|
|
}
|