1
0
Fork 0

Fix: Add TTL and custom Timeout in DigitalOcean DNS provider

This commit is contained in:
Ludovic Fernandez 2018-04-06 17:04:03 +02:00 committed by Traefiker Bot
parent 66485e81b4
commit 0ef1b7b683
120 changed files with 23764 additions and 9782 deletions

View file

@ -0,0 +1,36 @@
package client
import (
"encoding/json"
)
// Resource is the "base" type for all API resources
type Resource struct {
Complete chan bool `json:"-"`
}
// Init initializes the Complete channel, if it is necessary
// need to create a resource specific Init(), make sure to
// initialize the channel.
func (resource *Resource) Init() {
resource.Complete = make(chan bool, 1)
}
// PostUnmarshalJSON is a default implementation of the
// PostUnmarshalJSON hook that simply calls Init() and
// sends true to the Complete channel. This is overridden
// in many resources, in particular those that represent
// collections, and have to initialize sub-resources also.
func (resource *Resource) PostUnmarshalJSON() error {
resource.Init()
resource.Complete <- true
return nil
}
// GetJSON returns the raw (indented) JSON (as []bytes)
func (resource *Resource) GetJSON() ([]byte, error) {
return json.MarshalIndent(resource, "", " ")
}
// JSONBody is a generic struct for temporary JSON unmarshalling.
type JSONBody map[string]interface{}

View file

@ -0,0 +1,111 @@
// Package client is a simple library for http.Client to sign Akamai OPEN Edgegrid API requests
package client
import (
"bytes"
"errors"
"io"
"io/ioutil"
"net/http"
"net/url"
"runtime"
"strings"
"github.com/akamai/AkamaiOPEN-edgegrid-golang/edgegrid"
"github.com/akamai/AkamaiOPEN-edgegrid-golang/jsonhooks-v1"
)
var (
libraryVersion = "0.6.0"
// UserAgent is the User-Agent value sent for all requests
UserAgent = "Akamai-Open-Edgegrid-golang/" + libraryVersion + " golang/" + strings.TrimPrefix(runtime.Version(), "go")
// Client is the *http.Client to use
Client = http.DefaultClient
)
// NewRequest creates an HTTP request that can be sent to Akamai APIs. A relative URL can be provided in path, which will be resolved to the
// Host specified in Config. If body is specified, it will be sent as the request body.
func NewRequest(config edgegrid.Config, method, path string, body io.Reader) (*http.Request, error) {
var (
baseURL *url.URL
err error
)
if strings.HasPrefix(config.Host, "https://") {
baseURL, err = url.Parse(config.Host)
} else {
baseURL, err = url.Parse("https://" + config.Host)
}
if err != nil {
return nil, err
}
rel, err := url.Parse(strings.TrimPrefix(path, "/"))
if err != nil {
return nil, err
}
u := baseURL.ResolveReference(rel)
req, err := http.NewRequest(method, u.String(), body)
if err != nil {
return nil, err
}
req.Header.Add("User-Agent", UserAgent)
return req, nil
}
// NewJSONRequest creates an HTTP request that can be sent to the Akamai APIs with a JSON body
// The JSON body is encoded and the Content-Type/Accept headers are set automatically.
func NewJSONRequest(config edgegrid.Config, method, path string, body interface{}) (*http.Request, error) {
jsonBody, err := jsonhooks.Marshal(body)
if err != nil {
return nil, err
}
buf := bytes.NewReader(jsonBody)
req, err := NewRequest(config, method, path, buf)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json,*/*")
return req, nil
}
// Do performs a given HTTP Request, signed with the Akamai OPEN Edgegrid
// Authorization header. An edgegrid.Response or an error is returned.
func Do(config edgegrid.Config, req *http.Request) (*http.Response, error) {
Client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
req = edgegrid.AddRequestHeader(config, req)
return nil
}
req = edgegrid.AddRequestHeader(config, req)
res, err := Client.Do(req)
if err != nil {
return nil, err
}
return res, nil
}
// BodyJSON unmarshals the Response.Body into a given data structure
func BodyJSON(r *http.Response, data interface{}) error {
if data == nil {
return errors.New("You must pass in an interface{}")
}
body, err := ioutil.ReadAll(r.Body)
if err != nil {
return err
}
err = jsonhooks.Unmarshal(body, data)
return err
}

View file

@ -0,0 +1,88 @@
package client
import (
"fmt"
"io/ioutil"
"net/http"
"strings"
"github.com/akamai/AkamaiOPEN-edgegrid-golang/jsonhooks-v1"
)
// APIError exposes an Akamai OPEN Edgegrid Error
type APIError struct {
error
Type string `json:"type"`
Title string `json:"title"`
Status int `json:"status"`
Detail string `json:"detail"`
Instance string `json:"instance"`
Method string `json:"method"`
ServerIP string `json:"serverIp"`
ClientIP string `json:"clientIp"`
RequestID string `json:"requestId"`
RequestTime string `json:"requestTime"`
Response *http.Response `json:"-"`
RawBody string `json:"-"`
}
func (error APIError) Error() string {
return strings.TrimSpace(fmt.Sprintf("API Error: %d %s %s More Info %s", error.Status, error.Title, error.Detail, error.Type))
}
// NewAPIError creates a new API error based on a Response,
// or http.Response-like.
func NewAPIError(response *http.Response) APIError {
// TODO: handle this error
body, _ := ioutil.ReadAll(response.Body)
return NewAPIErrorFromBody(response, body)
}
// NewAPIErrorFromBody creates a new API error, allowing you to pass in a body
//
// This function is intended to be used after the body has already been read for
// other purposes.
func NewAPIErrorFromBody(response *http.Response, body []byte) APIError {
error := APIError{}
if err := jsonhooks.Unmarshal(body, &error); err == nil {
error.Status = response.StatusCode
error.Title = response.Status
}
error.Response = response
error.RawBody = string(body)
return error
}
// IsInformational determines if a response was informational (1XX status)
func IsInformational(r *http.Response) bool {
return r.StatusCode > 99 && r.StatusCode < 200
}
// IsSuccess determines if a response was successful (2XX status)
func IsSuccess(r *http.Response) bool {
return r.StatusCode > 199 && r.StatusCode < 300
}
// IsRedirection determines if a response was a redirect (3XX status)
func IsRedirection(r *http.Response) bool {
return r.StatusCode > 299 && r.StatusCode < 400
}
// IsClientError determines if a response was a client error (4XX status)
func IsClientError(r *http.Response) bool {
return r.StatusCode > 399 && r.StatusCode < 500
}
// IsServerError determines if a response was a server error (5XX status)
func IsServerError(r *http.Response) bool {
return r.StatusCode > 499 && r.StatusCode < 600
}
// IsError determines if the response was a client or server error (4XX or 5XX status)
func IsError(r *http.Response) bool {
return r.StatusCode > 399 && r.StatusCode < 600
}