Merge 'v1.7.3' into master
This commit is contained in:
commit
6dcb51a4bd
187 changed files with 26637 additions and 2091 deletions
25
CHANGELOG.md
25
CHANGELOG.md
|
@ -1,5 +1,30 @@
|
||||||
# Change Log
|
# Change Log
|
||||||
|
|
||||||
|
## [v1.7.3](https://github.com/containous/traefik/tree/v1.7.3) (2018-10-15)
|
||||||
|
[All Commits](https://github.com/containous/traefik/compare/v1.7.2...v1.7.3)
|
||||||
|
|
||||||
|
**Enhancements:**
|
||||||
|
- Improve the CLI help ([#3996](https://github.com/containous/traefik/pull/3996) by [dduportal](https://github.com/dduportal))
|
||||||
|
|
||||||
|
**Bug fixes:**
|
||||||
|
- **[acme]** DNS challenge Cloudflare auth zone ([#4042](https://github.com/containous/traefik/pull/4042) by [ldez](https://github.com/ldez))
|
||||||
|
- **[acme]** ACME DNS challenges ([#3998](https://github.com/containous/traefik/pull/3998) by [ldez](https://github.com/ldez))
|
||||||
|
- **[acme]** Don't initalize ACME provider if storage is empty ([#3988](https://github.com/containous/traefik/pull/3988) by [nmengin](https://github.com/nmengin))
|
||||||
|
- **[acme]** Fix: acme DNS providers ([#4021](https://github.com/containous/traefik/pull/4021) by [ldez](https://github.com/ldez))
|
||||||
|
- **[acme]** Prevent some malformed errors in LE. ([#4015](https://github.com/containous/traefik/pull/4015) by [ldez](https://github.com/ldez))
|
||||||
|
- **[authentication,consulcatalog,docker,ecs,etcd,kv,marathon,mesos,rancher]** Add the AuthResponseHeaders to the labels ([#3973](https://github.com/containous/traefik/pull/3973) by [Crypto89](https://github.com/Crypto89))
|
||||||
|
- **[docker]** usebindportip can fall back on the container ip / port ([#4018](https://github.com/containous/traefik/pull/4018) by [geraldcroes](https://github.com/geraldcroes))
|
||||||
|
- **[k8s]** Avoid flapping of multiple Ingress definitions ([#3862](https://github.com/containous/traefik/pull/3862) by [rtreffer](https://github.com/rtreffer))
|
||||||
|
- **[middleware,server]** Log stack on panic ([#4033](https://github.com/containous/traefik/pull/4033) by [ldez](https://github.com/ldez))
|
||||||
|
- **[middleware,server]** Fix recover from panic handler ([#4031](https://github.com/containous/traefik/pull/4031) by [mmatur](https://github.com/mmatur))
|
||||||
|
- **[server,websocket]** Fix update oxy ([#4009](https://github.com/containous/traefik/pull/4009) by [mmatur](https://github.com/mmatur))
|
||||||
|
|
||||||
|
**Documentation:**
|
||||||
|
- **[docker]** Add tags label to Docker provider documentation ([#3896](https://github.com/containous/traefik/pull/3896) by [artheus](https://github.com/artheus))
|
||||||
|
- **[docker]** Added two examples with labels in docker-compose.yml ([#3891](https://github.com/containous/traefik/pull/3891) by [pascalandy](https://github.com/pascalandy))
|
||||||
|
- **[k8s]** Move buffering annotation documentation to service ([#3991](https://github.com/containous/traefik/pull/3991) by [ldez](https://github.com/ldez))
|
||||||
|
- Fix a typo ([#3995](https://github.com/containous/traefik/pull/3995) by [arnydo](https://github.com/arnydo))
|
||||||
|
|
||||||
## [v1.7.2](https://github.com/containous/traefik/tree/v1.7.2) (2018-10-04)
|
## [v1.7.2](https://github.com/containous/traefik/tree/v1.7.2) (2018-10-04)
|
||||||
[All Commits](https://github.com/containous/traefik/compare/v1.7.1...v1.7.2)
|
[All Commits](https://github.com/containous/traefik/compare/v1.7.1...v1.7.2)
|
||||||
|
|
||||||
|
|
34
Gopkg.lock
generated
34
Gopkg.lock
generated
|
@ -258,6 +258,12 @@
|
||||||
packages = ["."]
|
packages = ["."]
|
||||||
revision = "2ea60e5f094469f9e65adb9cd103795b73ae743e"
|
revision = "2ea60e5f094469f9e65adb9cd103795b73ae743e"
|
||||||
|
|
||||||
|
[[projects]]
|
||||||
|
name = "github.com/cloudflare/cloudflare-go"
|
||||||
|
packages = ["."]
|
||||||
|
revision = "1f9007fbecae20711133c60519338c41cef1ffb4"
|
||||||
|
version = "v0.8.5"
|
||||||
|
|
||||||
[[projects]]
|
[[projects]]
|
||||||
branch = "master"
|
branch = "master"
|
||||||
name = "github.com/codahale/hdrhistogram"
|
name = "github.com/codahale/hdrhistogram"
|
||||||
|
@ -276,8 +282,8 @@
|
||||||
".",
|
".",
|
||||||
"parse"
|
"parse"
|
||||||
]
|
]
|
||||||
revision = "b4c2f060875361c070ed2bc300c5929b82f5fa2e"
|
revision = "aad81c7ac7f49671a59b9ede8ab22436e132a302"
|
||||||
version = "v1.1.2"
|
version = "v1.3.0"
|
||||||
|
|
||||||
[[projects]]
|
[[projects]]
|
||||||
branch = "master"
|
branch = "master"
|
||||||
|
@ -626,6 +632,12 @@
|
||||||
packages = ["."]
|
packages = ["."]
|
||||||
revision = "1d0bd113de87027671077d3c71eb3ac5d7dbba72"
|
revision = "1d0bd113de87027671077d3c71eb3ac5d7dbba72"
|
||||||
|
|
||||||
|
[[projects]]
|
||||||
|
name = "github.com/go-resty/resty"
|
||||||
|
packages = ["."]
|
||||||
|
revision = "d4920dcf5b7689548a6db640278a9b35a5b48ec6"
|
||||||
|
version = "v1.9.1"
|
||||||
|
|
||||||
[[projects]]
|
[[projects]]
|
||||||
name = "github.com/go-stack/stack"
|
name = "github.com/go-stack/stack"
|
||||||
packages = ["."]
|
packages = ["."]
|
||||||
|
@ -802,7 +814,6 @@
|
||||||
revision = "9b66602d496a139e4722bdde32f0f1ac1c12d4a8"
|
revision = "9b66602d496a139e4722bdde32f0f1ac1c12d4a8"
|
||||||
|
|
||||||
[[projects]]
|
[[projects]]
|
||||||
branch = "master"
|
|
||||||
name = "github.com/jjcollinge/servicefabric"
|
name = "github.com/jjcollinge/servicefabric"
|
||||||
packages = ["."]
|
packages = ["."]
|
||||||
revision = "8eebe170fa1ba25d3dfb928b3f86a7313b13b9fe"
|
revision = "8eebe170fa1ba25d3dfb928b3f86a7313b13b9fe"
|
||||||
|
@ -862,6 +873,12 @@
|
||||||
packages = ["."]
|
packages = ["."]
|
||||||
revision = "1113af38e5916529ad7317b0fe12e273e6e92af5"
|
revision = "1113af38e5916529ad7317b0fe12e273e6e92af5"
|
||||||
|
|
||||||
|
[[projects]]
|
||||||
|
name = "github.com/linode/linodego"
|
||||||
|
packages = ["."]
|
||||||
|
revision = "d0d31d8ca62fa3f7e4526ca0ce95de81e4ed001e"
|
||||||
|
version = "v0.5.1"
|
||||||
|
|
||||||
[[projects]]
|
[[projects]]
|
||||||
name = "github.com/mailgun/minheap"
|
name = "github.com/mailgun/minheap"
|
||||||
packages = ["."]
|
packages = ["."]
|
||||||
|
@ -1295,7 +1312,7 @@
|
||||||
"roundrobin",
|
"roundrobin",
|
||||||
"utils"
|
"utils"
|
||||||
]
|
]
|
||||||
revision = "fe51048067db50958154cd4040da878b10002a3a"
|
revision = "7d94d212f808222b72fd0b8bb171bfcd4e27ffca"
|
||||||
|
|
||||||
[[projects]]
|
[[projects]]
|
||||||
name = "github.com/vulcand/predicate"
|
name = "github.com/vulcand/predicate"
|
||||||
|
@ -1339,6 +1356,7 @@
|
||||||
"providers/dns/dnsimple",
|
"providers/dns/dnsimple",
|
||||||
"providers/dns/dnsmadeeasy",
|
"providers/dns/dnsmadeeasy",
|
||||||
"providers/dns/dnspod",
|
"providers/dns/dnspod",
|
||||||
|
"providers/dns/dreamhost",
|
||||||
"providers/dns/duckdns",
|
"providers/dns/duckdns",
|
||||||
"providers/dns/dyn",
|
"providers/dns/dyn",
|
||||||
"providers/dns/exec",
|
"providers/dns/exec",
|
||||||
|
@ -1353,6 +1371,7 @@
|
||||||
"providers/dns/iij",
|
"providers/dns/iij",
|
||||||
"providers/dns/lightsail",
|
"providers/dns/lightsail",
|
||||||
"providers/dns/linode",
|
"providers/dns/linode",
|
||||||
|
"providers/dns/linodev4",
|
||||||
"providers/dns/namecheap",
|
"providers/dns/namecheap",
|
||||||
"providers/dns/namedotcom",
|
"providers/dns/namedotcom",
|
||||||
"providers/dns/netcup",
|
"providers/dns/netcup",
|
||||||
|
@ -1365,10 +1384,11 @@
|
||||||
"providers/dns/rfc2136",
|
"providers/dns/rfc2136",
|
||||||
"providers/dns/route53",
|
"providers/dns/route53",
|
||||||
"providers/dns/sakuracloud",
|
"providers/dns/sakuracloud",
|
||||||
|
"providers/dns/stackpath",
|
||||||
"providers/dns/vegadns",
|
"providers/dns/vegadns",
|
||||||
"providers/dns/vultr"
|
"providers/dns/vultr"
|
||||||
]
|
]
|
||||||
revision = "621237d07213aa6dead90bdf6fd46251220fa669"
|
revision = "160d6fe60303699067faad57dc0b1e147ac499ef"
|
||||||
|
|
||||||
[[projects]]
|
[[projects]]
|
||||||
branch = "master"
|
branch = "master"
|
||||||
|
@ -1403,6 +1423,7 @@
|
||||||
"ipv4",
|
"ipv4",
|
||||||
"ipv6",
|
"ipv6",
|
||||||
"proxy",
|
"proxy",
|
||||||
|
"publicsuffix",
|
||||||
"trace",
|
"trace",
|
||||||
"websocket"
|
"websocket"
|
||||||
]
|
]
|
||||||
|
@ -1413,6 +1434,7 @@
|
||||||
name = "golang.org/x/oauth2"
|
name = "golang.org/x/oauth2"
|
||||||
packages = [
|
packages = [
|
||||||
".",
|
".",
|
||||||
|
"clientcredentials",
|
||||||
"google",
|
"google",
|
||||||
"internal",
|
"internal",
|
||||||
"jws",
|
"jws",
|
||||||
|
@ -1789,6 +1811,6 @@
|
||||||
[solve-meta]
|
[solve-meta]
|
||||||
analyzer-name = "dep"
|
analyzer-name = "dep"
|
||||||
analyzer-version = 1
|
analyzer-version = 1
|
||||||
inputs-digest = "8eb11befb583dfb89320dd7b7aea9e54b40e0523e60e66ed418788394b873d44"
|
inputs-digest = "37e89a543fca153d166cc70fd7fed689f06d894140bf617f69f5f664ffee621e"
|
||||||
solver-name = "gps-cdcl"
|
solver-name = "gps-cdcl"
|
||||||
solver-version = 1
|
solver-version = 1
|
||||||
|
|
|
@ -54,7 +54,7 @@
|
||||||
|
|
||||||
[[constraint]]
|
[[constraint]]
|
||||||
name = "github.com/containous/flaeg"
|
name = "github.com/containous/flaeg"
|
||||||
version = "1.0.1"
|
version = "1.3.0"
|
||||||
|
|
||||||
[[constraint]]
|
[[constraint]]
|
||||||
branch = "master"
|
branch = "master"
|
||||||
|
|
17
acme/acme.go
17
acme/acme.go
|
@ -692,16 +692,25 @@ func searchUncheckedDomains(domains []string, certs map[string]*tls.Certificate)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *ACME) getDomainsCertificates(domains []string) (*Certificate, error) {
|
func (a *ACME) getDomainsCertificates(domains []string) (*Certificate, error) {
|
||||||
domains = fun.Map(types.CanonicalDomain, domains).([]string)
|
var cleanDomains []string
|
||||||
log.Debugf("Loading ACME certificates %s...", domains)
|
for _, domain := range domains {
|
||||||
|
canonicalDomain := types.CanonicalDomain(domain)
|
||||||
|
cleanDomain := acme.UnFqdn(canonicalDomain)
|
||||||
|
if canonicalDomain != cleanDomain {
|
||||||
|
log.Warnf("FQDN detected, please remove the trailing dot: %s", canonicalDomain)
|
||||||
|
}
|
||||||
|
cleanDomains = append(cleanDomains, cleanDomain)
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Debugf("Loading ACME certificates %s...", cleanDomains)
|
||||||
bundle := true
|
bundle := true
|
||||||
|
|
||||||
certificate, err := a.client.ObtainCertificate(domains, bundle, nil, OSCPMustStaple)
|
certificate, err := a.client.ObtainCertificate(cleanDomains, bundle, nil, OSCPMustStaple)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("cannot obtain certificates: %+v", err)
|
return nil, fmt.Errorf("cannot obtain certificates: %+v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Debugf("Loaded ACME certificates %s", domains)
|
log.Debugf("Loaded ACME certificates %s", cleanDomains)
|
||||||
return &Certificate{
|
return &Certificate{
|
||||||
Domain: certificate.Domain,
|
Domain: certificate.Domain,
|
||||||
CertURL: certificate.CertURL,
|
CertURL: certificate.CertURL,
|
||||||
|
|
|
@ -162,6 +162,11 @@ var _templatesConsul_catalogTmpl = []byte(`[backends]
|
||||||
[frontends."frontend-{{ $service.ServiceName }}".auth.forward]
|
[frontends."frontend-{{ $service.ServiceName }}".auth.forward]
|
||||||
address = "{{ $auth.Forward.Address }}"
|
address = "{{ $auth.Forward.Address }}"
|
||||||
trustForwardHeader = {{ $auth.Forward.TrustForwardHeader }}
|
trustForwardHeader = {{ $auth.Forward.TrustForwardHeader }}
|
||||||
|
{{if $auth.Forward.AuthResponseHeaders }}
|
||||||
|
authResponseHeaders = [{{range $auth.Forward.AuthResponseHeaders }}
|
||||||
|
"{{.}}",
|
||||||
|
{{end}}]
|
||||||
|
{{end}}
|
||||||
|
|
||||||
{{if $auth.Forward.TLS }}
|
{{if $auth.Forward.TLS }}
|
||||||
[frontends."frontend-{{ $service.ServiceName }}".auth.forward.tls]
|
[frontends."frontend-{{ $service.ServiceName }}".auth.forward.tls]
|
||||||
|
@ -431,6 +436,11 @@ var _templatesDockerTmpl = []byte(`{{$backendServers := .Servers}}
|
||||||
[frontends."frontend-{{ $frontendName }}".auth.forward]
|
[frontends."frontend-{{ $frontendName }}".auth.forward]
|
||||||
address = "{{ $auth.Forward.Address }}"
|
address = "{{ $auth.Forward.Address }}"
|
||||||
trustForwardHeader = {{ $auth.Forward.TrustForwardHeader }}
|
trustForwardHeader = {{ $auth.Forward.TrustForwardHeader }}
|
||||||
|
{{if $auth.Forward.AuthResponseHeaders }}
|
||||||
|
authResponseHeaders = [{{range $auth.Forward.AuthResponseHeaders }}
|
||||||
|
"{{.}}",
|
||||||
|
{{end}}]
|
||||||
|
{{end}}
|
||||||
|
|
||||||
{{if $auth.Forward.TLS }}
|
{{if $auth.Forward.TLS }}
|
||||||
[frontends."frontend-{{ $frontendName }}".auth.forward.tls]
|
[frontends."frontend-{{ $frontendName }}".auth.forward.tls]
|
||||||
|
@ -703,6 +713,11 @@ var _templatesEcsTmpl = []byte(`[backends]
|
||||||
[frontends."frontend-{{ $frontendName }}".auth.forward]
|
[frontends."frontend-{{ $frontendName }}".auth.forward]
|
||||||
address = "{{ $auth.Forward.Address }}"
|
address = "{{ $auth.Forward.Address }}"
|
||||||
trustForwardHeader = {{ $auth.Forward.TrustForwardHeader }}
|
trustForwardHeader = {{ $auth.Forward.TrustForwardHeader }}
|
||||||
|
{{if $auth.Forward.AuthResponseHeaders }}
|
||||||
|
authResponseHeaders = [{{range $auth.Forward.AuthResponseHeaders }}
|
||||||
|
"{{.}}",
|
||||||
|
{{end}}]
|
||||||
|
{{end}}
|
||||||
|
|
||||||
{{if $auth.Forward.TLS }}
|
{{if $auth.Forward.TLS }}
|
||||||
[frontends."frontend-{{ $frontendName }}".auth.forward.tls]
|
[frontends."frontend-{{ $frontendName }}".auth.forward.tls]
|
||||||
|
@ -1230,6 +1245,11 @@ var _templatesKvTmpl = []byte(`[backends]
|
||||||
[frontends."{{ $frontendName }}".auth.forward]
|
[frontends."{{ $frontendName }}".auth.forward]
|
||||||
address = "{{ $auth.Forward.Address }}"
|
address = "{{ $auth.Forward.Address }}"
|
||||||
trustForwardHeader = {{ $auth.Forward.TrustForwardHeader }}
|
trustForwardHeader = {{ $auth.Forward.TrustForwardHeader }}
|
||||||
|
{{if $auth.Forward.AuthResponseHeaders }}
|
||||||
|
authResponseHeaders = [{{range $auth.Forward.AuthResponseHeaders }}
|
||||||
|
"{{.}}",
|
||||||
|
{{end}}]
|
||||||
|
{{end}}
|
||||||
|
|
||||||
{{if $auth.Forward.TLS }}
|
{{if $auth.Forward.TLS }}
|
||||||
[frontends."{{ $frontendName }}".auth.forward.tls]
|
[frontends."{{ $frontendName }}".auth.forward.tls]
|
||||||
|
@ -1516,6 +1536,11 @@ var _templatesMarathonTmpl = []byte(`{{ $apps := .Applications }}
|
||||||
[frontends."{{ $frontendName }}".auth.forward]
|
[frontends."{{ $frontendName }}".auth.forward]
|
||||||
address = "{{ $auth.Forward.Address }}"
|
address = "{{ $auth.Forward.Address }}"
|
||||||
trustForwardHeader = {{ $auth.Forward.TrustForwardHeader }}
|
trustForwardHeader = {{ $auth.Forward.TrustForwardHeader }}
|
||||||
|
{{if $auth.Forward.AuthResponseHeaders }}
|
||||||
|
authResponseHeaders = [{{range $auth.Forward.AuthResponseHeaders }}
|
||||||
|
"{{.}}",
|
||||||
|
{{end}}]
|
||||||
|
{{end}}
|
||||||
|
|
||||||
{{if $auth.Forward.TLS }}
|
{{if $auth.Forward.TLS }}
|
||||||
[frontends."{{ $frontendName }}".auth.forward.tls]
|
[frontends."{{ $frontendName }}".auth.forward.tls]
|
||||||
|
@ -1787,6 +1812,11 @@ var _templatesMesosTmpl = []byte(`[backends]
|
||||||
[frontends."frontend-{{ $frontendName }}".auth.forward]
|
[frontends."frontend-{{ $frontendName }}".auth.forward]
|
||||||
address = "{{ $auth.Forward.Address }}"
|
address = "{{ $auth.Forward.Address }}"
|
||||||
trustForwardHeader = {{ $auth.Forward.TrustForwardHeader }}
|
trustForwardHeader = {{ $auth.Forward.TrustForwardHeader }}
|
||||||
|
{{if $auth.Forward.AuthResponseHeaders }}
|
||||||
|
authResponseHeaders = [{{range $auth.Forward.AuthResponseHeaders }}
|
||||||
|
"{{.}}",
|
||||||
|
{{end}}]
|
||||||
|
{{end}}
|
||||||
|
|
||||||
{{if $auth.Forward.TLS }}
|
{{if $auth.Forward.TLS }}
|
||||||
[frontends."frontend-{{ $frontendName }}".auth.forward.tls]
|
[frontends."frontend-{{ $frontendName }}".auth.forward.tls]
|
||||||
|
@ -2080,6 +2110,11 @@ var _templatesRancherTmpl = []byte(`{{ $backendServers := .Backends }}
|
||||||
[frontends."frontend-{{ $frontendName }}".auth.forward]
|
[frontends."frontend-{{ $frontendName }}".auth.forward]
|
||||||
address = "{{ $auth.Forward.Address }}"
|
address = "{{ $auth.Forward.Address }}"
|
||||||
trustForwardHeader = {{ $auth.Forward.TrustForwardHeader }}
|
trustForwardHeader = {{ $auth.Forward.TrustForwardHeader }}
|
||||||
|
{{if $auth.Forward.AuthResponseHeaders }}
|
||||||
|
authResponseHeaders = [{{range $auth.Forward.AuthResponseHeaders }}
|
||||||
|
"{{.}}",
|
||||||
|
{{end}}]
|
||||||
|
{{end}}
|
||||||
|
|
||||||
{{if $auth.Forward.TLS }}
|
{{if $auth.Forward.TLS }}
|
||||||
[frontends."frontend-{{ $frontendName }}".auth.forward.tls]
|
[frontends."frontend-{{ $frontendName }}".auth.forward.tls]
|
||||||
|
|
|
@ -5,7 +5,7 @@ RUN apk --update upgrade \
|
||||||
&& rm -rf /var/cache/apk/*
|
&& rm -rf /var/cache/apk/*
|
||||||
|
|
||||||
RUN go get github.com/containous/go-bindata/... \
|
RUN go get github.com/containous/go-bindata/... \
|
||||||
&& go get github.com/golang/lint/golint \
|
&& go get golang.org/x/lint/golint \
|
||||||
&& go get github.com/kisielk/errcheck \
|
&& go get github.com/kisielk/errcheck \
|
||||||
&& go get github.com/client9/misspell/cmd/misspell
|
&& go get github.com/client9/misspell/cmd/misspell
|
||||||
|
|
||||||
|
|
|
@ -34,6 +34,7 @@ import (
|
||||||
"github.com/containous/traefik/tls"
|
"github.com/containous/traefik/tls"
|
||||||
"github.com/containous/traefik/types"
|
"github.com/containous/traefik/types"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
lego "github.com/xenolf/lego/acme"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -260,6 +261,17 @@ func (gc *GlobalConfiguration) initACMEProvider() {
|
||||||
gc.ACME.HTTPChallenge = nil
|
gc.ACME.HTTPChallenge = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for _, domain := range gc.ACME.Domains {
|
||||||
|
if domain.Main != lego.UnFqdn(domain.Main) {
|
||||||
|
log.Warnf("FQDN detected, please remove the trailing dot: %s", domain.Main)
|
||||||
|
}
|
||||||
|
for _, san := range domain.SANs {
|
||||||
|
if san != lego.UnFqdn(san) {
|
||||||
|
log.Warnf("FQDN detected, please remove the trailing dot: %s", san)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if len(gc.ACME.DNSProvider) > 0 {
|
if len(gc.ACME.DNSProvider) > 0 {
|
||||||
log.Warn("ACME.DNSProvider is deprecated, use ACME.DNSChallenge instead")
|
log.Warn("ACME.DNSProvider is deprecated, use ACME.DNSChallenge instead")
|
||||||
gc.ACME.DNSChallenge = &acmeprovider.DNSChallenge{Provider: gc.ACME.DNSProvider, DelayBeforeCheck: gc.ACME.DelayDontCheckDNS}
|
gc.ACME.DNSChallenge = &acmeprovider.DNSChallenge{Provider: gc.ACME.DNSProvider, DelayBeforeCheck: gc.ACME.DelayDontCheckDNS}
|
||||||
|
|
|
@ -257,19 +257,20 @@ Here is a list of supported `provider`s, that can automate the DNS verification,
|
||||||
| [Auroradns](https://www.pcextreme.com/aurora/dns) | `auroradns` | `AURORA_USER_ID`, `AURORA_KEY`, `AURORA_ENDPOINT` | Not tested yet |
|
| [Auroradns](https://www.pcextreme.com/aurora/dns) | `auroradns` | `AURORA_USER_ID`, `AURORA_KEY`, `AURORA_ENDPOINT` | Not tested yet |
|
||||||
| [Azure](https://azure.microsoft.com/services/dns/) | `azure` | `AZURE_CLIENT_ID`, `AZURE_CLIENT_SECRET`, `AZURE_SUBSCRIPTION_ID`, `AZURE_TENANT_ID`, `AZURE_RESOURCE_GROUP` | Not tested yet |
|
| [Azure](https://azure.microsoft.com/services/dns/) | `azure` | `AZURE_CLIENT_ID`, `AZURE_CLIENT_SECRET`, `AZURE_SUBSCRIPTION_ID`, `AZURE_TENANT_ID`, `AZURE_RESOURCE_GROUP` | Not tested yet |
|
||||||
| [Blue Cat](https://www.bluecatnetworks.com/) | `bluecat` | `BLUECAT_SERVER_URL`, `BLUECAT_USER_NAME`, `BLUECAT_PASSWORD`, `BLUECAT_CONFIG_NAME`, `BLUECAT_DNS_VIEW` | Not tested yet |
|
| [Blue Cat](https://www.bluecatnetworks.com/) | `bluecat` | `BLUECAT_SERVER_URL`, `BLUECAT_USER_NAME`, `BLUECAT_PASSWORD`, `BLUECAT_CONFIG_NAME`, `BLUECAT_DNS_VIEW` | Not tested yet |
|
||||||
| [Cloudflare](https://www.cloudflare.com) | `cloudflare` | `CLOUDFLARE_EMAIL`, `CLOUDFLARE_API_KEY` - The `Global API Key` needs to be used, not the `Origin CA Key` | YES |
|
| [Cloudflare](https://www.cloudflare.com) | `cloudflare` | `CF_API_EMAIL`, `CF_API_KEY` - The `Global API Key` needs to be used, not the `Origin CA Key` | YES |
|
||||||
| [CloudXNS](https://www.cloudxns.net) | `cloudxns` | `CLOUDXNS_API_KEY`, `CLOUDXNS_SECRET_KEY` | Not tested yet |
|
| [CloudXNS](https://www.cloudxns.net) | `cloudxns` | `CLOUDXNS_API_KEY`, `CLOUDXNS_SECRET_KEY` | Not tested yet |
|
||||||
| [DigitalOcean](https://www.digitalocean.com) | `digitalocean` | `DO_AUTH_TOKEN` | YES |
|
| [DigitalOcean](https://www.digitalocean.com) | `digitalocean` | `DO_AUTH_TOKEN` | YES |
|
||||||
| [DNSimple](https://dnsimple.com) | `dnsimple` | `DNSIMPLE_OAUTH_TOKEN`, `DNSIMPLE_BASE_URL` | Not tested yet |
|
| [DNSimple](https://dnsimple.com) | `dnsimple` | `DNSIMPLE_OAUTH_TOKEN`, `DNSIMPLE_BASE_URL` | Not tested yet |
|
||||||
| [DNS Made Easy](https://dnsmadeeasy.com) | `dnsmadeeasy` | `DNSMADEEASY_API_KEY`, `DNSMADEEASY_API_SECRET`, `DNSMADEEASY_SANDBOX` | Not tested yet |
|
| [DNS Made Easy](https://dnsmadeeasy.com) | `dnsmadeeasy` | `DNSMADEEASY_API_KEY`, `DNSMADEEASY_API_SECRET`, `DNSMADEEASY_SANDBOX` | Not tested yet |
|
||||||
| [DNSPod](http://www.dnspod.net/) | `dnspod` | `DNSPOD_API_KEY` | Not tested yet |
|
| [DNSPod](http://www.dnspod.net/) | `dnspod` | `DNSPOD_API_KEY` | Not tested yet |
|
||||||
|
| [DreamHost](https://www.dreamhost.com/) | `dreamhost` | `DREAMHOST_API_KEY` | YES |
|
||||||
| [Duck DNS](https://www.duckdns.org/) | `duckdns` | `DUCKDNS_TOKEN` | Not tested yet |
|
| [Duck DNS](https://www.duckdns.org/) | `duckdns` | `DUCKDNS_TOKEN` | Not tested yet |
|
||||||
| [Dyn](https://dyn.com) | `dyn` | `DYN_CUSTOMER_NAME`, `DYN_USER_NAME`, `DYN_PASSWORD` | Not tested yet |
|
| [Dyn](https://dyn.com) | `dyn` | `DYN_CUSTOMER_NAME`, `DYN_USER_NAME`, `DYN_PASSWORD` | Not tested yet |
|
||||||
| External Program | `exec` | `EXEC_PATH` | Not tested yet |
|
| External Program | `exec` | `EXEC_PATH` | Not tested yet |
|
||||||
| [Exoscale](https://www.exoscale.ch) | `exoscale` | `EXOSCALE_API_KEY`, `EXOSCALE_API_SECRET`, `EXOSCALE_ENDPOINT` | YES |
|
| [Exoscale](https://www.exoscale.ch) | `exoscale` | `EXOSCALE_API_KEY`, `EXOSCALE_API_SECRET`, `EXOSCALE_ENDPOINT` | YES |
|
||||||
| [Fast DNS](https://www.akamai.com/) | `fastdns` | `AKAMAI_CLIENT_TOKEN`, `AKAMAI_CLIENT_SECRET`, `AKAMAI_ACCESS_TOKEN` | Not tested yet |
|
| [Fast DNS](https://www.akamai.com/) | `fastdns` | `AKAMAI_CLIENT_TOKEN`, `AKAMAI_CLIENT_SECRET`, `AKAMAI_ACCESS_TOKEN` | Not tested yet |
|
||||||
| [Gandi](https://www.gandi.net) | `gandi` | `GANDI_API_KEY` | Not tested yet |
|
| [Gandi](https://www.gandi.net) | `gandi` | `GANDI_API_KEY` | Not tested yet |
|
||||||
| [Gandi V5](http://doc.livedns.gandi.net) | `gandiv5` | `GANDIV5_API_KEY` | YES |
|
| [Gandi v5](http://doc.livedns.gandi.net) | `gandiv5` | `GANDIV5_API_KEY` | YES |
|
||||||
| [Glesys](https://glesys.com/) | `glesys` | `GLESYS_API_USER`, `GLESYS_API_KEY`, `GLESYS_DOMAIN` | Not tested yet |
|
| [Glesys](https://glesys.com/) | `glesys` | `GLESYS_API_USER`, `GLESYS_API_KEY`, `GLESYS_DOMAIN` | Not tested yet |
|
||||||
| [GoDaddy](https://godaddy.com/domains) | `godaddy` | `GODADDY_API_KEY`, `GODADDY_API_SECRET` | Not tested yet |
|
| [GoDaddy](https://godaddy.com/domains) | `godaddy` | `GODADDY_API_KEY`, `GODADDY_API_SECRET` | Not tested yet |
|
||||||
| [Google Cloud DNS](https://cloud.google.com/dns/docs/) | `gcloud` | `GCE_PROJECT`, `GCE_SERVICE_ACCOUNT_FILE` | YES |
|
| [Google Cloud DNS](https://cloud.google.com/dns/docs/) | `gcloud` | `GCE_PROJECT`, `GCE_SERVICE_ACCOUNT_FILE` | YES |
|
||||||
|
@ -277,6 +278,7 @@ Here is a list of supported `provider`s, that can automate the DNS verification,
|
||||||
| [IIJ](https://www.iij.ad.jp/) | `iij` | `IIJ_API_ACCESS_KEY`, `IIJ_API_SECRET_KEY`, `IIJ_DO_SERVICE_CODE` | Not tested yet |
|
| [IIJ](https://www.iij.ad.jp/) | `iij` | `IIJ_API_ACCESS_KEY`, `IIJ_API_SECRET_KEY`, `IIJ_DO_SERVICE_CODE` | Not tested yet |
|
||||||
| [Lightsail](https://aws.amazon.com/lightsail/) | `lightsail` | `AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY`, `DNS_ZONE` | Not tested yet |
|
| [Lightsail](https://aws.amazon.com/lightsail/) | `lightsail` | `AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY`, `DNS_ZONE` | Not tested yet |
|
||||||
| [Linode](https://www.linode.com) | `linode` | `LINODE_API_KEY` | Not tested yet |
|
| [Linode](https://www.linode.com) | `linode` | `LINODE_API_KEY` | Not tested yet |
|
||||||
|
| [Linode v4](https://www.linode.com) | `linodev4` | `LINODE_TOKEN` | Not tested yet |
|
||||||
| manual | - | none, but you need to run Træfik interactively, turn on `acmeLogging` to see instructions and press <kbd>Enter</kbd>. | YES |
|
| manual | - | none, but you need to run Træfik interactively, turn on `acmeLogging` to see instructions and press <kbd>Enter</kbd>. | YES |
|
||||||
| [Namecheap](https://www.namecheap.com) | `namecheap` | `NAMECHEAP_API_USER`, `NAMECHEAP_API_KEY` | YES |
|
| [Namecheap](https://www.namecheap.com) | `namecheap` | `NAMECHEAP_API_USER`, `NAMECHEAP_API_KEY` | YES |
|
||||||
| [name.com](https://www.name.com/) | `namedotcom` | `NAMECOM_USERNAME`, `NAMECOM_API_TOKEN`, `NAMECOM_SERVER` | Not tested yet |
|
| [name.com](https://www.name.com/) | `namedotcom` | `NAMECOM_USERNAME`, `NAMECOM_API_TOKEN`, `NAMECOM_SERVER` | Not tested yet |
|
||||||
|
@ -290,6 +292,7 @@ Here is a list of supported `provider`s, that can automate the DNS verification,
|
||||||
| [RFC2136](https://tools.ietf.org/html/rfc2136) | `rfc2136` | `RFC2136_TSIG_KEY`, `RFC2136_TSIG_SECRET`, `RFC2136_TSIG_ALGORITHM`, `RFC2136_NAMESERVER` | Not tested yet |
|
| [RFC2136](https://tools.ietf.org/html/rfc2136) | `rfc2136` | `RFC2136_TSIG_KEY`, `RFC2136_TSIG_SECRET`, `RFC2136_TSIG_ALGORITHM`, `RFC2136_NAMESERVER` | Not tested yet |
|
||||||
| [Route 53](https://aws.amazon.com/route53/) | `route53` | `AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY`, `[AWS_REGION]`, `[AWS_HOSTED_ZONE_ID]` or a configured user/instance IAM profile. | YES |
|
| [Route 53](https://aws.amazon.com/route53/) | `route53` | `AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY`, `[AWS_REGION]`, `[AWS_HOSTED_ZONE_ID]` or a configured user/instance IAM profile. | YES |
|
||||||
| [Sakura Cloud](https://cloud.sakura.ad.jp/) | `sakuracloud` | `SAKURACLOUD_ACCESS_TOKEN`, `SAKURACLOUD_ACCESS_TOKEN_SECRET` | Not tested yet |
|
| [Sakura Cloud](https://cloud.sakura.ad.jp/) | `sakuracloud` | `SAKURACLOUD_ACCESS_TOKEN`, `SAKURACLOUD_ACCESS_TOKEN_SECRET` | Not tested yet |
|
||||||
|
| [Stackpath](https://www.stackpath.com/) | `stackpath` | `STACKPATH_CLIENT_ID`, `STACKPATH_CLIENT_SECRET`, `STACKPATH_STACK_ID` | Not tested yet |
|
||||||
| [VegaDNS](https://github.com/shupp/VegaDNS-API) | `vegadns` | `SECRET_VEGADNS_KEY`, `SECRET_VEGADNS_SECRET`, `VEGADNS_URL` | Not tested yet |
|
| [VegaDNS](https://github.com/shupp/VegaDNS-API) | `vegadns` | `SECRET_VEGADNS_KEY`, `SECRET_VEGADNS_SECRET`, `VEGADNS_URL` | Not tested yet |
|
||||||
| [VULTR](https://www.vultr.com) | `vultr` | `VULTR_API_KEY` | Not tested yet |
|
| [VULTR](https://www.vultr.com) | `vultr` | `VULTR_API_KEY` | Not tested yet |
|
||||||
|
|
||||||
|
|
|
@ -125,6 +125,7 @@ Additional settings can be defined using Consul Catalog tags.
|
||||||
| `<prefix>.frontend.auth.digest.users=EXPR` | Sets digest authentication to this frontend in CSV format: `User:Realm:Hash,User:Realm:Hash`. |
|
| `<prefix>.frontend.auth.digest.users=EXPR` | Sets digest authentication to this frontend in CSV format: `User:Realm:Hash,User:Realm:Hash`. |
|
||||||
| `<prefix>.frontend.auth.digest.usersfile=/path/.htdigest` | Sets digest authentication with an external file; if users and usersFile are provided, both are merged, with external file contents having precedence. |
|
| `<prefix>.frontend.auth.digest.usersfile=/path/.htdigest` | Sets digest authentication with an external file; if users and usersFile are provided, both are merged, with external file contents having precedence. |
|
||||||
| `<prefix>.frontend.auth.forward.address=https://example.com` | Sets the URL of the authentication server. |
|
| `<prefix>.frontend.auth.forward.address=https://example.com` | Sets the URL of the authentication server. |
|
||||||
|
| `<prefix>.frontend.auth.forward.authResponseHeaders=EXPR` | Sets the forward authentication authResponseHeaders in CSV format: `X-Auth-User,X-Auth-Header` |
|
||||||
| `<prefix>.frontend.auth.forward.tls.ca=/path/ca.pem` | Sets the Certificate Authority (CA) for the TLS connection with the authentication server. |
|
| `<prefix>.frontend.auth.forward.tls.ca=/path/ca.pem` | Sets the Certificate Authority (CA) for the TLS connection with the authentication server. |
|
||||||
| `<prefix>.frontend.auth.forward.tls.caOptional=true` | Checks the certificates if present but do not force to be signed by a specified Certificate Authority (CA). |
|
| `<prefix>.frontend.auth.forward.tls.caOptional=true` | Checks the certificates if present but do not force to be signed by a specified Certificate Authority (CA). |
|
||||||
| `<prefix>.frontend.auth.forward.tls.cert=/path/server.pem` | Sets the Certificate for the TLS connection with the authentication server. |
|
| `<prefix>.frontend.auth.forward.tls.cert=/path/server.pem` | Sets the Certificate for the TLS connection with the authentication server. |
|
||||||
|
|
|
@ -57,7 +57,9 @@ watch = true
|
||||||
exposedByDefault = true
|
exposedByDefault = true
|
||||||
|
|
||||||
# Use the IP address from the binded port instead of the inner network one.
|
# Use the IP address from the binded port instead of the inner network one.
|
||||||
# For specific use-case :)
|
#
|
||||||
|
# In case no IP address is attached to the binded port (or in case
|
||||||
|
# there is no bind), the inner network one will be used as a fallback.
|
||||||
#
|
#
|
||||||
# Optional
|
# Optional
|
||||||
# Default: false
|
# Default: false
|
||||||
|
@ -213,6 +215,7 @@ Labels can be used on containers to override default behavior.
|
||||||
| `traefik.domain` | Sets the default base domain for the frontend rules. For more information, check the [Container Labels section's of the user guide "Let's Encrypt & Docker"](/user-guide/docker-and-lets-encrypt/#container-labels) |
|
| `traefik.domain` | Sets the default base domain for the frontend rules. For more information, check the [Container Labels section's of the user guide "Let's Encrypt & Docker"](/user-guide/docker-and-lets-encrypt/#container-labels) |
|
||||||
| `traefik.enable=false` | Disables this container in Træfik. |
|
| `traefik.enable=false` | Disables this container in Træfik. |
|
||||||
| `traefik.port=80` | Registers this port. Useful when the container exposes multiples ports. |
|
| `traefik.port=80` | Registers this port. Useful when the container exposes multiples ports. |
|
||||||
|
| `traefik.tags=foo,bar,myTag` | Adds Træfik tags to the Docker container/service to be used in [constraints](/configuration/commons/#constraints). |
|
||||||
| `traefik.protocol=https` | Overrides the default `http` protocol |
|
| `traefik.protocol=https` | Overrides the default `http` protocol |
|
||||||
| `traefik.weight=10` | Assigns this weight to the container |
|
| `traefik.weight=10` | Assigns this weight to the container |
|
||||||
| `traefik.backend=foo` | Gives the name `foo` to the generated backend for this container. |
|
| `traefik.backend=foo` | Gives the name `foo` to the generated backend for this container. |
|
||||||
|
@ -244,6 +247,7 @@ Labels can be used on containers to override default behavior.
|
||||||
| `traefik.frontend.auth.digest.users=EXPR` | Sets the digest authentication to this frontend in CSV format: `User:Realm:Hash,User:Realm:Hash`. |
|
| `traefik.frontend.auth.digest.users=EXPR` | Sets the digest authentication to this frontend in CSV format: `User:Realm:Hash,User:Realm:Hash`. |
|
||||||
| `traefik.frontend.auth.digest.usersFile=/path/.htdigest` | Sets the digest authentication with an external file; if users and usersFile are provided, both are merged, with external file contents having precedence. |
|
| `traefik.frontend.auth.digest.usersFile=/path/.htdigest` | Sets the digest authentication with an external file; if users and usersFile are provided, both are merged, with external file contents having precedence. |
|
||||||
| `traefik.frontend.auth.forward.address=https://example.com` | Sets the URL of the authentication server. |
|
| `traefik.frontend.auth.forward.address=https://example.com` | Sets the URL of the authentication server. |
|
||||||
|
| `traefik.frontend.auth.forward.authResponseHeaders=EXPR` | Sets the forward authentication authResponseHeaders in CSV format: `X-Auth-User,X-Auth-Header` |
|
||||||
| `traefik.frontend.auth.forward.tls.ca=/path/ca.pem` | Sets the Certificate Authority (CA) for the TLS connection with the authentication server. |
|
| `traefik.frontend.auth.forward.tls.ca=/path/ca.pem` | Sets the Certificate Authority (CA) for the TLS connection with the authentication server. |
|
||||||
| `traefik.frontend.auth.forward.tls.caOptional=true` | Checks the certificates if present but do not force to be signed by a specified Certificate Authority (CA). |
|
| `traefik.frontend.auth.forward.tls.caOptional=true` | Checks the certificates if present but do not force to be signed by a specified Certificate Authority (CA). |
|
||||||
| `traefik.frontend.auth.forward.tls.cert=/path/server.pem` | Sets the Certificate for the TLS connection with the authentication server. |
|
| `traefik.frontend.auth.forward.tls.cert=/path/server.pem` | Sets the Certificate for the TLS connection with the authentication server. |
|
||||||
|
@ -347,6 +351,7 @@ Segment labels override the default behavior.
|
||||||
| `traefik.<segment_name>.frontend.auth.digest.users=EXPR` | Same as `traefik.frontend.auth.digest.users` |
|
| `traefik.<segment_name>.frontend.auth.digest.users=EXPR` | Same as `traefik.frontend.auth.digest.users` |
|
||||||
| `traefik.<segment_name>.frontend.auth.digest.usersFile=/path/.htdigest` | Same as `traefik.frontend.auth.digest.usersFile` |
|
| `traefik.<segment_name>.frontend.auth.digest.usersFile=/path/.htdigest` | Same as `traefik.frontend.auth.digest.usersFile` |
|
||||||
| `traefik.<segment_name>.frontend.auth.forward.address=https://example.com` | Same as `traefik.frontend.auth.forward.address` |
|
| `traefik.<segment_name>.frontend.auth.forward.address=https://example.com` | Same as `traefik.frontend.auth.forward.address` |
|
||||||
|
| `traefik.<segment_name>.frontend.auth.forward.authResponseHeaders=EXPR` | Same as `traefik.frontend.auth.forward.authResponseHeaders` |
|
||||||
| `traefik.<segment_name>.frontend.auth.forward.tls.ca=/path/ca.pem` | Same as `traefik.frontend.auth.forward.tls.ca` |
|
| `traefik.<segment_name>.frontend.auth.forward.tls.ca=/path/ca.pem` | Same as `traefik.frontend.auth.forward.tls.ca` |
|
||||||
| `traefik.<segment_name>.frontend.auth.forward.tls.caOptional=true` | Same as `traefik.frontend.auth.forward.tls.caOptional` |
|
| `traefik.<segment_name>.frontend.auth.forward.tls.caOptional=true` | Same as `traefik.frontend.auth.forward.tls.caOptional` |
|
||||||
| `traefik.<segment_name>.frontend.auth.forward.tls.cert=/path/server.pem` | Same as `traefik.frontend.auth.forward.tls.cert` |
|
| `traefik.<segment_name>.frontend.auth.forward.tls.cert=/path/server.pem` | Same as `traefik.frontend.auth.forward.tls.cert` |
|
||||||
|
@ -428,3 +433,25 @@ Segment labels override the default behavior.
|
||||||
When running inside a container, Træfik will need network access through:
|
When running inside a container, Træfik will need network access through:
|
||||||
|
|
||||||
`docker network connect <network> <traefik-container>`
|
`docker network connect <network> <traefik-container>`
|
||||||
|
|
||||||
|
## usebindportip
|
||||||
|
|
||||||
|
The default behavior of Træfik is to route requests to the IP/Port of the matching container.
|
||||||
|
When setting `usebindportip` to true, you tell Træfik to use the IP/Port attached to the container's binding instead of the inner network IP/Port.
|
||||||
|
|
||||||
|
When used in conjunction with the `traefik.port` label (that tells Træfik to route requests to a specific port), Træfik tries to find a binding with `traefik.port` port to select the container. If it can't find such a binding, Træfik falls back on the internal network IP of the container, but still uses the `traefik.port` that is set in the label.
|
||||||
|
|
||||||
|
Below is a recap of the behavior of `usebindportip` in different situations.
|
||||||
|
|
||||||
|
| traefik.port label | Container's binding | Routes to |
|
||||||
|
|--------------------|----------------------------------------------------|----------------|
|
||||||
|
| - | - | IntIP:IntPort |
|
||||||
|
| - | ExtPort:IntPort | IntIP:IntPort |
|
||||||
|
| - | ExtIp:ExtPort:IntPort | ExtIp:ExtPort |
|
||||||
|
| LblPort | - | IntIp:LblPort |
|
||||||
|
| LblPort | ExtIp:ExtPort:LblPort | ExtIp:ExtPort |
|
||||||
|
| LblPort | ExtIp:ExtPort:OtherPort | IntIp:LblPort |
|
||||||
|
| LblPort | ExtIp1:ExtPort1:IntPort1 & ExtIp2:LblPort:IntPort2 | ExtIp2:LblPort |
|
||||||
|
|
||||||
|
!!! note
|
||||||
|
In the above table, ExtIp stands for "external IP found in the binding", IntIp stands for "internal network container's IP", ExtPort stands for "external Port found in the binding", and IntPort stands for "internal network container's port."
|
|
@ -164,6 +164,7 @@ Labels can be used on task containers to override default behavior:
|
||||||
| `traefik.frontend.auth.digest.users=EXPR` | Sets digest authentication to this frontend in CSV format: `User:Realm:Hash,User:Realm:Hash`. |
|
| `traefik.frontend.auth.digest.users=EXPR` | Sets digest authentication to this frontend in CSV format: `User:Realm:Hash,User:Realm:Hash`. |
|
||||||
| `traefik.frontend.auth.digest.usersFile=/path/.htdigest` | Sets digest authentication with an external file; if users and usersFile are provided, both are merged, with external file contents having precedence. |
|
| `traefik.frontend.auth.digest.usersFile=/path/.htdigest` | Sets digest authentication with an external file; if users and usersFile are provided, both are merged, with external file contents having precedence. |
|
||||||
| `traefik.frontend.auth.forward.address=https://example.com` | Sets the URL of the authentication server. |
|
| `traefik.frontend.auth.forward.address=https://example.com` | Sets the URL of the authentication server. |
|
||||||
|
| `traefik.frontend.auth.forward.authResponseHeaders=EXPR` | Sets the forward authentication authResponseHeaders in CSV format: `X-Auth-User,X-Auth-Header` |
|
||||||
| `traefik.frontend.auth.forward.tls.ca=/path/ca.pem` | Sets the Certificate Authority (CA) for the TLS connection with the authentication server. |
|
| `traefik.frontend.auth.forward.tls.ca=/path/ca.pem` | Sets the Certificate Authority (CA) for the TLS connection with the authentication server. |
|
||||||
| `traefik.frontend.auth.forward.tls.caOptional=true` | Checks the certificates if present but do not force to be signed by a specified Certificate Authority (CA). |
|
| `traefik.frontend.auth.forward.tls.caOptional=true` | Checks the certificates if present but do not force to be signed by a specified Certificate Authority (CA). |
|
||||||
| `traefik.frontend.auth.forward.tls.cert=/path/server.pem` | Sets the Certificate for the TLS connection with the authentication server. |
|
| `traefik.frontend.auth.forward.tls.cert=/path/server.pem` | Sets the Certificate for the TLS connection with the authentication server. |
|
||||||
|
@ -258,6 +259,7 @@ Segment labels override the default behavior.
|
||||||
| `traefik.<segment_name>.frontend.auth.digest.users=EXPR` | Same as `traefik.frontend.auth.digest.users` |
|
| `traefik.<segment_name>.frontend.auth.digest.users=EXPR` | Same as `traefik.frontend.auth.digest.users` |
|
||||||
| `traefik.<segment_name>.frontend.auth.digest.usersFile=/path/.htdigest` | Same as `traefik.frontend.auth.digest.usersFile` |
|
| `traefik.<segment_name>.frontend.auth.digest.usersFile=/path/.htdigest` | Same as `traefik.frontend.auth.digest.usersFile` |
|
||||||
| `traefik.<segment_name>.frontend.auth.forward.address=https://example.com` | Same as `traefik.frontend.auth.forward.address` |
|
| `traefik.<segment_name>.frontend.auth.forward.address=https://example.com` | Same as `traefik.frontend.auth.forward.address` |
|
||||||
|
| `traefik.<segment_name>.frontend.auth.forward.authResponseHeaders=EXPR` | Same as `traefik.frontend.auth.forward.authResponseHeaders` |
|
||||||
| `traefik.<segment_name>.frontend.auth.forward.tls.ca=/path/ca.pem` | Same as `traefik.frontend.auth.forward.tls.ca` |
|
| `traefik.<segment_name>.frontend.auth.forward.tls.ca=/path/ca.pem` | Same as `traefik.frontend.auth.forward.tls.ca` |
|
||||||
| `traefik.<segment_name>.frontend.auth.forward.tls.caOptional=true` | Same as `traefik.frontend.auth.forward.tls.caOptional` |
|
| `traefik.<segment_name>.frontend.auth.forward.tls.caOptional=true` | Same as `traefik.frontend.auth.forward.tls.caOptional` |
|
||||||
| `traefik.<segment_name>.frontend.auth.forward.tls.cert=/path/server.pem` | Same as `traefik.frontend.auth.forward.tls.cert` |
|
| `traefik.<segment_name>.frontend.auth.forward.tls.cert=/path/server.pem` | Same as `traefik.frontend.auth.forward.tls.cert` |
|
||||||
|
|
|
@ -170,7 +170,6 @@ The following general annotations are applicable on the Ingress object:
|
||||||
| `traefik.ingress.kubernetes.io/service-weights: <YML>` | Set ingress backend weights specified as percentage or decimal numbers in YAML. (5) |
|
| `traefik.ingress.kubernetes.io/service-weights: <YML>` | Set ingress backend weights specified as percentage or decimal numbers in YAML. (5) |
|
||||||
| `ingress.kubernetes.io/protocol: <NAME>` | Set the protocol Traefik will use to communicate with pods. |
|
| `ingress.kubernetes.io/protocol: <NAME>` | Set the protocol Traefik will use to communicate with pods. |
|
||||||
|
|
||||||
|
|
||||||
<1> `traefik.ingress.kubernetes.io/error-pages` example:
|
<1> `traefik.ingress.kubernetes.io/error-pages` example:
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
|
@ -202,15 +201,8 @@ rateset:
|
||||||
burst: 18
|
burst: 18
|
||||||
```
|
```
|
||||||
|
|
||||||
<3> `traefik.ingress.kubernetes.io/buffering` example:
|
<3> `traefik.ingress.kubernetes.io/rule-type`
|
||||||
|
Note: `ReplacePath` is deprecated in this annotation, use the `traefik.ingress.kubernetes.io/request-modifier` annotation instead. Default: `PathPrefix`.
|
||||||
```yaml
|
|
||||||
maxrequestbodybytes: 10485760
|
|
||||||
memrequestbodybytes: 2097153
|
|
||||||
maxresponsebodybytes: 10485761
|
|
||||||
memresponsebodybytes: 2097152
|
|
||||||
retryexpression: IsNetworkError() && Attempts() <= 2
|
|
||||||
```
|
|
||||||
|
|
||||||
<4> `traefik.ingress.kubernetes.io/app-root`:
|
<4> `traefik.ingress.kubernetes.io/app-root`:
|
||||||
Non-root paths will not be affected by this annotation and handled normally.
|
Non-root paths will not be affected by this annotation and handled normally.
|
||||||
|
@ -259,13 +251,24 @@ The following annotations are applicable on the Service object associated with a
|
||||||
|
|
||||||
| Annotation | Description |
|
| Annotation | Description |
|
||||||
|--------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|
|--------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|
||||||
|
| `traefik.ingress.kubernetes.io/buffering: <YML>` | (1) See the [buffering](/configuration/commons/#buffering) section. |
|
||||||
| `traefik.ingress.kubernetes.io/affinity: "true"` | Enable backend sticky sessions. |
|
| `traefik.ingress.kubernetes.io/affinity: "true"` | Enable backend sticky sessions. |
|
||||||
| `traefik.ingress.kubernetes.io/circuit-breaker-expression: <expression>` | Set the circuit breaker expression for the backend. |
|
| `traefik.ingress.kubernetes.io/circuit-breaker-expression: <expression>` | Set the circuit breaker expression for the backend. |
|
||||||
| `traefik.ingress.kubernetes.io/load-balancer-method: drr` | Override the default `wrr` load balancer algorithm. |
|
| `traefik.ingress.kubernetes.io/load-balancer-method: drr` | Override the default `wrr` load balancer algorithm. |
|
||||||
| `traefik.ingress.kubernetes.io/max-conn-amount: "10"` | Set a maximum number of connections to the backend.<br>Must be used in conjunction with the below label to take effect. |
|
| `traefik.ingress.kubernetes.io/max-conn-amount: "10"` | Sets the maximum number of simultaneous connections to the backend.<br>Must be used in conjunction with the label below to take effect. |
|
||||||
| `traefik.ingress.kubernetes.io/max-conn-extractor-func: client.ip` | Set the function to be used against the request to determine what to limit maximum connections to the backend by.<br>Must be used in conjunction with the above label to take effect. |
|
| `traefik.ingress.kubernetes.io/max-conn-extractor-func: client.ip` | Set the function to be used against the request to determine what to limit maximum connections to the backend by.<br>Must be used in conjunction with the above label to take effect. |
|
||||||
| `traefik.ingress.kubernetes.io/session-cookie-name: <NAME>` | Manually set the cookie name for sticky sessions. |
|
| `traefik.ingress.kubernetes.io/session-cookie-name: <NAME>` | Manually set the cookie name for sticky sessions. |
|
||||||
|
|
||||||
|
<1> `traefik.ingress.kubernetes.io/buffering` example:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
maxrequestbodybytes: 10485760
|
||||||
|
memrequestbodybytes: 2097153
|
||||||
|
maxresponsebodybytes: 10485761
|
||||||
|
memresponsebodybytes: 2097152
|
||||||
|
retryexpression: IsNetworkError() && Attempts() <= 2
|
||||||
|
```
|
||||||
|
|
||||||
!!! note
|
!!! note
|
||||||
`traefik.ingress.kubernetes.io/` and `ingress.kubernetes.io/` are supported prefixes.
|
`traefik.ingress.kubernetes.io/` and `ingress.kubernetes.io/` are supported prefixes.
|
||||||
|
|
||||||
|
|
|
@ -228,6 +228,7 @@ The following labels can be defined on Marathon applications. They adjust the be
|
||||||
| `traefik.frontend.auth.digest.users=EXPR` | Sets digest authentication to this frontend in CSV format: `User:Realm:Hash,User:Realm:Hash`. |
|
| `traefik.frontend.auth.digest.users=EXPR` | Sets digest authentication to this frontend in CSV format: `User:Realm:Hash,User:Realm:Hash`. |
|
||||||
| `traefik.frontend.auth.digest.usersFile=/path/.htdigest` | Sets digest authentication with an external file; if users and usersFile are provided, both are merged, with external file contents having precedence. |
|
| `traefik.frontend.auth.digest.usersFile=/path/.htdigest` | Sets digest authentication with an external file; if users and usersFile are provided, both are merged, with external file contents having precedence. |
|
||||||
| `traefik.frontend.auth.forward.address=https://example.com` | Sets the URL of the authentication server. |
|
| `traefik.frontend.auth.forward.address=https://example.com` | Sets the URL of the authentication server. |
|
||||||
|
| `traefik.frontend.auth.forward.authResponseHeaders=EXPR` | Sets the forward authentication authResponseHeaders in CSV format: `X-Auth-User,X-Auth-Header` |
|
||||||
| `traefik.frontend.auth.forward.tls.ca=/path/ca.pem` | Sets the Certificate Authority (CA) for the TLS connection with the authentication server. |
|
| `traefik.frontend.auth.forward.tls.ca=/path/ca.pem` | Sets the Certificate Authority (CA) for the TLS connection with the authentication server. |
|
||||||
| `traefik.frontend.auth.forward.tls.caOptional=true` | Checks the certificates if present but do not force to be signed by a specified Certificate Authority (CA). |
|
| `traefik.frontend.auth.forward.tls.caOptional=true` | Checks the certificates if present but do not force to be signed by a specified Certificate Authority (CA). |
|
||||||
| `traefik.frontend.auth.forward.tls.cert=/path/server.pem` | Sets the Certificate for the TLS connection with the authentication server. |
|
| `traefik.frontend.auth.forward.tls.cert=/path/server.pem` | Sets the Certificate for the TLS connection with the authentication server. |
|
||||||
|
@ -308,60 +309,61 @@ You can define as many segments as ports exposed in an application.
|
||||||
|
|
||||||
Segment labels override the default behavior.
|
Segment labels override the default behavior.
|
||||||
|
|
||||||
| Label | Description |
|
|| Label | Description |
|
||||||
|------------------------------------------------------------------------------ |----------------------------------------------------------------|
|
|------------------------------------------------------------------------------------|------------------------------------------------------------------------|
|
||||||
| `traefik.<segment_name>.backend=BACKEND` | Same as `traefik.backend` |
|
| `traefik.<segment_name>.backend=BACKEND` | Same as `traefik.backend` |
|
||||||
| `traefik.<segment_name>.domain=DOMAIN` | Same as `traefik.domain` |
|
| `traefik.<segment_name>.domain=DOMAIN` | Same as `traefik.domain` |
|
||||||
| `traefik.<segment_name>.portIndex=1` | Same as `traefik.portIndex` |
|
| `traefik.<segment_name>.portIndex=1` | Same as `traefik.portIndex` |
|
||||||
| `traefik.<segment_name>.port=PORT` | Same as `traefik.port` |
|
| `traefik.<segment_name>.port=PORT` | Same as `traefik.port` |
|
||||||
| `traefik.<segment_name>.protocol=http` | Same as `traefik.protocol` |
|
| `traefik.<segment_name>.protocol=http` | Same as `traefik.protocol` |
|
||||||
| `traefik.<segment_name>.weight=10` | Same as `traefik.weight` |
|
| `traefik.<segment_name>.weight=10` | Same as `traefik.weight` |
|
||||||
| `traefik.<segment_name>.frontend.auth.basic=EXPR` | Same as `traefik.frontend.auth.basic` |
|
| `traefik.<segment_name>.frontend.auth.basic=EXPR` | Same as `traefik.frontend.auth.basic` |
|
||||||
| `traefik.<segment_name>.frontend.auth.basic.removeHeader=true` | Same as `traefik.frontend.auth.basic.removeHeader` |
|
| `traefik.<segment_name>.frontend.auth.basic.removeHeader=true` | Same as `traefik.frontend.auth.basic.removeHeader` |
|
||||||
| `traefik.<segment_name>.frontend.auth.basic.users=EXPR` | Same as `traefik.frontend.auth.basic.users` |
|
| `traefik.<segment_name>.frontend.auth.basic.users=EXPR` | Same as `traefik.frontend.auth.basic.users` |
|
||||||
| `traefik.<segment_name>.frontend.auth.basic.usersFile=/path/.htpasswd` | Same as `traefik.frontend.auth.basic.usersFile` |
|
| `traefik.<segment_name>.frontend.auth.basic.usersFile=/path/.htpasswd` | Same as `traefik.frontend.auth.basic.usersFile` |
|
||||||
| `traefik.<segment_name>.frontend.auth.digest.removeHeader=true` | Same as `traefik.frontend.auth.digest.removeHeader` |
|
| `traefik.<segment_name>.frontend.auth.digest.removeHeader=true` | Same as `traefik.frontend.auth.digest.removeHeader` |
|
||||||
| `traefik.<segment_name>.frontend.auth.digest.users=EXPR` | Same as `traefik.frontend.auth.digest.users` |
|
| `traefik.<segment_name>.frontend.auth.digest.users=EXPR` | Same as `traefik.frontend.auth.digest.users` |
|
||||||
| `traefik.<segment_name>.frontend.auth.digest.usersFile=/path/.htdigest` | Same as `traefik.frontend.auth.digest.usersFile` |
|
| `traefik.<segment_name>.frontend.auth.digest.usersFile=/path/.htdigest` | Same as `traefik.frontend.auth.digest.usersFile` |
|
||||||
| `traefik.<segment_name>.frontend.auth.forward.address=https://example.com` | Same as `traefik.frontend.auth.forward.address` |
|
| `traefik.<segment_name>.frontend.auth.forward.address=https://example.com` | Same as `traefik.frontend.auth.forward.address` |
|
||||||
| `traefik.<segment_name>.frontend.auth.forward.tls.ca=/path/ca.pem` | Same as `traefik.frontend.auth.forward.tls.ca` |
|
| `traefik.<segment_name>.frontend.auth.forward.authResponseHeaders=EXPR` | Same as `traefik.frontend.auth.forward.authResponseHeaders` |
|
||||||
| `traefik.<segment_name>.frontend.auth.forward.tls.caOptional=true` | Same as `traefik.frontend.auth.forward.tls.caOptional` |
|
| `traefik.<segment_name>.frontend.auth.forward.tls.ca=/path/ca.pem` | Same as `traefik.frontend.auth.forward.tls.ca` |
|
||||||
| `traefik.<segment_name>.frontend.auth.forward.tls.cert=/path/server.pem` | Same as `traefik.frontend.auth.forward.tls.cert` |
|
| `traefik.<segment_name>.frontend.auth.forward.tls.caOptional=true` | Same as `traefik.frontend.auth.forward.tls.caOptional` |
|
||||||
| `traefik.<segment_name>.frontend.auth.forward.tls.insecureSkipVerify=true` | Same as `traefik.frontend.auth.forward.tls.insecureSkipVerify` |
|
| `traefik.<segment_name>.frontend.auth.forward.tls.cert=/path/server.pem` | Same as `traefik.frontend.auth.forward.tls.cert` |
|
||||||
| `traefik.<segment_name>.frontend.auth.forward.tls.key=/path/server.key` | Same as `traefik.frontend.auth.forward.tls.key` |
|
| `traefik.<segment_name>.frontend.auth.forward.tls.insecureSkipVerify=true` | Same as `traefik.frontend.auth.forward.tls.insecureSkipVerify` |
|
||||||
| `traefik.<segment_name>.frontend.auth.forward.trustForwardHeader=true` | Same as `traefik.frontend.auth.forward.trustForwardHeader` |
|
| `traefik.<segment_name>.frontend.auth.forward.tls.key=/path/server.key` | Same as `traefik.frontend.auth.forward.tls.key` |
|
||||||
| `traefik.<segment_name>.frontend.auth.headerField=X-WebAuth-User` | Same as `traefik.frontend.auth.headerField` |
|
| `traefik.<segment_name>.frontend.auth.forward.trustForwardHeader=true` | Same as `traefik.frontend.auth.forward.trustForwardHeader` |
|
||||||
| `traefik.<segment_name>.frontend.auth.removeHeader=true` | Same as `traefik.frontend.auth.removeHeader` |
|
| `traefik.<segment_name>.frontend.auth.headerField=X-WebAuth-User` | Same as `traefik.frontend.auth.headerField` |
|
||||||
| `traefik.<segment_name>.frontend.entryPoints=https` | Same as `traefik.frontend.entryPoints` |
|
| `traefik.<segment_name>.frontend.auth.removeHeader=true` | Same as `traefik.frontend.auth.removeHeader` |
|
||||||
| `traefik.<segment_name>.frontend.errors.<name>.backend=NAME` | Same as `traefik.frontend.errors.<name>.backend` |
|
| `traefik.<segment_name>.frontend.entryPoints=https` | Same as `traefik.frontend.entryPoints` |
|
||||||
| `traefik.<segment_name>.frontend.errors.<name>.query=PATH` | Same as `traefik.frontend.errors.<name>.query` |
|
| `traefik.<segment_name>.frontend.errors.<name>.backend=NAME` | Same as `traefik.frontend.errors.<name>.backend` |
|
||||||
| `traefik.<segment_name>.frontend.errors.<name>.status=RANGE` | Same as `traefik.frontend.errors.<name>.status` |
|
| `traefik.<segment_name>.frontend.errors.<name>.query=PATH` | Same as `traefik.frontend.errors.<name>.query` |
|
||||||
| `traefik.<segment_name>.frontend.passHostHeader=true` | Same as `traefik.frontend.passHostHeader` |
|
| `traefik.<segment_name>.frontend.errors.<name>.status=RANGE` | Same as `traefik.frontend.errors.<name>.status` |
|
||||||
| `traefik.<segment_name>.frontend.passTLSClientCert.infos.notAfter=true` | Same as `traefik.frontend.passTLSClientCert.infos.notAfter` |
|
| `traefik.<segment_name>.frontend.passHostHeader=true` | Same as `traefik.frontend.passHostHeader` |
|
||||||
| `traefik.<segment_name>.frontend.passTLSClientCert.infos.notBefore=true` | Same as `traefik.frontend.passTLSClientCert.infos.notBefore` |
|
| `traefik.<segment_name>.frontend.passTLSClientCert.infos.notAfter=true` | Same as `traefik.frontend.passTLSClientCert.infos.notAfter` |
|
||||||
| `traefik.<segment_name>.frontend.passTLSClientCert.infos.sans=true` | Same as `traefik.frontend.passTLSClientCert.infos.sans` |
|
| `traefik.<segment_name>.frontend.passTLSClientCert.infos.notBefore=true` | Same as `traefik.frontend.passTLSClientCert.infos.notBefore` |
|
||||||
| `traefik.<segment_name>.frontend.passTLSClientCert.infos.subject.commonName=true` | Same as `traefik.frontend.passTLSClientCert.infos.subject.commonName` |
|
| `traefik.<segment_name>.frontend.passTLSClientCert.infos.sans=true` | Same as `traefik.frontend.passTLSClientCert.infos.sans` |
|
||||||
| `traefik.<segment_name>.frontend.passTLSClientCert.infos.subject.country=true` | Same as `traefik.frontend.passTLSClientCert.infos.subject.country` |
|
| `traefik.<segment_name>.frontend.passTLSClientCert.infos.subject.commonName=true` | Same as `traefik.frontend.passTLSClientCert.infos.subject.commonName` |
|
||||||
| `traefik.<segment_name>.frontend.passTLSClientCert.infos.subject.locality=true` | Same as `traefik.frontend.passTLSClientCert.infos.subject.locality` |
|
| `traefik.<segment_name>.frontend.passTLSClientCert.infos.subject.country=true` | Same as `traefik.frontend.passTLSClientCert.infos.subject.country` |
|
||||||
| `traefik.<segment_name>.frontend.passTLSClientCert.infos.subject.organization=true`| Same as `traefik.frontend.passTLSClientCert.infos.subject.organization`|
|
| `traefik.<segment_name>.frontend.passTLSClientCert.infos.subject.locality=true` | Same as `traefik.frontend.passTLSClientCert.infos.subject.locality` |
|
||||||
| `traefik.<segment_name>.frontend.passTLSClientCert.infos.subject.province=true` | Same as `traefik.frontend.passTLSClientCert.infos.subject.province` |
|
| `traefik.<segment_name>.frontend.passTLSClientCert.infos.subject.organization=true`| Same as `traefik.frontend.passTLSClientCert.infos.subject.organization`|
|
||||||
| `traefik.<segment_name>.frontend.passTLSClientCert.infos.subject.serialNumber=true`| Same as `traefik.frontend.passTLSClientCert.infos.subject.serialNumber`|
|
| `traefik.<segment_name>.frontend.passTLSClientCert.infos.subject.province=true` | Same as `traefik.frontend.passTLSClientCert.infos.subject.province` |
|
||||||
| `traefik.<segment_name>.frontend.passTLSClientCert.pem=true` | Same as `traefik.frontend.passTLSClientCert.infos.pem` |
|
| `traefik.<segment_name>.frontend.passTLSClientCert.infos.subject.serialNumber=true`| Same as `traefik.frontend.passTLSClientCert.infos.subject.serialNumber`|
|
||||||
| `traefik.<segment_name>.frontend.passTLSCert=true` | Same as `traefik.frontend.passTLSCert` |
|
| `traefik.<segment_name>.frontend.passTLSClientCert.pem=true` | Same as `traefik.frontend.passTLSClientCert.infos.pem` |
|
||||||
| `traefik.<segment_name>.frontend.priority=10` | Same as `traefik.frontend.priority` |
|
| `traefik.<segment_name>.frontend.passTLSCert=true` | Same as `traefik.frontend.passTLSCert` |
|
||||||
| `traefik.<segment_name>.frontend.rateLimit.extractorFunc=EXP` | Same as `traefik.frontend.rateLimit.extractorFunc` |
|
| `traefik.<segment_name>.frontend.priority=10` | Same as `traefik.frontend.priority` |
|
||||||
| `traefik.<segment_name>.frontend.rateLimit.rateSet.<name>.period=6` | Same as `traefik.frontend.rateLimit.rateSet.<name>.period` |
|
| `traefik.<segment_name>.frontend.rateLimit.extractorFunc=EXP` | Same as `traefik.frontend.rateLimit.extractorFunc` |
|
||||||
| `traefik.<segment_name>.frontend.rateLimit.rateSet.<name>.average=6` | Same as `traefik.frontend.rateLimit.rateSet.<name>.average` |
|
| `traefik.<segment_name>.frontend.rateLimit.rateSet.<name>.period=6` | Same as `traefik.frontend.rateLimit.rateSet.<name>.period` |
|
||||||
| `traefik.<segment_name>.frontend.rateLimit.rateSet.<name>.burst=6` | Same as `traefik.frontend.rateLimit.rateSet.<name>.burst` |
|
| `traefik.<segment_name>.frontend.rateLimit.rateSet.<name>.average=6` | Same as `traefik.frontend.rateLimit.rateSet.<name>.average` |
|
||||||
| `traefik.<segment_name>.frontend.redirect.entryPoint=https` | Same as `traefik.frontend.redirect.entryPoint` |
|
| `traefik.<segment_name>.frontend.rateLimit.rateSet.<name>.burst=6` | Same as `traefik.frontend.rateLimit.rateSet.<name>.burst` |
|
||||||
| `traefik.<segment_name>.frontend.redirect.regex=^http://localhost/(.*)` | Same as `traefik.frontend.redirect.regex` |
|
| `traefik.<segment_name>.frontend.redirect.entryPoint=https` | Same as `traefik.frontend.redirect.entryPoint` |
|
||||||
| `traefik.<segment_name>.frontend.redirect.replacement=http://mydomain/$1` | Same as `traefik.frontend.redirect.replacement` |
|
| `traefik.<segment_name>.frontend.redirect.regex=^http://localhost/(.*)` | Same as `traefik.frontend.redirect.regex` |
|
||||||
| `traefik.<segment_name>.frontend.redirect.permanent=true` | Same as `traefik.frontend.redirect.permanent` |
|
| `traefik.<segment_name>.frontend.redirect.replacement=http://mydomain/$1` | Same as `traefik.frontend.redirect.replacement` |
|
||||||
| `traefik.<segment_name>.frontend.rule=EXP` | Same as `traefik.frontend.rule` |
|
| `traefik.<segment_name>.frontend.redirect.permanent=true` | Same as `traefik.frontend.redirect.permanent` |
|
||||||
| `traefik.<segment_name>.frontend.whiteList.sourceRange=RANGE` | Same as `traefik.frontend.whiteList.sourceRange` |
|
| `traefik.<segment_name>.frontend.rule=EXP` | Same as `traefik.frontend.rule` |
|
||||||
| `traefik.<segment_name>.frontend.whiteList.ipStrategy=true` | Same as `traefik.frontend.whiteList.ipStrategy` |
|
| `traefik.<segment_name>.frontend.whiteList.sourceRange=RANGE` | Same as `traefik.frontend.whiteList.sourceRange` |
|
||||||
| `traefik.<segment_name>.frontend.whiteList.ipStrategy.depth=5` | Same as `traefik.frontend.whiteList.ipStrategy.depth` |
|
| `traefik.<segment_name>.frontend.whiteList.ipStrategy=true` | Same as `traefik.frontend.whiteList.ipStrategy` |
|
||||||
| `traefik.<segment_name>.frontend.whiteList.ipStrategy.excludedIPs=127.0.0.1` | Same as `traefik.frontend.whiteList.ipStrategy.excludedIPs` |
|
| `traefik.<segment_name>.frontend.whiteList.ipStrategy.depth=5` | Same as `traefik.frontend.whiteList.ipStrategy.depth` |
|
||||||
|
| `traefik.<segment_name>.frontend.whiteList.ipStrategy.excludedIPs=127.0.0.1` | Same as `traefik.frontend.whiteList.ipStrategy.excludedIPs` |
|
||||||
|
|
||||||
#### Custom Headers
|
#### Custom Headers
|
||||||
|
|
||||||
|
|
|
@ -106,55 +106,56 @@ domain = "mesos.localhost"
|
||||||
|
|
||||||
The following labels can be defined on Mesos tasks. They adjust the behavior for the entire application.
|
The following labels can be defined on Mesos tasks. They adjust the behavior for the entire application.
|
||||||
|
|
||||||
| Label | Description |
|
| Label | Description |
|
||||||
|-----------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|
|---------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|
||||||
| `traefik.domain` | Sets the default domain for the frontend rules. |
|
| `traefik.domain` | Sets the default domain for the frontend rules. |
|
||||||
| `traefik.enable=false` | Disables this container in Træfik. |
|
| `traefik.enable=false` | Disables this container in Træfik. |
|
||||||
| `traefik.port=80` | Registers this port. Useful when the application exposes multiple ports. |
|
| `traefik.port=80` | Registers this port. Useful when the application exposes multiple ports. |
|
||||||
| `traefik.portName=web` | Registers port by name in the application's ports array. Useful when the application exposes multiple ports. |
|
| `traefik.portName=web` | Registers port by name in the application's ports array. Useful when the application exposes multiple ports. |
|
||||||
| `traefik.portIndex=1` | Registers port by index in the application's ports array. Useful when the application exposes multiple ports. |
|
| `traefik.portIndex=1` | Registers port by index in the application's ports array. Useful when the application exposes multiple ports. |
|
||||||
| `traefik.protocol=https` | Overrides the default `http` protocol |
|
| `traefik.protocol=https` | Overrides the default `http` protocol |
|
||||||
| `traefik.weight=10` | Assigns this weight to the container |
|
| `traefik.weight=10` | Assigns this weight to the container |
|
||||||
| `traefik.backend=foo` | Gives the name `foo` to the generated backend for this container. |
|
| `traefik.backend=foo` | Gives the name `foo` to the generated backend for this container. |
|
||||||
| `traefik.backend.buffering.maxRequestBodyBytes=0` | See [buffering](/configuration/commons/#buffering) section. |
|
| `traefik.backend.buffering.maxRequestBodyBytes=0` | See [buffering](/configuration/commons/#buffering) section. |
|
||||||
| `traefik.backend.buffering.maxResponseBodyBytes=0` | See [buffering](/configuration/commons/#buffering) section. |
|
| `traefik.backend.buffering.maxResponseBodyBytes=0` | See [buffering](/configuration/commons/#buffering) section. |
|
||||||
| `traefik.backend.buffering.memRequestBodyBytes=0` | See [buffering](/configuration/commons/#buffering) section. |
|
| `traefik.backend.buffering.memRequestBodyBytes=0` | See [buffering](/configuration/commons/#buffering) section. |
|
||||||
| `traefik.backend.buffering.memResponseBodyBytes=0` | See [buffering](/configuration/commons/#buffering) section. |
|
| `traefik.backend.buffering.memResponseBodyBytes=0` | See [buffering](/configuration/commons/#buffering) section. |
|
||||||
| `traefik.backend.buffering.retryExpression=EXPR` | See [buffering](/configuration/commons/#buffering) section. |
|
| `traefik.backend.buffering.retryExpression=EXPR` | See [buffering](/configuration/commons/#buffering) section. |
|
||||||
| `traefik.backend.circuitbreaker.expression=EXPR` | Creates a [circuit breaker](/basics/#backends) to be used against the backend |
|
| `traefik.backend.circuitbreaker.expression=EXPR` | Creates a [circuit breaker](/basics/#backends) to be used against the backend |
|
||||||
| `traefik.backend.healthcheck.path=/health` | Enables health check for the backend, hitting the container at `path`. |
|
| `traefik.backend.healthcheck.path=/health` | Enables health check for the backend, hitting the container at `path`. |
|
||||||
| `traefik.backend.healthcheck.interval=5s` | Defines the health check interval. (Default: 30s) |
|
| `traefik.backend.healthcheck.interval=5s` | Defines the health check interval. (Default: 30s) |
|
||||||
| `traefik.backend.healthcheck.timeout=3s` | Defines the health check request timeout. (Default: 5s) |
|
| `traefik.backend.healthcheck.timeout=3s` | Defines the health check request timeout. (Default: 5s) |
|
||||||
| `traefik.backend.healthcheck.scheme=http` | Overrides the server URL scheme. |
|
| `traefik.backend.healthcheck.scheme=http` | Overrides the server URL scheme. |
|
||||||
| `traefik.backend.healthcheck.port=8080` | Sets a different port for the health check. |
|
| `traefik.backend.healthcheck.port=8080` | Sets a different port for the health check. |
|
||||||
| `traefik.backend.healthcheck.hostname=foobar.com` | Defines the health check hostname. |
|
| `traefik.backend.healthcheck.hostname=foobar.com` | Defines the health check hostname. |
|
||||||
| `traefik.backend.healthcheck.headers=EXPR` | Defines the health check request headers <br>Format: <code>HEADER:value||HEADER2:value2</code> |
|
| `traefik.backend.healthcheck.headers=EXPR` | Defines the health check request headers <br>Format: <code>HEADER:value||HEADER2:value2</code> |
|
||||||
| `traefik.backend.loadbalancer.method=drr` | Overrides the default `wrr` load balancer algorithm |
|
| `traefik.backend.loadbalancer.method=drr` | Overrides the default `wrr` load balancer algorithm |
|
||||||
| `traefik.backend.loadbalancer.stickiness=true` | Enables backend sticky sessions |
|
| `traefik.backend.loadbalancer.stickiness=true` | Enables backend sticky sessions |
|
||||||
| `traefik.backend.loadbalancer.stickiness.cookieName=NAME` | Sets the cookie manually name for sticky sessions |
|
| `traefik.backend.loadbalancer.stickiness.cookieName=NAME` | Sets the cookie manually name for sticky sessions |
|
||||||
| `traefik.backend.maxconn.amount=10` | Sets a maximum number of connections to the backend.<br>Must be used in conjunction with the below label to take effect. |
|
| `traefik.backend.maxconn.amount=10` | Sets a maximum number of connections to the backend.<br>Must be used in conjunction with the below label to take effect. |
|
||||||
| `traefik.backend.maxconn.extractorfunc=client.ip` | Sets the function to be used against the request to determine what to limit maximum connections to the backend by.<br>Must be used in conjunction with the above label to take effect. |
|
| `traefik.backend.maxconn.extractorfunc=client.ip` | Sets the function to be used against the request to determine what to limit maximum connections to the backend by.<br>Must be used in conjunction with the above label to take effect. |
|
||||||
| `traefik.frontend.auth.basic=EXPR` | Sets basic authentication to this frontend in CSV format: `User:Hash,User:Hash` (DEPRECATED). |
|
| `traefik.frontend.auth.basic=EXPR` | Sets basic authentication to this frontend in CSV format: `User:Hash,User:Hash` (DEPRECATED). |
|
||||||
| `traefik.frontend.auth.basic.users=EXPR` | Sets basic authentication to this frontend in CSV format: `User:Hash,User:Hash`. |
|
| `traefik.frontend.auth.basic.users=EXPR` | Sets basic authentication to this frontend in CSV format: `User:Hash,User:Hash`. |
|
||||||
| `traefik.frontend.auth.basic.removeHeader=true` | If set to `true`, removes the `Authorization` header. |
|
| `traefik.frontend.auth.basic.removeHeader=true` | If set to `true`, removes the `Authorization` header. |
|
||||||
| `traefik.frontend.auth.basic.usersFile=/path/.htpasswd` | Sets basic authentication with an external file; if users and usersFile are provided, both are merged, with external file contents having precedence. |
|
| `traefik.frontend.auth.basic.usersFile=/path/.htpasswd` | Sets basic authentication with an external file; if users and usersFile are provided, both are merged, with external file contents having precedence. |
|
||||||
| `traefik.frontend.auth.digest.removeHeader=true` | If set to `true`, removes the `Authorization` header. |
|
| `traefik.frontend.auth.digest.removeHeader=true` | If set to `true`, removes the `Authorization` header. |
|
||||||
| `traefik.frontend.auth.digest.users=EXPR` | Sets digest authentication to this frontend in CSV format: `User:Realm:Hash,User:Realm:Hash`. |
|
| `traefik.frontend.auth.digest.users=EXPR` | Sets digest authentication to this frontend in CSV format: `User:Realm:Hash,User:Realm:Hash`. |
|
||||||
| `traefik.frontend.auth.digest.usersFile=/path/.htdigest` | Sets digest authentication with an external file; if users and usersFile are provided, both are merged, with external file contents having precedence. |
|
| `traefik.frontend.auth.digest.usersFile=/path/.htdigest` | Sets digest authentication with an external file; if users and usersFile are provided, both are merged, with external file contents having precedence. |
|
||||||
| `traefik.frontend.auth.forward.address=https://example.com` | Sets the URL of the authentication server. |
|
| `traefik.frontend.auth.forward.address=https://example.com` | Sets the URL of the authentication server. |
|
||||||
| `traefik.frontend.auth.forward.tls.ca=/path/ca.pem` | Sets the Certificate Authority (CA) for the TLS connection with the authentication server. |
|
| `traefik.frontend.auth.forward.authResponseHeaders=EXPR` | Sets the forward authentication authResponseHeaders in CSV format: `X-Auth-User,X-Auth-Header` |
|
||||||
| `traefik.frontend.auth.forward.tls.caOptional=true` | Checks the certificates if present but do not force to be signed by a specified Certificate Authority (CA). |
|
| `traefik.frontend.auth.forward.tls.ca=/path/ca.pem` | Sets the Certificate Authority (CA) for the TLS connection with the authentication server. |
|
||||||
| `traefik.frontend.auth.forward.tls.cert=/path/server.pem` | Sets the Certificate for the TLS connection with the authentication server. |
|
| `traefik.frontend.auth.forward.tls.caOptional=true` | Checks the certificates if present but do not force to be signed by a specified Certificate Authority (CA). |
|
||||||
| `traefik.frontend.auth.forward.tls.insecureSkipVerify=true` | If set to true invalid SSL certificates are accepted. |
|
| `traefik.frontend.auth.forward.tls.cert=/path/server.pem` | Sets the Certificate for the TLS connection with the authentication server. |
|
||||||
| `traefik.frontend.auth.forward.tls.key=/path/server.key` | Sets the Certificate for the TLS connection with the authentication server. |
|
| `traefik.frontend.auth.forward.tls.insecureSkipVerify=true` | If set to true invalid SSL certificates are accepted. |
|
||||||
| `traefik.frontend.auth.forward.trustForwardHeader=true` | Trusts X-Forwarded-* headers. |
|
| `traefik.frontend.auth.forward.tls.key=/path/server.key` | Sets the Certificate for the TLS connection with the authentication server. |
|
||||||
| `traefik.frontend.auth.headerField=X-WebAuth-User` | Sets the header used to pass the authenticated user to the application. |
|
| `traefik.frontend.auth.forward.trustForwardHeader=true` | Trusts X-Forwarded-* headers. |
|
||||||
| `traefik.frontend.auth.removeHeader=true` | If set to true, removes the Authorization header. |
|
| `traefik.frontend.auth.headerField=X-WebAuth-User` | Sets the header used to pass the authenticated user to the application. |
|
||||||
| `traefik.frontend.entryPoints=http,https` | Assigns this frontend to entry points `http` and `https`.<br>Overrides `defaultEntryPoints` |
|
| `traefik.frontend.auth.removeHeader=true` | If set to true, removes the Authorization header. |
|
||||||
| `traefik.frontend.errors.<name>.backend=NAME` | See [custom error pages](/configuration/commons/#custom-error-pages) section. |
|
| `traefik.frontend.entryPoints=http,https` | Assigns this frontend to entry points `http` and `https`.<br>Overrides `defaultEntryPoints` |
|
||||||
| `traefik.frontend.errors.<name>.query=PATH` | See [custom error pages](/configuration/commons/#custom-error-pages) section. |
|
| `traefik.frontend.errors.<name>.backend=NAME` | See [custom error pages](/configuration/commons/#custom-error-pages) section. |
|
||||||
| `traefik.frontend.errors.<name>.status=RANGE` | See [custom error pages](/configuration/commons/#custom-error-pages) section. |
|
| `traefik.frontend.errors.<name>.query=PATH` | See [custom error pages](/configuration/commons/#custom-error-pages) section. |
|
||||||
| `traefik.frontend.passHostHeader=true` | Forwards client `Host` header to the backend. |
|
| `traefik.frontend.errors.<name>.status=RANGE` | See [custom error pages](/configuration/commons/#custom-error-pages) section. |
|
||||||
|
| `traefik.frontend.passHostHeader=true` | Forwards client `Host` header to the backend. |
|
||||||
| `traefik.frontend.passTLSClientCert.infos.notAfter=true` | Add the noAfter field in a escaped client infos in the `X-Forwarded-Ssl-Client-Cert-Infos` header. |
|
| `traefik.frontend.passTLSClientCert.infos.notAfter=true` | Add the noAfter field in a escaped client infos in the `X-Forwarded-Ssl-Client-Cert-Infos` header. |
|
||||||
| `traefik.frontend.passTLSClientCert.infos.notBefore=true` | Add the noBefore field in a escaped client infos in the `X-Forwarded-Ssl-Client-Cert-Infos` header. |
|
| `traefik.frontend.passTLSClientCert.infos.notBefore=true` | Add the noBefore field in a escaped client infos in the `X-Forwarded-Ssl-Client-Cert-Infos` header. |
|
||||||
| `traefik.frontend.passTLSClientCert.infos.sans=true` | Add the sans field in a escaped client infos in the `X-Forwarded-Ssl-Client-Cert-Infos` header. |
|
| `traefik.frontend.passTLSClientCert.infos.sans=true` | Add the sans field in a escaped client infos in the `X-Forwarded-Ssl-Client-Cert-Infos` header. |
|
||||||
|
@ -165,21 +166,21 @@ The following labels can be defined on Mesos tasks. They adjust the behavior for
|
||||||
| `traefik.frontend.passTLSClientCert.infos.subject.province=true` | Add the subject.province field in a escaped client infos in the `X-Forwarded-Ssl-Client-Cert-Infos` header. |
|
| `traefik.frontend.passTLSClientCert.infos.subject.province=true` | Add the subject.province field in a escaped client infos in the `X-Forwarded-Ssl-Client-Cert-Infos` header. |
|
||||||
| `traefik.frontend.passTLSClientCert.infos.subject.serialNumber=true`| Add the subject.serialNumber field in a escaped client infos in the `X-Forwarded-Ssl-Client-Cert-Infos` header. |
|
| `traefik.frontend.passTLSClientCert.infos.subject.serialNumber=true`| Add the subject.serialNumber field in a escaped client infos in the `X-Forwarded-Ssl-Client-Cert-Infos` header. |
|
||||||
| `traefik.frontend.passTLSClientCert.pem=true` | Pass the escaped pem in the `X-Forwarded-Ssl-Client-Cert` header. |
|
| `traefik.frontend.passTLSClientCert.pem=true` | Pass the escaped pem in the `X-Forwarded-Ssl-Client-Cert` header. |
|
||||||
| `traefik.frontend.passTLSCert=true` | Forwards TLS Client certificates to the backend. |
|
| `traefik.frontend.passTLSCert=true` | Forwards TLS Client certificates to the backend. |
|
||||||
| `traefik.frontend.priority=10` | Overrides default frontend priority |
|
| `traefik.frontend.priority=10` | Overrides default frontend priority |
|
||||||
| `traefik.frontend.rateLimit.extractorFunc=EXP` | See [rate limiting](/configuration/commons/#rate-limiting) section. |
|
| `traefik.frontend.rateLimit.extractorFunc=EXP` | See [rate limiting](/configuration/commons/#rate-limiting) section. |
|
||||||
| `traefik.frontend.rateLimit.rateSet.<name>.period=6` | See [rate limiting](/configuration/commons/#rate-limiting) section. |
|
| `traefik.frontend.rateLimit.rateSet.<name>.period=6` | See [rate limiting](/configuration/commons/#rate-limiting) section. |
|
||||||
| `traefik.frontend.rateLimit.rateSet.<name>.average=6` | See [rate limiting](/configuration/commons/#rate-limiting) section. |
|
| `traefik.frontend.rateLimit.rateSet.<name>.average=6` | See [rate limiting](/configuration/commons/#rate-limiting) section. |
|
||||||
| `traefik.frontend.rateLimit.rateSet.<name>.burst=6` | See [rate limiting](/configuration/commons/#rate-limiting) section. |
|
| `traefik.frontend.rateLimit.rateSet.<name>.burst=6` | See [rate limiting](/configuration/commons/#rate-limiting) section. |
|
||||||
| `traefik.frontend.redirect.entryPoint=https` | Enables Redirect to another entryPoint to this frontend (e.g. HTTPS) |
|
| `traefik.frontend.redirect.entryPoint=https` | Enables Redirect to another entryPoint to this frontend (e.g. HTTPS) |
|
||||||
| `traefik.frontend.redirect.regex=^http://localhost/(.*)` | Redirects to another URL to this frontend.<br>Must be set with `traefik.frontend.redirect.replacement`. |
|
| `traefik.frontend.redirect.regex=^http://localhost/(.*)` | Redirects to another URL to this frontend.<br>Must be set with `traefik.frontend.redirect.replacement`. |
|
||||||
| `traefik.frontend.redirect.replacement=http://mydomain/$1` | Redirects to another URL to this frontend.<br>Must be set with `traefik.frontend.redirect.regex`. |
|
| `traefik.frontend.redirect.replacement=http://mydomain/$1` | Redirects to another URL to this frontend.<br>Must be set with `traefik.frontend.redirect.regex`. |
|
||||||
| `traefik.frontend.redirect.permanent=true` | Returns 301 instead of 302. |
|
| `traefik.frontend.redirect.permanent=true` | Returns 301 instead of 302. |
|
||||||
| `traefik.frontend.rule=EXPR` | Overrides the default frontend rule. Default: `Host:{discovery_name}.{domain}`. |
|
| `traefik.frontend.rule=EXPR` | Overrides the default frontend rule. Default: `Host:{discovery_name}.{domain}`. |
|
||||||
| `traefik.frontend.whiteList.sourceRange=RANGE` | Sets a list of IP-Ranges which are allowed to access.<br>An unset or empty list allows all Source-IPs to access. If one of the Net-Specifications are invalid, the whole list is invalid and allows all Source-IPs to access. |
|
| `traefik.frontend.whiteList.sourceRange=RANGE` | Sets a list of IP-Ranges which are allowed to access.<br>An unset or empty list allows all Source-IPs to access. If one of the Net-Specifications are invalid, the whole list is invalid and allows all Source-IPs to access. |
|
||||||
| `traefik.frontend.whiteList.ipStrategy=true` | Uses the default IPStrategy.<br>Can be used when there is an existing `clientIPStrategy` but you want the remote address for whitelisting. |
|
| `traefik.frontend.whiteList.ipStrategy=true` | Uses the default IPStrategy.<br>Can be used when there is an existing `clientIPStrategy` but you want the remote address for whitelisting. |
|
||||||
| `traefik.frontend.whiteList.ipStrategy.depth=5` | See [whitelist](/configuration/entrypoints/#white-listing) |
|
| `traefik.frontend.whiteList.ipStrategy.depth=5` | See [whitelist](/configuration/entrypoints/#white-listing) |
|
||||||
| `traefik.frontend.whiteList.ipStrategy.excludedIPs=127.0.0.1` | See [whitelist](/configuration/entrypoints/#white-listing) |
|
| `traefik.frontend.whiteList.ipStrategy.excludedIPs=127.0.0.1` | See [whitelist](/configuration/entrypoints/#white-listing) |
|
||||||
|
|
||||||
### Custom Headers
|
### Custom Headers
|
||||||
|
|
||||||
|
@ -223,36 +224,37 @@ Additionally, if a segment name matches a named port, that port will be used unl
|
||||||
|
|
||||||
Segment labels override the default behavior.
|
Segment labels override the default behavior.
|
||||||
|
|
||||||
| Label | Description |
|
| Label | Description |
|
||||||
|------------------------------------------------------------------------------|----------------------------------------------------------------|
|
|------------------------------------------------------------------------------------|------------------------------------------------------------------------|
|
||||||
| `traefik.<segment_name>.backend=BACKEND` | Same as `traefik.backend` |
|
| `traefik.<segment_name>.backend=BACKEND` | Same as `traefik.backend` |
|
||||||
| `traefik.<segment_name>.domain=DOMAIN` | Same as `traefik.domain` |
|
| `traefik.<segment_name>.domain=DOMAIN` | Same as `traefik.domain` |
|
||||||
| `traefik.<segment_name>.portIndex=1` | Same as `traefik.portIndex` |
|
| `traefik.<segment_name>.portIndex=1` | Same as `traefik.portIndex` |
|
||||||
| `traefik.<segment_name>.portName=web` | Same as `traefik.portName` |
|
| `traefik.<segment_name>.portName=web` | Same as `traefik.portName` |
|
||||||
| `traefik.<segment_name>.port=PORT` | Same as `traefik.port` |
|
| `traefik.<segment_name>.port=PORT` | Same as `traefik.port` |
|
||||||
| `traefik.<segment_name>.protocol=http` | Same as `traefik.protocol` |
|
| `traefik.<segment_name>.protocol=http` | Same as `traefik.protocol` |
|
||||||
| `traefik.<segment_name>.weight=10` | Same as `traefik.weight` |
|
| `traefik.<segment_name>.weight=10` | Same as `traefik.weight` |
|
||||||
| `traefik.<segment_name>.frontend.auth.basic=EXPR` | Same as `traefik.frontend.auth.basic` |
|
| `traefik.<segment_name>.frontend.auth.basic=EXPR` | Same as `traefik.frontend.auth.basic` |
|
||||||
| `traefik.<segment_name>.frontend.auth.basic.removeHeader=true` | Same as `traefik.frontend.auth.basic.removeHeader` |
|
| `traefik.<segment_name>.frontend.auth.basic.removeHeader=true` | Same as `traefik.frontend.auth.basic.removeHeader` |
|
||||||
| `traefik.<segment_name>.frontend.auth.basic.users=EXPR` | Same as `traefik.frontend.auth.basic.users` |
|
| `traefik.<segment_name>.frontend.auth.basic.users=EXPR` | Same as `traefik.frontend.auth.basic.users` |
|
||||||
| `traefik.<segment_name>.frontend.auth.basic.usersFile=/path/.htpasswd` | Same as `traefik.frontend.auth.basic.usersFile` |
|
| `traefik.<segment_name>.frontend.auth.basic.usersFile=/path/.htpasswd` | Same as `traefik.frontend.auth.basic.usersFile` |
|
||||||
| `traefik.<segment_name>.frontend.auth.digest.removeHeader=true` | Same as `traefik.frontend.auth.digest.removeHeader` |
|
| `traefik.<segment_name>.frontend.auth.digest.removeHeader=true` | Same as `traefik.frontend.auth.digest.removeHeader` |
|
||||||
| `traefik.<segment_name>.frontend.auth.digest.users=EXPR` | Same as `traefik.frontend.auth.digest.users` |
|
| `traefik.<segment_name>.frontend.auth.digest.users=EXPR` | Same as `traefik.frontend.auth.digest.users` |
|
||||||
| `traefik.<segment_name>.frontend.auth.digest.usersFile=/path/.htdigest` | Same as `traefik.frontend.auth.digest.usersFile` |
|
| `traefik.<segment_name>.frontend.auth.digest.usersFile=/path/.htdigest` | Same as `traefik.frontend.auth.digest.usersFile` |
|
||||||
| `traefik.<segment_name>.frontend.auth.forward.address=https://example.com` | Same as `traefik.frontend.auth.forward.address` |
|
| `traefik.<segment_name>.frontend.auth.forward.address=https://example.com` | Same as `traefik.frontend.auth.forward.address` |
|
||||||
| `traefik.<segment_name>.frontend.auth.forward.tls.ca=/path/ca.pem` | Same as `traefik.frontend.auth.forward.tls.ca` |
|
| `traefik.<segment_name>.frontend.auth.forward.authResponseHeaders=EXPR` | Same as `traefik.frontend.auth.forward.authResponseHeaders` |
|
||||||
| `traefik.<segment_name>.frontend.auth.forward.tls.caOptional=true` | Same as `traefik.frontend.auth.forward.tls.caOptional` |
|
| `traefik.<segment_name>.frontend.auth.forward.tls.ca=/path/ca.pem` | Same as `traefik.frontend.auth.forward.tls.ca` |
|
||||||
| `traefik.<segment_name>.frontend.auth.forward.tls.cert=/path/server.pem` | Same as `traefik.frontend.auth.forward.tls.cert` |
|
| `traefik.<segment_name>.frontend.auth.forward.tls.caOptional=true` | Same as `traefik.frontend.auth.forward.tls.caOptional` |
|
||||||
| `traefik.<segment_name>.frontend.auth.forward.tls.insecureSkipVerify=true` | Same as `traefik.frontend.auth.forward.tls.insecureSkipVerify` |
|
| `traefik.<segment_name>.frontend.auth.forward.tls.cert=/path/server.pem` | Same as `traefik.frontend.auth.forward.tls.cert` |
|
||||||
| `traefik.<segment_name>.frontend.auth.forward.tls.key=/path/server.key` | Same as `traefik.frontend.auth.forward.tls.key` |
|
| `traefik.<segment_name>.frontend.auth.forward.tls.insecureSkipVerify=true` | Same as `traefik.frontend.auth.forward.tls.insecureSkipVerify` |
|
||||||
| `traefik.<segment_name>.frontend.auth.forward.trustForwardHeader=true` | Same as `traefik.frontend.auth.forward.trustForwardHeader` |
|
| `traefik.<segment_name>.frontend.auth.forward.tls.key=/path/server.key` | Same as `traefik.frontend.auth.forward.tls.key` |
|
||||||
| `traefik.<segment_name>.frontend.auth.headerField=X-WebAuth-User` | Same as `traefik.frontend.auth.headerField` |
|
| `traefik.<segment_name>.frontend.auth.forward.trustForwardHeader=true` | Same as `traefik.frontend.auth.forward.trustForwardHeader` |
|
||||||
| `traefik.<segment_name>.frontend.auth.removeHeader=true` | Same as `traefik.frontend.auth.removeHeader` |
|
| `traefik.<segment_name>.frontend.auth.headerField=X-WebAuth-User` | Same as `traefik.frontend.auth.headerField` |
|
||||||
| `traefik.<segment_name>.frontend.entryPoints=https` | Same as `traefik.frontend.entryPoints` |
|
| `traefik.<segment_name>.frontend.auth.removeHeader=true` | Same as `traefik.frontend.auth.removeHeader` |
|
||||||
| `traefik.<segment_name>.frontend.errors.<name>.backend=NAME` | Same as `traefik.frontend.errors.<name>.backend` |
|
| `traefik.<segment_name>.frontend.entryPoints=https` | Same as `traefik.frontend.entryPoints` |
|
||||||
| `traefik.<segment_name>.frontend.errors.<name>.query=PATH` | Same as `traefik.frontend.errors.<name>.query` |
|
| `traefik.<segment_name>.frontend.errors.<name>.backend=NAME` | Same as `traefik.frontend.errors.<name>.backend` |
|
||||||
| `traefik.<segment_name>.frontend.errors.<name>.status=RANGE` | Same as `traefik.frontend.errors.<name>.status` |
|
| `traefik.<segment_name>.frontend.errors.<name>.query=PATH` | Same as `traefik.frontend.errors.<name>.query` |
|
||||||
| `traefik.<segment_name>.frontend.passHostHeader=true` | Same as `traefik.frontend.passHostHeader` |
|
| `traefik.<segment_name>.frontend.errors.<name>.status=RANGE` | Same as `traefik.frontend.errors.<name>.status` |
|
||||||
|
| `traefik.<segment_name>.frontend.passHostHeader=true` | Same as `traefik.frontend.passHostHeader` |
|
||||||
| `traefik.<segment_name>.frontend.passTLSClientCert.infos.notAfter=true` | Same as `traefik.frontend.passTLSClientCert.infos.notAfter` |
|
| `traefik.<segment_name>.frontend.passTLSClientCert.infos.notAfter=true` | Same as `traefik.frontend.passTLSClientCert.infos.notAfter` |
|
||||||
| `traefik.<segment_name>.frontend.passTLSClientCert.infos.notBefore=true` | Same as `traefik.frontend.passTLSClientCert.infos.notBefore` |
|
| `traefik.<segment_name>.frontend.passTLSClientCert.infos.notBefore=true` | Same as `traefik.frontend.passTLSClientCert.infos.notBefore` |
|
||||||
| `traefik.<segment_name>.frontend.passTLSClientCert.infos.sans=true` | Same as `traefik.frontend.passTLSClientCert.infos.sans` |
|
| `traefik.<segment_name>.frontend.passTLSClientCert.infos.sans=true` | Same as `traefik.frontend.passTLSClientCert.infos.sans` |
|
||||||
|
@ -263,21 +265,21 @@ Segment labels override the default behavior.
|
||||||
| `traefik.<segment_name>.frontend.passTLSClientCert.infos.subject.province=true` | Same as `traefik.frontend.passTLSClientCert.infos.subject.province` |
|
| `traefik.<segment_name>.frontend.passTLSClientCert.infos.subject.province=true` | Same as `traefik.frontend.passTLSClientCert.infos.subject.province` |
|
||||||
| `traefik.<segment_name>.frontend.passTLSClientCert.infos.subject.serialNumber=true`| Same as `traefik.frontend.passTLSClientCert.infos.subject.serialNumber`|
|
| `traefik.<segment_name>.frontend.passTLSClientCert.infos.subject.serialNumber=true`| Same as `traefik.frontend.passTLSClientCert.infos.subject.serialNumber`|
|
||||||
| `traefik.<segment_name>.frontend.passTLSClientCert.pem=true` | Same as `traefik.frontend.passTLSClientCert.infos.pem` |
|
| `traefik.<segment_name>.frontend.passTLSClientCert.pem=true` | Same as `traefik.frontend.passTLSClientCert.infos.pem` |
|
||||||
| `traefik.<segment_name>.frontend.passTLSCert=true` | Same as `traefik.frontend.passTLSCert` |
|
| `traefik.<segment_name>.frontend.passTLSCert=true` | Same as `traefik.frontend.passTLSCert` |
|
||||||
| `traefik.<segment_name>.frontend.priority=10` | Same as `traefik.frontend.priority` |
|
| `traefik.<segment_name>.frontend.priority=10` | Same as `traefik.frontend.priority` |
|
||||||
| `traefik.<segment_name>.frontend.rateLimit.extractorFunc=EXP` | Same as `traefik.frontend.rateLimit.extractorFunc` |
|
| `traefik.<segment_name>.frontend.rateLimit.extractorFunc=EXP` | Same as `traefik.frontend.rateLimit.extractorFunc` |
|
||||||
| `traefik.<segment_name>.frontend.rateLimit.rateSet.<name>.period=6` | Same as `traefik.frontend.rateLimit.rateSet.<name>.period` |
|
| `traefik.<segment_name>.frontend.rateLimit.rateSet.<name>.period=6` | Same as `traefik.frontend.rateLimit.rateSet.<name>.period` |
|
||||||
| `traefik.<segment_name>.frontend.rateLimit.rateSet.<name>.average=6` | Same as `traefik.frontend.rateLimit.rateSet.<name>.average` |
|
| `traefik.<segment_name>.frontend.rateLimit.rateSet.<name>.average=6` | Same as `traefik.frontend.rateLimit.rateSet.<name>.average` |
|
||||||
| `traefik.<segment_name>.frontend.rateLimit.rateSet.<name>.burst=6` | Same as `traefik.frontend.rateLimit.rateSet.<name>.burst` |
|
| `traefik.<segment_name>.frontend.rateLimit.rateSet.<name>.burst=6` | Same as `traefik.frontend.rateLimit.rateSet.<name>.burst` |
|
||||||
| `traefik.<segment_name>.frontend.redirect.entryPoint=https` | Same as `traefik.frontend.redirect.entryPoint` |
|
| `traefik.<segment_name>.frontend.redirect.entryPoint=https` | Same as `traefik.frontend.redirect.entryPoint` |
|
||||||
| `traefik.<segment_name>.frontend.redirect.regex=^http://localhost/(.*)` | Same as `traefik.frontend.redirect.regex` |
|
| `traefik.<segment_name>.frontend.redirect.regex=^http://localhost/(.*)` | Same as `traefik.frontend.redirect.regex` |
|
||||||
| `traefik.<segment_name>.frontend.redirect.replacement=http://mydomain/$1` | Same as `traefik.frontend.redirect.replacement` |
|
| `traefik.<segment_name>.frontend.redirect.replacement=http://mydomain/$1` | Same as `traefik.frontend.redirect.replacement` |
|
||||||
| `traefik.<segment_name>.frontend.redirect.permanent=true` | Same as `traefik.frontend.redirect.permanent` |
|
| `traefik.<segment_name>.frontend.redirect.permanent=true` | Same as `traefik.frontend.redirect.permanent` |
|
||||||
| `traefik.<segment_name>.frontend.rule=EXP` | Same as `traefik.frontend.rule` |
|
| `traefik.<segment_name>.frontend.rule=EXP` | Same as `traefik.frontend.rule` |
|
||||||
| `traefik.<segment_name>.frontend.whiteList.sourceRange=RANGE` | Same as `traefik.frontend.whiteList.sourceRange` |
|
| `traefik.<segment_name>.frontend.whiteList.sourceRange=RANGE` | Same as `traefik.frontend.whiteList.sourceRange` |
|
||||||
| `traefik.<segment_name>.frontend.whiteList.ipStrategy=true` | Same as `traefik.frontend.whiteList.ipStrategy` |
|
| `traefik.<segment_name>.frontend.whiteList.ipStrategy=true` | Same as `traefik.frontend.whiteList.ipStrategy` |
|
||||||
| `traefik.<segment_name>.frontend.whiteList.ipStrategy.depth=5` | Same as `traefik.frontend.whiteList.ipStrategy.depth` |
|
| `traefik.<segment_name>.frontend.whiteList.ipStrategy.depth=5` | Same as `traefik.frontend.whiteList.ipStrategy.depth` |
|
||||||
| `traefik.<segment_name>.frontend.whiteList.ipStrategy.excludedIPs=127.0.0.1` | Same as `traefik.frontend.whiteList.ipStrategy.excludedIPs` |
|
| `traefik.<segment_name>.frontend.whiteList.ipStrategy.excludedIPs=127.0.0.1` | Same as `traefik.frontend.whiteList.ipStrategy.excludedIPs` |
|
||||||
|
|
||||||
#### Custom Headers
|
#### Custom Headers
|
||||||
|
|
||||||
|
|
|
@ -172,6 +172,7 @@ Labels can be used on task containers to override default behavior:
|
||||||
| `traefik.frontend.auth.digest.users=EXPR` | Sets the digest authentication to this frontend in CSV format: `User:Realm:Hash,User:Realm:Hash`. |
|
| `traefik.frontend.auth.digest.users=EXPR` | Sets the digest authentication to this frontend in CSV format: `User:Realm:Hash,User:Realm:Hash`. |
|
||||||
| `traefik.frontend.auth.digest.usersFile=/path/.htdigest` | Sets the digest authentication with an external file; if users and usersFile are provided, both are merged, with external file contents having precedence. |
|
| `traefik.frontend.auth.digest.usersFile=/path/.htdigest` | Sets the digest authentication with an external file; if users and usersFile are provided, both are merged, with external file contents having precedence. |
|
||||||
| `traefik.frontend.auth.forward.address=https://example.com` | Sets the URL of the authentication server. |
|
| `traefik.frontend.auth.forward.address=https://example.com` | Sets the URL of the authentication server. |
|
||||||
|
| `traefik.frontend.auth.forward.authResponseHeaders=EXPR` | Sets the forward authentication authResponseHeaders in CSV format: `X-Auth-User,X-Auth-Header` |
|
||||||
| `traefik.frontend.auth.forward.tls.ca=/path/ca.pem` | Sets the Certificate Authority (CA) for the TLS connection with the authentication server. |
|
| `traefik.frontend.auth.forward.tls.ca=/path/ca.pem` | Sets the Certificate Authority (CA) for the TLS connection with the authentication server. |
|
||||||
| `traefik.frontend.auth.forward.tls.caOptional=true` | Checks the certificates if present but do not force to be signed by a specified Certificate Authority (CA). |
|
| `traefik.frontend.auth.forward.tls.caOptional=true` | Checks the certificates if present but do not force to be signed by a specified Certificate Authority (CA). |
|
||||||
| `traefik.frontend.auth.forward.tls.cert=/path/server.pem` | Sets the Certificate for the TLS connection with the authentication server. |
|
| `traefik.frontend.auth.forward.tls.cert=/path/server.pem` | Sets the Certificate for the TLS connection with the authentication server. |
|
||||||
|
@ -265,6 +266,7 @@ Segment labels override the default behavior.
|
||||||
| `traefik.<segment_name>.frontend.auth.digest.users=EXPR` | Same as `traefik.frontend.auth.digest.users` |
|
| `traefik.<segment_name>.frontend.auth.digest.users=EXPR` | Same as `traefik.frontend.auth.digest.users` |
|
||||||
| `traefik.<segment_name>.frontend.auth.digest.usersFile=/path/.htdigest` | Same as `traefik.frontend.auth.digest.usersFile` |
|
| `traefik.<segment_name>.frontend.auth.digest.usersFile=/path/.htdigest` | Same as `traefik.frontend.auth.digest.usersFile` |
|
||||||
| `traefik.<segment_name>.frontend.auth.forward.address=https://example.com` | Same as `traefik.frontend.auth.forward.address` |
|
| `traefik.<segment_name>.frontend.auth.forward.address=https://example.com` | Same as `traefik.frontend.auth.forward.address` |
|
||||||
|
| `traefik.<segment_name>.frontend.auth.forward.authResponseHeaders=EXPR` | Same as `traefik.frontend.auth.forward.authResponseHeaders` |
|
||||||
| `traefik.<segment_name>.frontend.auth.forward.tls.ca=/path/ca.pem` | Same as `traefik.frontend.auth.forward.tls.ca` |
|
| `traefik.<segment_name>.frontend.auth.forward.tls.ca=/path/ca.pem` | Same as `traefik.frontend.auth.forward.tls.ca` |
|
||||||
| `traefik.<segment_name>.frontend.auth.forward.tls.caOptional=true` | Same as `traefik.frontend.auth.forward.tls.caOptional` |
|
| `traefik.<segment_name>.frontend.auth.forward.tls.caOptional=true` | Same as `traefik.frontend.auth.forward.tls.caOptional` |
|
||||||
| `traefik.<segment_name>.frontend.auth.forward.tls.cert=/path/server.pem` | Same as `traefik.frontend.auth.forward.tls.cert` |
|
| `traefik.<segment_name>.frontend.auth.forward.tls.cert=/path/server.pem` | Same as `traefik.frontend.auth.forward.tls.cert` |
|
||||||
|
|
|
@ -87,7 +87,7 @@ services:
|
||||||
```
|
```
|
||||||
|
|
||||||
!!! warning
|
!!! warning
|
||||||
Enabling the Web UI with the `--api` flag might exposes configuration elements. You can read more about this on the [API/Dashboard's Security section](/configuration/api#security).
|
Enabling the Web UI with the `--api` flag might expose configuration elements. You can read more about this on the [API/Dashboard's Security section](/configuration/api#security).
|
||||||
|
|
||||||
|
|
||||||
**That's it. Now you can launch Træfik!**
|
**That's it. Now you can launch Træfik!**
|
||||||
|
@ -218,4 +218,4 @@ Reported vulnerabilities can be found on
|
||||||
### Report a Vulnerability
|
### Report a Vulnerability
|
||||||
|
|
||||||
We want to keep Træfik safe for everyone.
|
We want to keep Træfik safe for everyone.
|
||||||
If you've discovered a security vulnerability in Træfik, we appreciate your help in disclosing it to us in a responsible manner, using [this form](https://security.traefik.io).
|
If you've discovered a security vulnerability in Træfik, we appreciate your help in disclosing it to us in a responsible manner, using [this form](https://security.traefik.io).
|
||||||
|
|
|
@ -329,3 +329,87 @@ providersThrottleDuration = "5s"
|
||||||
[respondingTimeouts]
|
[respondingTimeouts]
|
||||||
idleTimeout = "360s"
|
idleTimeout = "360s"
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Using labels in docker-compose.yml
|
||||||
|
|
||||||
|
Pay attention to the **labels** section:
|
||||||
|
|
||||||
|
```
|
||||||
|
home:
|
||||||
|
image: abiosoft/caddy:0.10.14
|
||||||
|
networks:
|
||||||
|
- ntw_front
|
||||||
|
volumes:
|
||||||
|
- ./www/home/srv/:/srv/
|
||||||
|
deploy:
|
||||||
|
mode: replicated
|
||||||
|
replicas: 2
|
||||||
|
#placement:
|
||||||
|
# constraints: [node.role==manager]
|
||||||
|
restart_policy:
|
||||||
|
condition: on-failure
|
||||||
|
max_attempts: 5
|
||||||
|
resources:
|
||||||
|
limits:
|
||||||
|
cpus: '0.20'
|
||||||
|
memory: 9M
|
||||||
|
reservations:
|
||||||
|
cpus: '0.05'
|
||||||
|
memory: 9M
|
||||||
|
labels:
|
||||||
|
- "traefik.frontend.rule=PathPrefixStrip:/"
|
||||||
|
- "traefik.backend=home"
|
||||||
|
- "traefik.port=2015"
|
||||||
|
- "traefik.weight=10"
|
||||||
|
- "traefik.enable=true"
|
||||||
|
- "traefik.passHostHeader=true"
|
||||||
|
- "traefik.docker.network=ntw_front"
|
||||||
|
- "traefik.frontend.entryPoints=http"
|
||||||
|
- "traefik.backend.loadbalancer.swarm=true"
|
||||||
|
- "traefik.backend.loadbalancer.method=drr"
|
||||||
|
```
|
||||||
|
|
||||||
|
Something more tricky using `regex`.
|
||||||
|
|
||||||
|
In this case a slash is added to `siteexample.io/portainer` and redirect to `siteexample.io/portainer/`. For more details: https://github.com/containous/traefik/issues/563
|
||||||
|
|
||||||
|
The double sign `$$` are variables managed by the docker compose file ([documentation](https://docs.docker.com/compose/compose-file/#variable-substitution)).
|
||||||
|
|
||||||
|
```
|
||||||
|
portainer:
|
||||||
|
image: portainer/portainer:1.16.5
|
||||||
|
networks:
|
||||||
|
- ntw_front
|
||||||
|
volumes:
|
||||||
|
- /var/run/docker.sock:/var/run/docker.sock
|
||||||
|
deploy:
|
||||||
|
mode: replicated
|
||||||
|
replicas: 1
|
||||||
|
placement:
|
||||||
|
constraints: [node.role==manager]
|
||||||
|
restart_policy:
|
||||||
|
condition: on-failure
|
||||||
|
max_attempts: 5
|
||||||
|
resources:
|
||||||
|
limits:
|
||||||
|
cpus: '0.33'
|
||||||
|
memory: 20M
|
||||||
|
reservations:
|
||||||
|
cpus: '0.05'
|
||||||
|
memory: 10M
|
||||||
|
labels:
|
||||||
|
- "traefik.frontend.rule=PathPrefixStrip:/portainer"
|
||||||
|
- "traefik.backend=portainer"
|
||||||
|
- "traefik.port=9000"
|
||||||
|
- "traefik.weight=10"
|
||||||
|
- "traefik.enable=true"
|
||||||
|
- "traefik.passHostHeader=true"
|
||||||
|
- "traefik.docker.network=ntw_front"
|
||||||
|
- "traefik.frontend.entryPoints=http"
|
||||||
|
- "traefik.backend.loadbalancer.swarm=true"
|
||||||
|
- "traefik.backend.loadbalancer.method=drr"
|
||||||
|
# https://github.com/containous/traefik/issues/563#issuecomment-421360934
|
||||||
|
- "traefik.frontend.redirect.regex=^(.*)/portainer$$"
|
||||||
|
- "traefik.frontend.redirect.replacement=$$1/portainer/"
|
||||||
|
- "traefik.frontend.rule=PathPrefix:/portainer;ReplacePathRegex: ^/portainer/(.*) /$$1"
|
||||||
|
```
|
||||||
|
|
|
@ -19,5 +19,6 @@ logLevel = "DEBUG"
|
||||||
[frontends]
|
[frontends]
|
||||||
[frontends.frontend1]
|
[frontends.frontend1]
|
||||||
backend = "backend1"
|
backend = "backend1"
|
||||||
|
passHostHeader = true
|
||||||
[frontends.frontend1.routes.test_1]
|
[frontends.frontend1.routes.test_1]
|
||||||
rule = "PathPrefix:/ws"
|
rule = "PathPrefix:/ws"
|
||||||
|
|
|
@ -24,5 +24,6 @@ insecureSkipVerify=true
|
||||||
[frontends]
|
[frontends]
|
||||||
[frontends.frontend1]
|
[frontends.frontend1]
|
||||||
backend = "backend1"
|
backend = "backend1"
|
||||||
|
passHostHeader = true
|
||||||
[frontends.frontend1.routes.test_1]
|
[frontends.frontend1.routes.test_1]
|
||||||
rule = "Path:/echo,/ws"
|
rule = "Path:/echo,/ws"
|
||||||
|
|
|
@ -2,6 +2,7 @@ package middlewares
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"runtime"
|
||||||
|
|
||||||
"github.com/containous/traefik/log"
|
"github.com/containous/traefik/log"
|
||||||
"github.com/urfave/negroni"
|
"github.com/urfave/negroni"
|
||||||
|
@ -10,7 +11,7 @@ import (
|
||||||
// RecoverHandler recovers from a panic in http handlers
|
// RecoverHandler recovers from a panic in http handlers
|
||||||
func RecoverHandler(next http.Handler) http.Handler {
|
func RecoverHandler(next http.Handler) http.Handler {
|
||||||
fn := func(w http.ResponseWriter, r *http.Request) {
|
fn := func(w http.ResponseWriter, r *http.Request) {
|
||||||
defer recoverFunc(w)
|
defer recoverFunc(w, r)
|
||||||
next.ServeHTTP(w, r)
|
next.ServeHTTP(w, r)
|
||||||
}
|
}
|
||||||
return http.HandlerFunc(fn)
|
return http.HandlerFunc(fn)
|
||||||
|
@ -19,15 +20,32 @@ func RecoverHandler(next http.Handler) http.Handler {
|
||||||
// NegroniRecoverHandler recovers from a panic in negroni handlers
|
// NegroniRecoverHandler recovers from a panic in negroni handlers
|
||||||
func NegroniRecoverHandler() negroni.Handler {
|
func NegroniRecoverHandler() negroni.Handler {
|
||||||
fn := func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
|
fn := func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
|
||||||
defer recoverFunc(w)
|
defer recoverFunc(w, r)
|
||||||
next.ServeHTTP(w, r)
|
next.ServeHTTP(w, r)
|
||||||
}
|
}
|
||||||
return negroni.HandlerFunc(fn)
|
return negroni.HandlerFunc(fn)
|
||||||
}
|
}
|
||||||
|
|
||||||
func recoverFunc(w http.ResponseWriter) {
|
func recoverFunc(w http.ResponseWriter, r *http.Request) {
|
||||||
if err := recover(); err != nil {
|
if err := recover(); err != nil {
|
||||||
log.Errorf("Recovered from panic in http handler: %+v", err)
|
if !shouldLogPanic(err) {
|
||||||
|
log.Debugf("Request has been aborted [%s - %s]: %v", r.RemoteAddr, r.URL, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Errorf("Recovered from panic in HTTP handler [%s - %s]: %+v", r.RemoteAddr, r.URL, err)
|
||||||
|
|
||||||
|
const size = 64 << 10
|
||||||
|
buf := make([]byte, size)
|
||||||
|
buf = buf[:runtime.Stack(buf, false)]
|
||||||
|
log.Errorf("Stack: %s", buf)
|
||||||
|
|
||||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// https://github.com/golang/go/blob/a0d6420d8be2ae7164797051ec74fa2a2df466a1/src/net/http/server.go#L1761-L1775
|
||||||
|
// https://github.com/golang/go/blob/c33153f7b416c03983324b3e8f869ce1116d84bc/src/net/http/httputil/reverseproxy.go#L284
|
||||||
|
func shouldLogPanic(panicValue interface{}) bool {
|
||||||
|
return panicValue != nil && panicValue != http.ErrAbortHandler
|
||||||
|
}
|
||||||
|
|
|
@ -12,7 +12,6 @@ import (
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/BurntSushi/ty/fun"
|
|
||||||
"github.com/cenk/backoff"
|
"github.com/cenk/backoff"
|
||||||
"github.com/containous/flaeg/parse"
|
"github.com/containous/flaeg/parse"
|
||||||
"github.com/containous/traefik/log"
|
"github.com/containous/traefik/log"
|
||||||
|
@ -762,8 +761,17 @@ func (p *Provider) getValidDomains(domain types.Domain, wildcardAllowed bool) ([
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
domains = fun.Map(types.CanonicalDomain, domains).([]string)
|
var cleanDomains []string
|
||||||
return domains, nil
|
for _, domain := range domains {
|
||||||
|
canonicalDomain := types.CanonicalDomain(domain)
|
||||||
|
cleanDomain := acme.UnFqdn(canonicalDomain)
|
||||||
|
if canonicalDomain != cleanDomain {
|
||||||
|
log.Warnf("FQDN detected, please remove the trailing dot: %s", canonicalDomain)
|
||||||
|
}
|
||||||
|
cleanDomains = append(cleanDomains, cleanDomain)
|
||||||
|
}
|
||||||
|
|
||||||
|
return cleanDomains, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func isDomainAlreadyChecked(domainToCheck string, existentDomains []string) bool {
|
func isDomainAlreadyChecked(domainToCheck string, existentDomains []string) bool {
|
||||||
|
|
|
@ -329,6 +329,7 @@ func TestProviderBuildConfiguration(t *testing.T) {
|
||||||
Attributes: []string{
|
Attributes: []string{
|
||||||
"random.foo=bar",
|
"random.foo=bar",
|
||||||
label.TraefikFrontendAuthForwardAddress + "=auth.server",
|
label.TraefikFrontendAuthForwardAddress + "=auth.server",
|
||||||
|
label.TraefikFrontendAuthForwardAuthResponseHeaders + "=X-Auth-User,X-Auth-Token",
|
||||||
label.TraefikFrontendAuthForwardTrustForwardHeader + "=true",
|
label.TraefikFrontendAuthForwardTrustForwardHeader + "=true",
|
||||||
label.TraefikFrontendAuthForwardTLSCa + "=ca.crt",
|
label.TraefikFrontendAuthForwardTLSCa + "=ca.crt",
|
||||||
label.TraefikFrontendAuthForwardTLSCaOptional + "=true",
|
label.TraefikFrontendAuthForwardTLSCaOptional + "=true",
|
||||||
|
@ -371,8 +372,7 @@ func TestProviderBuildConfiguration(t *testing.T) {
|
||||||
Auth: &types.Auth{
|
Auth: &types.Auth{
|
||||||
HeaderField: "X-WebAuth-User",
|
HeaderField: "X-WebAuth-User",
|
||||||
Forward: &types.Forward{
|
Forward: &types.Forward{
|
||||||
Address: "auth.server",
|
Address: "auth.server",
|
||||||
TrustForwardHeader: true,
|
|
||||||
TLS: &types.ClientTLS{
|
TLS: &types.ClientTLS{
|
||||||
CA: "ca.crt",
|
CA: "ca.crt",
|
||||||
CAOptional: true,
|
CAOptional: true,
|
||||||
|
@ -380,6 +380,8 @@ func TestProviderBuildConfiguration(t *testing.T) {
|
||||||
Cert: "server.crt",
|
Cert: "server.crt",
|
||||||
Key: "server.key",
|
Key: "server.key",
|
||||||
},
|
},
|
||||||
|
TrustForwardHeader: true,
|
||||||
|
AuthResponseHeaders: []string{"X-Auth-User", "X-Auth-Token"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
EntryPoints: []string{},
|
EntryPoints: []string{},
|
||||||
|
@ -443,6 +445,7 @@ func TestProviderBuildConfiguration(t *testing.T) {
|
||||||
label.TraefikFrontendAuthDigestUsers + "=test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/,test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0",
|
label.TraefikFrontendAuthDigestUsers + "=test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/,test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0",
|
||||||
label.TraefikFrontendAuthDigestUsersFile + "=.htpasswd",
|
label.TraefikFrontendAuthDigestUsersFile + "=.htpasswd",
|
||||||
label.TraefikFrontendAuthForwardAddress + "=auth.server",
|
label.TraefikFrontendAuthForwardAddress + "=auth.server",
|
||||||
|
label.TraefikFrontendAuthForwardAuthResponseHeaders + "=X-Auth-User,X-Auth-Token",
|
||||||
label.TraefikFrontendAuthForwardTrustForwardHeader + "=true",
|
label.TraefikFrontendAuthForwardTrustForwardHeader + "=true",
|
||||||
label.TraefikFrontendAuthForwardTLSCa + "=ca.crt",
|
label.TraefikFrontendAuthForwardTLSCa + "=ca.crt",
|
||||||
label.TraefikFrontendAuthForwardTLSCaOptional + "=true",
|
label.TraefikFrontendAuthForwardTLSCaOptional + "=true",
|
||||||
|
|
|
@ -337,21 +337,22 @@ func (p *Provider) getPortBinding(container dockerData) (*nat.PortBinding, error
|
||||||
|
|
||||||
func (p *Provider) getIPPort(container dockerData) (string, string, error) {
|
func (p *Provider) getIPPort(container dockerData) (string, string, error) {
|
||||||
var ip, port string
|
var ip, port string
|
||||||
|
usedBound := false
|
||||||
|
|
||||||
if p.UseBindPortIP {
|
if p.UseBindPortIP {
|
||||||
portBinding, err := p.getPortBinding(container)
|
portBinding, err := p.getPortBinding(container)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", "", fmt.Errorf("unable to find a binding for the container %q: ignoring server", container.Name)
|
log.Infof("Unable to find a binding for container %q, falling back on its internal IP/Port.", container.Name)
|
||||||
|
} else if (portBinding.HostIP == "0.0.0.0") || (len(portBinding.HostIP) == 0) {
|
||||||
|
log.Infof("Cannot determine the IP address (got %q) for %q's binding, falling back on its internal IP/Port.", portBinding.HostIP, container.Name)
|
||||||
|
} else {
|
||||||
|
ip = portBinding.HostIP
|
||||||
|
port = portBinding.HostPort
|
||||||
|
usedBound = true
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if portBinding.HostIP == "0.0.0.0" {
|
if !usedBound {
|
||||||
return "", "", fmt.Errorf("cannot determine the IP address (got 0.0.0.0) for the container %q: ignoring server", container.Name)
|
|
||||||
}
|
|
||||||
|
|
||||||
ip = portBinding.HostIP
|
|
||||||
port = portBinding.HostPort
|
|
||||||
|
|
||||||
} else {
|
|
||||||
ip = p.getIPAddress(container)
|
ip = p.getIPAddress(container)
|
||||||
port = getPort(container)
|
port = getPort(container)
|
||||||
}
|
}
|
||||||
|
@ -359,6 +360,7 @@ func (p *Provider) getIPPort(container dockerData) (string, string, error) {
|
||||||
if len(ip) == 0 {
|
if len(ip) == 0 {
|
||||||
return "", "", fmt.Errorf("unable to find the IP address for the container %q: the server is ignored", container.Name)
|
return "", "", fmt.Errorf("unable to find the IP address for the container %q: the server is ignored", container.Name)
|
||||||
}
|
}
|
||||||
|
|
||||||
return ip, port, nil
|
return ip, port, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -63,56 +63,6 @@ func TestDockerBuildConfiguration(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
|
||||||
desc: "when frontend basic auth",
|
|
||||||
containers: []docker.ContainerJSON{
|
|
||||||
containerJSON(
|
|
||||||
name("test"),
|
|
||||||
labels(map[string]string{
|
|
||||||
label.TraefikFrontendAuthBasicUsers: "test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/,test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0",
|
|
||||||
label.TraefikFrontendAuthBasicUsersFile: ".htpasswd",
|
|
||||||
label.TraefikFrontendAuthBasicRemoveHeader: "true",
|
|
||||||
label.TraefikFrontendAuthBasicRealm: "myRealm",
|
|
||||||
}),
|
|
||||||
ports(nat.PortMap{
|
|
||||||
"80/tcp": {},
|
|
||||||
}),
|
|
||||||
withNetwork("bridge", ipv4("127.0.0.1")),
|
|
||||||
),
|
|
||||||
},
|
|
||||||
expectedFrontends: map[string]*types.Frontend{
|
|
||||||
"frontend-Host-test-docker-localhost-0": {
|
|
||||||
Backend: "backend-test",
|
|
||||||
PassHostHeader: true,
|
|
||||||
EntryPoints: []string{},
|
|
||||||
Auth: &types.Auth{
|
|
||||||
Basic: &types.Basic{
|
|
||||||
RemoveHeader: true,
|
|
||||||
Realm: "myRealm",
|
|
||||||
Users: []string{"test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/",
|
|
||||||
"test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0"},
|
|
||||||
UsersFile: ".htpasswd",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
Routes: map[string]types.Route{
|
|
||||||
"route-frontend-Host-test-docker-localhost-0": {
|
|
||||||
Rule: "Host:test.docker.localhost",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
expectedBackends: map[string]*types.Backend{
|
|
||||||
"backend-test": {
|
|
||||||
Servers: map[string]types.Server{
|
|
||||||
"server-test-842895ca2aca17f6ee36ddb2f621194d": {
|
|
||||||
URL: "http://127.0.0.1:80",
|
|
||||||
Weight: label.DefaultWeight,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
CircuitBreaker: nil,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
desc: "when pass tls client certificate",
|
desc: "when pass tls client certificate",
|
||||||
containers: []docker.ContainerJSON{
|
containers: []docker.ContainerJSON{
|
||||||
|
@ -175,6 +125,55 @@ func TestDockerBuildConfiguration(t *testing.T) {
|
||||||
CircuitBreaker: nil,
|
CircuitBreaker: nil,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
}, {
|
||||||
|
desc: "when frontend basic auth",
|
||||||
|
containers: []docker.ContainerJSON{
|
||||||
|
containerJSON(
|
||||||
|
name("test"),
|
||||||
|
labels(map[string]string{
|
||||||
|
label.TraefikFrontendAuthBasicUsers: "test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/,test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0",
|
||||||
|
label.TraefikFrontendAuthBasicUsersFile: ".htpasswd",
|
||||||
|
label.TraefikFrontendAuthBasicRemoveHeader: "true",
|
||||||
|
label.TraefikFrontendAuthBasicRealm: "myRealm",
|
||||||
|
}),
|
||||||
|
ports(nat.PortMap{
|
||||||
|
"80/tcp": {},
|
||||||
|
}),
|
||||||
|
withNetwork("bridge", ipv4("127.0.0.1")),
|
||||||
|
),
|
||||||
|
},
|
||||||
|
expectedFrontends: map[string]*types.Frontend{
|
||||||
|
"frontend-Host-test-docker-localhost-0": {
|
||||||
|
Backend: "backend-test",
|
||||||
|
PassHostHeader: true,
|
||||||
|
EntryPoints: []string{},
|
||||||
|
Auth: &types.Auth{
|
||||||
|
Basic: &types.Basic{
|
||||||
|
RemoveHeader: true,
|
||||||
|
Realm: "myRealm",
|
||||||
|
Users: []string{"test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/",
|
||||||
|
"test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0"},
|
||||||
|
UsersFile: ".htpasswd",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Routes: map[string]types.Route{
|
||||||
|
"route-frontend-Host-test-docker-localhost-0": {
|
||||||
|
Rule: "Host:test.docker.localhost",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
expectedBackends: map[string]*types.Backend{
|
||||||
|
"backend-test": {
|
||||||
|
Servers: map[string]types.Server{
|
||||||
|
"server-test-842895ca2aca17f6ee36ddb2f621194d": {
|
||||||
|
URL: "http://127.0.0.1:80",
|
||||||
|
Weight: label.DefaultWeight,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
CircuitBreaker: nil,
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
desc: "when frontend basic auth backward compatibility",
|
desc: "when frontend basic auth backward compatibility",
|
||||||
|
@ -281,6 +280,7 @@ func TestDockerBuildConfiguration(t *testing.T) {
|
||||||
label.TraefikFrontendAuthForwardTLSCert: "server.crt",
|
label.TraefikFrontendAuthForwardTLSCert: "server.crt",
|
||||||
label.TraefikFrontendAuthForwardTLSKey: "server.key",
|
label.TraefikFrontendAuthForwardTLSKey: "server.key",
|
||||||
label.TraefikFrontendAuthForwardTLSInsecureSkipVerify: "true",
|
label.TraefikFrontendAuthForwardTLSInsecureSkipVerify: "true",
|
||||||
|
label.TraefikFrontendAuthForwardAuthResponseHeaders: "X-Auth-User,X-Auth-Token",
|
||||||
}),
|
}),
|
||||||
ports(nat.PortMap{
|
ports(nat.PortMap{
|
||||||
"80/tcp": {},
|
"80/tcp": {},
|
||||||
|
@ -295,8 +295,7 @@ func TestDockerBuildConfiguration(t *testing.T) {
|
||||||
EntryPoints: []string{},
|
EntryPoints: []string{},
|
||||||
Auth: &types.Auth{
|
Auth: &types.Auth{
|
||||||
Forward: &types.Forward{
|
Forward: &types.Forward{
|
||||||
Address: "auth.server",
|
Address: "auth.server",
|
||||||
TrustForwardHeader: true,
|
|
||||||
TLS: &types.ClientTLS{
|
TLS: &types.ClientTLS{
|
||||||
CA: "ca.crt",
|
CA: "ca.crt",
|
||||||
CAOptional: true,
|
CAOptional: true,
|
||||||
|
@ -304,6 +303,8 @@ func TestDockerBuildConfiguration(t *testing.T) {
|
||||||
Cert: "server.crt",
|
Cert: "server.crt",
|
||||||
Key: "server.key",
|
Key: "server.key",
|
||||||
},
|
},
|
||||||
|
TrustForwardHeader: true,
|
||||||
|
AuthResponseHeaders: []string{"X-Auth-User", "X-Auth-Token"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Routes: map[string]types.Route{
|
Routes: map[string]types.Route{
|
||||||
|
@ -1398,6 +1399,31 @@ func TestDockerGetIPPort(t *testing.T) {
|
||||||
ip, port string
|
ip, port string
|
||||||
expectsError bool
|
expectsError bool
|
||||||
}{
|
}{
|
||||||
|
{
|
||||||
|
desc: "label traefik.port not set, no binding, falling back on the container's IP/Port",
|
||||||
|
container: containerJSON(
|
||||||
|
ports(nat.PortMap{
|
||||||
|
"8080/tcp": {},
|
||||||
|
}),
|
||||||
|
withNetwork("testnet", ipv4("10.11.12.13"))),
|
||||||
|
ip: "10.11.12.13",
|
||||||
|
port: "8080",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: "label traefik.port not set, single binding with port only, falling back on the container's IP/Port",
|
||||||
|
container: containerJSON(
|
||||||
|
withNetwork("testnet", ipv4("10.11.12.13")),
|
||||||
|
ports(nat.PortMap{
|
||||||
|
"80/tcp": []nat.PortBinding{
|
||||||
|
{
|
||||||
|
HostPort: "8082",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
ip: "10.11.12.13",
|
||||||
|
port: "80",
|
||||||
|
},
|
||||||
{
|
{
|
||||||
desc: "label traefik.port not set, binding with ip:port should create a route to the bound ip:port",
|
desc: "label traefik.port not set, binding with ip:port should create a route to the bound ip:port",
|
||||||
container: containerJSON(
|
container: containerJSON(
|
||||||
|
@ -1413,6 +1439,52 @@ func TestDockerGetIPPort(t *testing.T) {
|
||||||
ip: "1.2.3.4",
|
ip: "1.2.3.4",
|
||||||
port: "8081",
|
port: "8081",
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
desc: "label traefik.port set, no binding, falling back on the container's IP/traefik.port",
|
||||||
|
container: containerJSON(
|
||||||
|
labels(map[string]string{
|
||||||
|
label.TraefikPort: "80",
|
||||||
|
}),
|
||||||
|
withNetwork("testnet", ipv4("10.11.12.13"))),
|
||||||
|
ip: "10.11.12.13",
|
||||||
|
port: "80",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: "label traefik.port set, single binding with ip:port for the label, creates the route",
|
||||||
|
container: containerJSON(
|
||||||
|
labels(map[string]string{
|
||||||
|
label.TraefikPort: "443",
|
||||||
|
}),
|
||||||
|
ports(nat.PortMap{
|
||||||
|
"443/tcp": []nat.PortBinding{
|
||||||
|
{
|
||||||
|
HostIP: "5.6.7.8",
|
||||||
|
HostPort: "8082",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
withNetwork("testnet", ipv4("10.11.12.13"))),
|
||||||
|
ip: "5.6.7.8",
|
||||||
|
port: "8082",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: "label traefik.port set, no binding on the corresponding port, falling back on the container's IP/label.port",
|
||||||
|
container: containerJSON(
|
||||||
|
labels(map[string]string{
|
||||||
|
label.TraefikPort: "80",
|
||||||
|
}),
|
||||||
|
ports(nat.PortMap{
|
||||||
|
"443/tcp": []nat.PortBinding{
|
||||||
|
{
|
||||||
|
HostIP: "5.6.7.8",
|
||||||
|
HostPort: "8082",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
withNetwork("testnet", ipv4("10.11.12.13"))),
|
||||||
|
ip: "10.11.12.13",
|
||||||
|
port: "80",
|
||||||
|
},
|
||||||
{
|
{
|
||||||
desc: "label traefik.port set, multiple bindings on different ports, uses the label to select the correct (first) binding",
|
desc: "label traefik.port set, multiple bindings on different ports, uses the label to select the correct (first) binding",
|
||||||
container: containerJSON(
|
container: containerJSON(
|
||||||
|
@ -1461,69 +1533,6 @@ func TestDockerGetIPPort(t *testing.T) {
|
||||||
ip: "5.6.7.8",
|
ip: "5.6.7.8",
|
||||||
port: "8082",
|
port: "8082",
|
||||||
},
|
},
|
||||||
{
|
|
||||||
desc: "label traefik.port set, single binding with ip:port for the label, creates the route",
|
|
||||||
container: containerJSON(
|
|
||||||
labels(map[string]string{
|
|
||||||
label.TraefikPort: "443",
|
|
||||||
}),
|
|
||||||
ports(nat.PortMap{
|
|
||||||
"443/tcp": []nat.PortBinding{
|
|
||||||
{
|
|
||||||
HostIP: "5.6.7.8",
|
|
||||||
HostPort: "8082",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
withNetwork("testnet", ipv4("10.11.12.13"))),
|
|
||||||
ip: "5.6.7.8",
|
|
||||||
port: "8082",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
desc: "label traefik.port not set, single binding with port only, server ignored",
|
|
||||||
container: containerJSON(
|
|
||||||
ports(nat.PortMap{
|
|
||||||
"80/tcp": []nat.PortBinding{
|
|
||||||
{
|
|
||||||
HostPort: "8082",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
withNetwork("testnet", ipv4("10.11.12.13"))),
|
|
||||||
expectsError: true,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
desc: "label traefik.port not set, no binding, server ignored",
|
|
||||||
container: containerJSON(
|
|
||||||
withNetwork("testnet", ipv4("10.11.12.13"))),
|
|
||||||
expectsError: true,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
desc: "label traefik.port set, no binding on the corresponding port, server ignored",
|
|
||||||
container: containerJSON(
|
|
||||||
labels(map[string]string{
|
|
||||||
label.TraefikPort: "80",
|
|
||||||
}),
|
|
||||||
ports(nat.PortMap{
|
|
||||||
"443/tcp": []nat.PortBinding{
|
|
||||||
{
|
|
||||||
HostIP: "5.6.7.8",
|
|
||||||
HostPort: "8082",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
withNetwork("testnet", ipv4("10.11.12.13"))),
|
|
||||||
expectsError: true,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
desc: "label traefik.port set, no binding, server ignored",
|
|
||||||
container: containerJSON(
|
|
||||||
labels(map[string]string{
|
|
||||||
label.TraefikPort: "80",
|
|
||||||
}),
|
|
||||||
withNetwork("testnet", ipv4("10.11.12.13"))),
|
|
||||||
expectsError: true,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range testCases {
|
for _, test := range testCases {
|
||||||
|
@ -1536,7 +1545,7 @@ func TestDockerGetIPPort(t *testing.T) {
|
||||||
dData.SegmentLabels = segmentProperties[""]
|
dData.SegmentLabels = segmentProperties[""]
|
||||||
|
|
||||||
provider := &Provider{
|
provider := &Provider{
|
||||||
Network: "webnet",
|
Network: "testnet",
|
||||||
UseBindPortIP: true,
|
UseBindPortIP: true,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -324,6 +324,7 @@ func TestSwarmBuildConfiguration(t *testing.T) {
|
||||||
label.TraefikFrontendAuthForwardTLSCert: "server.crt",
|
label.TraefikFrontendAuthForwardTLSCert: "server.crt",
|
||||||
label.TraefikFrontendAuthForwardTLSKey: "server.key",
|
label.TraefikFrontendAuthForwardTLSKey: "server.key",
|
||||||
label.TraefikFrontendAuthForwardTLSInsecureSkipVerify: "true",
|
label.TraefikFrontendAuthForwardTLSInsecureSkipVerify: "true",
|
||||||
|
label.TraefikFrontendAuthForwardAuthResponseHeaders: "X-Auth-User,X-Auth-Token",
|
||||||
}),
|
}),
|
||||||
withEndpointSpec(modeVIP),
|
withEndpointSpec(modeVIP),
|
||||||
withEndpoint(virtualIP("1", "127.0.0.1/24")),
|
withEndpoint(virtualIP("1", "127.0.0.1/24")),
|
||||||
|
@ -336,8 +337,7 @@ func TestSwarmBuildConfiguration(t *testing.T) {
|
||||||
EntryPoints: []string{},
|
EntryPoints: []string{},
|
||||||
Auth: &types.Auth{
|
Auth: &types.Auth{
|
||||||
Forward: &types.Forward{
|
Forward: &types.Forward{
|
||||||
Address: "auth.server",
|
Address: "auth.server",
|
||||||
TrustForwardHeader: true,
|
|
||||||
TLS: &types.ClientTLS{
|
TLS: &types.ClientTLS{
|
||||||
CA: "ca.crt",
|
CA: "ca.crt",
|
||||||
CAOptional: true,
|
CAOptional: true,
|
||||||
|
@ -345,6 +345,8 @@ func TestSwarmBuildConfiguration(t *testing.T) {
|
||||||
Key: "server.key",
|
Key: "server.key",
|
||||||
InsecureSkipVerify: true,
|
InsecureSkipVerify: true,
|
||||||
},
|
},
|
||||||
|
TrustForwardHeader: true,
|
||||||
|
AuthResponseHeaders: []string{"X-Auth-User", "X-Auth-Token"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Routes: map[string]types.Route{
|
Routes: map[string]types.Route{
|
||||||
|
|
|
@ -298,6 +298,7 @@ func TestSegmentBuildConfiguration(t *testing.T) {
|
||||||
label.Prefix + "sauternes." + label.SuffixFrontendAuthForwardTLSCert: "server.crt",
|
label.Prefix + "sauternes." + label.SuffixFrontendAuthForwardTLSCert: "server.crt",
|
||||||
label.Prefix + "sauternes." + label.SuffixFrontendAuthForwardTLSKey: "server.key",
|
label.Prefix + "sauternes." + label.SuffixFrontendAuthForwardTLSKey: "server.key",
|
||||||
label.Prefix + "sauternes." + label.SuffixFrontendAuthForwardTLSInsecureSkipVerify: "true",
|
label.Prefix + "sauternes." + label.SuffixFrontendAuthForwardTLSInsecureSkipVerify: "true",
|
||||||
|
label.Prefix + "sauternes." + label.SuffixFrontendAuthForwardAuthResponseHeaders: "X-Auth-User,X-Auth-Token",
|
||||||
}),
|
}),
|
||||||
ports(nat.PortMap{
|
ports(nat.PortMap{
|
||||||
"80/tcp": {},
|
"80/tcp": {},
|
||||||
|
@ -318,8 +319,7 @@ func TestSegmentBuildConfiguration(t *testing.T) {
|
||||||
Auth: &types.Auth{
|
Auth: &types.Auth{
|
||||||
HeaderField: "X-WebAuth-User",
|
HeaderField: "X-WebAuth-User",
|
||||||
Forward: &types.Forward{
|
Forward: &types.Forward{
|
||||||
Address: "auth.server",
|
Address: "auth.server",
|
||||||
TrustForwardHeader: true,
|
|
||||||
TLS: &types.ClientTLS{
|
TLS: &types.ClientTLS{
|
||||||
CA: "ca.crt",
|
CA: "ca.crt",
|
||||||
CAOptional: true,
|
CAOptional: true,
|
||||||
|
@ -327,6 +327,8 @@ func TestSegmentBuildConfiguration(t *testing.T) {
|
||||||
Key: "server.key",
|
Key: "server.key",
|
||||||
InsecureSkipVerify: true,
|
InsecureSkipVerify: true,
|
||||||
},
|
},
|
||||||
|
TrustForwardHeader: true,
|
||||||
|
AuthResponseHeaders: []string{"X-Auth-User", "X-Auth-Token"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
|
@ -247,6 +247,7 @@ func TestSegmentBuildConfiguration(t *testing.T) {
|
||||||
label.Prefix + "sauternes." + label.SuffixFrontendAuthForwardTLSCert: "server.crt",
|
label.Prefix + "sauternes." + label.SuffixFrontendAuthForwardTLSCert: "server.crt",
|
||||||
label.Prefix + "sauternes." + label.SuffixFrontendAuthForwardTLSKey: "server.key",
|
label.Prefix + "sauternes." + label.SuffixFrontendAuthForwardTLSKey: "server.key",
|
||||||
label.Prefix + "sauternes." + label.SuffixFrontendAuthForwardTLSInsecureSkipVerify: "true",
|
label.Prefix + "sauternes." + label.SuffixFrontendAuthForwardTLSInsecureSkipVerify: "true",
|
||||||
|
label.Prefix + "sauternes." + label.SuffixFrontendAuthForwardAuthResponseHeaders: "X-Auth-User,X-Auth-Token",
|
||||||
}),
|
}),
|
||||||
iMachine(
|
iMachine(
|
||||||
mState(ec2.InstanceStateNameRunning),
|
mState(ec2.InstanceStateNameRunning),
|
||||||
|
@ -270,8 +271,7 @@ func TestSegmentBuildConfiguration(t *testing.T) {
|
||||||
Auth: &types.Auth{
|
Auth: &types.Auth{
|
||||||
HeaderField: "X-WebAuth-User",
|
HeaderField: "X-WebAuth-User",
|
||||||
Forward: &types.Forward{
|
Forward: &types.Forward{
|
||||||
Address: "auth.server",
|
Address: "auth.server",
|
||||||
TrustForwardHeader: true,
|
|
||||||
TLS: &types.ClientTLS{
|
TLS: &types.ClientTLS{
|
||||||
CA: "ca.crt",
|
CA: "ca.crt",
|
||||||
CAOptional: true,
|
CAOptional: true,
|
||||||
|
@ -279,6 +279,8 @@ func TestSegmentBuildConfiguration(t *testing.T) {
|
||||||
Key: "server.key",
|
Key: "server.key",
|
||||||
InsecureSkipVerify: true,
|
InsecureSkipVerify: true,
|
||||||
},
|
},
|
||||||
|
TrustForwardHeader: true,
|
||||||
|
AuthResponseHeaders: []string{"X-Auth-User", "X-Auth-Token"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
|
@ -278,7 +278,9 @@ func TestBuildConfiguration(t *testing.T) {
|
||||||
label.TraefikFrontendAuthForwardTLSCaOptional: aws.String("true"),
|
label.TraefikFrontendAuthForwardTLSCaOptional: aws.String("true"),
|
||||||
label.TraefikFrontendAuthForwardTLSCert: aws.String("server.crt"),
|
label.TraefikFrontendAuthForwardTLSCert: aws.String("server.crt"),
|
||||||
label.TraefikFrontendAuthForwardTLSKey: aws.String("server.key"),
|
label.TraefikFrontendAuthForwardTLSKey: aws.String("server.key"),
|
||||||
label.TraefikFrontendAuthForwardTLSInsecureSkipVerify: aws.String("true"), label.TraefikFrontendAuthHeaderField: aws.String("X-WebAuth-User"),
|
label.TraefikFrontendAuthForwardTLSInsecureSkipVerify: aws.String("true"),
|
||||||
|
label.TraefikFrontendAuthHeaderField: aws.String("X-WebAuth-User"),
|
||||||
|
label.TraefikFrontendAuthForwardAuthResponseHeaders: aws.String("X-Auth-User,X-Auth-Token"),
|
||||||
}),
|
}),
|
||||||
iMachine(
|
iMachine(
|
||||||
mState(ec2.InstanceStateNameRunning),
|
mState(ec2.InstanceStateNameRunning),
|
||||||
|
@ -311,8 +313,7 @@ func TestBuildConfiguration(t *testing.T) {
|
||||||
Auth: &types.Auth{
|
Auth: &types.Auth{
|
||||||
HeaderField: "X-WebAuth-User",
|
HeaderField: "X-WebAuth-User",
|
||||||
Forward: &types.Forward{
|
Forward: &types.Forward{
|
||||||
Address: "auth.server",
|
Address: "auth.server",
|
||||||
TrustForwardHeader: true,
|
|
||||||
TLS: &types.ClientTLS{
|
TLS: &types.ClientTLS{
|
||||||
CA: "ca.crt",
|
CA: "ca.crt",
|
||||||
CAOptional: true,
|
CAOptional: true,
|
||||||
|
@ -320,6 +321,8 @@ func TestBuildConfiguration(t *testing.T) {
|
||||||
Cert: "server.crt",
|
Cert: "server.crt",
|
||||||
Key: "server.key",
|
Key: "server.key",
|
||||||
},
|
},
|
||||||
|
TrustForwardHeader: true,
|
||||||
|
AuthResponseHeaders: []string{"X-Auth-User", "X-Auth-Token"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
PassHostHeader: true,
|
PassHostHeader: true,
|
||||||
|
|
|
@ -49,6 +49,7 @@ const (
|
||||||
pathFrontendWhiteListIPStrategyExcludedIPs = pathFrontendWhiteListIPStrategy + "/excludedips"
|
pathFrontendWhiteListIPStrategyExcludedIPs = pathFrontendWhiteListIPStrategy + "/excludedips"
|
||||||
|
|
||||||
pathFrontendAuth = "/auth/"
|
pathFrontendAuth = "/auth/"
|
||||||
|
pathFrontendAuthHeaderField = pathFrontendAuth + "headerfield"
|
||||||
pathFrontendAuthBasic = pathFrontendAuth + "basic/"
|
pathFrontendAuthBasic = pathFrontendAuth + "basic/"
|
||||||
pathFrontendAuthBasicRemoveHeader = pathFrontendAuthBasic + "removeheader"
|
pathFrontendAuthBasicRemoveHeader = pathFrontendAuthBasic + "removeheader"
|
||||||
pathFrontendAuthBasicUsers = pathFrontendAuthBasic + "users"
|
pathFrontendAuthBasicUsers = pathFrontendAuthBasic + "users"
|
||||||
|
@ -59,6 +60,7 @@ const (
|
||||||
pathFrontendAuthDigestUsersFile = pathFrontendAuthDigest + "usersfile"
|
pathFrontendAuthDigestUsersFile = pathFrontendAuthDigest + "usersfile"
|
||||||
pathFrontendAuthForward = pathFrontendAuth + "forward/"
|
pathFrontendAuthForward = pathFrontendAuth + "forward/"
|
||||||
pathFrontendAuthForwardAddress = pathFrontendAuthForward + "address"
|
pathFrontendAuthForwardAddress = pathFrontendAuthForward + "address"
|
||||||
|
pathFrontendAuthForwardAuthResponseHeaders = pathFrontendAuthForward + ".authresponseheaders"
|
||||||
pathFrontendAuthForwardTLS = pathFrontendAuthForward + "tls/"
|
pathFrontendAuthForwardTLS = pathFrontendAuthForward + "tls/"
|
||||||
pathFrontendAuthForwardTLSCa = pathFrontendAuthForwardTLS + "ca"
|
pathFrontendAuthForwardTLSCa = pathFrontendAuthForwardTLS + "ca"
|
||||||
pathFrontendAuthForwardTLSCaOptional = pathFrontendAuthForwardTLS + "caoptional"
|
pathFrontendAuthForwardTLSCaOptional = pathFrontendAuthForwardTLS + "caoptional"
|
||||||
|
@ -66,7 +68,6 @@ const (
|
||||||
pathFrontendAuthForwardTLSInsecureSkipVerify = pathFrontendAuthForwardTLS + "insecureskipverify"
|
pathFrontendAuthForwardTLSInsecureSkipVerify = pathFrontendAuthForwardTLS + "insecureskipverify"
|
||||||
pathFrontendAuthForwardTLSKey = pathFrontendAuthForwardTLS + "key"
|
pathFrontendAuthForwardTLSKey = pathFrontendAuthForwardTLS + "key"
|
||||||
pathFrontendAuthForwardTrustForwardHeader = pathFrontendAuthForward + "trustforwardheader"
|
pathFrontendAuthForwardTrustForwardHeader = pathFrontendAuthForward + "trustforwardheader"
|
||||||
pathFrontendAuthHeaderField = pathFrontendAuth + "headerfield"
|
|
||||||
|
|
||||||
pathFrontendEntryPoints = "/entrypoints"
|
pathFrontendEntryPoints = "/entrypoints"
|
||||||
pathFrontendRedirectEntryPoint = "/redirect/entrypoint"
|
pathFrontendRedirectEntryPoint = "/redirect/entrypoint"
|
||||||
|
|
|
@ -411,8 +411,9 @@ func (p *Provider) getAuthDigest(rootPath string) *types.Digest {
|
||||||
// getAuthForward Create Forward Auth from path
|
// getAuthForward Create Forward Auth from path
|
||||||
func (p *Provider) getAuthForward(rootPath string) *types.Forward {
|
func (p *Provider) getAuthForward(rootPath string) *types.Forward {
|
||||||
forwardAuth := &types.Forward{
|
forwardAuth := &types.Forward{
|
||||||
Address: p.get("", rootPath, pathFrontendAuthForwardAddress),
|
Address: p.get("", rootPath, pathFrontendAuthForwardAddress),
|
||||||
TrustForwardHeader: p.getBool(false, rootPath, pathFrontendAuthForwardTrustForwardHeader),
|
TrustForwardHeader: p.getBool(false, rootPath, pathFrontendAuthForwardTrustForwardHeader),
|
||||||
|
AuthResponseHeaders: p.getList(rootPath, pathFrontendAuthForwardAuthResponseHeaders),
|
||||||
}
|
}
|
||||||
|
|
||||||
// TLS configuration
|
// TLS configuration
|
||||||
|
|
|
@ -181,6 +181,7 @@ func TestProviderBuildConfiguration(t *testing.T) {
|
||||||
withPair(pathFrontendAuthForwardTLSCert, "server.crt"),
|
withPair(pathFrontendAuthForwardTLSCert, "server.crt"),
|
||||||
withPair(pathFrontendAuthForwardTLSKey, "server.key"),
|
withPair(pathFrontendAuthForwardTLSKey, "server.key"),
|
||||||
withPair(pathFrontendAuthForwardTLSInsecureSkipVerify, "true"),
|
withPair(pathFrontendAuthForwardTLSInsecureSkipVerify, "true"),
|
||||||
|
withPair(pathFrontendAuthForwardAuthResponseHeaders, "X-Auth-User,X-Auth-Token"),
|
||||||
),
|
),
|
||||||
backend("backend"),
|
backend("backend"),
|
||||||
),
|
),
|
||||||
|
@ -200,8 +201,7 @@ func TestProviderBuildConfiguration(t *testing.T) {
|
||||||
Auth: &types.Auth{
|
Auth: &types.Auth{
|
||||||
HeaderField: "X-WebAuth-User",
|
HeaderField: "X-WebAuth-User",
|
||||||
Forward: &types.Forward{
|
Forward: &types.Forward{
|
||||||
Address: "auth.server",
|
Address: "auth.server",
|
||||||
TrustForwardHeader: true,
|
|
||||||
TLS: &types.ClientTLS{
|
TLS: &types.ClientTLS{
|
||||||
CA: "ca.crt",
|
CA: "ca.crt",
|
||||||
CAOptional: true,
|
CAOptional: true,
|
||||||
|
@ -209,6 +209,8 @@ func TestProviderBuildConfiguration(t *testing.T) {
|
||||||
Cert: "server.crt",
|
Cert: "server.crt",
|
||||||
Key: "server.key",
|
Key: "server.key",
|
||||||
},
|
},
|
||||||
|
TrustForwardHeader: true,
|
||||||
|
AuthResponseHeaders: []string{"X-Auth-User", "X-Auth-Token"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
|
@ -47,6 +47,7 @@ const (
|
||||||
SuffixFrontendAuthDigestUsersFile = SuffixFrontendAuthDigest + ".usersFile"
|
SuffixFrontendAuthDigestUsersFile = SuffixFrontendAuthDigest + ".usersFile"
|
||||||
SuffixFrontendAuthForward = SuffixFrontendAuth + ".forward"
|
SuffixFrontendAuthForward = SuffixFrontendAuth + ".forward"
|
||||||
SuffixFrontendAuthForwardAddress = SuffixFrontendAuthForward + ".address"
|
SuffixFrontendAuthForwardAddress = SuffixFrontendAuthForward + ".address"
|
||||||
|
SuffixFrontendAuthForwardAuthResponseHeaders = SuffixFrontendAuthForward + ".authResponseHeaders"
|
||||||
SuffixFrontendAuthForwardTLS = SuffixFrontendAuthForward + ".tls"
|
SuffixFrontendAuthForwardTLS = SuffixFrontendAuthForward + ".tls"
|
||||||
SuffixFrontendAuthForwardTLSCa = SuffixFrontendAuthForwardTLS + ".ca"
|
SuffixFrontendAuthForwardTLSCa = SuffixFrontendAuthForwardTLS + ".ca"
|
||||||
SuffixFrontendAuthForwardTLSCaOptional = SuffixFrontendAuthForwardTLS + ".caOptional"
|
SuffixFrontendAuthForwardTLSCaOptional = SuffixFrontendAuthForwardTLS + ".caOptional"
|
||||||
|
@ -150,6 +151,7 @@ const (
|
||||||
TraefikFrontendAuthDigestUsersFile = Prefix + SuffixFrontendAuthDigestUsersFile
|
TraefikFrontendAuthDigestUsersFile = Prefix + SuffixFrontendAuthDigestUsersFile
|
||||||
TraefikFrontendAuthForward = Prefix + SuffixFrontendAuthForward
|
TraefikFrontendAuthForward = Prefix + SuffixFrontendAuthForward
|
||||||
TraefikFrontendAuthForwardAddress = Prefix + SuffixFrontendAuthForwardAddress
|
TraefikFrontendAuthForwardAddress = Prefix + SuffixFrontendAuthForwardAddress
|
||||||
|
TraefikFrontendAuthForwardAuthResponseHeaders = Prefix + SuffixFrontendAuthForwardAuthResponseHeaders
|
||||||
TraefikFrontendAuthForwardTLS = Prefix + SuffixFrontendAuthForwardTLS
|
TraefikFrontendAuthForwardTLS = Prefix + SuffixFrontendAuthForwardTLS
|
||||||
TraefikFrontendAuthForwardTLSCa = Prefix + SuffixFrontendAuthForwardTLSCa
|
TraefikFrontendAuthForwardTLSCa = Prefix + SuffixFrontendAuthForwardTLSCa
|
||||||
TraefikFrontendAuthForwardTLSCaOptional = Prefix + SuffixFrontendAuthForwardTLSCaOptional
|
TraefikFrontendAuthForwardTLSCaOptional = Prefix + SuffixFrontendAuthForwardTLSCaOptional
|
||||||
|
|
|
@ -147,8 +147,9 @@ func getAuthDigest(labels map[string]string) *types.Digest {
|
||||||
// getAuthForward Create Forward Auth from labels
|
// getAuthForward Create Forward Auth from labels
|
||||||
func getAuthForward(labels map[string]string) *types.Forward {
|
func getAuthForward(labels map[string]string) *types.Forward {
|
||||||
forwardAuth := &types.Forward{
|
forwardAuth := &types.Forward{
|
||||||
Address: GetStringValue(labels, TraefikFrontendAuthForwardAddress, ""),
|
Address: GetStringValue(labels, TraefikFrontendAuthForwardAddress, ""),
|
||||||
TrustForwardHeader: GetBoolValue(labels, TraefikFrontendAuthForwardTrustForwardHeader, false),
|
AuthResponseHeaders: GetSliceStringValue(labels, TraefikFrontendAuthForwardAuthResponseHeaders),
|
||||||
|
TrustForwardHeader: GetBoolValue(labels, TraefikFrontendAuthForwardTrustForwardHeader, false),
|
||||||
}
|
}
|
||||||
|
|
||||||
// TLS configuration
|
// TLS configuration
|
||||||
|
|
370
provider/marathon/config_segment_test.go
Normal file
370
provider/marathon/config_segment_test.go
Normal file
|
@ -0,0 +1,370 @@
|
||||||
|
package marathon
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/containous/flaeg/parse"
|
||||||
|
"github.com/containous/traefik/provider/label"
|
||||||
|
"github.com/containous/traefik/types"
|
||||||
|
"github.com/gambol99/go-marathon"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestBuildConfigurationSegments(t *testing.T) {
|
||||||
|
testCases := []struct {
|
||||||
|
desc string
|
||||||
|
applications *marathon.Applications
|
||||||
|
expectedFrontends map[string]*types.Frontend
|
||||||
|
expectedBackends map[string]*types.Backend
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
desc: "multiple ports with segments",
|
||||||
|
applications: withApplications(
|
||||||
|
application(
|
||||||
|
appID("/app"),
|
||||||
|
appPorts(80, 81),
|
||||||
|
withTasks(localhostTask(taskPorts(80, 81))),
|
||||||
|
|
||||||
|
withLabel(label.TraefikBackendMaxConnAmount, "1000"),
|
||||||
|
withLabel(label.TraefikBackendMaxConnExtractorFunc, "client.ip"),
|
||||||
|
withSegmentLabel(label.TraefikPort, "80", "web"),
|
||||||
|
withSegmentLabel(label.TraefikPort, "81", "admin"),
|
||||||
|
withLabel("traefik..port", "82"), // This should be ignored, as it fails to match the segmentPropertiesRegexp regex.
|
||||||
|
withSegmentLabel(label.TraefikFrontendRule, "Host:web.app.marathon.localhost", "web"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendRule, "Host:admin.app.marathon.localhost", "admin"),
|
||||||
|
)),
|
||||||
|
expectedFrontends: map[string]*types.Frontend{
|
||||||
|
"frontend-app-service-web": {
|
||||||
|
Backend: "backend-app-service-web",
|
||||||
|
Routes: map[string]types.Route{
|
||||||
|
`route-host-app-service-web`: {
|
||||||
|
Rule: "Host:web.app.marathon.localhost",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
PassHostHeader: true,
|
||||||
|
EntryPoints: []string{},
|
||||||
|
},
|
||||||
|
"frontend-app-service-admin": {
|
||||||
|
Backend: "backend-app-service-admin",
|
||||||
|
Routes: map[string]types.Route{
|
||||||
|
`route-host-app-service-admin`: {
|
||||||
|
Rule: "Host:admin.app.marathon.localhost",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
PassHostHeader: true,
|
||||||
|
EntryPoints: []string{},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
expectedBackends: map[string]*types.Backend{
|
||||||
|
"backend-app-service-web": {
|
||||||
|
Servers: map[string]types.Server{
|
||||||
|
"server-app-taskID-service-web": {
|
||||||
|
URL: "http://localhost:80",
|
||||||
|
Weight: label.DefaultWeight,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
MaxConn: &types.MaxConn{
|
||||||
|
Amount: 1000,
|
||||||
|
ExtractorFunc: "client.ip",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"backend-app-service-admin": {
|
||||||
|
Servers: map[string]types.Server{
|
||||||
|
"server-app-taskID-service-admin": {
|
||||||
|
URL: "http://localhost:81",
|
||||||
|
Weight: label.DefaultWeight,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
MaxConn: &types.MaxConn{
|
||||||
|
Amount: 1000,
|
||||||
|
ExtractorFunc: "client.ip",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: "when all labels are set",
|
||||||
|
applications: withApplications(
|
||||||
|
application(
|
||||||
|
appID("/app"),
|
||||||
|
appPorts(80, 81),
|
||||||
|
withTasks(localhostTask(taskPorts(80, 81))),
|
||||||
|
|
||||||
|
// withLabel(label.TraefikBackend, "foobar"),
|
||||||
|
|
||||||
|
withLabel(label.TraefikBackendCircuitBreakerExpression, "NetworkErrorRatio() > 0.5"),
|
||||||
|
withLabel(label.TraefikBackendHealthCheckPath, "/health"),
|
||||||
|
withLabel(label.TraefikBackendHealthCheckPort, "880"),
|
||||||
|
withLabel(label.TraefikBackendHealthCheckInterval, "6"),
|
||||||
|
withLabel(label.TraefikBackendHealthCheckTimeout, "3"),
|
||||||
|
withLabel(label.TraefikBackendLoadBalancerMethod, "drr"),
|
||||||
|
withLabel(label.TraefikBackendLoadBalancerStickiness, "true"),
|
||||||
|
withLabel(label.TraefikBackendLoadBalancerStickinessCookieName, "chocolate"),
|
||||||
|
withLabel(label.TraefikBackendMaxConnAmount, "666"),
|
||||||
|
withLabel(label.TraefikBackendMaxConnExtractorFunc, "client.ip"),
|
||||||
|
withLabel(label.TraefikBackendBufferingMaxResponseBodyBytes, "10485760"),
|
||||||
|
withLabel(label.TraefikBackendBufferingMemResponseBodyBytes, "2097152"),
|
||||||
|
withLabel(label.TraefikBackendBufferingMaxRequestBodyBytes, "10485760"),
|
||||||
|
withLabel(label.TraefikBackendBufferingMemRequestBodyBytes, "2097152"),
|
||||||
|
withLabel(label.TraefikBackendBufferingRetryExpression, "IsNetworkError() && Attempts() <= 2"),
|
||||||
|
|
||||||
|
withSegmentLabel(label.TraefikPort, "80", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikProtocol, "https", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikWeight, "12", "containous"),
|
||||||
|
|
||||||
|
withSegmentLabel(label.TraefikFrontendPassTLSClientCertPem, "true", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendPassTLSClientCertInfosNotBefore, "true", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendPassTLSClientCertInfosNotAfter, "true", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendPassTLSClientCertInfosSans, "true", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendPassTLSClientCertInfosSubjectCommonName, "true", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendPassTLSClientCertInfosSubjectCountry, "true", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendPassTLSClientCertInfosSubjectLocality, "true", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendPassTLSClientCertInfosSubjectOrganization, "true", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendPassTLSClientCertInfosSubjectProvince, "true", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendPassTLSClientCertInfosSubjectSerialNumber, "true", "containous"),
|
||||||
|
|
||||||
|
withSegmentLabel(label.TraefikFrontendAuthBasic, "test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/,test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendAuthBasicRemoveHeader, "true", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendAuthBasicUsers, "test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/,test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendAuthBasicUsersFile, ".htpasswd", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendAuthDigestRemoveHeader, "true", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendAuthDigestUsers, "test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/,test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendAuthDigestUsersFile, ".htpasswd", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendAuthForwardAddress, "auth.server", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendAuthForwardTrustForwardHeader, "true", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendAuthForwardTLSCa, "ca.crt", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendAuthForwardTLSCaOptional, "true", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendAuthForwardTLSCert, "server.crt", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendAuthForwardTLSKey, "server.key", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendAuthForwardTLSInsecureSkipVerify, "true", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendAuthHeaderField, "X-WebAuth-User", "containous"),
|
||||||
|
|
||||||
|
withSegmentLabel(label.TraefikFrontendEntryPoints, "http,https", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendPassHostHeader, "true", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendPassTLSCert, "true", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendPriority, "666", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendRedirectEntryPoint, "https", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendRedirectRegex, "nope", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendRedirectReplacement, "nope", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendRedirectPermanent, "true", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendRule, "Host:traefik.io", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendWhiteListSourceRange, "10.10.10.10", "containous"),
|
||||||
|
|
||||||
|
withSegmentLabel(label.TraefikFrontendRequestHeaders, "Access-Control-Allow-Methods:POST,GET,OPTIONS || Content-type: application/json; charset=utf-8", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendResponseHeaders, "Access-Control-Allow-Methods:POST,GET,OPTIONS || Content-type: application/json; charset=utf-8", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendSSLProxyHeaders, "Access-Control-Allow-Methods:POST,GET,OPTIONS || Content-type: application/json; charset=utf-8", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendAllowedHosts, "foo,bar,bor", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendHostsProxyHeaders, "foo,bar,bor", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendSSLForceHost, "true", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendSSLHost, "foo", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendCustomFrameOptionsValue, "foo", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendContentSecurityPolicy, "foo", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendPublicKey, "foo", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendReferrerPolicy, "foo", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendCustomBrowserXSSValue, "foo", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendSTSSeconds, "666", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendSSLRedirect, "true", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendSSLTemporaryRedirect, "true", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendSTSIncludeSubdomains, "true", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendSTSPreload, "true", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendForceSTSHeader, "true", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendFrameDeny, "true", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendContentTypeNosniff, "true", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendBrowserXSSFilter, "true", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendIsDevelopment, "true", "containous"),
|
||||||
|
|
||||||
|
withLabel(label.Prefix+"containous."+label.BaseFrontendErrorPage+"foo."+label.SuffixErrorPageStatus, "404"),
|
||||||
|
withLabel(label.Prefix+"containous."+label.BaseFrontendErrorPage+"foo."+label.SuffixErrorPageBackend, "foobar"),
|
||||||
|
withLabel(label.Prefix+"containous."+label.BaseFrontendErrorPage+"foo."+label.SuffixErrorPageQuery, "foo_query"),
|
||||||
|
withLabel(label.Prefix+"containous."+label.BaseFrontendErrorPage+"bar."+label.SuffixErrorPageStatus, "500,600"),
|
||||||
|
withLabel(label.Prefix+"containous."+label.BaseFrontendErrorPage+"bar."+label.SuffixErrorPageBackend, "foobar"),
|
||||||
|
withLabel(label.Prefix+"containous."+label.BaseFrontendErrorPage+"bar."+label.SuffixErrorPageQuery, "bar_query"),
|
||||||
|
|
||||||
|
withSegmentLabel(label.TraefikFrontendRateLimitExtractorFunc, "client.ip", "containous"),
|
||||||
|
withLabel(label.Prefix+"containous."+label.BaseFrontendRateLimit+"foo."+label.SuffixRateLimitPeriod, "6"),
|
||||||
|
withLabel(label.Prefix+"containous."+label.BaseFrontendRateLimit+"foo."+label.SuffixRateLimitAverage, "12"),
|
||||||
|
withLabel(label.Prefix+"containous."+label.BaseFrontendRateLimit+"foo."+label.SuffixRateLimitBurst, "18"),
|
||||||
|
withLabel(label.Prefix+"containous."+label.BaseFrontendRateLimit+"bar."+label.SuffixRateLimitPeriod, "3"),
|
||||||
|
withLabel(label.Prefix+"containous."+label.BaseFrontendRateLimit+"bar."+label.SuffixRateLimitAverage, "6"),
|
||||||
|
withLabel(label.Prefix+"containous."+label.BaseFrontendRateLimit+"bar."+label.SuffixRateLimitBurst, "9"),
|
||||||
|
)),
|
||||||
|
expectedFrontends: map[string]*types.Frontend{
|
||||||
|
"frontend-app-service-containous": {
|
||||||
|
EntryPoints: []string{
|
||||||
|
"http",
|
||||||
|
"https",
|
||||||
|
},
|
||||||
|
Backend: "backend-app-service-containous",
|
||||||
|
Routes: map[string]types.Route{
|
||||||
|
"route-host-app-service-containous": {
|
||||||
|
Rule: "Host:traefik.io",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
PassHostHeader: true,
|
||||||
|
PassTLSCert: true,
|
||||||
|
Priority: 666,
|
||||||
|
PassTLSClientCert: &types.TLSClientHeaders{
|
||||||
|
PEM: true,
|
||||||
|
Infos: &types.TLSClientCertificateInfos{
|
||||||
|
NotBefore: true,
|
||||||
|
Sans: true,
|
||||||
|
NotAfter: true,
|
||||||
|
Subject: &types.TLSCLientCertificateSubjectInfos{
|
||||||
|
CommonName: true,
|
||||||
|
Country: true,
|
||||||
|
Locality: true,
|
||||||
|
Organization: true,
|
||||||
|
Province: true,
|
||||||
|
SerialNumber: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Auth: &types.Auth{
|
||||||
|
HeaderField: "X-WebAuth-User",
|
||||||
|
Basic: &types.Basic{
|
||||||
|
RemoveHeader: true,
|
||||||
|
Users: []string{"test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/",
|
||||||
|
"test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0"},
|
||||||
|
UsersFile: ".htpasswd",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
WhiteList: &types.WhiteList{
|
||||||
|
SourceRange: []string{"10.10.10.10"},
|
||||||
|
},
|
||||||
|
Headers: &types.Headers{
|
||||||
|
CustomRequestHeaders: map[string]string{
|
||||||
|
"Access-Control-Allow-Methods": "POST,GET,OPTIONS",
|
||||||
|
"Content-Type": "application/json; charset=utf-8",
|
||||||
|
},
|
||||||
|
CustomResponseHeaders: map[string]string{
|
||||||
|
"Access-Control-Allow-Methods": "POST,GET,OPTIONS",
|
||||||
|
"Content-Type": "application/json; charset=utf-8",
|
||||||
|
},
|
||||||
|
AllowedHosts: []string{
|
||||||
|
"foo",
|
||||||
|
"bar",
|
||||||
|
"bor",
|
||||||
|
},
|
||||||
|
HostsProxyHeaders: []string{
|
||||||
|
"foo",
|
||||||
|
"bar",
|
||||||
|
"bor",
|
||||||
|
},
|
||||||
|
SSLRedirect: true,
|
||||||
|
SSLTemporaryRedirect: true,
|
||||||
|
SSLForceHost: true,
|
||||||
|
SSLHost: "foo",
|
||||||
|
SSLProxyHeaders: map[string]string{
|
||||||
|
"Access-Control-Allow-Methods": "POST,GET,OPTIONS",
|
||||||
|
"Content-Type": "application/json; charset=utf-8",
|
||||||
|
},
|
||||||
|
STSSeconds: 666,
|
||||||
|
STSIncludeSubdomains: true,
|
||||||
|
STSPreload: true,
|
||||||
|
ForceSTSHeader: true,
|
||||||
|
FrameDeny: true,
|
||||||
|
CustomFrameOptionsValue: "foo",
|
||||||
|
ContentTypeNosniff: true,
|
||||||
|
BrowserXSSFilter: true,
|
||||||
|
CustomBrowserXSSValue: "foo",
|
||||||
|
ContentSecurityPolicy: "foo",
|
||||||
|
PublicKey: "foo",
|
||||||
|
ReferrerPolicy: "foo",
|
||||||
|
IsDevelopment: true,
|
||||||
|
},
|
||||||
|
Errors: map[string]*types.ErrorPage{
|
||||||
|
"bar": {
|
||||||
|
Status: []string{
|
||||||
|
"500",
|
||||||
|
"600",
|
||||||
|
},
|
||||||
|
Backend: "backendfoobar",
|
||||||
|
Query: "bar_query",
|
||||||
|
},
|
||||||
|
"foo": {
|
||||||
|
Status: []string{
|
||||||
|
"404",
|
||||||
|
},
|
||||||
|
Backend: "backendfoobar",
|
||||||
|
Query: "foo_query",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
RateLimit: &types.RateLimit{
|
||||||
|
RateSet: map[string]*types.Rate{
|
||||||
|
"bar": {
|
||||||
|
Period: parse.Duration(3 * time.Second),
|
||||||
|
Average: 6,
|
||||||
|
Burst: 9,
|
||||||
|
},
|
||||||
|
"foo": {
|
||||||
|
Period: parse.Duration(6 * time.Second),
|
||||||
|
Average: 12,
|
||||||
|
Burst: 18,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
ExtractorFunc: "client.ip",
|
||||||
|
},
|
||||||
|
Redirect: &types.Redirect{
|
||||||
|
EntryPoint: "https",
|
||||||
|
Permanent: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
expectedBackends: map[string]*types.Backend{
|
||||||
|
"backend-app-service-containous": {
|
||||||
|
Servers: map[string]types.Server{
|
||||||
|
"server-app-taskID-service-containous": {
|
||||||
|
URL: "https://localhost:80",
|
||||||
|
Weight: 12,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
CircuitBreaker: &types.CircuitBreaker{
|
||||||
|
Expression: "NetworkErrorRatio() > 0.5",
|
||||||
|
},
|
||||||
|
LoadBalancer: &types.LoadBalancer{
|
||||||
|
Method: "drr",
|
||||||
|
Stickiness: &types.Stickiness{
|
||||||
|
CookieName: "chocolate",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
MaxConn: &types.MaxConn{
|
||||||
|
Amount: 666,
|
||||||
|
ExtractorFunc: "client.ip",
|
||||||
|
},
|
||||||
|
HealthCheck: &types.HealthCheck{
|
||||||
|
Path: "/health",
|
||||||
|
Port: 880,
|
||||||
|
Interval: "6",
|
||||||
|
Timeout: "3",
|
||||||
|
},
|
||||||
|
Buffering: &types.Buffering{
|
||||||
|
MaxResponseBodyBytes: 10485760,
|
||||||
|
MemResponseBodyBytes: 2097152,
|
||||||
|
MaxRequestBodyBytes: 10485760,
|
||||||
|
MemRequestBodyBytes: 2097152,
|
||||||
|
RetryExpression: "IsNetworkError() && Attempts() <= 2",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range testCases {
|
||||||
|
test := test
|
||||||
|
t.Run(test.desc, func(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
p := &Provider{
|
||||||
|
Domain: "marathon.localhost",
|
||||||
|
ExposedByDefault: true,
|
||||||
|
}
|
||||||
|
|
||||||
|
actualConfig := p.buildConfiguration(test.applications)
|
||||||
|
|
||||||
|
assert.NotNil(t, actualConfig)
|
||||||
|
assert.Equal(t, test.expectedBackends, actualConfig.Backends)
|
||||||
|
assert.Equal(t, test.expectedFrontends, actualConfig.Frontends)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
|
@ -299,6 +299,7 @@ func TestBuildConfiguration(t *testing.T) {
|
||||||
withLabel(label.TraefikFrontendAuthForwardTLSCert, "server.crt"),
|
withLabel(label.TraefikFrontendAuthForwardTLSCert, "server.crt"),
|
||||||
withLabel(label.TraefikFrontendAuthForwardTLSKey, "server.key"),
|
withLabel(label.TraefikFrontendAuthForwardTLSKey, "server.key"),
|
||||||
withLabel(label.TraefikFrontendAuthForwardTLSInsecureSkipVerify, "true"),
|
withLabel(label.TraefikFrontendAuthForwardTLSInsecureSkipVerify, "true"),
|
||||||
|
withLabel(label.TraefikFrontendAuthForwardAuthResponseHeaders, "X-Auth-User,X-Auth-Token"),
|
||||||
|
|
||||||
withTasks(localhostTask(taskPorts(80))),
|
withTasks(localhostTask(taskPorts(80))),
|
||||||
)),
|
)),
|
||||||
|
@ -313,8 +314,7 @@ func TestBuildConfiguration(t *testing.T) {
|
||||||
Auth: &types.Auth{
|
Auth: &types.Auth{
|
||||||
HeaderField: "X-WebAuth-User",
|
HeaderField: "X-WebAuth-User",
|
||||||
Forward: &types.Forward{
|
Forward: &types.Forward{
|
||||||
Address: "auth.server",
|
Address: "auth.server",
|
||||||
TrustForwardHeader: true,
|
|
||||||
TLS: &types.ClientTLS{
|
TLS: &types.ClientTLS{
|
||||||
CA: "ca.crt",
|
CA: "ca.crt",
|
||||||
CAOptional: true,
|
CAOptional: true,
|
||||||
|
@ -322,6 +322,8 @@ func TestBuildConfiguration(t *testing.T) {
|
||||||
Cert: "server.crt",
|
Cert: "server.crt",
|
||||||
Key: "server.key",
|
Key: "server.key",
|
||||||
},
|
},
|
||||||
|
TrustForwardHeader: true,
|
||||||
|
AuthResponseHeaders: []string{"X-Auth-User", "X-Auth-Token"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
PassHostHeader: true,
|
PassHostHeader: true,
|
||||||
|
@ -695,364 +697,6 @@ func TestBuildConfiguration(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBuildConfigurationSegments(t *testing.T) {
|
|
||||||
testCases := []struct {
|
|
||||||
desc string
|
|
||||||
applications *marathon.Applications
|
|
||||||
expectedFrontends map[string]*types.Frontend
|
|
||||||
expectedBackends map[string]*types.Backend
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
desc: "multiple ports with segments",
|
|
||||||
applications: withApplications(
|
|
||||||
application(
|
|
||||||
appID("/app"),
|
|
||||||
appPorts(80, 81),
|
|
||||||
withTasks(localhostTask(taskPorts(80, 81))),
|
|
||||||
|
|
||||||
withLabel(label.TraefikBackendMaxConnAmount, "1000"),
|
|
||||||
withLabel(label.TraefikBackendMaxConnExtractorFunc, "client.ip"),
|
|
||||||
withSegmentLabel(label.TraefikPort, "80", "web"),
|
|
||||||
withSegmentLabel(label.TraefikPort, "81", "admin"),
|
|
||||||
withLabel("traefik..port", "82"), // This should be ignored, as it fails to match the segmentPropertiesRegexp regex.
|
|
||||||
withSegmentLabel(label.TraefikFrontendRule, "Host:web.app.marathon.localhost", "web"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendRule, "Host:admin.app.marathon.localhost", "admin"),
|
|
||||||
)),
|
|
||||||
expectedFrontends: map[string]*types.Frontend{
|
|
||||||
"frontend-app-service-web": {
|
|
||||||
Backend: "backend-app-service-web",
|
|
||||||
Routes: map[string]types.Route{
|
|
||||||
`route-host-app-service-web`: {
|
|
||||||
Rule: "Host:web.app.marathon.localhost",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
PassHostHeader: true,
|
|
||||||
EntryPoints: []string{},
|
|
||||||
},
|
|
||||||
"frontend-app-service-admin": {
|
|
||||||
Backend: "backend-app-service-admin",
|
|
||||||
Routes: map[string]types.Route{
|
|
||||||
`route-host-app-service-admin`: {
|
|
||||||
Rule: "Host:admin.app.marathon.localhost",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
PassHostHeader: true,
|
|
||||||
EntryPoints: []string{},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
expectedBackends: map[string]*types.Backend{
|
|
||||||
"backend-app-service-web": {
|
|
||||||
Servers: map[string]types.Server{
|
|
||||||
"server-app-taskID-service-web": {
|
|
||||||
URL: "http://localhost:80",
|
|
||||||
Weight: label.DefaultWeight,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
MaxConn: &types.MaxConn{
|
|
||||||
Amount: 1000,
|
|
||||||
ExtractorFunc: "client.ip",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"backend-app-service-admin": {
|
|
||||||
Servers: map[string]types.Server{
|
|
||||||
"server-app-taskID-service-admin": {
|
|
||||||
URL: "http://localhost:81",
|
|
||||||
Weight: label.DefaultWeight,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
MaxConn: &types.MaxConn{
|
|
||||||
Amount: 1000,
|
|
||||||
ExtractorFunc: "client.ip",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
desc: "when all labels are set",
|
|
||||||
applications: withApplications(
|
|
||||||
application(
|
|
||||||
appID("/app"),
|
|
||||||
appPorts(80, 81),
|
|
||||||
withTasks(localhostTask(taskPorts(80, 81))),
|
|
||||||
|
|
||||||
// withLabel(label.TraefikBackend, "foobar"),
|
|
||||||
|
|
||||||
withLabel(label.TraefikBackendCircuitBreakerExpression, "NetworkErrorRatio() > 0.5"),
|
|
||||||
withLabel(label.TraefikBackendHealthCheckPath, "/health"),
|
|
||||||
withLabel(label.TraefikBackendHealthCheckPort, "880"),
|
|
||||||
withLabel(label.TraefikBackendHealthCheckInterval, "6"),
|
|
||||||
withLabel(label.TraefikBackendHealthCheckTimeout, "3"),
|
|
||||||
withLabel(label.TraefikBackendLoadBalancerMethod, "drr"),
|
|
||||||
withLabel(label.TraefikBackendLoadBalancerStickiness, "true"),
|
|
||||||
withLabel(label.TraefikBackendLoadBalancerStickinessCookieName, "chocolate"),
|
|
||||||
withLabel(label.TraefikBackendMaxConnAmount, "666"),
|
|
||||||
withLabel(label.TraefikBackendMaxConnExtractorFunc, "client.ip"),
|
|
||||||
withLabel(label.TraefikBackendBufferingMaxResponseBodyBytes, "10485760"),
|
|
||||||
withLabel(label.TraefikBackendBufferingMemResponseBodyBytes, "2097152"),
|
|
||||||
withLabel(label.TraefikBackendBufferingMaxRequestBodyBytes, "10485760"),
|
|
||||||
withLabel(label.TraefikBackendBufferingMemRequestBodyBytes, "2097152"),
|
|
||||||
withLabel(label.TraefikBackendBufferingRetryExpression, "IsNetworkError() && Attempts() <= 2"),
|
|
||||||
|
|
||||||
withSegmentLabel(label.TraefikPort, "80", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikProtocol, "https", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikWeight, "12", "containous"),
|
|
||||||
|
|
||||||
withSegmentLabel(label.TraefikFrontendPassTLSClientCertPem, "true", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendPassTLSClientCertInfosNotBefore, "true", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendPassTLSClientCertInfosNotAfter, "true", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendPassTLSClientCertInfosSans, "true", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendPassTLSClientCertInfosSubjectCommonName, "true", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendPassTLSClientCertInfosSubjectCountry, "true", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendPassTLSClientCertInfosSubjectLocality, "true", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendPassTLSClientCertInfosSubjectOrganization, "true", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendPassTLSClientCertInfosSubjectProvince, "true", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendPassTLSClientCertInfosSubjectSerialNumber, "true", "containous"),
|
|
||||||
|
|
||||||
withSegmentLabel(label.TraefikFrontendAuthBasic, "test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/,test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendAuthBasicRemoveHeader, "true", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendAuthBasicUsers, "test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/,test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendAuthBasicUsersFile, ".htpasswd", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendAuthDigestRemoveHeader, "true", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendAuthDigestUsers, "test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/,test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendAuthDigestUsersFile, ".htpasswd", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendAuthForwardAddress, "auth.server", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendAuthForwardTrustForwardHeader, "true", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendAuthForwardTLSCa, "ca.crt", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendAuthForwardTLSCaOptional, "true", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendAuthForwardTLSCert, "server.crt", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendAuthForwardTLSKey, "server.key", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendAuthForwardTLSInsecureSkipVerify, "true", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendAuthHeaderField, "X-WebAuth-User", "containous"),
|
|
||||||
|
|
||||||
withSegmentLabel(label.TraefikFrontendEntryPoints, "http,https", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendPassHostHeader, "true", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendPassTLSCert, "true", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendPriority, "666", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendRedirectEntryPoint, "https", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendRedirectRegex, "nope", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendRedirectReplacement, "nope", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendRedirectPermanent, "true", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendRule, "Host:traefik.io", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendWhiteListSourceRange, "10.10.10.10", "containous"),
|
|
||||||
|
|
||||||
withSegmentLabel(label.TraefikFrontendRequestHeaders, "Access-Control-Allow-Methods:POST,GET,OPTIONS || Content-type: application/json; charset=utf-8", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendResponseHeaders, "Access-Control-Allow-Methods:POST,GET,OPTIONS || Content-type: application/json; charset=utf-8", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendSSLProxyHeaders, "Access-Control-Allow-Methods:POST,GET,OPTIONS || Content-type: application/json; charset=utf-8", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendAllowedHosts, "foo,bar,bor", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendHostsProxyHeaders, "foo,bar,bor", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendSSLForceHost, "true", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendSSLHost, "foo", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendCustomFrameOptionsValue, "foo", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendContentSecurityPolicy, "foo", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendPublicKey, "foo", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendReferrerPolicy, "foo", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendCustomBrowserXSSValue, "foo", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendSTSSeconds, "666", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendSSLRedirect, "true", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendSSLTemporaryRedirect, "true", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendSTSIncludeSubdomains, "true", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendSTSPreload, "true", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendForceSTSHeader, "true", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendFrameDeny, "true", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendContentTypeNosniff, "true", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendBrowserXSSFilter, "true", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendIsDevelopment, "true", "containous"),
|
|
||||||
|
|
||||||
withLabel(label.Prefix+"containous."+label.BaseFrontendErrorPage+"foo."+label.SuffixErrorPageStatus, "404"),
|
|
||||||
withLabel(label.Prefix+"containous."+label.BaseFrontendErrorPage+"foo."+label.SuffixErrorPageBackend, "foobar"),
|
|
||||||
withLabel(label.Prefix+"containous."+label.BaseFrontendErrorPage+"foo."+label.SuffixErrorPageQuery, "foo_query"),
|
|
||||||
withLabel(label.Prefix+"containous."+label.BaseFrontendErrorPage+"bar."+label.SuffixErrorPageStatus, "500,600"),
|
|
||||||
withLabel(label.Prefix+"containous."+label.BaseFrontendErrorPage+"bar."+label.SuffixErrorPageBackend, "foobar"),
|
|
||||||
withLabel(label.Prefix+"containous."+label.BaseFrontendErrorPage+"bar."+label.SuffixErrorPageQuery, "bar_query"),
|
|
||||||
|
|
||||||
withSegmentLabel(label.TraefikFrontendRateLimitExtractorFunc, "client.ip", "containous"),
|
|
||||||
withLabel(label.Prefix+"containous."+label.BaseFrontendRateLimit+"foo."+label.SuffixRateLimitPeriod, "6"),
|
|
||||||
withLabel(label.Prefix+"containous."+label.BaseFrontendRateLimit+"foo."+label.SuffixRateLimitAverage, "12"),
|
|
||||||
withLabel(label.Prefix+"containous."+label.BaseFrontendRateLimit+"foo."+label.SuffixRateLimitBurst, "18"),
|
|
||||||
withLabel(label.Prefix+"containous."+label.BaseFrontendRateLimit+"bar."+label.SuffixRateLimitPeriod, "3"),
|
|
||||||
withLabel(label.Prefix+"containous."+label.BaseFrontendRateLimit+"bar."+label.SuffixRateLimitAverage, "6"),
|
|
||||||
withLabel(label.Prefix+"containous."+label.BaseFrontendRateLimit+"bar."+label.SuffixRateLimitBurst, "9"),
|
|
||||||
)),
|
|
||||||
expectedFrontends: map[string]*types.Frontend{
|
|
||||||
"frontend-app-service-containous": {
|
|
||||||
EntryPoints: []string{
|
|
||||||
"http",
|
|
||||||
"https",
|
|
||||||
},
|
|
||||||
Backend: "backend-app-service-containous",
|
|
||||||
Routes: map[string]types.Route{
|
|
||||||
"route-host-app-service-containous": {
|
|
||||||
Rule: "Host:traefik.io",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
PassHostHeader: true,
|
|
||||||
PassTLSCert: true,
|
|
||||||
Priority: 666,
|
|
||||||
PassTLSClientCert: &types.TLSClientHeaders{
|
|
||||||
PEM: true,
|
|
||||||
Infos: &types.TLSClientCertificateInfos{
|
|
||||||
NotBefore: true,
|
|
||||||
Sans: true,
|
|
||||||
NotAfter: true,
|
|
||||||
Subject: &types.TLSCLientCertificateSubjectInfos{
|
|
||||||
CommonName: true,
|
|
||||||
Country: true,
|
|
||||||
Locality: true,
|
|
||||||
Organization: true,
|
|
||||||
Province: true,
|
|
||||||
SerialNumber: true,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
Auth: &types.Auth{
|
|
||||||
HeaderField: "X-WebAuth-User",
|
|
||||||
Basic: &types.Basic{
|
|
||||||
RemoveHeader: true,
|
|
||||||
Users: []string{"test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/",
|
|
||||||
"test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0"},
|
|
||||||
UsersFile: ".htpasswd",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
WhiteList: &types.WhiteList{
|
|
||||||
SourceRange: []string{"10.10.10.10"},
|
|
||||||
},
|
|
||||||
Headers: &types.Headers{
|
|
||||||
CustomRequestHeaders: map[string]string{
|
|
||||||
"Access-Control-Allow-Methods": "POST,GET,OPTIONS",
|
|
||||||
"Content-Type": "application/json; charset=utf-8",
|
|
||||||
},
|
|
||||||
CustomResponseHeaders: map[string]string{
|
|
||||||
"Access-Control-Allow-Methods": "POST,GET,OPTIONS",
|
|
||||||
"Content-Type": "application/json; charset=utf-8",
|
|
||||||
},
|
|
||||||
AllowedHosts: []string{
|
|
||||||
"foo",
|
|
||||||
"bar",
|
|
||||||
"bor",
|
|
||||||
},
|
|
||||||
HostsProxyHeaders: []string{
|
|
||||||
"foo",
|
|
||||||
"bar",
|
|
||||||
"bor",
|
|
||||||
},
|
|
||||||
SSLRedirect: true,
|
|
||||||
SSLTemporaryRedirect: true,
|
|
||||||
SSLForceHost: true,
|
|
||||||
SSLHost: "foo",
|
|
||||||
SSLProxyHeaders: map[string]string{
|
|
||||||
"Access-Control-Allow-Methods": "POST,GET,OPTIONS",
|
|
||||||
"Content-Type": "application/json; charset=utf-8",
|
|
||||||
},
|
|
||||||
STSSeconds: 666,
|
|
||||||
STSIncludeSubdomains: true,
|
|
||||||
STSPreload: true,
|
|
||||||
ForceSTSHeader: true,
|
|
||||||
FrameDeny: true,
|
|
||||||
CustomFrameOptionsValue: "foo",
|
|
||||||
ContentTypeNosniff: true,
|
|
||||||
BrowserXSSFilter: true,
|
|
||||||
CustomBrowserXSSValue: "foo",
|
|
||||||
ContentSecurityPolicy: "foo",
|
|
||||||
PublicKey: "foo",
|
|
||||||
ReferrerPolicy: "foo",
|
|
||||||
IsDevelopment: true,
|
|
||||||
},
|
|
||||||
Errors: map[string]*types.ErrorPage{
|
|
||||||
"bar": {
|
|
||||||
Status: []string{
|
|
||||||
"500",
|
|
||||||
"600",
|
|
||||||
},
|
|
||||||
Backend: "backendfoobar",
|
|
||||||
Query: "bar_query",
|
|
||||||
},
|
|
||||||
"foo": {
|
|
||||||
Status: []string{
|
|
||||||
"404",
|
|
||||||
},
|
|
||||||
Backend: "backendfoobar",
|
|
||||||
Query: "foo_query",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
RateLimit: &types.RateLimit{
|
|
||||||
RateSet: map[string]*types.Rate{
|
|
||||||
"bar": {
|
|
||||||
Period: parse.Duration(3 * time.Second),
|
|
||||||
Average: 6,
|
|
||||||
Burst: 9,
|
|
||||||
},
|
|
||||||
"foo": {
|
|
||||||
Period: parse.Duration(6 * time.Second),
|
|
||||||
Average: 12,
|
|
||||||
Burst: 18,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
ExtractorFunc: "client.ip",
|
|
||||||
},
|
|
||||||
Redirect: &types.Redirect{
|
|
||||||
EntryPoint: "https",
|
|
||||||
Permanent: true,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
expectedBackends: map[string]*types.Backend{
|
|
||||||
"backend-app-service-containous": {
|
|
||||||
Servers: map[string]types.Server{
|
|
||||||
"server-app-taskID-service-containous": {
|
|
||||||
URL: "https://localhost:80",
|
|
||||||
Weight: 12,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
CircuitBreaker: &types.CircuitBreaker{
|
|
||||||
Expression: "NetworkErrorRatio() > 0.5",
|
|
||||||
},
|
|
||||||
LoadBalancer: &types.LoadBalancer{
|
|
||||||
Method: "drr",
|
|
||||||
Stickiness: &types.Stickiness{
|
|
||||||
CookieName: "chocolate",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
MaxConn: &types.MaxConn{
|
|
||||||
Amount: 666,
|
|
||||||
ExtractorFunc: "client.ip",
|
|
||||||
},
|
|
||||||
HealthCheck: &types.HealthCheck{
|
|
||||||
Path: "/health",
|
|
||||||
Port: 880,
|
|
||||||
Interval: "6",
|
|
||||||
Timeout: "3",
|
|
||||||
},
|
|
||||||
Buffering: &types.Buffering{
|
|
||||||
MaxResponseBodyBytes: 10485760,
|
|
||||||
MemResponseBodyBytes: 2097152,
|
|
||||||
MaxRequestBodyBytes: 10485760,
|
|
||||||
MemRequestBodyBytes: 2097152,
|
|
||||||
RetryExpression: "IsNetworkError() && Attempts() <= 2",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, test := range testCases {
|
|
||||||
test := test
|
|
||||||
t.Run(test.desc, func(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
p := &Provider{
|
|
||||||
Domain: "marathon.localhost",
|
|
||||||
ExposedByDefault: true,
|
|
||||||
}
|
|
||||||
|
|
||||||
actualConfig := p.buildConfiguration(test.applications)
|
|
||||||
|
|
||||||
assert.NotNil(t, actualConfig)
|
|
||||||
assert.Equal(t, test.expectedBackends, actualConfig.Backends)
|
|
||||||
assert.Equal(t, test.expectedFrontends, actualConfig.Frontends)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestApplicationFilterConstraints(t *testing.T) {
|
func TestApplicationFilterConstraints(t *testing.T) {
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
desc string
|
desc string
|
||||||
|
|
392
provider/mesos/config_segment_test.go
Normal file
392
provider/mesos/config_segment_test.go
Normal file
|
@ -0,0 +1,392 @@
|
||||||
|
package mesos
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/containous/flaeg/parse"
|
||||||
|
"github.com/containous/traefik/provider/label"
|
||||||
|
"github.com/containous/traefik/types"
|
||||||
|
"github.com/mesosphere/mesos-dns/records/state"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestBuildConfigurationSegments(t *testing.T) {
|
||||||
|
p := &Provider{
|
||||||
|
Domain: "mesos.localhost",
|
||||||
|
ExposedByDefault: true,
|
||||||
|
IPSources: "host",
|
||||||
|
}
|
||||||
|
|
||||||
|
testCases := []struct {
|
||||||
|
desc string
|
||||||
|
tasks []state.Task
|
||||||
|
expectedFrontends map[string]*types.Frontend
|
||||||
|
expectedBackends map[string]*types.Backend
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
desc: "multiple ports with segments",
|
||||||
|
tasks: []state.Task{
|
||||||
|
aTask("app-taskID",
|
||||||
|
withIP("127.0.0.1"),
|
||||||
|
withInfo("/app",
|
||||||
|
withPorts(
|
||||||
|
withPort("TCP", 80, "web"),
|
||||||
|
withPort("TCP", 81, "admin"),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
withStatus(withHealthy(true), withState("TASK_RUNNING")),
|
||||||
|
withLabel(label.TraefikBackendMaxConnAmount, "1000"),
|
||||||
|
withLabel(label.TraefikBackendMaxConnExtractorFunc, "client.ip"),
|
||||||
|
withSegmentLabel(label.TraefikPort, "80", "web"),
|
||||||
|
withSegmentLabel(label.TraefikPort, "81", "admin"),
|
||||||
|
withLabel("traefik..port", "82"), // This should be ignored, as it fails to match the segmentPropertiesRegexp regex.
|
||||||
|
withSegmentLabel(label.TraefikFrontendRule, "Host:web.app.mesos.localhost", "web"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendRule, "Host:admin.app.mesos.localhost", "admin"),
|
||||||
|
),
|
||||||
|
},
|
||||||
|
expectedFrontends: map[string]*types.Frontend{
|
||||||
|
"frontend-app-taskID-service-web": {
|
||||||
|
Backend: "backend-app-service-web",
|
||||||
|
Routes: map[string]types.Route{
|
||||||
|
`route-host-app-taskID-service-web`: {
|
||||||
|
Rule: "Host:web.app.mesos.localhost",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
PassHostHeader: true,
|
||||||
|
EntryPoints: []string{},
|
||||||
|
},
|
||||||
|
"frontend-app-taskID-service-admin": {
|
||||||
|
Backend: "backend-app-service-admin",
|
||||||
|
Routes: map[string]types.Route{
|
||||||
|
`route-host-app-taskID-service-admin`: {
|
||||||
|
Rule: "Host:admin.app.mesos.localhost",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
PassHostHeader: true,
|
||||||
|
EntryPoints: []string{},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
expectedBackends: map[string]*types.Backend{
|
||||||
|
"backend-app-service-web": {
|
||||||
|
Servers: map[string]types.Server{
|
||||||
|
"server-app-taskID-service-web": {
|
||||||
|
URL: "http://127.0.0.1:80",
|
||||||
|
Weight: label.DefaultWeight,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
MaxConn: &types.MaxConn{
|
||||||
|
Amount: 1000,
|
||||||
|
ExtractorFunc: "client.ip",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"backend-app-service-admin": {
|
||||||
|
Servers: map[string]types.Server{
|
||||||
|
"server-app-taskID-service-admin": {
|
||||||
|
URL: "http://127.0.0.1:81",
|
||||||
|
Weight: label.DefaultWeight,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
MaxConn: &types.MaxConn{
|
||||||
|
Amount: 1000,
|
||||||
|
ExtractorFunc: "client.ip",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: "when all labels are set",
|
||||||
|
tasks: []state.Task{
|
||||||
|
aTask("app-taskID",
|
||||||
|
withIP("127.0.0.1"),
|
||||||
|
withInfo("/app",
|
||||||
|
withPorts(
|
||||||
|
withPort("TCP", 80, "web"),
|
||||||
|
withPort("TCP", 81, "admin"),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
withStatus(withHealthy(true), withState("TASK_RUNNING")),
|
||||||
|
|
||||||
|
withLabel(label.TraefikBackendCircuitBreakerExpression, "NetworkErrorRatio() > 0.5"),
|
||||||
|
withLabel(label.TraefikBackendHealthCheckScheme, "http"),
|
||||||
|
withLabel(label.TraefikBackendHealthCheckPath, "/health"),
|
||||||
|
withLabel(label.TraefikBackendHealthCheckPort, "880"),
|
||||||
|
withLabel(label.TraefikBackendHealthCheckInterval, "6"),
|
||||||
|
withLabel(label.TraefikBackendHealthCheckTimeout, "3"),
|
||||||
|
withLabel(label.TraefikBackendHealthCheckHostname, "foo.com"),
|
||||||
|
withLabel(label.TraefikBackendHealthCheckHeaders, "Foo:bar || Bar:foo"),
|
||||||
|
withLabel(label.TraefikBackendLoadBalancerMethod, "drr"),
|
||||||
|
withLabel(label.TraefikBackendLoadBalancerStickiness, "true"),
|
||||||
|
withLabel(label.TraefikBackendLoadBalancerStickinessCookieName, "chocolate"),
|
||||||
|
withLabel(label.TraefikBackendMaxConnAmount, "666"),
|
||||||
|
withLabel(label.TraefikBackendMaxConnExtractorFunc, "client.ip"),
|
||||||
|
withLabel(label.TraefikBackendBufferingMaxResponseBodyBytes, "10485760"),
|
||||||
|
withLabel(label.TraefikBackendBufferingMemResponseBodyBytes, "2097152"),
|
||||||
|
withLabel(label.TraefikBackendBufferingMaxRequestBodyBytes, "10485760"),
|
||||||
|
withLabel(label.TraefikBackendBufferingMemRequestBodyBytes, "2097152"),
|
||||||
|
withLabel(label.TraefikBackendBufferingRetryExpression, "IsNetworkError() && Attempts() <= 2"),
|
||||||
|
|
||||||
|
withSegmentLabel(label.TraefikPort, "80", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikPortName, "web", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikProtocol, "https", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikWeight, "12", "containous"),
|
||||||
|
|
||||||
|
withSegmentLabel(label.TraefikFrontendPassTLSClientCertPem, "true", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendPassTLSClientCertInfosNotBefore, "true", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendPassTLSClientCertInfosNotAfter, "true", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendPassTLSClientCertInfosSans, "true", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendPassTLSClientCertInfosSubjectCommonName, "true", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendPassTLSClientCertInfosSubjectCountry, "true", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendPassTLSClientCertInfosSubjectLocality, "true", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendPassTLSClientCertInfosSubjectOrganization, "true", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendPassTLSClientCertInfosSubjectProvince, "true", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendPassTLSClientCertInfosSubjectSerialNumber, "true", "containous"),
|
||||||
|
|
||||||
|
withSegmentLabel(label.TraefikFrontendAuthBasic, "test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/,test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendAuthBasicRemoveHeader, "true", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendAuthBasicUsers, "test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/,test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendAuthBasicUsersFile, ".htpasswd", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendAuthDigestRemoveHeader, "true", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendAuthDigestUsers, "test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/,test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendAuthDigestUsersFile, ".htpasswd", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendAuthForwardAddress, "auth.server", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendAuthForwardTrustForwardHeader, "true", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendAuthForwardTLSCa, "ca.crt", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendAuthForwardTLSCaOptional, "true", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendAuthForwardTLSCert, "server.crt", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendAuthForwardTLSKey, "server.key", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendAuthForwardTLSInsecureSkipVerify, "true", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendAuthHeaderField, "X-WebAuth-User", "containous"),
|
||||||
|
|
||||||
|
withSegmentLabel(label.TraefikFrontendEntryPoints, "http,https", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendPassHostHeader, "true", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendPassTLSCert, "true", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendPriority, "666", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendRedirectEntryPoint, "https", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendRedirectRegex, "nope", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendRedirectReplacement, "nope", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendRedirectPermanent, "true", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendRule, "Host:traefik.io", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendWhiteListSourceRange, "10.10.10.10", "containous"),
|
||||||
|
|
||||||
|
withSegmentLabel(label.TraefikFrontendRequestHeaders, "Access-Control-Allow-Methods:POST,GET,OPTIONS || Content-type: application/json; charset=utf-8", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendResponseHeaders, "Access-Control-Allow-Methods:POST,GET,OPTIONS || Content-type: application/json; charset=utf-8", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendSSLProxyHeaders, "Access-Control-Allow-Methods:POST,GET,OPTIONS || Content-type: application/json; charset=utf-8", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendAllowedHosts, "foo,bar,bor", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendHostsProxyHeaders, "foo,bar,bor", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendSSLForceHost, "true", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendSSLHost, "foo", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendCustomFrameOptionsValue, "foo", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendContentSecurityPolicy, "foo", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendPublicKey, "foo", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendReferrerPolicy, "foo", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendCustomBrowserXSSValue, "foo", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendSTSSeconds, "666", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendSSLRedirect, "true", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendSSLTemporaryRedirect, "true", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendSTSIncludeSubdomains, "true", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendSTSPreload, "true", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendForceSTSHeader, "true", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendFrameDeny, "true", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendContentTypeNosniff, "true", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendBrowserXSSFilter, "true", "containous"),
|
||||||
|
withSegmentLabel(label.TraefikFrontendIsDevelopment, "true", "containous"),
|
||||||
|
|
||||||
|
withLabel(label.Prefix+"containous."+label.BaseFrontendErrorPage+"foo."+label.SuffixErrorPageStatus, "404"),
|
||||||
|
withLabel(label.Prefix+"containous."+label.BaseFrontendErrorPage+"foo."+label.SuffixErrorPageBackend, "foobar"),
|
||||||
|
withLabel(label.Prefix+"containous."+label.BaseFrontendErrorPage+"foo."+label.SuffixErrorPageQuery, "foo_query"),
|
||||||
|
withLabel(label.Prefix+"containous."+label.BaseFrontendErrorPage+"bar."+label.SuffixErrorPageStatus, "500,600"),
|
||||||
|
withLabel(label.Prefix+"containous."+label.BaseFrontendErrorPage+"bar."+label.SuffixErrorPageBackend, "foobar"),
|
||||||
|
withLabel(label.Prefix+"containous."+label.BaseFrontendErrorPage+"bar."+label.SuffixErrorPageQuery, "bar_query"),
|
||||||
|
|
||||||
|
withSegmentLabel(label.TraefikFrontendRateLimitExtractorFunc, "client.ip", "containous"),
|
||||||
|
withLabel(label.Prefix+"containous."+label.BaseFrontendRateLimit+"foo."+label.SuffixRateLimitPeriod, "6"),
|
||||||
|
withLabel(label.Prefix+"containous."+label.BaseFrontendRateLimit+"foo."+label.SuffixRateLimitAverage, "12"),
|
||||||
|
withLabel(label.Prefix+"containous."+label.BaseFrontendRateLimit+"foo."+label.SuffixRateLimitBurst, "18"),
|
||||||
|
withLabel(label.Prefix+"containous."+label.BaseFrontendRateLimit+"bar."+label.SuffixRateLimitPeriod, "3"),
|
||||||
|
withLabel(label.Prefix+"containous."+label.BaseFrontendRateLimit+"bar."+label.SuffixRateLimitAverage, "6"),
|
||||||
|
withLabel(label.Prefix+"containous."+label.BaseFrontendRateLimit+"bar."+label.SuffixRateLimitBurst, "9"),
|
||||||
|
),
|
||||||
|
},
|
||||||
|
expectedFrontends: map[string]*types.Frontend{
|
||||||
|
"frontend-app-taskID-service-containous": {
|
||||||
|
EntryPoints: []string{
|
||||||
|
"http",
|
||||||
|
"https",
|
||||||
|
},
|
||||||
|
Backend: "backend-app-service-containous",
|
||||||
|
Routes: map[string]types.Route{
|
||||||
|
"route-host-app-taskID-service-containous": {
|
||||||
|
Rule: "Host:traefik.io",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
PassHostHeader: true,
|
||||||
|
PassTLSCert: true,
|
||||||
|
Priority: 666,
|
||||||
|
PassTLSClientCert: &types.TLSClientHeaders{
|
||||||
|
PEM: true,
|
||||||
|
Infos: &types.TLSClientCertificateInfos{
|
||||||
|
NotBefore: true,
|
||||||
|
Sans: true,
|
||||||
|
NotAfter: true,
|
||||||
|
Subject: &types.TLSCLientCertificateSubjectInfos{
|
||||||
|
CommonName: true,
|
||||||
|
Country: true,
|
||||||
|
Locality: true,
|
||||||
|
Organization: true,
|
||||||
|
Province: true,
|
||||||
|
SerialNumber: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Auth: &types.Auth{
|
||||||
|
HeaderField: "X-WebAuth-User",
|
||||||
|
Basic: &types.Basic{
|
||||||
|
RemoveHeader: true,
|
||||||
|
Users: []string{"test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/",
|
||||||
|
"test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0"},
|
||||||
|
UsersFile: ".htpasswd",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
|
WhiteList: &types.WhiteList{
|
||||||
|
SourceRange: []string{"10.10.10.10"},
|
||||||
|
},
|
||||||
|
Headers: &types.Headers{
|
||||||
|
CustomRequestHeaders: map[string]string{
|
||||||
|
"Access-Control-Allow-Methods": "POST,GET,OPTIONS",
|
||||||
|
"Content-Type": "application/json; charset=utf-8",
|
||||||
|
},
|
||||||
|
CustomResponseHeaders: map[string]string{
|
||||||
|
"Access-Control-Allow-Methods": "POST,GET,OPTIONS",
|
||||||
|
"Content-Type": "application/json; charset=utf-8",
|
||||||
|
},
|
||||||
|
AllowedHosts: []string{
|
||||||
|
"foo",
|
||||||
|
"bar",
|
||||||
|
"bor",
|
||||||
|
},
|
||||||
|
HostsProxyHeaders: []string{
|
||||||
|
"foo",
|
||||||
|
"bar",
|
||||||
|
"bor",
|
||||||
|
},
|
||||||
|
SSLRedirect: true,
|
||||||
|
SSLTemporaryRedirect: true,
|
||||||
|
SSLForceHost: true,
|
||||||
|
SSLHost: "foo",
|
||||||
|
SSLProxyHeaders: map[string]string{
|
||||||
|
"Access-Control-Allow-Methods": "POST,GET,OPTIONS",
|
||||||
|
"Content-Type": "application/json; charset=utf-8",
|
||||||
|
},
|
||||||
|
STSSeconds: 666,
|
||||||
|
STSIncludeSubdomains: true,
|
||||||
|
STSPreload: true,
|
||||||
|
ForceSTSHeader: true,
|
||||||
|
FrameDeny: true,
|
||||||
|
CustomFrameOptionsValue: "foo",
|
||||||
|
ContentTypeNosniff: true,
|
||||||
|
BrowserXSSFilter: true,
|
||||||
|
CustomBrowserXSSValue: "foo",
|
||||||
|
ContentSecurityPolicy: "foo",
|
||||||
|
PublicKey: "foo",
|
||||||
|
ReferrerPolicy: "foo",
|
||||||
|
IsDevelopment: true,
|
||||||
|
},
|
||||||
|
Errors: map[string]*types.ErrorPage{
|
||||||
|
"bar": {
|
||||||
|
Status: []string{
|
||||||
|
"500",
|
||||||
|
"600",
|
||||||
|
},
|
||||||
|
Backend: "backend-foobar",
|
||||||
|
Query: "bar_query",
|
||||||
|
},
|
||||||
|
"foo": {
|
||||||
|
Status: []string{
|
||||||
|
"404",
|
||||||
|
},
|
||||||
|
Backend: "backend-foobar",
|
||||||
|
Query: "foo_query",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
RateLimit: &types.RateLimit{
|
||||||
|
RateSet: map[string]*types.Rate{
|
||||||
|
"bar": {
|
||||||
|
Period: parse.Duration(3 * time.Second),
|
||||||
|
Average: 6,
|
||||||
|
Burst: 9,
|
||||||
|
},
|
||||||
|
"foo": {
|
||||||
|
Period: parse.Duration(6 * time.Second),
|
||||||
|
Average: 12,
|
||||||
|
Burst: 18,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
ExtractorFunc: "client.ip",
|
||||||
|
},
|
||||||
|
Redirect: &types.Redirect{
|
||||||
|
EntryPoint: "https",
|
||||||
|
Permanent: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
expectedBackends: map[string]*types.Backend{
|
||||||
|
"backend-app-service-containous": {
|
||||||
|
Servers: map[string]types.Server{
|
||||||
|
"server-app-taskID-service-containous": {
|
||||||
|
URL: "https://127.0.0.1:80",
|
||||||
|
Weight: 12,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
CircuitBreaker: &types.CircuitBreaker{
|
||||||
|
Expression: "NetworkErrorRatio() > 0.5",
|
||||||
|
},
|
||||||
|
LoadBalancer: &types.LoadBalancer{
|
||||||
|
Method: "drr",
|
||||||
|
Stickiness: &types.Stickiness{
|
||||||
|
CookieName: "chocolate",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
MaxConn: &types.MaxConn{
|
||||||
|
Amount: 666,
|
||||||
|
ExtractorFunc: "client.ip",
|
||||||
|
},
|
||||||
|
HealthCheck: &types.HealthCheck{
|
||||||
|
Scheme: "http",
|
||||||
|
Path: "/health",
|
||||||
|
Port: 880,
|
||||||
|
Interval: "6",
|
||||||
|
Timeout: "3",
|
||||||
|
Hostname: "foo.com",
|
||||||
|
Headers: map[string]string{
|
||||||
|
"Bar": "foo",
|
||||||
|
"Foo": "bar",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Buffering: &types.Buffering{
|
||||||
|
MaxResponseBodyBytes: 10485760,
|
||||||
|
MemResponseBodyBytes: 2097152,
|
||||||
|
MaxRequestBodyBytes: 10485760,
|
||||||
|
MemRequestBodyBytes: 2097152,
|
||||||
|
RetryExpression: "IsNetworkError() && Attempts() <= 2",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range testCases {
|
||||||
|
test := test
|
||||||
|
t.Run(test.desc, func(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
actualConfig := p.buildConfiguration(test.tasks)
|
||||||
|
|
||||||
|
require.NotNil(t, actualConfig)
|
||||||
|
assert.Equal(t, test.expectedBackends, actualConfig.Backends)
|
||||||
|
assert.Equal(t, test.expectedFrontends, actualConfig.Frontends)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
|
@ -262,6 +262,7 @@ func TestBuildConfiguration(t *testing.T) {
|
||||||
withLabel(label.TraefikFrontendAuthForwardTLSKey, "server.key"),
|
withLabel(label.TraefikFrontendAuthForwardTLSKey, "server.key"),
|
||||||
withLabel(label.TraefikFrontendAuthForwardTLSInsecureSkipVerify, "true"),
|
withLabel(label.TraefikFrontendAuthForwardTLSInsecureSkipVerify, "true"),
|
||||||
withLabel(label.TraefikFrontendAuthHeaderField, "X-WebAuth-User"),
|
withLabel(label.TraefikFrontendAuthHeaderField, "X-WebAuth-User"),
|
||||||
|
withLabel(label.TraefikFrontendAuthForwardAuthResponseHeaders, "X-Auth-User,X-Auth-Token"),
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
expectedFrontends: map[string]*types.Frontend{
|
expectedFrontends: map[string]*types.Frontend{
|
||||||
|
@ -277,8 +278,7 @@ func TestBuildConfiguration(t *testing.T) {
|
||||||
Auth: &types.Auth{
|
Auth: &types.Auth{
|
||||||
HeaderField: "X-WebAuth-User",
|
HeaderField: "X-WebAuth-User",
|
||||||
Forward: &types.Forward{
|
Forward: &types.Forward{
|
||||||
Address: "auth.server",
|
Address: "auth.server",
|
||||||
TrustForwardHeader: true,
|
|
||||||
TLS: &types.ClientTLS{
|
TLS: &types.ClientTLS{
|
||||||
CA: "ca.crt",
|
CA: "ca.crt",
|
||||||
CAOptional: true,
|
CAOptional: true,
|
||||||
|
@ -286,6 +286,8 @@ func TestBuildConfiguration(t *testing.T) {
|
||||||
Cert: "server.crt",
|
Cert: "server.crt",
|
||||||
Key: "server.key",
|
Key: "server.key",
|
||||||
},
|
},
|
||||||
|
TrustForwardHeader: true,
|
||||||
|
AuthResponseHeaders: []string{"X-Auth-User", "X-Auth-Token"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -597,385 +599,6 @@ func TestBuildConfiguration(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBuildConfigurationSegments(t *testing.T) {
|
|
||||||
p := &Provider{
|
|
||||||
Domain: "mesos.localhost",
|
|
||||||
ExposedByDefault: true,
|
|
||||||
IPSources: "host",
|
|
||||||
}
|
|
||||||
|
|
||||||
testCases := []struct {
|
|
||||||
desc string
|
|
||||||
tasks []state.Task
|
|
||||||
expectedFrontends map[string]*types.Frontend
|
|
||||||
expectedBackends map[string]*types.Backend
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
desc: "multiple ports with segments",
|
|
||||||
tasks: []state.Task{
|
|
||||||
aTask("app-taskID",
|
|
||||||
withIP("127.0.0.1"),
|
|
||||||
withInfo("/app",
|
|
||||||
withPorts(
|
|
||||||
withPort("TCP", 80, "web"),
|
|
||||||
withPort("TCP", 81, "admin"),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
withStatus(withHealthy(true), withState("TASK_RUNNING")),
|
|
||||||
withLabel(label.TraefikBackendMaxConnAmount, "1000"),
|
|
||||||
withLabel(label.TraefikBackendMaxConnExtractorFunc, "client.ip"),
|
|
||||||
withSegmentLabel(label.TraefikPort, "80", "web"),
|
|
||||||
withSegmentLabel(label.TraefikPort, "81", "admin"),
|
|
||||||
withLabel("traefik..port", "82"), // This should be ignored, as it fails to match the segmentPropertiesRegexp regex.
|
|
||||||
withSegmentLabel(label.TraefikFrontendRule, "Host:web.app.mesos.localhost", "web"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendRule, "Host:admin.app.mesos.localhost", "admin"),
|
|
||||||
),
|
|
||||||
},
|
|
||||||
expectedFrontends: map[string]*types.Frontend{
|
|
||||||
"frontend-app-taskID-service-web": {
|
|
||||||
Backend: "backend-app-service-web",
|
|
||||||
Routes: map[string]types.Route{
|
|
||||||
`route-host-app-taskID-service-web`: {
|
|
||||||
Rule: "Host:web.app.mesos.localhost",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
PassHostHeader: true,
|
|
||||||
EntryPoints: []string{},
|
|
||||||
},
|
|
||||||
"frontend-app-taskID-service-admin": {
|
|
||||||
Backend: "backend-app-service-admin",
|
|
||||||
Routes: map[string]types.Route{
|
|
||||||
`route-host-app-taskID-service-admin`: {
|
|
||||||
Rule: "Host:admin.app.mesos.localhost",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
PassHostHeader: true,
|
|
||||||
EntryPoints: []string{},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
expectedBackends: map[string]*types.Backend{
|
|
||||||
"backend-app-service-web": {
|
|
||||||
Servers: map[string]types.Server{
|
|
||||||
"server-app-taskID-service-web": {
|
|
||||||
URL: "http://127.0.0.1:80",
|
|
||||||
Weight: label.DefaultWeight,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
MaxConn: &types.MaxConn{
|
|
||||||
Amount: 1000,
|
|
||||||
ExtractorFunc: "client.ip",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"backend-app-service-admin": {
|
|
||||||
Servers: map[string]types.Server{
|
|
||||||
"server-app-taskID-service-admin": {
|
|
||||||
URL: "http://127.0.0.1:81",
|
|
||||||
Weight: label.DefaultWeight,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
MaxConn: &types.MaxConn{
|
|
||||||
Amount: 1000,
|
|
||||||
ExtractorFunc: "client.ip",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
desc: "when all labels are set",
|
|
||||||
tasks: []state.Task{
|
|
||||||
aTask("app-taskID",
|
|
||||||
withIP("127.0.0.1"),
|
|
||||||
withInfo("/app",
|
|
||||||
withPorts(
|
|
||||||
withPort("TCP", 80, "web"),
|
|
||||||
withPort("TCP", 81, "admin"),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
withStatus(withHealthy(true), withState("TASK_RUNNING")),
|
|
||||||
|
|
||||||
withLabel(label.TraefikBackendCircuitBreakerExpression, "NetworkErrorRatio() > 0.5"),
|
|
||||||
withLabel(label.TraefikBackendHealthCheckScheme, "http"),
|
|
||||||
withLabel(label.TraefikBackendHealthCheckPath, "/health"),
|
|
||||||
withLabel(label.TraefikBackendHealthCheckPort, "880"),
|
|
||||||
withLabel(label.TraefikBackendHealthCheckInterval, "6"),
|
|
||||||
withLabel(label.TraefikBackendHealthCheckTimeout, "3"),
|
|
||||||
withLabel(label.TraefikBackendHealthCheckHostname, "foo.com"),
|
|
||||||
withLabel(label.TraefikBackendHealthCheckHeaders, "Foo:bar || Bar:foo"),
|
|
||||||
withLabel(label.TraefikBackendLoadBalancerMethod, "drr"),
|
|
||||||
withLabel(label.TraefikBackendLoadBalancerStickiness, "true"),
|
|
||||||
withLabel(label.TraefikBackendLoadBalancerStickinessCookieName, "chocolate"),
|
|
||||||
withLabel(label.TraefikBackendMaxConnAmount, "666"),
|
|
||||||
withLabel(label.TraefikBackendMaxConnExtractorFunc, "client.ip"),
|
|
||||||
withLabel(label.TraefikBackendBufferingMaxResponseBodyBytes, "10485760"),
|
|
||||||
withLabel(label.TraefikBackendBufferingMemResponseBodyBytes, "2097152"),
|
|
||||||
withLabel(label.TraefikBackendBufferingMaxRequestBodyBytes, "10485760"),
|
|
||||||
withLabel(label.TraefikBackendBufferingMemRequestBodyBytes, "2097152"),
|
|
||||||
withLabel(label.TraefikBackendBufferingRetryExpression, "IsNetworkError() && Attempts() <= 2"),
|
|
||||||
|
|
||||||
withSegmentLabel(label.TraefikPort, "80", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikPortName, "web", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikProtocol, "https", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikWeight, "12", "containous"),
|
|
||||||
|
|
||||||
withSegmentLabel(label.TraefikFrontendPassTLSClientCertPem, "true", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendPassTLSClientCertInfosNotBefore, "true", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendPassTLSClientCertInfosNotAfter, "true", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendPassTLSClientCertInfosSans, "true", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendPassTLSClientCertInfosSubjectCommonName, "true", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendPassTLSClientCertInfosSubjectCountry, "true", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendPassTLSClientCertInfosSubjectLocality, "true", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendPassTLSClientCertInfosSubjectOrganization, "true", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendPassTLSClientCertInfosSubjectProvince, "true", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendPassTLSClientCertInfosSubjectSerialNumber, "true", "containous"),
|
|
||||||
|
|
||||||
withSegmentLabel(label.TraefikFrontendAuthBasic, "test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/,test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendAuthBasicRemoveHeader, "true", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendAuthBasicUsers, "test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/,test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendAuthBasicUsersFile, ".htpasswd", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendAuthDigestRemoveHeader, "true", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendAuthDigestUsers, "test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/,test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendAuthDigestUsersFile, ".htpasswd", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendAuthForwardAddress, "auth.server", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendAuthForwardTrustForwardHeader, "true", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendAuthForwardTLSCa, "ca.crt", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendAuthForwardTLSCaOptional, "true", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendAuthForwardTLSCert, "server.crt", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendAuthForwardTLSKey, "server.key", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendAuthForwardTLSInsecureSkipVerify, "true", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendAuthHeaderField, "X-WebAuth-User", "containous"),
|
|
||||||
|
|
||||||
withSegmentLabel(label.TraefikFrontendEntryPoints, "http,https", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendPassHostHeader, "true", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendPassTLSCert, "true", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendPriority, "666", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendRedirectEntryPoint, "https", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendRedirectRegex, "nope", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendRedirectReplacement, "nope", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendRedirectPermanent, "true", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendRule, "Host:traefik.io", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendWhiteListSourceRange, "10.10.10.10", "containous"),
|
|
||||||
|
|
||||||
withSegmentLabel(label.TraefikFrontendRequestHeaders, "Access-Control-Allow-Methods:POST,GET,OPTIONS || Content-type: application/json; charset=utf-8", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendResponseHeaders, "Access-Control-Allow-Methods:POST,GET,OPTIONS || Content-type: application/json; charset=utf-8", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendSSLProxyHeaders, "Access-Control-Allow-Methods:POST,GET,OPTIONS || Content-type: application/json; charset=utf-8", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendAllowedHosts, "foo,bar,bor", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendHostsProxyHeaders, "foo,bar,bor", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendSSLForceHost, "true", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendSSLHost, "foo", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendCustomFrameOptionsValue, "foo", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendContentSecurityPolicy, "foo", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendPublicKey, "foo", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendReferrerPolicy, "foo", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendCustomBrowserXSSValue, "foo", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendSTSSeconds, "666", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendSSLRedirect, "true", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendSSLTemporaryRedirect, "true", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendSTSIncludeSubdomains, "true", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendSTSPreload, "true", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendForceSTSHeader, "true", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendFrameDeny, "true", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendContentTypeNosniff, "true", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendBrowserXSSFilter, "true", "containous"),
|
|
||||||
withSegmentLabel(label.TraefikFrontendIsDevelopment, "true", "containous"),
|
|
||||||
|
|
||||||
withLabel(label.Prefix+"containous."+label.BaseFrontendErrorPage+"foo."+label.SuffixErrorPageStatus, "404"),
|
|
||||||
withLabel(label.Prefix+"containous."+label.BaseFrontendErrorPage+"foo."+label.SuffixErrorPageBackend, "foobar"),
|
|
||||||
withLabel(label.Prefix+"containous."+label.BaseFrontendErrorPage+"foo."+label.SuffixErrorPageQuery, "foo_query"),
|
|
||||||
withLabel(label.Prefix+"containous."+label.BaseFrontendErrorPage+"bar."+label.SuffixErrorPageStatus, "500,600"),
|
|
||||||
withLabel(label.Prefix+"containous."+label.BaseFrontendErrorPage+"bar."+label.SuffixErrorPageBackend, "foobar"),
|
|
||||||
withLabel(label.Prefix+"containous."+label.BaseFrontendErrorPage+"bar."+label.SuffixErrorPageQuery, "bar_query"),
|
|
||||||
|
|
||||||
withSegmentLabel(label.TraefikFrontendRateLimitExtractorFunc, "client.ip", "containous"),
|
|
||||||
withLabel(label.Prefix+"containous."+label.BaseFrontendRateLimit+"foo."+label.SuffixRateLimitPeriod, "6"),
|
|
||||||
withLabel(label.Prefix+"containous."+label.BaseFrontendRateLimit+"foo."+label.SuffixRateLimitAverage, "12"),
|
|
||||||
withLabel(label.Prefix+"containous."+label.BaseFrontendRateLimit+"foo."+label.SuffixRateLimitBurst, "18"),
|
|
||||||
withLabel(label.Prefix+"containous."+label.BaseFrontendRateLimit+"bar."+label.SuffixRateLimitPeriod, "3"),
|
|
||||||
withLabel(label.Prefix+"containous."+label.BaseFrontendRateLimit+"bar."+label.SuffixRateLimitAverage, "6"),
|
|
||||||
withLabel(label.Prefix+"containous."+label.BaseFrontendRateLimit+"bar."+label.SuffixRateLimitBurst, "9"),
|
|
||||||
),
|
|
||||||
},
|
|
||||||
expectedFrontends: map[string]*types.Frontend{
|
|
||||||
"frontend-app-taskID-service-containous": {
|
|
||||||
EntryPoints: []string{
|
|
||||||
"http",
|
|
||||||
"https",
|
|
||||||
},
|
|
||||||
Backend: "backend-app-service-containous",
|
|
||||||
Routes: map[string]types.Route{
|
|
||||||
"route-host-app-taskID-service-containous": {
|
|
||||||
Rule: "Host:traefik.io",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
PassHostHeader: true,
|
|
||||||
PassTLSCert: true,
|
|
||||||
Priority: 666,
|
|
||||||
PassTLSClientCert: &types.TLSClientHeaders{
|
|
||||||
PEM: true,
|
|
||||||
Infos: &types.TLSClientCertificateInfos{
|
|
||||||
NotBefore: true,
|
|
||||||
Sans: true,
|
|
||||||
NotAfter: true,
|
|
||||||
Subject: &types.TLSCLientCertificateSubjectInfos{
|
|
||||||
CommonName: true,
|
|
||||||
Country: true,
|
|
||||||
Locality: true,
|
|
||||||
Organization: true,
|
|
||||||
Province: true,
|
|
||||||
SerialNumber: true,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
Auth: &types.Auth{
|
|
||||||
HeaderField: "X-WebAuth-User",
|
|
||||||
Basic: &types.Basic{
|
|
||||||
RemoveHeader: true,
|
|
||||||
Users: []string{"test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/",
|
|
||||||
"test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0"},
|
|
||||||
UsersFile: ".htpasswd",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
|
|
||||||
WhiteList: &types.WhiteList{
|
|
||||||
SourceRange: []string{"10.10.10.10"},
|
|
||||||
},
|
|
||||||
Headers: &types.Headers{
|
|
||||||
CustomRequestHeaders: map[string]string{
|
|
||||||
"Access-Control-Allow-Methods": "POST,GET,OPTIONS",
|
|
||||||
"Content-Type": "application/json; charset=utf-8",
|
|
||||||
},
|
|
||||||
CustomResponseHeaders: map[string]string{
|
|
||||||
"Access-Control-Allow-Methods": "POST,GET,OPTIONS",
|
|
||||||
"Content-Type": "application/json; charset=utf-8",
|
|
||||||
},
|
|
||||||
AllowedHosts: []string{
|
|
||||||
"foo",
|
|
||||||
"bar",
|
|
||||||
"bor",
|
|
||||||
},
|
|
||||||
HostsProxyHeaders: []string{
|
|
||||||
"foo",
|
|
||||||
"bar",
|
|
||||||
"bor",
|
|
||||||
},
|
|
||||||
SSLRedirect: true,
|
|
||||||
SSLTemporaryRedirect: true,
|
|
||||||
SSLForceHost: true,
|
|
||||||
SSLHost: "foo",
|
|
||||||
SSLProxyHeaders: map[string]string{
|
|
||||||
"Access-Control-Allow-Methods": "POST,GET,OPTIONS",
|
|
||||||
"Content-Type": "application/json; charset=utf-8",
|
|
||||||
},
|
|
||||||
STSSeconds: 666,
|
|
||||||
STSIncludeSubdomains: true,
|
|
||||||
STSPreload: true,
|
|
||||||
ForceSTSHeader: true,
|
|
||||||
FrameDeny: true,
|
|
||||||
CustomFrameOptionsValue: "foo",
|
|
||||||
ContentTypeNosniff: true,
|
|
||||||
BrowserXSSFilter: true,
|
|
||||||
CustomBrowserXSSValue: "foo",
|
|
||||||
ContentSecurityPolicy: "foo",
|
|
||||||
PublicKey: "foo",
|
|
||||||
ReferrerPolicy: "foo",
|
|
||||||
IsDevelopment: true,
|
|
||||||
},
|
|
||||||
Errors: map[string]*types.ErrorPage{
|
|
||||||
"bar": {
|
|
||||||
Status: []string{
|
|
||||||
"500",
|
|
||||||
"600",
|
|
||||||
},
|
|
||||||
Backend: "backend-foobar",
|
|
||||||
Query: "bar_query",
|
|
||||||
},
|
|
||||||
"foo": {
|
|
||||||
Status: []string{
|
|
||||||
"404",
|
|
||||||
},
|
|
||||||
Backend: "backend-foobar",
|
|
||||||
Query: "foo_query",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
RateLimit: &types.RateLimit{
|
|
||||||
RateSet: map[string]*types.Rate{
|
|
||||||
"bar": {
|
|
||||||
Period: parse.Duration(3 * time.Second),
|
|
||||||
Average: 6,
|
|
||||||
Burst: 9,
|
|
||||||
},
|
|
||||||
"foo": {
|
|
||||||
Period: parse.Duration(6 * time.Second),
|
|
||||||
Average: 12,
|
|
||||||
Burst: 18,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
ExtractorFunc: "client.ip",
|
|
||||||
},
|
|
||||||
Redirect: &types.Redirect{
|
|
||||||
EntryPoint: "https",
|
|
||||||
Permanent: true,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
expectedBackends: map[string]*types.Backend{
|
|
||||||
"backend-app-service-containous": {
|
|
||||||
Servers: map[string]types.Server{
|
|
||||||
"server-app-taskID-service-containous": {
|
|
||||||
URL: "https://127.0.0.1:80",
|
|
||||||
Weight: 12,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
CircuitBreaker: &types.CircuitBreaker{
|
|
||||||
Expression: "NetworkErrorRatio() > 0.5",
|
|
||||||
},
|
|
||||||
LoadBalancer: &types.LoadBalancer{
|
|
||||||
Method: "drr",
|
|
||||||
Stickiness: &types.Stickiness{
|
|
||||||
CookieName: "chocolate",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
MaxConn: &types.MaxConn{
|
|
||||||
Amount: 666,
|
|
||||||
ExtractorFunc: "client.ip",
|
|
||||||
},
|
|
||||||
HealthCheck: &types.HealthCheck{
|
|
||||||
Scheme: "http",
|
|
||||||
Path: "/health",
|
|
||||||
Port: 880,
|
|
||||||
Interval: "6",
|
|
||||||
Timeout: "3",
|
|
||||||
Hostname: "foo.com",
|
|
||||||
Headers: map[string]string{
|
|
||||||
"Bar": "foo",
|
|
||||||
"Foo": "bar",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
Buffering: &types.Buffering{
|
|
||||||
MaxResponseBodyBytes: 10485760,
|
|
||||||
MemResponseBodyBytes: 2097152,
|
|
||||||
MaxRequestBodyBytes: 10485760,
|
|
||||||
MemRequestBodyBytes: 2097152,
|
|
||||||
RetryExpression: "IsNetworkError() && Attempts() <= 2",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, test := range testCases {
|
|
||||||
test := test
|
|
||||||
t.Run(test.desc, func(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
actualConfig := p.buildConfiguration(test.tasks)
|
|
||||||
|
|
||||||
require.NotNil(t, actualConfig)
|
|
||||||
assert.Equal(t, test.expectedBackends, actualConfig.Backends)
|
|
||||||
assert.Equal(t, test.expectedFrontends, actualConfig.Frontends)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestTaskFilter(t *testing.T) {
|
func TestTaskFilter(t *testing.T) {
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
desc string
|
desc string
|
||||||
|
|
|
@ -683,6 +683,7 @@ func TestProviderBuildConfiguration(t *testing.T) {
|
||||||
label.TraefikFrontendAuthForwardTLSKey: "server.key",
|
label.TraefikFrontendAuthForwardTLSKey: "server.key",
|
||||||
label.TraefikFrontendAuthForwardTLSInsecureSkipVerify: "true",
|
label.TraefikFrontendAuthForwardTLSInsecureSkipVerify: "true",
|
||||||
label.TraefikFrontendAuthHeaderField: "X-WebAuth-User",
|
label.TraefikFrontendAuthHeaderField: "X-WebAuth-User",
|
||||||
|
label.TraefikFrontendAuthForwardAuthResponseHeaders: "X-Auth-User,X-Auth-Token",
|
||||||
},
|
},
|
||||||
Health: "healthy",
|
Health: "healthy",
|
||||||
Containers: []string{"127.0.0.1"},
|
Containers: []string{"127.0.0.1"},
|
||||||
|
@ -696,8 +697,7 @@ func TestProviderBuildConfiguration(t *testing.T) {
|
||||||
Auth: &types.Auth{
|
Auth: &types.Auth{
|
||||||
HeaderField: "X-WebAuth-User",
|
HeaderField: "X-WebAuth-User",
|
||||||
Forward: &types.Forward{
|
Forward: &types.Forward{
|
||||||
Address: "auth.server",
|
Address: "auth.server",
|
||||||
TrustForwardHeader: true,
|
|
||||||
TLS: &types.ClientTLS{
|
TLS: &types.ClientTLS{
|
||||||
CA: "ca.crt",
|
CA: "ca.crt",
|
||||||
CAOptional: true,
|
CAOptional: true,
|
||||||
|
@ -705,6 +705,8 @@ func TestProviderBuildConfiguration(t *testing.T) {
|
||||||
Cert: "server.crt",
|
Cert: "server.crt",
|
||||||
Key: "server.key",
|
Key: "server.key",
|
||||||
},
|
},
|
||||||
|
TrustForwardHeader: true,
|
||||||
|
AuthResponseHeaders: []string{"X-Auth-User", "X-Auth-Token"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Priority: 0,
|
Priority: 0,
|
||||||
|
|
|
@ -145,7 +145,7 @@ func GoWithRecover(goroutine func(), customRecover func(err interface{})) {
|
||||||
|
|
||||||
func defaultRecoverGoroutine(err interface{}) {
|
func defaultRecoverGoroutine(err interface{}) {
|
||||||
log.Errorf("Error in Go routine: %s", err)
|
log.Errorf("Error in Go routine: %s", err)
|
||||||
debug.PrintStack()
|
log.Errorf("Stack: %s", debug.Stack())
|
||||||
}
|
}
|
||||||
|
|
||||||
// OperationWithRecover wrap a backoff operation in a Recover
|
// OperationWithRecover wrap a backoff operation in a Recover
|
||||||
|
|
|
@ -13,9 +13,12 @@ import (
|
||||||
func TestNewPoolContext(t *testing.T) {
|
func TestNewPoolContext(t *testing.T) {
|
||||||
type testKeyType string
|
type testKeyType string
|
||||||
testKey := testKeyType("test")
|
testKey := testKeyType("test")
|
||||||
|
|
||||||
ctx := context.WithValue(context.Background(), testKey, "test")
|
ctx := context.WithValue(context.Background(), testKey, "test")
|
||||||
p := NewPool(ctx)
|
p := NewPool(ctx)
|
||||||
|
|
||||||
retCtx := p.Ctx()
|
retCtx := p.Ctx()
|
||||||
|
|
||||||
retCtxVal, ok := retCtx.Value(testKey).(string)
|
retCtxVal, ok := retCtx.Value(testKey).(string)
|
||||||
if !ok || retCtxVal != "test" {
|
if !ok || retCtxVal != "test" {
|
||||||
t.Errorf("Pool.Ctx() did not return a derived context, got %#v, expected context with test value", retCtx)
|
t.Errorf("Pool.Ctx() did not return a derived context, got %#v, expected context with test value", retCtx)
|
||||||
|
@ -52,7 +55,8 @@ func (tr *fakeRoutine) routine(stop chan bool) {
|
||||||
|
|
||||||
func TestPoolWithCtx(t *testing.T) {
|
func TestPoolWithCtx(t *testing.T) {
|
||||||
testRoutine := newFakeRoutine()
|
testRoutine := newFakeRoutine()
|
||||||
tt := []struct {
|
|
||||||
|
testCases := []struct {
|
||||||
desc string
|
desc string
|
||||||
fn func(*Pool)
|
fn func(*Pool)
|
||||||
}{
|
}{
|
||||||
|
@ -70,19 +74,20 @@ func TestPoolWithCtx(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
for _, tc := range tt {
|
|
||||||
tc := tc
|
for _, test := range testCases {
|
||||||
t.Run(tc.desc, func(t *testing.T) {
|
test := test
|
||||||
|
t.Run(test.desc, func(t *testing.T) {
|
||||||
// These subtests cannot be run in parallel, since the testRoutine
|
// These subtests cannot be run in parallel, since the testRoutine
|
||||||
// is shared across the subtests.
|
// is shared across the subtests.
|
||||||
p := NewPool(context.Background())
|
p := NewPool(context.Background())
|
||||||
timer := time.NewTimer(500 * time.Millisecond)
|
timer := time.NewTimer(500 * time.Millisecond)
|
||||||
defer timer.Stop()
|
defer timer.Stop()
|
||||||
|
|
||||||
tc.fn(p)
|
test.fn(p)
|
||||||
defer p.Cleanup()
|
defer p.Cleanup()
|
||||||
if len(p.routinesCtx) != 1 {
|
if len(p.routinesCtx) != 1 {
|
||||||
t.Fatalf("After %s, Pool did have %d goroutineCtxs, expected 1", tc.desc, len(p.routinesCtx))
|
t.Fatalf("After %s, Pool did have %d goroutineCtxs, expected 1", test.desc, len(p.routinesCtx))
|
||||||
}
|
}
|
||||||
|
|
||||||
testDone := make(chan bool, 1)
|
testDone := make(chan bool, 1)
|
||||||
|
@ -91,6 +96,7 @@ func TestPoolWithCtx(t *testing.T) {
|
||||||
p.Cleanup()
|
p.Cleanup()
|
||||||
testDone <- true
|
testDone <- true
|
||||||
}()
|
}()
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case <-timer.C:
|
case <-timer.C:
|
||||||
testRoutine.Lock()
|
testRoutine.Lock()
|
||||||
|
@ -105,8 +111,9 @@ func TestPoolWithCtx(t *testing.T) {
|
||||||
|
|
||||||
func TestPoolWithStopChan(t *testing.T) {
|
func TestPoolWithStopChan(t *testing.T) {
|
||||||
testRoutine := newFakeRoutine()
|
testRoutine := newFakeRoutine()
|
||||||
ctx := context.Background()
|
|
||||||
p := NewPool(ctx)
|
p := NewPool(context.Background())
|
||||||
|
|
||||||
timer := time.NewTimer(500 * time.Millisecond)
|
timer := time.NewTimer(500 * time.Millisecond)
|
||||||
defer timer.Stop()
|
defer timer.Stop()
|
||||||
|
|
||||||
|
@ -121,6 +128,7 @@ func TestPoolWithStopChan(t *testing.T) {
|
||||||
p.Cleanup()
|
p.Cleanup()
|
||||||
testDone <- true
|
testDone <- true
|
||||||
}()
|
}()
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case <-timer.C:
|
case <-timer.C:
|
||||||
testRoutine.Lock()
|
testRoutine.Lock()
|
||||||
|
@ -133,8 +141,9 @@ func TestPoolWithStopChan(t *testing.T) {
|
||||||
|
|
||||||
func TestPoolStartWithStopChan(t *testing.T) {
|
func TestPoolStartWithStopChan(t *testing.T) {
|
||||||
testRoutine := newFakeRoutine()
|
testRoutine := newFakeRoutine()
|
||||||
ctx := context.Background()
|
|
||||||
p := NewPool(ctx)
|
p := NewPool(context.Background())
|
||||||
|
|
||||||
timer := time.NewTimer(500 * time.Millisecond)
|
timer := time.NewTimer(500 * time.Millisecond)
|
||||||
defer timer.Stop()
|
defer timer.Stop()
|
||||||
|
|
||||||
|
|
|
@ -5,19 +5,25 @@ import "testing"
|
||||||
func TestSafe(t *testing.T) {
|
func TestSafe(t *testing.T) {
|
||||||
const ts1 = "test1"
|
const ts1 = "test1"
|
||||||
const ts2 = "test2"
|
const ts2 = "test2"
|
||||||
|
|
||||||
s := New(ts1)
|
s := New(ts1)
|
||||||
|
|
||||||
result, ok := s.Get().(string)
|
result, ok := s.Get().(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
t.Fatalf("Safe.Get() failed, got type '%T', expected string", s.Get())
|
t.Fatalf("Safe.Get() failed, got type '%T', expected string", s.Get())
|
||||||
}
|
}
|
||||||
|
|
||||||
if result != ts1 {
|
if result != ts1 {
|
||||||
t.Errorf("Safe.Get() failed, got '%s', expected '%s'", result, ts1)
|
t.Errorf("Safe.Get() failed, got '%s', expected '%s'", result, ts1)
|
||||||
}
|
}
|
||||||
|
|
||||||
s.Set(ts2)
|
s.Set(ts2)
|
||||||
|
|
||||||
result, ok = s.Get().(string)
|
result, ok = s.Get().(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
t.Fatalf("Safe.Get() after Safe.Set() failed, got type '%T', expected string", s.Get())
|
t.Fatalf("Safe.Get() after Safe.Set() failed, got type '%T', expected string", s.Get())
|
||||||
}
|
}
|
||||||
|
|
||||||
if result != ts2 {
|
if result != ts2 {
|
||||||
t.Errorf("Safe.Get() after Safe.Set() failed, got '%s', expected '%s'", result, ts2)
|
t.Errorf("Safe.Get() after Safe.Set() failed, got '%s', expected '%s'", result, ts2)
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,7 +32,7 @@ find vendor -type f \( ! -iname 'licen[cs]e*' \
|
||||||
-a ! -iname '*.hxx' \
|
-a ! -iname '*.hxx' \
|
||||||
-a ! -iname '*.s' \) -exec rm -f {} +
|
-a ! -iname '*.s' \) -exec rm -f {} +
|
||||||
|
|
||||||
find -type d \( -iname '*Godeps*' \) -exec rm -rf {} +
|
find . -type d \( -iname '*Godeps*' \) -exec rm -rf {} +
|
||||||
|
|
||||||
find vendor -type l \( ! -iname 'licen[cs]e*' \
|
find vendor -type l \( ! -iname 'licen[cs]e*' \
|
||||||
-a ! -iname '*notice*' \
|
-a ! -iname '*notice*' \
|
||||||
|
@ -55,4 +55,4 @@ find vendor -type l \( ! -iname 'licen[cs]e*' \
|
||||||
-a ! -iname '*.hh' \
|
-a ! -iname '*.hh' \
|
||||||
-a ! -iname '*.hpp' \
|
-a ! -iname '*.hpp' \
|
||||||
-a ! -iname '*.hxx' \
|
-a ! -iname '*.hxx' \
|
||||||
-a ! -iname '*.s' \) -exec rm -f {} +
|
-a ! -iname '*.s' \) -exec rm -f {} +
|
||||||
|
|
|
@ -106,6 +106,11 @@
|
||||||
[frontends."frontend-{{ $service.ServiceName }}".auth.forward]
|
[frontends."frontend-{{ $service.ServiceName }}".auth.forward]
|
||||||
address = "{{ $auth.Forward.Address }}"
|
address = "{{ $auth.Forward.Address }}"
|
||||||
trustForwardHeader = {{ $auth.Forward.TrustForwardHeader }}
|
trustForwardHeader = {{ $auth.Forward.TrustForwardHeader }}
|
||||||
|
{{if $auth.Forward.AuthResponseHeaders }}
|
||||||
|
authResponseHeaders = [{{range $auth.Forward.AuthResponseHeaders }}
|
||||||
|
"{{.}}",
|
||||||
|
{{end}}]
|
||||||
|
{{end}}
|
||||||
|
|
||||||
{{if $auth.Forward.TLS }}
|
{{if $auth.Forward.TLS }}
|
||||||
[frontends."frontend-{{ $service.ServiceName }}".auth.forward.tls]
|
[frontends."frontend-{{ $service.ServiceName }}".auth.forward.tls]
|
||||||
|
|
|
@ -107,6 +107,11 @@
|
||||||
[frontends."frontend-{{ $frontendName }}".auth.forward]
|
[frontends."frontend-{{ $frontendName }}".auth.forward]
|
||||||
address = "{{ $auth.Forward.Address }}"
|
address = "{{ $auth.Forward.Address }}"
|
||||||
trustForwardHeader = {{ $auth.Forward.TrustForwardHeader }}
|
trustForwardHeader = {{ $auth.Forward.TrustForwardHeader }}
|
||||||
|
{{if $auth.Forward.AuthResponseHeaders }}
|
||||||
|
authResponseHeaders = [{{range $auth.Forward.AuthResponseHeaders }}
|
||||||
|
"{{.}}",
|
||||||
|
{{end}}]
|
||||||
|
{{end}}
|
||||||
|
|
||||||
{{if $auth.Forward.TLS }}
|
{{if $auth.Forward.TLS }}
|
||||||
[frontends."frontend-{{ $frontendName }}".auth.forward.tls]
|
[frontends."frontend-{{ $frontendName }}".auth.forward.tls]
|
||||||
|
|
|
@ -108,6 +108,11 @@
|
||||||
[frontends."frontend-{{ $frontendName }}".auth.forward]
|
[frontends."frontend-{{ $frontendName }}".auth.forward]
|
||||||
address = "{{ $auth.Forward.Address }}"
|
address = "{{ $auth.Forward.Address }}"
|
||||||
trustForwardHeader = {{ $auth.Forward.TrustForwardHeader }}
|
trustForwardHeader = {{ $auth.Forward.TrustForwardHeader }}
|
||||||
|
{{if $auth.Forward.AuthResponseHeaders }}
|
||||||
|
authResponseHeaders = [{{range $auth.Forward.AuthResponseHeaders }}
|
||||||
|
"{{.}}",
|
||||||
|
{{end}}]
|
||||||
|
{{end}}
|
||||||
|
|
||||||
{{if $auth.Forward.TLS }}
|
{{if $auth.Forward.TLS }}
|
||||||
[frontends."frontend-{{ $frontendName }}".auth.forward.tls]
|
[frontends."frontend-{{ $frontendName }}".auth.forward.tls]
|
||||||
|
|
|
@ -106,6 +106,11 @@
|
||||||
[frontends."{{ $frontendName }}".auth.forward]
|
[frontends."{{ $frontendName }}".auth.forward]
|
||||||
address = "{{ $auth.Forward.Address }}"
|
address = "{{ $auth.Forward.Address }}"
|
||||||
trustForwardHeader = {{ $auth.Forward.TrustForwardHeader }}
|
trustForwardHeader = {{ $auth.Forward.TrustForwardHeader }}
|
||||||
|
{{if $auth.Forward.AuthResponseHeaders }}
|
||||||
|
authResponseHeaders = [{{range $auth.Forward.AuthResponseHeaders }}
|
||||||
|
"{{.}}",
|
||||||
|
{{end}}]
|
||||||
|
{{end}}
|
||||||
|
|
||||||
{{if $auth.Forward.TLS }}
|
{{if $auth.Forward.TLS }}
|
||||||
[frontends."{{ $frontendName }}".auth.forward.tls]
|
[frontends."{{ $frontendName }}".auth.forward.tls]
|
||||||
|
|
|
@ -109,6 +109,11 @@
|
||||||
[frontends."{{ $frontendName }}".auth.forward]
|
[frontends."{{ $frontendName }}".auth.forward]
|
||||||
address = "{{ $auth.Forward.Address }}"
|
address = "{{ $auth.Forward.Address }}"
|
||||||
trustForwardHeader = {{ $auth.Forward.TrustForwardHeader }}
|
trustForwardHeader = {{ $auth.Forward.TrustForwardHeader }}
|
||||||
|
{{if $auth.Forward.AuthResponseHeaders }}
|
||||||
|
authResponseHeaders = [{{range $auth.Forward.AuthResponseHeaders }}
|
||||||
|
"{{.}}",
|
||||||
|
{{end}}]
|
||||||
|
{{end}}
|
||||||
|
|
||||||
{{if $auth.Forward.TLS }}
|
{{if $auth.Forward.TLS }}
|
||||||
[frontends."{{ $frontendName }}".auth.forward.tls]
|
[frontends."{{ $frontendName }}".auth.forward.tls]
|
||||||
|
|
|
@ -109,6 +109,11 @@
|
||||||
[frontends."frontend-{{ $frontendName }}".auth.forward]
|
[frontends."frontend-{{ $frontendName }}".auth.forward]
|
||||||
address = "{{ $auth.Forward.Address }}"
|
address = "{{ $auth.Forward.Address }}"
|
||||||
trustForwardHeader = {{ $auth.Forward.TrustForwardHeader }}
|
trustForwardHeader = {{ $auth.Forward.TrustForwardHeader }}
|
||||||
|
{{if $auth.Forward.AuthResponseHeaders }}
|
||||||
|
authResponseHeaders = [{{range $auth.Forward.AuthResponseHeaders }}
|
||||||
|
"{{.}}",
|
||||||
|
{{end}}]
|
||||||
|
{{end}}
|
||||||
|
|
||||||
{{if $auth.Forward.TLS }}
|
{{if $auth.Forward.TLS }}
|
||||||
[frontends."frontend-{{ $frontendName }}".auth.forward.tls]
|
[frontends."frontend-{{ $frontendName }}".auth.forward.tls]
|
||||||
|
|
|
@ -107,6 +107,11 @@
|
||||||
[frontends."frontend-{{ $frontendName }}".auth.forward]
|
[frontends."frontend-{{ $frontendName }}".auth.forward]
|
||||||
address = "{{ $auth.Forward.Address }}"
|
address = "{{ $auth.Forward.Address }}"
|
||||||
trustForwardHeader = {{ $auth.Forward.TrustForwardHeader }}
|
trustForwardHeader = {{ $auth.Forward.TrustForwardHeader }}
|
||||||
|
{{if $auth.Forward.AuthResponseHeaders }}
|
||||||
|
authResponseHeaders = [{{range $auth.Forward.AuthResponseHeaders }}
|
||||||
|
"{{.}}",
|
||||||
|
{{end}}]
|
||||||
|
{{end}}
|
||||||
|
|
||||||
{{if $auth.Forward.TLS }}
|
{{if $auth.Forward.TLS }}
|
||||||
[frontends."frontend-{{ $frontendName }}".auth.forward.tls]
|
[frontends."frontend-{{ $frontendName }}".auth.forward.tls]
|
||||||
|
|
26
vendor/github.com/cloudflare/cloudflare-go/LICENSE
generated
vendored
Normal file
26
vendor/github.com/cloudflare/cloudflare-go/LICENSE
generated
vendored
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
Copyright (c) 2015-2016, Cloudflare. All rights reserved.
|
||||||
|
|
||||||
|
Redistribution and use in source and binary forms, with or without modification,
|
||||||
|
are permitted provided that the following conditions are met:
|
||||||
|
|
||||||
|
1. Redistributions of source code must retain the above copyright notice, this
|
||||||
|
list of conditions and the following disclaimer.
|
||||||
|
|
||||||
|
2. Redistributions in binary form must reproduce the above copyright notice,
|
||||||
|
this list of conditions and the following disclaimer in the documentation and/or
|
||||||
|
other materials provided with the distribution.
|
||||||
|
|
||||||
|
3. Neither the name of the copyright holder nor the names of its contributors
|
||||||
|
may be used to endorse or promote products derived from this software without
|
||||||
|
specific prior written permission.
|
||||||
|
|
||||||
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||||
|
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||||
|
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||||
|
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
|
||||||
|
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||||
|
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||||
|
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||||
|
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||||
|
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
318
vendor/github.com/cloudflare/cloudflare-go/cloudflare.go
generated
vendored
Normal file
318
vendor/github.com/cloudflare/cloudflare-go/cloudflare.go
generated
vendored
Normal file
|
@ -0,0 +1,318 @@
|
||||||
|
// Package cloudflare implements the Cloudflare v4 API.
|
||||||
|
package cloudflare
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
|
"math"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
"golang.org/x/time/rate"
|
||||||
|
)
|
||||||
|
|
||||||
|
const apiURL = "https://api.cloudflare.com/client/v4"
|
||||||
|
const (
|
||||||
|
// AuthKeyEmail specifies that we should authenticate with API key and email address
|
||||||
|
AuthKeyEmail = 1 << iota
|
||||||
|
// AuthUserService specifies that we should authenticate with a User-Service key
|
||||||
|
AuthUserService
|
||||||
|
)
|
||||||
|
|
||||||
|
// API holds the configuration for the current API client. A client should not
|
||||||
|
// be modified concurrently.
|
||||||
|
type API struct {
|
||||||
|
APIKey string
|
||||||
|
APIEmail string
|
||||||
|
APIUserServiceKey string
|
||||||
|
BaseURL string
|
||||||
|
organizationID string
|
||||||
|
headers http.Header
|
||||||
|
httpClient *http.Client
|
||||||
|
authType int
|
||||||
|
rateLimiter *rate.Limiter
|
||||||
|
retryPolicy RetryPolicy
|
||||||
|
logger Logger
|
||||||
|
}
|
||||||
|
|
||||||
|
// New creates a new Cloudflare v4 API client.
|
||||||
|
func New(key, email string, opts ...Option) (*API, error) {
|
||||||
|
if key == "" || email == "" {
|
||||||
|
return nil, errors.New(errEmptyCredentials)
|
||||||
|
}
|
||||||
|
|
||||||
|
silentLogger := log.New(ioutil.Discard, "", log.LstdFlags)
|
||||||
|
|
||||||
|
api := &API{
|
||||||
|
APIKey: key,
|
||||||
|
APIEmail: email,
|
||||||
|
BaseURL: apiURL,
|
||||||
|
headers: make(http.Header),
|
||||||
|
authType: AuthKeyEmail,
|
||||||
|
rateLimiter: rate.NewLimiter(rate.Limit(4), 1), // 4rps equates to default api limit (1200 req/5 min)
|
||||||
|
retryPolicy: RetryPolicy{
|
||||||
|
MaxRetries: 3,
|
||||||
|
MinRetryDelay: time.Duration(1) * time.Second,
|
||||||
|
MaxRetryDelay: time.Duration(30) * time.Second,
|
||||||
|
},
|
||||||
|
logger: silentLogger,
|
||||||
|
}
|
||||||
|
|
||||||
|
err := api.parseOptions(opts...)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, "options parsing failed")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fall back to http.DefaultClient if the package user does not provide
|
||||||
|
// their own.
|
||||||
|
if api.httpClient == nil {
|
||||||
|
api.httpClient = http.DefaultClient
|
||||||
|
}
|
||||||
|
|
||||||
|
return api, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetAuthType sets the authentication method (AuthyKeyEmail or AuthUserService).
|
||||||
|
func (api *API) SetAuthType(authType int) {
|
||||||
|
api.authType = authType
|
||||||
|
}
|
||||||
|
|
||||||
|
// ZoneIDByName retrieves a zone's ID from the name.
|
||||||
|
func (api *API) ZoneIDByName(zoneName string) (string, error) {
|
||||||
|
res, err := api.ListZones(zoneName)
|
||||||
|
if err != nil {
|
||||||
|
return "", errors.Wrap(err, "ListZones command failed")
|
||||||
|
}
|
||||||
|
for _, zone := range res {
|
||||||
|
if zone.Name == zoneName {
|
||||||
|
return zone.ID, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return "", errors.New("Zone could not be found")
|
||||||
|
}
|
||||||
|
|
||||||
|
// makeRequest makes a HTTP request and returns the body as a byte slice,
|
||||||
|
// closing it before returnng. params will be serialized to JSON.
|
||||||
|
func (api *API) makeRequest(method, uri string, params interface{}) ([]byte, error) {
|
||||||
|
return api.makeRequestWithAuthType(method, uri, params, api.authType)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (api *API) makeRequestWithAuthType(method, uri string, params interface{}, authType int) ([]byte, error) {
|
||||||
|
// Replace nil with a JSON object if needed
|
||||||
|
var jsonBody []byte
|
||||||
|
var err error
|
||||||
|
if params != nil {
|
||||||
|
jsonBody, err = json.Marshal(params)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, "error marshalling params to JSON")
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
jsonBody = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var resp *http.Response
|
||||||
|
var respErr error
|
||||||
|
var reqBody io.Reader
|
||||||
|
var respBody []byte
|
||||||
|
for i := 0; i <= api.retryPolicy.MaxRetries; i++ {
|
||||||
|
if jsonBody != nil {
|
||||||
|
reqBody = bytes.NewReader(jsonBody)
|
||||||
|
}
|
||||||
|
|
||||||
|
if i > 0 {
|
||||||
|
// expect the backoff introduced here on errored requests to dominate the effect of rate limiting
|
||||||
|
// dont need a random component here as the rate limiter should do something similar
|
||||||
|
// nb time duration could truncate an arbitrary float. Since our inputs are all ints, we should be ok
|
||||||
|
sleepDuration := time.Duration(math.Pow(2, float64(i-1)) * float64(api.retryPolicy.MinRetryDelay))
|
||||||
|
|
||||||
|
if sleepDuration > api.retryPolicy.MaxRetryDelay {
|
||||||
|
sleepDuration = api.retryPolicy.MaxRetryDelay
|
||||||
|
}
|
||||||
|
// useful to do some simple logging here, maybe introduce levels later
|
||||||
|
api.logger.Printf("Sleeping %s before retry attempt number %d for request %s %s", sleepDuration.String(), i, method, uri)
|
||||||
|
time.Sleep(sleepDuration)
|
||||||
|
}
|
||||||
|
api.rateLimiter.Wait(context.TODO())
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, "Error caused by request rate limiting")
|
||||||
|
}
|
||||||
|
resp, respErr = api.request(method, uri, reqBody, authType)
|
||||||
|
|
||||||
|
// retry if the server is rate limiting us or if it failed
|
||||||
|
// assumes server operations are rolled back on failure
|
||||||
|
if respErr != nil || resp.StatusCode == http.StatusTooManyRequests || resp.StatusCode >= 500 {
|
||||||
|
// if we got a valid http response, try to read body so we can reuse the connection
|
||||||
|
// see https://golang.org/pkg/net/http/#Client.Do
|
||||||
|
if respErr == nil {
|
||||||
|
respBody, err = ioutil.ReadAll(resp.Body)
|
||||||
|
resp.Body.Close()
|
||||||
|
|
||||||
|
respErr = errors.Wrap(err, "could not read response body")
|
||||||
|
|
||||||
|
api.logger.Printf("Request: %s %s got an error response %d: %s\n", method, uri, resp.StatusCode,
|
||||||
|
strings.Replace(strings.Replace(string(respBody), "\n", "", -1), "\t", "", -1))
|
||||||
|
} else {
|
||||||
|
api.logger.Printf("Error performing request: %s %s : %s \n", method, uri, respErr.Error())
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
} else {
|
||||||
|
respBody, err = ioutil.ReadAll(resp.Body)
|
||||||
|
defer resp.Body.Close()
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, "could not read response body")
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if respErr != nil {
|
||||||
|
return nil, respErr
|
||||||
|
}
|
||||||
|
|
||||||
|
switch {
|
||||||
|
case resp.StatusCode >= http.StatusOK && resp.StatusCode < http.StatusMultipleChoices:
|
||||||
|
case resp.StatusCode == http.StatusUnauthorized:
|
||||||
|
return nil, errors.Errorf("HTTP status %d: invalid credentials", resp.StatusCode)
|
||||||
|
case resp.StatusCode == http.StatusForbidden:
|
||||||
|
return nil, errors.Errorf("HTTP status %d: insufficient permissions", resp.StatusCode)
|
||||||
|
case resp.StatusCode == http.StatusServiceUnavailable,
|
||||||
|
resp.StatusCode == http.StatusBadGateway,
|
||||||
|
resp.StatusCode == http.StatusGatewayTimeout,
|
||||||
|
resp.StatusCode == 522,
|
||||||
|
resp.StatusCode == 523,
|
||||||
|
resp.StatusCode == 524:
|
||||||
|
return nil, errors.Errorf("HTTP status %d: service failure", resp.StatusCode)
|
||||||
|
default:
|
||||||
|
var s string
|
||||||
|
if respBody != nil {
|
||||||
|
s = string(respBody)
|
||||||
|
}
|
||||||
|
return nil, errors.Errorf("HTTP status %d: content %q", resp.StatusCode, s)
|
||||||
|
}
|
||||||
|
|
||||||
|
return respBody, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// request makes a HTTP request to the given API endpoint, returning the raw
|
||||||
|
// *http.Response, or an error if one occurred. The caller is responsible for
|
||||||
|
// closing the response body.
|
||||||
|
func (api *API) request(method, uri string, reqBody io.Reader, authType int) (*http.Response, error) {
|
||||||
|
req, err := http.NewRequest(method, api.BaseURL+uri, reqBody)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, "HTTP request creation failed")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Apply any user-defined headers first.
|
||||||
|
req.Header = cloneHeader(api.headers)
|
||||||
|
if authType&AuthKeyEmail != 0 {
|
||||||
|
req.Header.Set("X-Auth-Key", api.APIKey)
|
||||||
|
req.Header.Set("X-Auth-Email", api.APIEmail)
|
||||||
|
}
|
||||||
|
if authType&AuthUserService != 0 {
|
||||||
|
req.Header.Set("X-Auth-User-Service-Key", api.APIUserServiceKey)
|
||||||
|
}
|
||||||
|
|
||||||
|
if req.Header.Get("Content-Type") == "" {
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err := api.httpClient.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, "HTTP request failed")
|
||||||
|
}
|
||||||
|
|
||||||
|
return resp, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Returns the base URL to use for API endpoints that exist for both accounts and organizations.
|
||||||
|
// If an Organization option was used when creating the API instance, returns the org URL.
|
||||||
|
//
|
||||||
|
// accountBase is the base URL for endpoints referring to the current user. It exists as a
|
||||||
|
// parameter because it is not consistent across APIs.
|
||||||
|
func (api *API) userBaseURL(accountBase string) string {
|
||||||
|
if api.organizationID != "" {
|
||||||
|
return "/organizations/" + api.organizationID
|
||||||
|
}
|
||||||
|
return accountBase
|
||||||
|
}
|
||||||
|
|
||||||
|
// cloneHeader returns a shallow copy of the header.
|
||||||
|
// copied from https://godoc.org/github.com/golang/gddo/httputil/header#Copy
|
||||||
|
func cloneHeader(header http.Header) http.Header {
|
||||||
|
h := make(http.Header)
|
||||||
|
for k, vs := range header {
|
||||||
|
h[k] = vs
|
||||||
|
}
|
||||||
|
return h
|
||||||
|
}
|
||||||
|
|
||||||
|
// ResponseInfo contains a code and message returned by the API as errors or
|
||||||
|
// informational messages inside the response.
|
||||||
|
type ResponseInfo struct {
|
||||||
|
Code int `json:"code"`
|
||||||
|
Message string `json:"message"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Response is a template. There will also be a result struct. There will be a
|
||||||
|
// unique response type for each response, which will include this type.
|
||||||
|
type Response struct {
|
||||||
|
Success bool `json:"success"`
|
||||||
|
Errors []ResponseInfo `json:"errors"`
|
||||||
|
Messages []ResponseInfo `json:"messages"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// ResultInfo contains metadata about the Response.
|
||||||
|
type ResultInfo struct {
|
||||||
|
Page int `json:"page"`
|
||||||
|
PerPage int `json:"per_page"`
|
||||||
|
TotalPages int `json:"total_pages"`
|
||||||
|
Count int `json:"count"`
|
||||||
|
Total int `json:"total_count"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// RawResponse keeps the result as JSON form
|
||||||
|
type RawResponse struct {
|
||||||
|
Response
|
||||||
|
Result json.RawMessage `json:"result"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Raw makes a HTTP request with user provided params and returns the
|
||||||
|
// result as untouched JSON.
|
||||||
|
func (api *API) Raw(method, endpoint string, data interface{}) (json.RawMessage, error) {
|
||||||
|
res, err := api.makeRequest(method, endpoint, data)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
|
||||||
|
var r RawResponse
|
||||||
|
if err := json.Unmarshal(res, &r); err != nil {
|
||||||
|
return nil, errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
return r.Result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// PaginationOptions can be passed to a list request to configure paging
|
||||||
|
// These values will be defaulted if omitted, and PerPage has min/max limits set by resource
|
||||||
|
type PaginationOptions struct {
|
||||||
|
Page int `json:"page,omitempty"`
|
||||||
|
PerPage int `json:"per_page,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// RetryPolicy specifies number of retries and min/max retry delays
|
||||||
|
// This config is used when the client exponentially backs off after errored requests
|
||||||
|
type RetryPolicy struct {
|
||||||
|
MaxRetries int
|
||||||
|
MinRetryDelay time.Duration
|
||||||
|
MaxRetryDelay time.Duration
|
||||||
|
}
|
||||||
|
|
||||||
|
// Logger defines the interface this library needs to use logging
|
||||||
|
// This is a subset of the methods implemented in the log package
|
||||||
|
type Logger interface {
|
||||||
|
Printf(format string, v ...interface{})
|
||||||
|
}
|
29
vendor/github.com/cloudflare/cloudflare-go/cpage.go
generated
vendored
Normal file
29
vendor/github.com/cloudflare/cloudflare-go/cpage.go
generated
vendored
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
package cloudflare
|
||||||
|
|
||||||
|
import "time"
|
||||||
|
|
||||||
|
// CustomPage represents a custom page configuration.
|
||||||
|
type CustomPage struct {
|
||||||
|
CreatedOn string `json:"created_on"`
|
||||||
|
ModifiedOn time.Time `json:"modified_on"`
|
||||||
|
URL string `json:"url"`
|
||||||
|
State string `json:"state"`
|
||||||
|
RequiredTokens []string `json:"required_tokens"`
|
||||||
|
PreviewTarget string `json:"preview_target"`
|
||||||
|
Description string `json:"description"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// CustomPageResponse represents the response from the custom pages endpoint.
|
||||||
|
type CustomPageResponse struct {
|
||||||
|
Response
|
||||||
|
Result []CustomPage `json:"result"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// https://api.cloudflare.com/#custom-pages-for-a-zone-available-custom-pages
|
||||||
|
// GET /zones/:zone_identifier/custom_pages
|
||||||
|
|
||||||
|
// https://api.cloudflare.com/#custom-pages-for-a-zone-custom-page-details
|
||||||
|
// GET /zones/:zone_identifier/custom_pages/:identifier
|
||||||
|
|
||||||
|
// https://api.cloudflare.com/#custom-pages-for-a-zone-update-custom-page-url
|
||||||
|
// PUT /zones/:zone_identifier/custom_pages/:identifier
|
149
vendor/github.com/cloudflare/cloudflare-go/custom_hostname.go
generated
vendored
Normal file
149
vendor/github.com/cloudflare/cloudflare-go/custom_hostname.go
generated
vendored
Normal file
|
@ -0,0 +1,149 @@
|
||||||
|
package cloudflare
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"net/url"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
// CustomHostnameSSL represents the SSL section in a given custom hostname.
|
||||||
|
type CustomHostnameSSL struct {
|
||||||
|
Status string `json:"status,omitempty"`
|
||||||
|
Method string `json:"method,omitempty"`
|
||||||
|
Type string `json:"type,omitempty"`
|
||||||
|
CnameTarget string `json:"cname_target,omitempty"`
|
||||||
|
CnameName string `json:"cname_name,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// CustomMetadata defines custom metadata for the hostname. This requires logic to be implemented by Cloudflare to act on the data provided.
|
||||||
|
type CustomMetadata map[string]interface{}
|
||||||
|
|
||||||
|
// CustomHostname represents a custom hostname in a zone.
|
||||||
|
type CustomHostname struct {
|
||||||
|
ID string `json:"id,omitempty"`
|
||||||
|
Hostname string `json:"hostname,omitempty"`
|
||||||
|
SSL CustomHostnameSSL `json:"ssl,omitempty"`
|
||||||
|
CustomMetadata CustomMetadata `json:"custom_metadata,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// CustomHostNameResponse represents a response from the Custom Hostnames endpoints.
|
||||||
|
type CustomHostnameResponse struct {
|
||||||
|
Result CustomHostname `json:"result"`
|
||||||
|
Response
|
||||||
|
}
|
||||||
|
|
||||||
|
// CustomHostnameListResponse represents a response from the Custom Hostnames endpoints.
|
||||||
|
type CustomHostnameListResponse struct {
|
||||||
|
Result []CustomHostname `json:"result"`
|
||||||
|
Response
|
||||||
|
ResultInfo `json:"result_info"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Modify SSL configuration for the given custom hostname in the given zone.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#custom-hostname-for-a-zone-update-custom-hostname-configuration
|
||||||
|
func (api *API) UpdateCustomHostnameSSL(zoneID string, customHostnameID string, ssl CustomHostnameSSL) (CustomHostname, error) {
|
||||||
|
return CustomHostname{}, errors.New("Not implemented")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete a custom hostname (and any issued SSL certificates)
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#custom-hostname-for-a-zone-delete-a-custom-hostname-and-any-issued-ssl-certificates-
|
||||||
|
func (api *API) DeleteCustomHostname(zoneID string, customHostnameID string) error {
|
||||||
|
uri := "/zones/" + zoneID + "/custom_hostnames/" + customHostnameID
|
||||||
|
res, err := api.makeRequest("DELETE", uri, nil)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
|
||||||
|
var response *CustomHostnameResponse
|
||||||
|
err = json.Unmarshal(res, &response)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreateCustomHostname creates a new custom hostname and requests that an SSL certificate be issued for it.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#custom-hostname-for-a-zone-create-custom-hostname
|
||||||
|
func (api *API) CreateCustomHostname(zoneID string, ch CustomHostname) (*CustomHostnameResponse, error) {
|
||||||
|
uri := "/zones/" + zoneID + "/custom_hostnames"
|
||||||
|
res, err := api.makeRequest("POST", uri, ch)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
|
||||||
|
var response *CustomHostnameResponse
|
||||||
|
err = json.Unmarshal(res, &response)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
|
||||||
|
return response, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// CustomHostnames fetches custom hostnames for the given zone,
|
||||||
|
// by applying filter.Hostname if not empty and scoping the result to page'th 50 items.
|
||||||
|
//
|
||||||
|
// The returned ResultInfo can be used to implement pagination.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#custom-hostname-for-a-zone-list-custom-hostnames
|
||||||
|
func (api *API) CustomHostnames(zoneID string, page int, filter CustomHostname) ([]CustomHostname, ResultInfo, error) {
|
||||||
|
v := url.Values{}
|
||||||
|
v.Set("per_page", "50")
|
||||||
|
v.Set("page", strconv.Itoa(page))
|
||||||
|
if filter.Hostname != "" {
|
||||||
|
v.Set("hostname", filter.Hostname)
|
||||||
|
}
|
||||||
|
query := "?" + v.Encode()
|
||||||
|
|
||||||
|
uri := "/zones/" + zoneID + "/custom_hostnames" + query
|
||||||
|
res, err := api.makeRequest("GET", uri, nil)
|
||||||
|
if err != nil {
|
||||||
|
return []CustomHostname{}, ResultInfo{}, errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
var customHostnameListResponse CustomHostnameListResponse
|
||||||
|
err = json.Unmarshal(res, &customHostnameListResponse)
|
||||||
|
if err != nil {
|
||||||
|
return []CustomHostname{}, ResultInfo{}, errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
|
||||||
|
return customHostnameListResponse.Result, customHostnameListResponse.ResultInfo, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// CustomHostname inspects the given custom hostname in the given zone.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#custom-hostname-for-a-zone-custom-hostname-configuration-details
|
||||||
|
func (api *API) CustomHostname(zoneID string, customHostnameID string) (CustomHostname, error) {
|
||||||
|
uri := "/zones/" + zoneID + "/custom_hostnames/" + customHostnameID
|
||||||
|
res, err := api.makeRequest("GET", uri, nil)
|
||||||
|
if err != nil {
|
||||||
|
return CustomHostname{}, errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
|
||||||
|
var response CustomHostnameResponse
|
||||||
|
err = json.Unmarshal(res, &response)
|
||||||
|
if err != nil {
|
||||||
|
return CustomHostname{}, errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
|
||||||
|
return response.Result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// CustomHostnameIDByName retrieves the ID for the given hostname in the given zone.
|
||||||
|
func (api *API) CustomHostnameIDByName(zoneID string, hostname string) (string, error) {
|
||||||
|
customHostnames, _, err := api.CustomHostnames(zoneID, 1, CustomHostname{Hostname: hostname})
|
||||||
|
if err != nil {
|
||||||
|
return "", errors.Wrap(err, "CustomHostnames command failed")
|
||||||
|
}
|
||||||
|
for _, ch := range customHostnames {
|
||||||
|
if ch.Hostname == hostname {
|
||||||
|
return ch.ID, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return "", errors.New("CustomHostname could not be found")
|
||||||
|
}
|
174
vendor/github.com/cloudflare/cloudflare-go/dns.go
generated
vendored
Normal file
174
vendor/github.com/cloudflare/cloudflare-go/dns.go
generated
vendored
Normal file
|
@ -0,0 +1,174 @@
|
||||||
|
package cloudflare
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"net/url"
|
||||||
|
"strconv"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
// DNSRecord represents a DNS record in a zone.
|
||||||
|
type DNSRecord struct {
|
||||||
|
ID string `json:"id,omitempty"`
|
||||||
|
Type string `json:"type,omitempty"`
|
||||||
|
Name string `json:"name,omitempty"`
|
||||||
|
Content string `json:"content,omitempty"`
|
||||||
|
Proxiable bool `json:"proxiable,omitempty"`
|
||||||
|
Proxied bool `json:"proxied,omitempty"`
|
||||||
|
TTL int `json:"ttl,omitempty"`
|
||||||
|
Locked bool `json:"locked,omitempty"`
|
||||||
|
ZoneID string `json:"zone_id,omitempty"`
|
||||||
|
ZoneName string `json:"zone_name,omitempty"`
|
||||||
|
CreatedOn time.Time `json:"created_on,omitempty"`
|
||||||
|
ModifiedOn time.Time `json:"modified_on,omitempty"`
|
||||||
|
Data interface{} `json:"data,omitempty"` // data returned by: SRV, LOC
|
||||||
|
Meta interface{} `json:"meta,omitempty"`
|
||||||
|
Priority int `json:"priority,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// DNSRecordResponse represents the response from the DNS endpoint.
|
||||||
|
type DNSRecordResponse struct {
|
||||||
|
Result DNSRecord `json:"result"`
|
||||||
|
Response
|
||||||
|
ResultInfo `json:"result_info"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// DNSListResponse represents the response from the list DNS records endpoint.
|
||||||
|
type DNSListResponse struct {
|
||||||
|
Result []DNSRecord `json:"result"`
|
||||||
|
Response
|
||||||
|
ResultInfo `json:"result_info"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreateDNSRecord creates a DNS record for the zone identifier.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#dns-records-for-a-zone-create-dns-record
|
||||||
|
func (api *API) CreateDNSRecord(zoneID string, rr DNSRecord) (*DNSRecordResponse, error) {
|
||||||
|
uri := "/zones/" + zoneID + "/dns_records"
|
||||||
|
res, err := api.makeRequest("POST", uri, rr)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
|
||||||
|
var recordResp *DNSRecordResponse
|
||||||
|
err = json.Unmarshal(res, &recordResp)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
|
||||||
|
return recordResp, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// DNSRecords returns a slice of DNS records for the given zone identifier.
|
||||||
|
//
|
||||||
|
// This takes a DNSRecord to allow filtering of the results returned.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#dns-records-for-a-zone-list-dns-records
|
||||||
|
func (api *API) DNSRecords(zoneID string, rr DNSRecord) ([]DNSRecord, error) {
|
||||||
|
// Construct a query string
|
||||||
|
v := url.Values{}
|
||||||
|
// Request as many records as possible per page - API max is 50
|
||||||
|
v.Set("per_page", "50")
|
||||||
|
if rr.Name != "" {
|
||||||
|
v.Set("name", rr.Name)
|
||||||
|
}
|
||||||
|
if rr.Type != "" {
|
||||||
|
v.Set("type", rr.Type)
|
||||||
|
}
|
||||||
|
if rr.Content != "" {
|
||||||
|
v.Set("content", rr.Content)
|
||||||
|
}
|
||||||
|
|
||||||
|
var query string
|
||||||
|
var records []DNSRecord
|
||||||
|
page := 1
|
||||||
|
|
||||||
|
// Loop over makeRequest until what we've fetched all records
|
||||||
|
for {
|
||||||
|
v.Set("page", strconv.Itoa(page))
|
||||||
|
query = "?" + v.Encode()
|
||||||
|
uri := "/zones/" + zoneID + "/dns_records" + query
|
||||||
|
res, err := api.makeRequest("GET", uri, nil)
|
||||||
|
if err != nil {
|
||||||
|
return []DNSRecord{}, errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
var r DNSListResponse
|
||||||
|
err = json.Unmarshal(res, &r)
|
||||||
|
if err != nil {
|
||||||
|
return []DNSRecord{}, errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
records = append(records, r.Result...)
|
||||||
|
if r.ResultInfo.Page >= r.ResultInfo.TotalPages {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
// Loop around and fetch the next page
|
||||||
|
page++
|
||||||
|
}
|
||||||
|
return records, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// DNSRecord returns a single DNS record for the given zone & record
|
||||||
|
// identifiers.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#dns-records-for-a-zone-dns-record-details
|
||||||
|
func (api *API) DNSRecord(zoneID, recordID string) (DNSRecord, error) {
|
||||||
|
uri := "/zones/" + zoneID + "/dns_records/" + recordID
|
||||||
|
res, err := api.makeRequest("GET", uri, nil)
|
||||||
|
if err != nil {
|
||||||
|
return DNSRecord{}, errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
var r DNSRecordResponse
|
||||||
|
err = json.Unmarshal(res, &r)
|
||||||
|
if err != nil {
|
||||||
|
return DNSRecord{}, errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
return r.Result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateDNSRecord updates a single DNS record for the given zone & record
|
||||||
|
// identifiers.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#dns-records-for-a-zone-update-dns-record
|
||||||
|
func (api *API) UpdateDNSRecord(zoneID, recordID string, rr DNSRecord) error {
|
||||||
|
rec, err := api.DNSRecord(zoneID, recordID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
// Populate the record name from the existing one if the update didn't
|
||||||
|
// specify it.
|
||||||
|
if rr.Name == "" {
|
||||||
|
rr.Name = rec.Name
|
||||||
|
}
|
||||||
|
rr.Type = rec.Type
|
||||||
|
uri := "/zones/" + zoneID + "/dns_records/" + recordID
|
||||||
|
res, err := api.makeRequest("PUT", uri, rr)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
var r DNSRecordResponse
|
||||||
|
err = json.Unmarshal(res, &r)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeleteDNSRecord deletes a single DNS record for the given zone & record
|
||||||
|
// identifiers.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#dns-records-for-a-zone-delete-dns-record
|
||||||
|
func (api *API) DeleteDNSRecord(zoneID, recordID string) error {
|
||||||
|
uri := "/zones/" + zoneID + "/dns_records/" + recordID
|
||||||
|
res, err := api.makeRequest("DELETE", uri, nil)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
var r DNSRecordResponse
|
||||||
|
err = json.Unmarshal(res, &r)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
48
vendor/github.com/cloudflare/cloudflare-go/errors.go
generated
vendored
Normal file
48
vendor/github.com/cloudflare/cloudflare-go/errors.go
generated
vendored
Normal file
|
@ -0,0 +1,48 @@
|
||||||
|
package cloudflare
|
||||||
|
|
||||||
|
// Error messages
|
||||||
|
const (
|
||||||
|
errEmptyCredentials = "invalid credentials: key & email must not be empty"
|
||||||
|
errMakeRequestError = "error from makeRequest"
|
||||||
|
errUnmarshalError = "error unmarshalling the JSON response"
|
||||||
|
errRequestNotSuccessful = "error reported by API"
|
||||||
|
)
|
||||||
|
|
||||||
|
var _ Error = &UserError{}
|
||||||
|
|
||||||
|
// Error represents an error returned from this library.
|
||||||
|
type Error interface {
|
||||||
|
error
|
||||||
|
// Raised when user credentials or configuration is invalid.
|
||||||
|
User() bool
|
||||||
|
// Raised when a parsing error (e.g. JSON) occurs.
|
||||||
|
Parse() bool
|
||||||
|
// Raised when a network error occurs.
|
||||||
|
Network() bool
|
||||||
|
// Contains the most recent error.
|
||||||
|
}
|
||||||
|
|
||||||
|
// UserError represents a user-generated error.
|
||||||
|
type UserError struct {
|
||||||
|
Err error
|
||||||
|
}
|
||||||
|
|
||||||
|
// User is a user-caused error.
|
||||||
|
func (e *UserError) User() bool {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// Network error.
|
||||||
|
func (e *UserError) Network() bool {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse error.
|
||||||
|
func (e *UserError) Parse() bool {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// Error wraps the underlying error.
|
||||||
|
func (e *UserError) Error() string {
|
||||||
|
return e.Err.Error()
|
||||||
|
}
|
241
vendor/github.com/cloudflare/cloudflare-go/firewall.go
generated
vendored
Normal file
241
vendor/github.com/cloudflare/cloudflare-go/firewall.go
generated
vendored
Normal file
|
@ -0,0 +1,241 @@
|
||||||
|
package cloudflare
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"net/url"
|
||||||
|
"strconv"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
// AccessRule represents a firewall access rule.
|
||||||
|
type AccessRule struct {
|
||||||
|
ID string `json:"id,omitempty"`
|
||||||
|
Notes string `json:"notes,omitempty"`
|
||||||
|
AllowedModes []string `json:"allowed_modes,omitempty"`
|
||||||
|
Mode string `json:"mode,omitempty"`
|
||||||
|
Configuration AccessRuleConfiguration `json:"configuration,omitempty"`
|
||||||
|
Scope AccessRuleScope `json:"scope,omitempty"`
|
||||||
|
CreatedOn time.Time `json:"created_on,omitempty"`
|
||||||
|
ModifiedOn time.Time `json:"modified_on,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// AccessRuleConfiguration represents the configuration of a firewall
|
||||||
|
// access rule.
|
||||||
|
type AccessRuleConfiguration struct {
|
||||||
|
Target string `json:"target,omitempty"`
|
||||||
|
Value string `json:"value,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// AccessRuleScope represents the scope of a firewall access rule.
|
||||||
|
type AccessRuleScope struct {
|
||||||
|
ID string `json:"id,omitempty"`
|
||||||
|
Email string `json:"email,omitempty"`
|
||||||
|
Name string `json:"name,omitempty"`
|
||||||
|
Type string `json:"type,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// AccessRuleResponse represents the response from the firewall access
|
||||||
|
// rule endpoint.
|
||||||
|
type AccessRuleResponse struct {
|
||||||
|
Result AccessRule `json:"result"`
|
||||||
|
Response
|
||||||
|
ResultInfo `json:"result_info"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// AccessRuleListResponse represents the response from the list access rules
|
||||||
|
// endpoint.
|
||||||
|
type AccessRuleListResponse struct {
|
||||||
|
Result []AccessRule `json:"result"`
|
||||||
|
Response
|
||||||
|
ResultInfo `json:"result_info"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// ListUserAccessRules returns a slice of access rules for the logged-in user.
|
||||||
|
//
|
||||||
|
// This takes an AccessRule to allow filtering of the results returned.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#user-level-firewall-access-rule-list-access-rules
|
||||||
|
func (api *API) ListUserAccessRules(accessRule AccessRule, page int) (*AccessRuleListResponse, error) {
|
||||||
|
return api.listAccessRules("/user", accessRule, page)
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreateUserAccessRule creates a firewall access rule for the logged-in user.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#user-level-firewall-access-rule-create-access-rule
|
||||||
|
func (api *API) CreateUserAccessRule(accessRule AccessRule) (*AccessRuleResponse, error) {
|
||||||
|
return api.createAccessRule("/user", accessRule)
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateUserAccessRule updates a single access rule for the logged-in user &
|
||||||
|
// given access rule identifier.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#user-level-firewall-access-rule-update-access-rule
|
||||||
|
func (api *API) UpdateUserAccessRule(accessRuleID string, accessRule AccessRule) (*AccessRuleResponse, error) {
|
||||||
|
return api.updateAccessRule("/user", accessRuleID, accessRule)
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeleteUserAccessRule deletes a single access rule for the logged-in user and
|
||||||
|
// access rule identifiers.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#user-level-firewall-access-rule-update-access-rule
|
||||||
|
func (api *API) DeleteUserAccessRule(accessRuleID string) (*AccessRuleResponse, error) {
|
||||||
|
return api.deleteAccessRule("/user", accessRuleID)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ListZoneAccessRules returns a slice of access rules for the given zone
|
||||||
|
// identifier.
|
||||||
|
//
|
||||||
|
// This takes an AccessRule to allow filtering of the results returned.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#firewall-access-rule-for-a-zone-list-access-rules
|
||||||
|
func (api *API) ListZoneAccessRules(zoneID string, accessRule AccessRule, page int) (*AccessRuleListResponse, error) {
|
||||||
|
return api.listAccessRules("/zones/"+zoneID, accessRule, page)
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreateZoneAccessRule creates a firewall access rule for the given zone
|
||||||
|
// identifier.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#firewall-access-rule-for-a-zone-create-access-rule
|
||||||
|
func (api *API) CreateZoneAccessRule(zoneID string, accessRule AccessRule) (*AccessRuleResponse, error) {
|
||||||
|
return api.createAccessRule("/zones/"+zoneID, accessRule)
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateZoneAccessRule updates a single access rule for the given zone &
|
||||||
|
// access rule identifiers.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#firewall-access-rule-for-a-zone-update-access-rule
|
||||||
|
func (api *API) UpdateZoneAccessRule(zoneID, accessRuleID string, accessRule AccessRule) (*AccessRuleResponse, error) {
|
||||||
|
return api.updateAccessRule("/zones/"+zoneID, accessRuleID, accessRule)
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeleteZoneAccessRule deletes a single access rule for the given zone and
|
||||||
|
// access rule identifiers.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#firewall-access-rule-for-a-zone-delete-access-rule
|
||||||
|
func (api *API) DeleteZoneAccessRule(zoneID, accessRuleID string) (*AccessRuleResponse, error) {
|
||||||
|
return api.deleteAccessRule("/zones/"+zoneID, accessRuleID)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ListOrganizationAccessRules returns a slice of access rules for the given
|
||||||
|
// organization identifier.
|
||||||
|
//
|
||||||
|
// This takes an AccessRule to allow filtering of the results returned.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#organization-level-firewall-access-rule-list-access-rules
|
||||||
|
func (api *API) ListOrganizationAccessRules(organizationID string, accessRule AccessRule, page int) (*AccessRuleListResponse, error) {
|
||||||
|
return api.listAccessRules("/organizations/"+organizationID, accessRule, page)
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreateOrganizationAccessRule creates a firewall access rule for the given
|
||||||
|
// organization identifier.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#organization-level-firewall-access-rule-create-access-rule
|
||||||
|
func (api *API) CreateOrganizationAccessRule(organizationID string, accessRule AccessRule) (*AccessRuleResponse, error) {
|
||||||
|
return api.createAccessRule("/organizations/"+organizationID, accessRule)
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateOrganizationAccessRule updates a single access rule for the given
|
||||||
|
// organization & access rule identifiers.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#organization-level-firewall-access-rule-update-access-rule
|
||||||
|
func (api *API) UpdateOrganizationAccessRule(organizationID, accessRuleID string, accessRule AccessRule) (*AccessRuleResponse, error) {
|
||||||
|
return api.updateAccessRule("/organizations/"+organizationID, accessRuleID, accessRule)
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeleteOrganizationAccessRule deletes a single access rule for the given
|
||||||
|
// organization and access rule identifiers.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#organization-level-firewall-access-rule-delete-access-rule
|
||||||
|
func (api *API) DeleteOrganizationAccessRule(organizationID, accessRuleID string) (*AccessRuleResponse, error) {
|
||||||
|
return api.deleteAccessRule("/organizations/"+organizationID, accessRuleID)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (api *API) listAccessRules(prefix string, accessRule AccessRule, page int) (*AccessRuleListResponse, error) {
|
||||||
|
// Construct a query string
|
||||||
|
v := url.Values{}
|
||||||
|
if page <= 0 {
|
||||||
|
page = 1
|
||||||
|
}
|
||||||
|
v.Set("page", strconv.Itoa(page))
|
||||||
|
// Request as many rules as possible per page - API max is 100
|
||||||
|
v.Set("per_page", "100")
|
||||||
|
if accessRule.Notes != "" {
|
||||||
|
v.Set("notes", accessRule.Notes)
|
||||||
|
}
|
||||||
|
if accessRule.Mode != "" {
|
||||||
|
v.Set("mode", accessRule.Mode)
|
||||||
|
}
|
||||||
|
if accessRule.Scope.Type != "" {
|
||||||
|
v.Set("scope_type", accessRule.Scope.Type)
|
||||||
|
}
|
||||||
|
if accessRule.Configuration.Value != "" {
|
||||||
|
v.Set("configuration_value", accessRule.Configuration.Value)
|
||||||
|
}
|
||||||
|
if accessRule.Configuration.Target != "" {
|
||||||
|
v.Set("configuration_target", accessRule.Configuration.Target)
|
||||||
|
}
|
||||||
|
v.Set("page", strconv.Itoa(page))
|
||||||
|
query := "?" + v.Encode()
|
||||||
|
|
||||||
|
uri := prefix + "/firewall/access_rules/rules" + query
|
||||||
|
res, err := api.makeRequest("GET", uri, nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
|
||||||
|
response := &AccessRuleListResponse{}
|
||||||
|
err = json.Unmarshal(res, &response)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
return response, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (api *API) createAccessRule(prefix string, accessRule AccessRule) (*AccessRuleResponse, error) {
|
||||||
|
uri := prefix + "/firewall/access_rules/rules"
|
||||||
|
res, err := api.makeRequest("POST", uri, accessRule)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
|
||||||
|
response := &AccessRuleResponse{}
|
||||||
|
err = json.Unmarshal(res, &response)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
|
||||||
|
return response, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (api *API) updateAccessRule(prefix, accessRuleID string, accessRule AccessRule) (*AccessRuleResponse, error) {
|
||||||
|
uri := prefix + "/firewall/access_rules/rules/" + accessRuleID
|
||||||
|
res, err := api.makeRequest("PATCH", uri, accessRule)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
|
||||||
|
response := &AccessRuleResponse{}
|
||||||
|
err = json.Unmarshal(res, &response)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
return response, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (api *API) deleteAccessRule(prefix, accessRuleID string) (*AccessRuleResponse, error) {
|
||||||
|
uri := prefix + "/firewall/access_rules/rules/" + accessRuleID
|
||||||
|
res, err := api.makeRequest("DELETE", uri, nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
|
||||||
|
response := &AccessRuleResponse{}
|
||||||
|
err = json.Unmarshal(res, &response)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
|
||||||
|
return response, nil
|
||||||
|
}
|
44
vendor/github.com/cloudflare/cloudflare-go/ips.go
generated
vendored
Normal file
44
vendor/github.com/cloudflare/cloudflare-go/ips.go
generated
vendored
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
package cloudflare
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"io/ioutil"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
// IPRanges contains lists of IPv4 and IPv6 CIDRs.
|
||||||
|
type IPRanges struct {
|
||||||
|
IPv4CIDRs []string `json:"ipv4_cidrs"`
|
||||||
|
IPv6CIDRs []string `json:"ipv6_cidrs"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// IPsResponse is the API response containing a list of IPs.
|
||||||
|
type IPsResponse struct {
|
||||||
|
Response
|
||||||
|
Result IPRanges `json:"result"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// IPs gets a list of Cloudflare's IP ranges.
|
||||||
|
//
|
||||||
|
// This does not require logging in to the API.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#cloudflare-ips
|
||||||
|
func IPs() (IPRanges, error) {
|
||||||
|
resp, err := http.Get(apiURL + "/ips")
|
||||||
|
if err != nil {
|
||||||
|
return IPRanges{}, errors.Wrap(err, "HTTP request failed")
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
body, err := ioutil.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
return IPRanges{}, errors.Wrap(err, "Response body could not be read")
|
||||||
|
}
|
||||||
|
var r IPsResponse
|
||||||
|
err = json.Unmarshal(body, &r)
|
||||||
|
if err != nil {
|
||||||
|
return IPRanges{}, errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
return r.Result, nil
|
||||||
|
}
|
52
vendor/github.com/cloudflare/cloudflare-go/keyless.go
generated
vendored
Normal file
52
vendor/github.com/cloudflare/cloudflare-go/keyless.go
generated
vendored
Normal file
|
@ -0,0 +1,52 @@
|
||||||
|
package cloudflare
|
||||||
|
|
||||||
|
import "time"
|
||||||
|
|
||||||
|
// KeylessSSL represents Keyless SSL configuration.
|
||||||
|
type KeylessSSL struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
Host string `json:"host"`
|
||||||
|
Port int `json:"port"`
|
||||||
|
Status string `json:"success"`
|
||||||
|
Enabled bool `json:"enabled"`
|
||||||
|
Permissions []string `json:"permissions"`
|
||||||
|
CreatedOn time.Time `json:"created_on"`
|
||||||
|
ModifiedOn time.Time `json:"modifed_on"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// KeylessSSLResponse represents the response from the Keyless SSL endpoint.
|
||||||
|
type KeylessSSLResponse struct {
|
||||||
|
Response
|
||||||
|
Result []KeylessSSL `json:"result"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreateKeyless creates a new Keyless SSL configuration for the zone.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#keyless-ssl-for-a-zone-create-a-keyless-ssl-configuration
|
||||||
|
func (api *API) CreateKeyless() {
|
||||||
|
}
|
||||||
|
|
||||||
|
// ListKeyless lists Keyless SSL configurations for a zone.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#keyless-ssl-for-a-zone-list-keyless-ssls
|
||||||
|
func (api *API) ListKeyless() {
|
||||||
|
}
|
||||||
|
|
||||||
|
// Keyless provides the configuration for a given Keyless SSL identifier.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#keyless-ssl-for-a-zone-keyless-ssl-details
|
||||||
|
func (api *API) Keyless() {
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateKeyless updates an existing Keyless SSL configuration.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#keyless-ssl-for-a-zone-update-keyless-configuration
|
||||||
|
func (api *API) UpdateKeyless() {
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeleteKeyless deletes an existing Keyless SSL configuration.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#keyless-ssl-for-a-zone-delete-keyless-configuration
|
||||||
|
func (api *API) DeleteKeyless() {
|
||||||
|
}
|
330
vendor/github.com/cloudflare/cloudflare-go/load_balancing.go
generated
vendored
Normal file
330
vendor/github.com/cloudflare/cloudflare-go/load_balancing.go
generated
vendored
Normal file
|
@ -0,0 +1,330 @@
|
||||||
|
package cloudflare
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
// LoadBalancerPool represents a load balancer pool's properties.
|
||||||
|
type LoadBalancerPool struct {
|
||||||
|
ID string `json:"id,omitempty"`
|
||||||
|
CreatedOn *time.Time `json:"created_on,omitempty"`
|
||||||
|
ModifiedOn *time.Time `json:"modified_on,omitempty"`
|
||||||
|
Description string `json:"description"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
Enabled bool `json:"enabled"`
|
||||||
|
MinimumOrigins int `json:"minimum_origins,omitempty"`
|
||||||
|
Monitor string `json:"monitor,omitempty"`
|
||||||
|
Origins []LoadBalancerOrigin `json:"origins"`
|
||||||
|
NotificationEmail string `json:"notification_email,omitempty"`
|
||||||
|
|
||||||
|
// CheckRegions defines the geographic region(s) from where to run health-checks from - e.g. "WNAM", "WEU", "SAF", "SAM".
|
||||||
|
// Providing a null/empty value means "all regions", which may not be available to all plan types.
|
||||||
|
CheckRegions []string `json:"check_regions"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type LoadBalancerOrigin struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
Address string `json:"address"`
|
||||||
|
Enabled bool `json:"enabled"`
|
||||||
|
Weight float64 `json:"weight"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// LoadBalancerMonitor represents a load balancer monitor's properties.
|
||||||
|
type LoadBalancerMonitor struct {
|
||||||
|
ID string `json:"id,omitempty"`
|
||||||
|
CreatedOn *time.Time `json:"created_on,omitempty"`
|
||||||
|
ModifiedOn *time.Time `json:"modified_on,omitempty"`
|
||||||
|
Type string `json:"type"`
|
||||||
|
Description string `json:"description"`
|
||||||
|
Method string `json:"method"`
|
||||||
|
Path string `json:"path"`
|
||||||
|
Header map[string][]string `json:"header"`
|
||||||
|
Timeout int `json:"timeout"`
|
||||||
|
Retries int `json:"retries"`
|
||||||
|
Interval int `json:"interval"`
|
||||||
|
ExpectedBody string `json:"expected_body"`
|
||||||
|
ExpectedCodes string `json:"expected_codes"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// LoadBalancer represents a load balancer's properties.
|
||||||
|
type LoadBalancer struct {
|
||||||
|
ID string `json:"id,omitempty"`
|
||||||
|
CreatedOn *time.Time `json:"created_on,omitempty"`
|
||||||
|
ModifiedOn *time.Time `json:"modified_on,omitempty"`
|
||||||
|
Description string `json:"description"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
TTL int `json:"ttl,omitempty"`
|
||||||
|
FallbackPool string `json:"fallback_pool"`
|
||||||
|
DefaultPools []string `json:"default_pools"`
|
||||||
|
RegionPools map[string][]string `json:"region_pools"`
|
||||||
|
PopPools map[string][]string `json:"pop_pools"`
|
||||||
|
Proxied bool `json:"proxied"`
|
||||||
|
Persistence string `json:"session_affinity,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// loadBalancerPoolResponse represents the response from the load balancer pool endpoints.
|
||||||
|
type loadBalancerPoolResponse struct {
|
||||||
|
Response
|
||||||
|
Result LoadBalancerPool `json:"result"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// loadBalancerPoolListResponse represents the response from the List Pools endpoint.
|
||||||
|
type loadBalancerPoolListResponse struct {
|
||||||
|
Response
|
||||||
|
Result []LoadBalancerPool `json:"result"`
|
||||||
|
ResultInfo ResultInfo `json:"result_info"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// loadBalancerMonitorResponse represents the response from the load balancer monitor endpoints.
|
||||||
|
type loadBalancerMonitorResponse struct {
|
||||||
|
Response
|
||||||
|
Result LoadBalancerMonitor `json:"result"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// loadBalancerMonitorListResponse represents the response from the List Monitors endpoint.
|
||||||
|
type loadBalancerMonitorListResponse struct {
|
||||||
|
Response
|
||||||
|
Result []LoadBalancerMonitor `json:"result"`
|
||||||
|
ResultInfo ResultInfo `json:"result_info"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// loadBalancerResponse represents the response from the load balancer endpoints.
|
||||||
|
type loadBalancerResponse struct {
|
||||||
|
Response
|
||||||
|
Result LoadBalancer `json:"result"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// loadBalancerListResponse represents the response from the List Load Balancers endpoint.
|
||||||
|
type loadBalancerListResponse struct {
|
||||||
|
Response
|
||||||
|
Result []LoadBalancer `json:"result"`
|
||||||
|
ResultInfo ResultInfo `json:"result_info"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreateLoadBalancerPool creates a new load balancer pool.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#load-balancer-pools-create-a-pool
|
||||||
|
func (api *API) CreateLoadBalancerPool(pool LoadBalancerPool) (LoadBalancerPool, error) {
|
||||||
|
uri := api.userBaseURL("/user") + "/load_balancers/pools"
|
||||||
|
res, err := api.makeRequest("POST", uri, pool)
|
||||||
|
if err != nil {
|
||||||
|
return LoadBalancerPool{}, errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
var r loadBalancerPoolResponse
|
||||||
|
if err := json.Unmarshal(res, &r); err != nil {
|
||||||
|
return LoadBalancerPool{}, errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
return r.Result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ListLoadBalancerPools lists load balancer pools connected to an account.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#load-balancer-pools-list-pools
|
||||||
|
func (api *API) ListLoadBalancerPools() ([]LoadBalancerPool, error) {
|
||||||
|
uri := api.userBaseURL("/user") + "/load_balancers/pools"
|
||||||
|
res, err := api.makeRequest("GET", uri, nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
var r loadBalancerPoolListResponse
|
||||||
|
if err := json.Unmarshal(res, &r); err != nil {
|
||||||
|
return nil, errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
return r.Result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// LoadBalancerPoolDetails returns the details for a load balancer pool.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#load-balancer-pools-pool-details
|
||||||
|
func (api *API) LoadBalancerPoolDetails(poolID string) (LoadBalancerPool, error) {
|
||||||
|
uri := api.userBaseURL("/user") + "/load_balancers/pools/" + poolID
|
||||||
|
res, err := api.makeRequest("GET", uri, nil)
|
||||||
|
if err != nil {
|
||||||
|
return LoadBalancerPool{}, errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
var r loadBalancerPoolResponse
|
||||||
|
if err := json.Unmarshal(res, &r); err != nil {
|
||||||
|
return LoadBalancerPool{}, errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
return r.Result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeleteLoadBalancerPool disables and deletes a load balancer pool.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#load-balancer-pools-delete-a-pool
|
||||||
|
func (api *API) DeleteLoadBalancerPool(poolID string) error {
|
||||||
|
uri := api.userBaseURL("/user") + "/load_balancers/pools/" + poolID
|
||||||
|
if _, err := api.makeRequest("DELETE", uri, nil); err != nil {
|
||||||
|
return errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ModifyLoadBalancerPool modifies a configured load balancer pool.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#load-balancer-pools-modify-a-pool
|
||||||
|
func (api *API) ModifyLoadBalancerPool(pool LoadBalancerPool) (LoadBalancerPool, error) {
|
||||||
|
uri := api.userBaseURL("/user") + "/load_balancers/pools/" + pool.ID
|
||||||
|
res, err := api.makeRequest("PUT", uri, pool)
|
||||||
|
if err != nil {
|
||||||
|
return LoadBalancerPool{}, errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
var r loadBalancerPoolResponse
|
||||||
|
if err := json.Unmarshal(res, &r); err != nil {
|
||||||
|
return LoadBalancerPool{}, errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
return r.Result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreateLoadBalancerMonitor creates a new load balancer monitor.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#load-balancer-monitors-create-a-monitor
|
||||||
|
func (api *API) CreateLoadBalancerMonitor(monitor LoadBalancerMonitor) (LoadBalancerMonitor, error) {
|
||||||
|
uri := api.userBaseURL("/user") + "/load_balancers/monitors"
|
||||||
|
res, err := api.makeRequest("POST", uri, monitor)
|
||||||
|
if err != nil {
|
||||||
|
return LoadBalancerMonitor{}, errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
var r loadBalancerMonitorResponse
|
||||||
|
if err := json.Unmarshal(res, &r); err != nil {
|
||||||
|
return LoadBalancerMonitor{}, errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
return r.Result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ListLoadBalancerMonitors lists load balancer monitors connected to an account.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#load-balancer-monitors-list-monitors
|
||||||
|
func (api *API) ListLoadBalancerMonitors() ([]LoadBalancerMonitor, error) {
|
||||||
|
uri := api.userBaseURL("/user") + "/load_balancers/monitors"
|
||||||
|
res, err := api.makeRequest("GET", uri, nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
var r loadBalancerMonitorListResponse
|
||||||
|
if err := json.Unmarshal(res, &r); err != nil {
|
||||||
|
return nil, errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
return r.Result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// LoadBalancerMonitorDetails returns the details for a load balancer monitor.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#load-balancer-monitors-monitor-details
|
||||||
|
func (api *API) LoadBalancerMonitorDetails(monitorID string) (LoadBalancerMonitor, error) {
|
||||||
|
uri := api.userBaseURL("/user") + "/load_balancers/monitors/" + monitorID
|
||||||
|
res, err := api.makeRequest("GET", uri, nil)
|
||||||
|
if err != nil {
|
||||||
|
return LoadBalancerMonitor{}, errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
var r loadBalancerMonitorResponse
|
||||||
|
if err := json.Unmarshal(res, &r); err != nil {
|
||||||
|
return LoadBalancerMonitor{}, errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
return r.Result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeleteLoadBalancerMonitor disables and deletes a load balancer monitor.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#load-balancer-monitors-delete-a-monitor
|
||||||
|
func (api *API) DeleteLoadBalancerMonitor(monitorID string) error {
|
||||||
|
uri := api.userBaseURL("/user") + "/load_balancers/monitors/" + monitorID
|
||||||
|
if _, err := api.makeRequest("DELETE", uri, nil); err != nil {
|
||||||
|
return errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ModifyLoadBalancerMonitor modifies a configured load balancer monitor.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#load-balancer-monitors-modify-a-monitor
|
||||||
|
func (api *API) ModifyLoadBalancerMonitor(monitor LoadBalancerMonitor) (LoadBalancerMonitor, error) {
|
||||||
|
uri := api.userBaseURL("/user") + "/load_balancers/monitors/" + monitor.ID
|
||||||
|
res, err := api.makeRequest("PUT", uri, monitor)
|
||||||
|
if err != nil {
|
||||||
|
return LoadBalancerMonitor{}, errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
var r loadBalancerMonitorResponse
|
||||||
|
if err := json.Unmarshal(res, &r); err != nil {
|
||||||
|
return LoadBalancerMonitor{}, errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
return r.Result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreateLoadBalancer creates a new load balancer.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#load-balancers-create-a-load-balancer
|
||||||
|
func (api *API) CreateLoadBalancer(zoneID string, lb LoadBalancer) (LoadBalancer, error) {
|
||||||
|
uri := "/zones/" + zoneID + "/load_balancers"
|
||||||
|
res, err := api.makeRequest("POST", uri, lb)
|
||||||
|
if err != nil {
|
||||||
|
return LoadBalancer{}, errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
var r loadBalancerResponse
|
||||||
|
if err := json.Unmarshal(res, &r); err != nil {
|
||||||
|
return LoadBalancer{}, errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
return r.Result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ListLoadBalancers lists load balancers configured on a zone.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#load-balancers-list-load-balancers
|
||||||
|
func (api *API) ListLoadBalancers(zoneID string) ([]LoadBalancer, error) {
|
||||||
|
uri := "/zones/" + zoneID + "/load_balancers"
|
||||||
|
res, err := api.makeRequest("GET", uri, nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
var r loadBalancerListResponse
|
||||||
|
if err := json.Unmarshal(res, &r); err != nil {
|
||||||
|
return nil, errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
return r.Result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// LoadBalancerDetails returns the details for a load balancer.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#load-balancers-load-balancer-details
|
||||||
|
func (api *API) LoadBalancerDetails(zoneID, lbID string) (LoadBalancer, error) {
|
||||||
|
uri := "/zones/" + zoneID + "/load_balancers/" + lbID
|
||||||
|
res, err := api.makeRequest("GET", uri, nil)
|
||||||
|
if err != nil {
|
||||||
|
return LoadBalancer{}, errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
var r loadBalancerResponse
|
||||||
|
if err := json.Unmarshal(res, &r); err != nil {
|
||||||
|
return LoadBalancer{}, errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
return r.Result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeleteLoadBalancer disables and deletes a load balancer.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#load-balancers-delete-a-load-balancer
|
||||||
|
func (api *API) DeleteLoadBalancer(zoneID, lbID string) error {
|
||||||
|
uri := "/zones/" + zoneID + "/load_balancers/" + lbID
|
||||||
|
if _, err := api.makeRequest("DELETE", uri, nil); err != nil {
|
||||||
|
return errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ModifyLoadBalancer modifies a configured load balancer.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#load-balancers-modify-a-load-balancer
|
||||||
|
func (api *API) ModifyLoadBalancer(zoneID string, lb LoadBalancer) (LoadBalancer, error) {
|
||||||
|
uri := "/zones/" + zoneID + "/load_balancers/" + lb.ID
|
||||||
|
res, err := api.makeRequest("PUT", uri, lb)
|
||||||
|
if err != nil {
|
||||||
|
return LoadBalancer{}, errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
var r loadBalancerResponse
|
||||||
|
if err := json.Unmarshal(res, &r); err != nil {
|
||||||
|
return LoadBalancer{}, errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
return r.Result, nil
|
||||||
|
}
|
150
vendor/github.com/cloudflare/cloudflare-go/lockdown.go
generated
vendored
Normal file
150
vendor/github.com/cloudflare/cloudflare-go/lockdown.go
generated
vendored
Normal file
|
@ -0,0 +1,150 @@
|
||||||
|
package cloudflare
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"net/url"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ZoneLockdown represents a Zone Lockdown rule. A rule only permits access to
|
||||||
|
// the provided URL pattern(s) from the given IP address(es) or subnet(s).
|
||||||
|
type ZoneLockdown struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
Description string `json:"description"`
|
||||||
|
URLs []string `json:"urls"`
|
||||||
|
Configurations []ZoneLockdownConfig `json:"configurations"`
|
||||||
|
Paused bool `json:"paused"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// ZoneLockdownConfig represents a Zone Lockdown config, which comprises
|
||||||
|
// a Target ("ip" or "ip_range") and a Value (an IP address or IP+mask,
|
||||||
|
// respectively.)
|
||||||
|
type ZoneLockdownConfig struct {
|
||||||
|
Target string `json:"target"`
|
||||||
|
Value string `json:"value"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// ZoneLockdownResponse represents a response from the Zone Lockdown endpoint.
|
||||||
|
type ZoneLockdownResponse struct {
|
||||||
|
Result ZoneLockdown `json:"result"`
|
||||||
|
Response
|
||||||
|
ResultInfo `json:"result_info"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// ZoneLockdownListResponse represents a response from the List Zone Lockdown
|
||||||
|
// endpoint.
|
||||||
|
type ZoneLockdownListResponse struct {
|
||||||
|
Result []ZoneLockdown `json:"result"`
|
||||||
|
Response
|
||||||
|
ResultInfo `json:"result_info"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreateZoneLockdown creates a Zone ZoneLockdown rule for the given zone ID.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#zone-ZoneLockdown-create-a-ZoneLockdown-rule
|
||||||
|
func (api *API) CreateZoneLockdown(zoneID string, ld ZoneLockdown) (*ZoneLockdownResponse, error) {
|
||||||
|
uri := "/zones/" + zoneID + "/firewall/lockdowns"
|
||||||
|
res, err := api.makeRequest("POST", uri, ld)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
|
||||||
|
response := &ZoneLockdownResponse{}
|
||||||
|
err = json.Unmarshal(res, &response)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
|
||||||
|
return response, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateZoneLockdown updates a Zone ZoneLockdown rule (based on the ID) for the
|
||||||
|
// given zone ID.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#zone-ZoneLockdown-update-ZoneLockdown-rule
|
||||||
|
func (api *API) UpdateZoneLockdown(zoneID string, id string, ld ZoneLockdown) (*ZoneLockdownResponse, error) {
|
||||||
|
uri := "/zones/" + zoneID + "/firewall/lockdowns"
|
||||||
|
res, err := api.makeRequest("PUT", uri, ld)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
|
||||||
|
response := &ZoneLockdownResponse{}
|
||||||
|
err = json.Unmarshal(res, &response)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
|
||||||
|
return response, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeleteZoneLockdown deletes a Zone ZoneLockdown rule (based on the ID) for the
|
||||||
|
// given zone ID.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#zone-ZoneLockdown-delete-ZoneLockdown-rule
|
||||||
|
func (api *API) DeleteZoneLockdown(zoneID string, id string) (*ZoneLockdownResponse, error) {
|
||||||
|
uri := "/zones/" + zoneID + "/firewall/lockdowns/" + id
|
||||||
|
res, err := api.makeRequest("DELETE", uri, nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
|
||||||
|
response := &ZoneLockdownResponse{}
|
||||||
|
err = json.Unmarshal(res, &response)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
|
||||||
|
return response, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ZoneLockdown retrieves a Zone ZoneLockdown rule (based on the ID) for the
|
||||||
|
// given zone ID.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#zone-ZoneLockdown-ZoneLockdown-rule-details
|
||||||
|
func (api *API) ZoneLockdown(zoneID string, id string) (*ZoneLockdownResponse, error) {
|
||||||
|
uri := "/zones/" + zoneID + "/firewall/lockdowns/" + id
|
||||||
|
res, err := api.makeRequest("GET", uri, nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
|
||||||
|
response := &ZoneLockdownResponse{}
|
||||||
|
err = json.Unmarshal(res, &response)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
|
||||||
|
return response, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ListZoneLockdowns retrieves a list of Zone ZoneLockdown rules for a given
|
||||||
|
// zone ID by page number.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#zone-ZoneLockdown-list-ZoneLockdown-rules
|
||||||
|
func (api *API) ListZoneLockdowns(zoneID string, page int) (*ZoneLockdownListResponse, error) {
|
||||||
|
v := url.Values{}
|
||||||
|
if page <= 0 {
|
||||||
|
page = 1
|
||||||
|
}
|
||||||
|
|
||||||
|
v.Set("page", strconv.Itoa(page))
|
||||||
|
v.Set("per_page", strconv.Itoa(100))
|
||||||
|
query := "?" + v.Encode()
|
||||||
|
|
||||||
|
uri := "/zones/" + zoneID + "/firewall/lockdowns" + query
|
||||||
|
res, err := api.makeRequest("GET", uri, nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
|
||||||
|
response := &ZoneLockdownListResponse{}
|
||||||
|
err = json.Unmarshal(res, &response)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
|
||||||
|
return response, nil
|
||||||
|
}
|
90
vendor/github.com/cloudflare/cloudflare-go/options.go
generated
vendored
Normal file
90
vendor/github.com/cloudflare/cloudflare-go/options.go
generated
vendored
Normal file
|
@ -0,0 +1,90 @@
|
||||||
|
package cloudflare
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"golang.org/x/time/rate"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Option is a functional option for configuring the API client.
|
||||||
|
type Option func(*API) error
|
||||||
|
|
||||||
|
// HTTPClient accepts a custom *http.Client for making API calls.
|
||||||
|
func HTTPClient(client *http.Client) Option {
|
||||||
|
return func(api *API) error {
|
||||||
|
api.httpClient = client
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Headers allows you to set custom HTTP headers when making API calls (e.g. for
|
||||||
|
// satisfying HTTP proxies, or for debugging).
|
||||||
|
func Headers(headers http.Header) Option {
|
||||||
|
return func(api *API) error {
|
||||||
|
api.headers = headers
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Organization allows you to apply account-level changes (Load Balancing, Railguns)
|
||||||
|
// to an organization instead.
|
||||||
|
func UsingOrganization(orgID string) Option {
|
||||||
|
return func(api *API) error {
|
||||||
|
api.organizationID = orgID
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// UsingRateLimit applies a non-default rate limit to client API requests
|
||||||
|
// If not specified the default of 4rps will be applied
|
||||||
|
func UsingRateLimit(rps float64) Option {
|
||||||
|
return func(api *API) error {
|
||||||
|
// because ratelimiter doesnt do any windowing
|
||||||
|
// setting burst makes it difficult to enforce a fixed rate
|
||||||
|
// so setting it equal to 1 this effectively disables bursting
|
||||||
|
// this doesn't check for sensible values, ultimately the api will enforce that the value is ok
|
||||||
|
api.rateLimiter = rate.NewLimiter(rate.Limit(rps), 1)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// UsingRetryPolicy applies a non-default number of retries and min/max retry delays
|
||||||
|
// This will be used when the client exponentially backs off after errored requests
|
||||||
|
func UsingRetryPolicy(maxRetries int, minRetryDelaySecs int, maxRetryDelaySecs int) Option {
|
||||||
|
// seconds is very granular for a minimum delay - but this is only in case of failure
|
||||||
|
return func(api *API) error {
|
||||||
|
api.retryPolicy = RetryPolicy{
|
||||||
|
MaxRetries: maxRetries,
|
||||||
|
MinRetryDelay: time.Duration(minRetryDelaySecs) * time.Second,
|
||||||
|
MaxRetryDelay: time.Duration(maxRetryDelaySecs) * time.Second,
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// UsingLogger can be set if you want to get log output from this API instance
|
||||||
|
// By default no log output is emitted
|
||||||
|
func UsingLogger(logger Logger) Option {
|
||||||
|
return func(api *API) error {
|
||||||
|
api.logger = logger
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// parseOptions parses the supplied options functions and returns a configured
|
||||||
|
// *API instance.
|
||||||
|
func (api *API) parseOptions(opts ...Option) error {
|
||||||
|
// Range over each options function and apply it to our API type to
|
||||||
|
// configure it. Options functions are applied in order, with any
|
||||||
|
// conflicting options overriding earlier calls.
|
||||||
|
for _, option := range opts {
|
||||||
|
err := option(api)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
185
vendor/github.com/cloudflare/cloudflare-go/organizations.go
generated
vendored
Normal file
185
vendor/github.com/cloudflare/cloudflare-go/organizations.go
generated
vendored
Normal file
|
@ -0,0 +1,185 @@
|
||||||
|
package cloudflare
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Organization represents a multi-user organization.
|
||||||
|
type Organization struct {
|
||||||
|
ID string `json:"id,omitempty"`
|
||||||
|
Name string `json:"name,omitempty"`
|
||||||
|
Status string `json:"status,omitempty"`
|
||||||
|
Permissions []string `json:"permissions,omitempty"`
|
||||||
|
Roles []string `json:"roles,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// organizationResponse represents the response from the Organization endpoint.
|
||||||
|
type organizationResponse struct {
|
||||||
|
Response
|
||||||
|
Result []Organization `json:"result"`
|
||||||
|
ResultInfo `json:"result_info"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// OrganizationMember has details on a member.
|
||||||
|
type OrganizationMember struct {
|
||||||
|
ID string `json:"id,omitempty"`
|
||||||
|
Name string `json:"name,omitempty"`
|
||||||
|
Email string `json:"email,omitempty"`
|
||||||
|
Status string `json:"status,omitempty"`
|
||||||
|
Roles []OrganizationRole `json:"roles,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// OrganizationInvite has details on an invite.
|
||||||
|
type OrganizationInvite struct {
|
||||||
|
ID string `json:"id,omitempty"`
|
||||||
|
InvitedMemberID string `json:"invited_member_id,omitempty"`
|
||||||
|
InvitedMemberEmail string `json:"invited_member_email,omitempty"`
|
||||||
|
OrganizationID string `json:"organization_id,omitempty"`
|
||||||
|
OrganizationName string `json:"organization_name,omitempty"`
|
||||||
|
Roles []OrganizationRole `json:"roles,omitempty"`
|
||||||
|
InvitedBy string `json:"invited_by,omitempty"`
|
||||||
|
InvitedOn *time.Time `json:"invited_on,omitempty"`
|
||||||
|
ExpiresOn *time.Time `json:"expires_on,omitempty"`
|
||||||
|
Status string `json:"status,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// OrganizationRole has details on a role.
|
||||||
|
type OrganizationRole struct {
|
||||||
|
ID string `json:"id,omitempty"`
|
||||||
|
Name string `json:"name,omitempty"`
|
||||||
|
Description string `json:"description,omitempty"`
|
||||||
|
Permissions []string `json:"permissions,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// OrganizationDetails represents details of an organization.
|
||||||
|
type OrganizationDetails struct {
|
||||||
|
ID string `json:"id,omitempty"`
|
||||||
|
Name string `json:"name,omitempty"`
|
||||||
|
Members []OrganizationMember `json:"members"`
|
||||||
|
Invites []OrganizationInvite `json:"invites"`
|
||||||
|
Roles []OrganizationRole `json:"roles,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// organizationDetailsResponse represents the response from the OrganizationDetails endpoint.
|
||||||
|
type organizationDetailsResponse struct {
|
||||||
|
Response
|
||||||
|
Result OrganizationDetails `json:"result"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// ListOrganizations lists organizations of the logged-in user.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#user-s-organizations-list-organizations
|
||||||
|
func (api *API) ListOrganizations() ([]Organization, ResultInfo, error) {
|
||||||
|
var r organizationResponse
|
||||||
|
res, err := api.makeRequest("GET", "/user/organizations", nil)
|
||||||
|
if err != nil {
|
||||||
|
return []Organization{}, ResultInfo{}, errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = json.Unmarshal(res, &r)
|
||||||
|
if err != nil {
|
||||||
|
return []Organization{}, ResultInfo{}, errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
|
||||||
|
return r.Result, r.ResultInfo, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// OrganizationDetails returns details for the specified organization of the logged-in user.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#organizations-organization-details
|
||||||
|
func (api *API) OrganizationDetails(organizationID string) (OrganizationDetails, error) {
|
||||||
|
var r organizationDetailsResponse
|
||||||
|
uri := "/organizations/" + organizationID
|
||||||
|
res, err := api.makeRequest("GET", uri, nil)
|
||||||
|
if err != nil {
|
||||||
|
return OrganizationDetails{}, errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = json.Unmarshal(res, &r)
|
||||||
|
if err != nil {
|
||||||
|
return OrganizationDetails{}, errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
|
||||||
|
return r.Result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// organizationMembersResponse represents the response from the Organization members endpoint.
|
||||||
|
type organizationMembersResponse struct {
|
||||||
|
Response
|
||||||
|
Result []OrganizationMember `json:"result"`
|
||||||
|
ResultInfo `json:"result_info"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// OrganizationMembers returns list of members for specified organization of the logged-in user.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#organization-members-list-members
|
||||||
|
func (api *API) OrganizationMembers(organizationID string) ([]OrganizationMember, ResultInfo, error) {
|
||||||
|
var r organizationMembersResponse
|
||||||
|
uri := "/organizations/" + organizationID + "/members"
|
||||||
|
res, err := api.makeRequest("GET", uri, nil)
|
||||||
|
if err != nil {
|
||||||
|
return []OrganizationMember{}, ResultInfo{}, errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = json.Unmarshal(res, &r)
|
||||||
|
if err != nil {
|
||||||
|
return []OrganizationMember{}, ResultInfo{}, errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
|
||||||
|
return r.Result, r.ResultInfo, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// organizationInvitesResponse represents the response from the Organization invites endpoint.
|
||||||
|
type organizationInvitesResponse struct {
|
||||||
|
Response
|
||||||
|
Result []OrganizationInvite `json:"result"`
|
||||||
|
ResultInfo `json:"result_info"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// OrganizationMembers returns list of invites for specified organization of the logged-in user.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#organization-invites
|
||||||
|
func (api *API) OrganizationInvites(organizationID string) ([]OrganizationInvite, ResultInfo, error) {
|
||||||
|
var r organizationInvitesResponse
|
||||||
|
uri := "/organizations/" + organizationID + "/invites"
|
||||||
|
res, err := api.makeRequest("GET", uri, nil)
|
||||||
|
if err != nil {
|
||||||
|
return []OrganizationInvite{}, ResultInfo{}, errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = json.Unmarshal(res, &r)
|
||||||
|
if err != nil {
|
||||||
|
return []OrganizationInvite{}, ResultInfo{}, errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
|
||||||
|
return r.Result, r.ResultInfo, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// organizationRolesResponse represents the response from the Organization roles endpoint.
|
||||||
|
type organizationRolesResponse struct {
|
||||||
|
Response
|
||||||
|
Result []OrganizationRole `json:"result"`
|
||||||
|
ResultInfo `json:"result_info"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// OrganizationRoles returns list of roles for specified organization of the logged-in user.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#organization-roles-list-roles
|
||||||
|
func (api *API) OrganizationRoles(organizationID string) ([]OrganizationRole, ResultInfo, error) {
|
||||||
|
var r organizationRolesResponse
|
||||||
|
uri := "/organizations/" + organizationID + "/roles"
|
||||||
|
res, err := api.makeRequest("GET", uri, nil)
|
||||||
|
if err != nil {
|
||||||
|
return []OrganizationRole{}, ResultInfo{}, errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = json.Unmarshal(res, &r)
|
||||||
|
if err != nil {
|
||||||
|
return []OrganizationRole{}, ResultInfo{}, errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
|
||||||
|
return r.Result, r.ResultInfo, nil
|
||||||
|
}
|
168
vendor/github.com/cloudflare/cloudflare-go/origin_ca.go
generated
vendored
Normal file
168
vendor/github.com/cloudflare/cloudflare-go/origin_ca.go
generated
vendored
Normal file
|
@ -0,0 +1,168 @@
|
||||||
|
package cloudflare
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"net/url"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
// OriginCACertificate represents a Cloudflare-issued certificate.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#cloudflare-ca
|
||||||
|
type OriginCACertificate struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
Certificate string `json:"certificate"`
|
||||||
|
Hostnames []string `json:"hostnames"`
|
||||||
|
ExpiresOn time.Time `json:"expires_on"`
|
||||||
|
RequestType string `json:"request_type"`
|
||||||
|
RequestValidity int `json:"requested_validity"`
|
||||||
|
CSR string `json:"csr"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// OriginCACertificateListOptions represents the parameters used to list Cloudflare-issued certificates.
|
||||||
|
type OriginCACertificateListOptions struct {
|
||||||
|
ZoneID string
|
||||||
|
}
|
||||||
|
|
||||||
|
// OriginCACertificateID represents the ID of the revoked certificate from the Revoke Certificate endpoint.
|
||||||
|
type OriginCACertificateID struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// originCACertificateResponse represents the response from the Create Certificate and the Certificate Details endpoints.
|
||||||
|
type originCACertificateResponse struct {
|
||||||
|
Response
|
||||||
|
Result OriginCACertificate `json:"result"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// originCACertificateResponseList represents the response from the List Certificates endpoint.
|
||||||
|
type originCACertificateResponseList struct {
|
||||||
|
Response
|
||||||
|
Result []OriginCACertificate `json:"result"`
|
||||||
|
ResultInfo ResultInfo `json:"result_info"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// originCACertificateResponseRevoke represents the response from the Revoke Certificate endpoint.
|
||||||
|
type originCACertificateResponseRevoke struct {
|
||||||
|
Response
|
||||||
|
Result OriginCACertificateID `json:"result"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreateOriginCertificate creates a Cloudflare-signed certificate.
|
||||||
|
//
|
||||||
|
// This function requires api.APIUserServiceKey be set to your Certificates API key.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#cloudflare-ca-create-certificate
|
||||||
|
func (api *API) CreateOriginCertificate(certificate OriginCACertificate) (*OriginCACertificate, error) {
|
||||||
|
uri := "/certificates"
|
||||||
|
res, err := api.makeRequestWithAuthType("POST", uri, certificate, AuthUserService)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
|
||||||
|
var originResponse *originCACertificateResponse
|
||||||
|
|
||||||
|
err = json.Unmarshal(res, &originResponse)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !originResponse.Success {
|
||||||
|
return nil, errors.New(errRequestNotSuccessful)
|
||||||
|
}
|
||||||
|
|
||||||
|
return &originResponse.Result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// OriginCertificates lists all Cloudflare-issued certificates.
|
||||||
|
//
|
||||||
|
// This function requires api.APIUserServiceKey be set to your Certificates API key.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#cloudflare-ca-list-certificates
|
||||||
|
func (api *API) OriginCertificates(options OriginCACertificateListOptions) ([]OriginCACertificate, error) {
|
||||||
|
v := url.Values{}
|
||||||
|
if options.ZoneID != "" {
|
||||||
|
v.Set("zone_id", options.ZoneID)
|
||||||
|
}
|
||||||
|
uri := "/certificates" + "?" + v.Encode()
|
||||||
|
res, err := api.makeRequestWithAuthType("GET", uri, nil, AuthUserService)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
|
||||||
|
var originResponse *originCACertificateResponseList
|
||||||
|
|
||||||
|
err = json.Unmarshal(res, &originResponse)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !originResponse.Success {
|
||||||
|
return nil, errors.New(errRequestNotSuccessful)
|
||||||
|
}
|
||||||
|
|
||||||
|
return originResponse.Result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// OriginCertificate returns the details for a Cloudflare-issued certificate.
|
||||||
|
//
|
||||||
|
// This function requires api.APIUserServiceKey be set to your Certificates API key.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#cloudflare-ca-certificate-details
|
||||||
|
func (api *API) OriginCertificate(certificateID string) (*OriginCACertificate, error) {
|
||||||
|
uri := "/certificates/" + certificateID
|
||||||
|
res, err := api.makeRequestWithAuthType("GET", uri, nil, AuthUserService)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
|
||||||
|
var originResponse *originCACertificateResponse
|
||||||
|
|
||||||
|
err = json.Unmarshal(res, &originResponse)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !originResponse.Success {
|
||||||
|
return nil, errors.New(errRequestNotSuccessful)
|
||||||
|
}
|
||||||
|
|
||||||
|
return &originResponse.Result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// RevokeOriginCertificate revokes a created certificate for a zone.
|
||||||
|
//
|
||||||
|
// This function requires api.APIUserServiceKey be set to your Certificates API key.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#cloudflare-ca-revoke-certificate
|
||||||
|
func (api *API) RevokeOriginCertificate(certificateID string) (*OriginCACertificateID, error) {
|
||||||
|
uri := "/certificates/" + certificateID
|
||||||
|
res, err := api.makeRequestWithAuthType("DELETE", uri, nil, AuthUserService)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
|
||||||
|
var originResponse *originCACertificateResponseRevoke
|
||||||
|
|
||||||
|
err = json.Unmarshal(res, &originResponse)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !originResponse.Success {
|
||||||
|
return nil, errors.New(errRequestNotSuccessful)
|
||||||
|
}
|
||||||
|
|
||||||
|
return &originResponse.Result, nil
|
||||||
|
|
||||||
|
}
|
206
vendor/github.com/cloudflare/cloudflare-go/page_rules.go
generated
vendored
Normal file
206
vendor/github.com/cloudflare/cloudflare-go/page_rules.go
generated
vendored
Normal file
|
@ -0,0 +1,206 @@
|
||||||
|
package cloudflare
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
// PageRuleTarget is the target to evaluate on a request.
|
||||||
|
//
|
||||||
|
// Currently Target must always be "url" and Operator must be "matches". Value
|
||||||
|
// is the URL pattern to match against.
|
||||||
|
type PageRuleTarget struct {
|
||||||
|
Target string `json:"target"`
|
||||||
|
Constraint struct {
|
||||||
|
Operator string `json:"operator"`
|
||||||
|
Value string `json:"value"`
|
||||||
|
} `json:"constraint"`
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
PageRuleAction is the action to take when the target is matched.
|
||||||
|
|
||||||
|
Valid IDs are:
|
||||||
|
|
||||||
|
always_online
|
||||||
|
always_use_https
|
||||||
|
browser_cache_ttl
|
||||||
|
browser_check
|
||||||
|
cache_level
|
||||||
|
disable_apps
|
||||||
|
disable_performance
|
||||||
|
disable_railgun
|
||||||
|
disable_security
|
||||||
|
edge_cache_ttl
|
||||||
|
email_obfuscation
|
||||||
|
forwarding_url
|
||||||
|
ip_geolocation
|
||||||
|
mirage
|
||||||
|
rocket_loader
|
||||||
|
security_level
|
||||||
|
server_side_exclude
|
||||||
|
smart_errors
|
||||||
|
ssl
|
||||||
|
waf
|
||||||
|
*/
|
||||||
|
type PageRuleAction struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
Value interface{} `json:"value"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// PageRuleActions maps API action IDs to human-readable strings.
|
||||||
|
var PageRuleActions = map[string]string{
|
||||||
|
"always_online": "Always Online", // Value of type string
|
||||||
|
"always_use_https": "Always Use HTTPS", // Value of type interface{}
|
||||||
|
"browser_cache_ttl": "Browser Cache TTL", // Value of type int
|
||||||
|
"browser_check": "Browser Integrity Check", // Value of type string
|
||||||
|
"cache_level": "Cache Level", // Value of type string
|
||||||
|
"disable_apps": "Disable Apps", // Value of type interface{}
|
||||||
|
"disable_performance": "Disable Performance", // Value of type interface{}
|
||||||
|
"disable_railgun": "Disable Railgun", // Value of type string
|
||||||
|
"disable_security": "Disable Security", // Value of type interface{}
|
||||||
|
"edge_cache_ttl": "Edge Cache TTL", // Value of type int
|
||||||
|
"email_obfuscation": "Email Obfuscation", // Value of type string
|
||||||
|
"forwarding_url": "Forwarding URL", // Value of type map[string]interface
|
||||||
|
"ip_geolocation": "IP Geolocation Header", // Value of type string
|
||||||
|
"mirage": "Mirage", // Value of type string
|
||||||
|
"rocket_loader": "Rocker Loader", // Value of type string
|
||||||
|
"security_level": "Security Level", // Value of type string
|
||||||
|
"server_side_exclude": "Server Side Excludes", // Value of type string
|
||||||
|
"smart_errors": "Smart Errors", // Value of type string
|
||||||
|
"ssl": "SSL", // Value of type string
|
||||||
|
"waf": "Web Application Firewall", // Value of type string
|
||||||
|
}
|
||||||
|
|
||||||
|
// PageRule describes a Page Rule.
|
||||||
|
type PageRule struct {
|
||||||
|
ID string `json:"id,omitempty"`
|
||||||
|
Targets []PageRuleTarget `json:"targets"`
|
||||||
|
Actions []PageRuleAction `json:"actions"`
|
||||||
|
Priority int `json:"priority"`
|
||||||
|
Status string `json:"status"` // can be: active, paused
|
||||||
|
ModifiedOn time.Time `json:"modified_on,omitempty"`
|
||||||
|
CreatedOn time.Time `json:"created_on,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// PageRuleDetailResponse is the API response, containing a single PageRule.
|
||||||
|
type PageRuleDetailResponse struct {
|
||||||
|
Success bool `json:"success"`
|
||||||
|
Errors []string `json:"errors"`
|
||||||
|
Messages []string `json:"messages"`
|
||||||
|
Result PageRule `json:"result"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// PageRulesResponse is the API response, containing an array of PageRules.
|
||||||
|
type PageRulesResponse struct {
|
||||||
|
Success bool `json:"success"`
|
||||||
|
Errors []string `json:"errors"`
|
||||||
|
Messages []string `json:"messages"`
|
||||||
|
Result []PageRule `json:"result"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreatePageRule creates a new Page Rule for a zone.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#page-rules-for-a-zone-create-a-page-rule
|
||||||
|
func (api *API) CreatePageRule(zoneID string, rule PageRule) (*PageRule, error) {
|
||||||
|
uri := "/zones/" + zoneID + "/pagerules"
|
||||||
|
res, err := api.makeRequest("POST", uri, rule)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
var r PageRuleDetailResponse
|
||||||
|
err = json.Unmarshal(res, &r)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
return &r.Result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ListPageRules returns all Page Rules for a zone.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#page-rules-for-a-zone-list-page-rules
|
||||||
|
func (api *API) ListPageRules(zoneID string) ([]PageRule, error) {
|
||||||
|
uri := "/zones/" + zoneID + "/pagerules"
|
||||||
|
res, err := api.makeRequest("GET", uri, nil)
|
||||||
|
if err != nil {
|
||||||
|
return []PageRule{}, errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
var r PageRulesResponse
|
||||||
|
err = json.Unmarshal(res, &r)
|
||||||
|
if err != nil {
|
||||||
|
return []PageRule{}, errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
return r.Result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// PageRule fetches detail about one Page Rule for a zone.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#page-rules-for-a-zone-page-rule-details
|
||||||
|
func (api *API) PageRule(zoneID, ruleID string) (PageRule, error) {
|
||||||
|
uri := "/zones/" + zoneID + "/pagerules/" + ruleID
|
||||||
|
res, err := api.makeRequest("GET", uri, nil)
|
||||||
|
if err != nil {
|
||||||
|
return PageRule{}, errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
var r PageRuleDetailResponse
|
||||||
|
err = json.Unmarshal(res, &r)
|
||||||
|
if err != nil {
|
||||||
|
return PageRule{}, errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
return r.Result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ChangePageRule lets you change individual settings for a Page Rule. This is
|
||||||
|
// in contrast to UpdatePageRule which replaces the entire Page Rule.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#page-rules-for-a-zone-change-a-page-rule
|
||||||
|
func (api *API) ChangePageRule(zoneID, ruleID string, rule PageRule) error {
|
||||||
|
uri := "/zones/" + zoneID + "/pagerules/" + ruleID
|
||||||
|
res, err := api.makeRequest("PATCH", uri, rule)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
var r PageRuleDetailResponse
|
||||||
|
err = json.Unmarshal(res, &r)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdatePageRule lets you replace a Page Rule. This is in contrast to
|
||||||
|
// ChangePageRule which lets you change individual settings.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#page-rules-for-a-zone-update-a-page-rule
|
||||||
|
func (api *API) UpdatePageRule(zoneID, ruleID string, rule PageRule) error {
|
||||||
|
uri := "/zones/" + zoneID + "/pagerules/" + ruleID
|
||||||
|
res, err := api.makeRequest("PUT", uri, nil)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
var r PageRuleDetailResponse
|
||||||
|
err = json.Unmarshal(res, &r)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeletePageRule deletes a Page Rule for a zone.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#page-rules-for-a-zone-delete-a-page-rule
|
||||||
|
func (api *API) DeletePageRule(zoneID, ruleID string) error {
|
||||||
|
uri := "/zones/" + zoneID + "/pagerules/" + ruleID
|
||||||
|
res, err := api.makeRequest("DELETE", uri, nil)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
var r PageRuleDetailResponse
|
||||||
|
err = json.Unmarshal(res, &r)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
297
vendor/github.com/cloudflare/cloudflare-go/railgun.go
generated
vendored
Normal file
297
vendor/github.com/cloudflare/cloudflare-go/railgun.go
generated
vendored
Normal file
|
@ -0,0 +1,297 @@
|
||||||
|
package cloudflare
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"net/url"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Railgun represents a Railgun's properties.
|
||||||
|
type Railgun struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
Status string `json:"status"`
|
||||||
|
Enabled bool `json:"enabled"`
|
||||||
|
ZonesConnected int `json:"zones_connected"`
|
||||||
|
Build string `json:"build"`
|
||||||
|
Version string `json:"version"`
|
||||||
|
Revision string `json:"revision"`
|
||||||
|
ActivationKey string `json:"activation_key"`
|
||||||
|
ActivatedOn time.Time `json:"activated_on"`
|
||||||
|
CreatedOn time.Time `json:"created_on"`
|
||||||
|
ModifiedOn time.Time `json:"modified_on"`
|
||||||
|
UpgradeInfo struct {
|
||||||
|
LatestVersion string `json:"latest_version"`
|
||||||
|
DownloadLink string `json:"download_link"`
|
||||||
|
} `json:"upgrade_info"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// RailgunListOptions represents the parameters used to list railguns.
|
||||||
|
type RailgunListOptions struct {
|
||||||
|
Direction string
|
||||||
|
}
|
||||||
|
|
||||||
|
// railgunResponse represents the response from the Create Railgun and the Railgun Details endpoints.
|
||||||
|
type railgunResponse struct {
|
||||||
|
Response
|
||||||
|
Result Railgun `json:"result"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// railgunsResponse represents the response from the List Railguns endpoint.
|
||||||
|
type railgunsResponse struct {
|
||||||
|
Response
|
||||||
|
Result []Railgun `json:"result"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreateRailgun creates a new Railgun.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#railgun-create-railgun
|
||||||
|
func (api *API) CreateRailgun(name string) (Railgun, error) {
|
||||||
|
uri := api.userBaseURL("") + "/railguns"
|
||||||
|
params := struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
}{
|
||||||
|
Name: name,
|
||||||
|
}
|
||||||
|
res, err := api.makeRequest("POST", uri, params)
|
||||||
|
if err != nil {
|
||||||
|
return Railgun{}, errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
var r railgunResponse
|
||||||
|
if err := json.Unmarshal(res, &r); err != nil {
|
||||||
|
return Railgun{}, errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
return r.Result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ListRailguns lists Railguns connected to an account.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#railgun-list-railguns
|
||||||
|
func (api *API) ListRailguns(options RailgunListOptions) ([]Railgun, error) {
|
||||||
|
v := url.Values{}
|
||||||
|
if options.Direction != "" {
|
||||||
|
v.Set("direction", options.Direction)
|
||||||
|
}
|
||||||
|
uri := api.userBaseURL("") + "/railguns" + "?" + v.Encode()
|
||||||
|
res, err := api.makeRequest("GET", uri, nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
var r railgunsResponse
|
||||||
|
if err := json.Unmarshal(res, &r); err != nil {
|
||||||
|
return nil, errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
return r.Result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// RailgunDetails returns the details for a Railgun.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#railgun-railgun-details
|
||||||
|
func (api *API) RailgunDetails(railgunID string) (Railgun, error) {
|
||||||
|
uri := api.userBaseURL("") + "/railguns/" + railgunID
|
||||||
|
res, err := api.makeRequest("GET", uri, nil)
|
||||||
|
if err != nil {
|
||||||
|
return Railgun{}, errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
var r railgunResponse
|
||||||
|
if err := json.Unmarshal(res, &r); err != nil {
|
||||||
|
return Railgun{}, errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
return r.Result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// RailgunZones returns the zones that are currently using a Railgun.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#railgun-get-zones-connected-to-a-railgun
|
||||||
|
func (api *API) RailgunZones(railgunID string) ([]Zone, error) {
|
||||||
|
uri := api.userBaseURL("") + "/railguns/" + railgunID + "/zones"
|
||||||
|
res, err := api.makeRequest("GET", uri, nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
var r ZonesResponse
|
||||||
|
if err := json.Unmarshal(res, &r); err != nil {
|
||||||
|
return nil, errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
return r.Result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// enableRailgun enables (true) or disables (false) a Railgun for all zones connected to it.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#railgun-enable-or-disable-a-railgun
|
||||||
|
func (api *API) enableRailgun(railgunID string, enable bool) (Railgun, error) {
|
||||||
|
uri := api.userBaseURL("") + "/railguns/" + railgunID
|
||||||
|
params := struct {
|
||||||
|
Enabled bool `json:"enabled"`
|
||||||
|
}{
|
||||||
|
Enabled: enable,
|
||||||
|
}
|
||||||
|
res, err := api.makeRequest("PATCH", uri, params)
|
||||||
|
if err != nil {
|
||||||
|
return Railgun{}, errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
var r railgunResponse
|
||||||
|
if err := json.Unmarshal(res, &r); err != nil {
|
||||||
|
return Railgun{}, errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
return r.Result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// EnableRailgun enables a Railgun for all zones connected to it.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#railgun-enable-or-disable-a-railgun
|
||||||
|
func (api *API) EnableRailgun(railgunID string) (Railgun, error) {
|
||||||
|
return api.enableRailgun(railgunID, true)
|
||||||
|
}
|
||||||
|
|
||||||
|
// DisableRailgun enables a Railgun for all zones connected to it.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#railgun-enable-or-disable-a-railgun
|
||||||
|
func (api *API) DisableRailgun(railgunID string) (Railgun, error) {
|
||||||
|
return api.enableRailgun(railgunID, false)
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeleteRailgun disables and deletes a Railgun.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#railgun-delete-railgun
|
||||||
|
func (api *API) DeleteRailgun(railgunID string) error {
|
||||||
|
uri := api.userBaseURL("") + "/railguns/" + railgunID
|
||||||
|
if _, err := api.makeRequest("DELETE", uri, nil); err != nil {
|
||||||
|
return errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ZoneRailgun represents the status of a Railgun on a zone.
|
||||||
|
type ZoneRailgun struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
Enabled bool `json:"enabled"`
|
||||||
|
Connected bool `json:"connected"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// zoneRailgunResponse represents the response from the Zone Railgun Details endpoint.
|
||||||
|
type zoneRailgunResponse struct {
|
||||||
|
Response
|
||||||
|
Result ZoneRailgun `json:"result"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// zoneRailgunsResponse represents the response from the Zone Railgun endpoint.
|
||||||
|
type zoneRailgunsResponse struct {
|
||||||
|
Response
|
||||||
|
Result []ZoneRailgun `json:"result"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// RailgunDiagnosis represents the test results from testing railgun connections
|
||||||
|
// to a zone.
|
||||||
|
type RailgunDiagnosis struct {
|
||||||
|
Method string `json:"method"`
|
||||||
|
HostName string `json:"host_name"`
|
||||||
|
HTTPStatus int `json:"http_status"`
|
||||||
|
Railgun string `json:"railgun"`
|
||||||
|
URL string `json:"url"`
|
||||||
|
ResponseStatus string `json:"response_status"`
|
||||||
|
Protocol string `json:"protocol"`
|
||||||
|
ElapsedTime string `json:"elapsed_time"`
|
||||||
|
BodySize string `json:"body_size"`
|
||||||
|
BodyHash string `json:"body_hash"`
|
||||||
|
MissingHeaders string `json:"missing_headers"`
|
||||||
|
ConnectionClose bool `json:"connection_close"`
|
||||||
|
Cloudflare string `json:"cloudflare"`
|
||||||
|
CFRay string `json:"cf-ray"`
|
||||||
|
// NOTE: Cloudflare's online API documentation does not yet have definitions
|
||||||
|
// for the following fields. See: https://api.cloudflare.com/#railgun-connections-for-a-zone-test-railgun-connection/
|
||||||
|
CFWANError string `json:"cf-wan-error"`
|
||||||
|
CFCacheStatus string `json:"cf-cache-status"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// railgunDiagnosisResponse represents the response from the Test Railgun Connection enpoint.
|
||||||
|
type railgunDiagnosisResponse struct {
|
||||||
|
Response
|
||||||
|
Result RailgunDiagnosis `json:"result"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// ZoneRailguns returns the available Railguns for a zone.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#railguns-for-a-zone-get-available-railguns
|
||||||
|
func (api *API) ZoneRailguns(zoneID string) ([]ZoneRailgun, error) {
|
||||||
|
uri := "/zones/" + zoneID + "/railguns"
|
||||||
|
res, err := api.makeRequest("GET", uri, nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
var r zoneRailgunsResponse
|
||||||
|
if err := json.Unmarshal(res, &r); err != nil {
|
||||||
|
return nil, errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
return r.Result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ZoneRailgunDetails returns the configuration for a given Railgun.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#railguns-for-a-zone-get-railgun-details
|
||||||
|
func (api *API) ZoneRailgunDetails(zoneID, railgunID string) (ZoneRailgun, error) {
|
||||||
|
uri := "/zones/" + zoneID + "/railguns/" + railgunID
|
||||||
|
res, err := api.makeRequest("GET", uri, nil)
|
||||||
|
if err != nil {
|
||||||
|
return ZoneRailgun{}, errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
var r zoneRailgunResponse
|
||||||
|
if err := json.Unmarshal(res, &r); err != nil {
|
||||||
|
return ZoneRailgun{}, errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
return r.Result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestRailgunConnection tests a Railgun connection for a given zone.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#railgun-connections-for-a-zone-test-railgun-connection
|
||||||
|
func (api *API) TestRailgunConnection(zoneID, railgunID string) (RailgunDiagnosis, error) {
|
||||||
|
uri := "/zones/" + zoneID + "/railguns/" + railgunID + "/diagnose"
|
||||||
|
res, err := api.makeRequest("GET", uri, nil)
|
||||||
|
if err != nil {
|
||||||
|
return RailgunDiagnosis{}, errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
var r railgunDiagnosisResponse
|
||||||
|
if err := json.Unmarshal(res, &r); err != nil {
|
||||||
|
return RailgunDiagnosis{}, errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
return r.Result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// connectZoneRailgun connects (true) or disconnects (false) a Railgun for a given zone.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#railguns-for-a-zone-connect-or-disconnect-a-railgun
|
||||||
|
func (api *API) connectZoneRailgun(zoneID, railgunID string, connect bool) (ZoneRailgun, error) {
|
||||||
|
uri := "/zones/" + zoneID + "/railguns/" + railgunID
|
||||||
|
params := struct {
|
||||||
|
Connected bool `json:"connected"`
|
||||||
|
}{
|
||||||
|
Connected: connect,
|
||||||
|
}
|
||||||
|
res, err := api.makeRequest("PATCH", uri, params)
|
||||||
|
if err != nil {
|
||||||
|
return ZoneRailgun{}, errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
var r zoneRailgunResponse
|
||||||
|
if err := json.Unmarshal(res, &r); err != nil {
|
||||||
|
return ZoneRailgun{}, errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
return r.Result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ConnectZoneRailgun connects a Railgun for a given zone.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#railguns-for-a-zone-connect-or-disconnect-a-railgun
|
||||||
|
func (api *API) ConnectZoneRailgun(zoneID, railgunID string) (ZoneRailgun, error) {
|
||||||
|
return api.connectZoneRailgun(zoneID, railgunID, true)
|
||||||
|
}
|
||||||
|
|
||||||
|
// DisconnectZoneRailgun disconnects a Railgun for a given zone.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#railguns-for-a-zone-connect-or-disconnect-a-railgun
|
||||||
|
func (api *API) DisconnectZoneRailgun(zoneID, railgunID string) (ZoneRailgun, error) {
|
||||||
|
return api.connectZoneRailgun(zoneID, railgunID, false)
|
||||||
|
}
|
195
vendor/github.com/cloudflare/cloudflare-go/rate_limiting.go
generated
vendored
Normal file
195
vendor/github.com/cloudflare/cloudflare-go/rate_limiting.go
generated
vendored
Normal file
|
@ -0,0 +1,195 @@
|
||||||
|
package cloudflare
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"net/url"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
// RateLimit is a policy than can be applied to limit traffic within a customer domain
|
||||||
|
type RateLimit struct {
|
||||||
|
ID string `json:"id,omitempty"`
|
||||||
|
Disabled bool `json:"disabled,omitempty"`
|
||||||
|
Description string `json:"description,omitempty"`
|
||||||
|
Match RateLimitTrafficMatcher `json:"match"`
|
||||||
|
Bypass []RateLimitKeyValue `json:"bypass,omitempty"`
|
||||||
|
Threshold int `json:"threshold"`
|
||||||
|
Period int `json:"period"`
|
||||||
|
Action RateLimitAction `json:"action"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// RateLimitTrafficMatcher contains the rules that will be used to apply a rate limit to traffic
|
||||||
|
type RateLimitTrafficMatcher struct {
|
||||||
|
Request RateLimitRequestMatcher `json:"request"`
|
||||||
|
Response RateLimitResponseMatcher `json:"response"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// RateLimitRequestMatcher contains the matching rules pertaining to requests
|
||||||
|
type RateLimitRequestMatcher struct {
|
||||||
|
Methods []string `json:"methods,omitempty"`
|
||||||
|
Schemes []string `json:"schemes,omitempty"`
|
||||||
|
URLPattern string `json:"url,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// RateLimitResponseMatcher contains the matching rules pertaining to responses
|
||||||
|
type RateLimitResponseMatcher struct {
|
||||||
|
Statuses []int `json:"status,omitempty"`
|
||||||
|
OriginTraffic *bool `json:"origin_traffic,omitempty"` // api defaults to true so we need an explicit empty value
|
||||||
|
}
|
||||||
|
|
||||||
|
// RateLimitKeyValue is k-v formatted as expected in the rate limit description
|
||||||
|
type RateLimitKeyValue struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
Value string `json:"value"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// RateLimitAction is the action that will be taken when the rate limit threshold is reached
|
||||||
|
type RateLimitAction struct {
|
||||||
|
Mode string `json:"mode"`
|
||||||
|
Timeout int `json:"timeout"`
|
||||||
|
Response *RateLimitActionResponse `json:"response"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// RateLimitActionResponse is the response that will be returned when rate limit action is triggered
|
||||||
|
type RateLimitActionResponse struct {
|
||||||
|
ContentType string `json:"content_type"`
|
||||||
|
Body string `json:"body"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type rateLimitResponse struct {
|
||||||
|
Response
|
||||||
|
Result RateLimit `json:"result"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type rateLimitListResponse struct {
|
||||||
|
Response
|
||||||
|
Result []RateLimit `json:"result"`
|
||||||
|
ResultInfo ResultInfo `json:"result_info"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreateRateLimit creates a new rate limit for a zone.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#rate-limits-for-a-zone-create-a-ratelimit
|
||||||
|
func (api *API) CreateRateLimit(zoneID string, limit RateLimit) (RateLimit, error) {
|
||||||
|
uri := "/zones/" + zoneID + "/rate_limits"
|
||||||
|
res, err := api.makeRequest("POST", uri, limit)
|
||||||
|
if err != nil {
|
||||||
|
return RateLimit{}, errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
var r rateLimitResponse
|
||||||
|
if err := json.Unmarshal(res, &r); err != nil {
|
||||||
|
return RateLimit{}, errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
return r.Result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ListRateLimits returns Rate Limits for a zone, paginated according to the provided options
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#rate-limits-for-a-zone-list-rate-limits
|
||||||
|
func (api *API) ListRateLimits(zoneID string, pageOpts PaginationOptions) ([]RateLimit, ResultInfo, error) {
|
||||||
|
v := url.Values{}
|
||||||
|
if pageOpts.PerPage > 0 {
|
||||||
|
v.Set("per_page", strconv.Itoa(pageOpts.PerPage))
|
||||||
|
}
|
||||||
|
if pageOpts.Page > 0 {
|
||||||
|
v.Set("page", strconv.Itoa(pageOpts.Page))
|
||||||
|
}
|
||||||
|
|
||||||
|
uri := "/zones/" + zoneID + "/rate_limits"
|
||||||
|
if len(v) > 0 {
|
||||||
|
uri = uri + "?" + v.Encode()
|
||||||
|
}
|
||||||
|
|
||||||
|
res, err := api.makeRequest("GET", uri, nil)
|
||||||
|
if err != nil {
|
||||||
|
return []RateLimit{}, ResultInfo{}, errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
|
||||||
|
var r rateLimitListResponse
|
||||||
|
err = json.Unmarshal(res, &r)
|
||||||
|
if err != nil {
|
||||||
|
return []RateLimit{}, ResultInfo{}, errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
return r.Result, r.ResultInfo, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ListAllRateLimits returns all Rate Limits for a zone.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#rate-limits-for-a-zone-list-rate-limits
|
||||||
|
func (api *API) ListAllRateLimits(zoneID string) ([]RateLimit, error) {
|
||||||
|
pageOpts := PaginationOptions{
|
||||||
|
PerPage: 100, // this is the max page size allowed
|
||||||
|
Page: 1,
|
||||||
|
}
|
||||||
|
|
||||||
|
allRateLimits := make([]RateLimit, 0)
|
||||||
|
for {
|
||||||
|
rateLimits, resultInfo, err := api.ListRateLimits(zoneID, pageOpts)
|
||||||
|
if err != nil {
|
||||||
|
return []RateLimit{}, err
|
||||||
|
}
|
||||||
|
allRateLimits = append(allRateLimits, rateLimits...)
|
||||||
|
// total pages is not returned on this call
|
||||||
|
// if number of records is less than the max, this must be the last page
|
||||||
|
// in case TotalCount % PerPage = 0, the last request will return an empty list
|
||||||
|
if resultInfo.Count < resultInfo.PerPage {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
// continue with the next page
|
||||||
|
pageOpts.Page = pageOpts.Page + 1
|
||||||
|
}
|
||||||
|
|
||||||
|
return allRateLimits, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// RateLimit fetches detail about one Rate Limit for a zone.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#rate-limits-for-a-zone-rate-limit-details
|
||||||
|
func (api *API) RateLimit(zoneID, limitID string) (RateLimit, error) {
|
||||||
|
uri := "/zones/" + zoneID + "/rate_limits/" + limitID
|
||||||
|
res, err := api.makeRequest("GET", uri, nil)
|
||||||
|
if err != nil {
|
||||||
|
return RateLimit{}, errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
var r rateLimitResponse
|
||||||
|
err = json.Unmarshal(res, &r)
|
||||||
|
if err != nil {
|
||||||
|
return RateLimit{}, errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
return r.Result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateRateLimit lets you replace a Rate Limit for a zone.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#rate-limits-for-a-zone-update-rate-limit
|
||||||
|
func (api *API) UpdateRateLimit(zoneID, limitID string, limit RateLimit) (RateLimit, error) {
|
||||||
|
uri := "/zones/" + zoneID + "/rate_limits/" + limitID
|
||||||
|
res, err := api.makeRequest("PUT", uri, limit)
|
||||||
|
if err != nil {
|
||||||
|
return RateLimit{}, errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
var r rateLimitResponse
|
||||||
|
if err := json.Unmarshal(res, &r); err != nil {
|
||||||
|
return RateLimit{}, errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
return r.Result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeleteRateLimit deletes a Rate Limit for a zone.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#rate-limits-for-a-zone-delete-rate-limit
|
||||||
|
func (api *API) DeleteRateLimit(zoneID, limitID string) error {
|
||||||
|
uri := "/zones/" + zoneID + "/rate_limits/" + limitID
|
||||||
|
res, err := api.makeRequest("DELETE", uri, nil)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
var r rateLimitResponse
|
||||||
|
err = json.Unmarshal(res, &r)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
148
vendor/github.com/cloudflare/cloudflare-go/ssl.go
generated
vendored
Normal file
148
vendor/github.com/cloudflare/cloudflare-go/ssl.go
generated
vendored
Normal file
|
@ -0,0 +1,148 @@
|
||||||
|
package cloudflare
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ZoneCustomSSL represents custom SSL certificate metadata.
|
||||||
|
type ZoneCustomSSL struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
Hosts []string `json:"hosts"`
|
||||||
|
Issuer string `json:"issuer"`
|
||||||
|
Signature string `json:"signature"`
|
||||||
|
Status string `json:"status"`
|
||||||
|
BundleMethod string `json:"bundle_method"`
|
||||||
|
ZoneID string `json:"zone_id"`
|
||||||
|
UploadedOn time.Time `json:"uploaded_on"`
|
||||||
|
ModifiedOn time.Time `json:"modified_on"`
|
||||||
|
ExpiresOn time.Time `json:"expires_on"`
|
||||||
|
Priority int `json:"priority"`
|
||||||
|
KeylessServer KeylessSSL `json:"keyless_server"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// zoneCustomSSLResponse represents the response from the zone SSL details endpoint.
|
||||||
|
type zoneCustomSSLResponse struct {
|
||||||
|
Response
|
||||||
|
Result ZoneCustomSSL `json:"result"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// zoneCustomSSLsResponse represents the response from the zone SSL list endpoint.
|
||||||
|
type zoneCustomSSLsResponse struct {
|
||||||
|
Response
|
||||||
|
Result []ZoneCustomSSL `json:"result"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// ZoneCustomSSLOptions represents the parameters to create or update an existing
|
||||||
|
// custom SSL configuration.
|
||||||
|
type ZoneCustomSSLOptions struct {
|
||||||
|
Certificate string `json:"certificate"`
|
||||||
|
PrivateKey string `json:"private_key"`
|
||||||
|
BundleMethod string `json:"bundle_method,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// ZoneCustomSSLPriority represents a certificate's ID and priority. It is a
|
||||||
|
// subset of ZoneCustomSSL used for patch requests.
|
||||||
|
type ZoneCustomSSLPriority struct {
|
||||||
|
ID string `json:"ID"`
|
||||||
|
Priority int `json:"priority"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreateSSL allows you to add a custom SSL certificate to the given zone.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#custom-ssl-for-a-zone-create-ssl-configuration
|
||||||
|
func (api *API) CreateSSL(zoneID string, options ZoneCustomSSLOptions) (ZoneCustomSSL, error) {
|
||||||
|
uri := "/zones/" + zoneID + "/custom_certificates"
|
||||||
|
res, err := api.makeRequest("POST", uri, options)
|
||||||
|
if err != nil {
|
||||||
|
return ZoneCustomSSL{}, errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
var r zoneCustomSSLResponse
|
||||||
|
if err := json.Unmarshal(res, &r); err != nil {
|
||||||
|
return ZoneCustomSSL{}, errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
return r.Result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ListSSL lists the custom certificates for the given zone.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#custom-ssl-for-a-zone-list-ssl-configurations
|
||||||
|
func (api *API) ListSSL(zoneID string) ([]ZoneCustomSSL, error) {
|
||||||
|
uri := "/zones/" + zoneID + "/custom_certificates"
|
||||||
|
res, err := api.makeRequest("GET", uri, nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
var r zoneCustomSSLsResponse
|
||||||
|
if err := json.Unmarshal(res, &r); err != nil {
|
||||||
|
return nil, errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
return r.Result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// SSLDetails returns the configuration details for a custom SSL certificate.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#custom-ssl-for-a-zone-ssl-configuration-details
|
||||||
|
func (api *API) SSLDetails(zoneID, certificateID string) (ZoneCustomSSL, error) {
|
||||||
|
uri := "/zones/" + zoneID + "/custom_certificates/" + certificateID
|
||||||
|
res, err := api.makeRequest("GET", uri, nil)
|
||||||
|
if err != nil {
|
||||||
|
return ZoneCustomSSL{}, errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
var r zoneCustomSSLResponse
|
||||||
|
if err := json.Unmarshal(res, &r); err != nil {
|
||||||
|
return ZoneCustomSSL{}, errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
return r.Result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateSSL updates (replaces) a custom SSL certificate.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#custom-ssl-for-a-zone-update-ssl-configuration
|
||||||
|
func (api *API) UpdateSSL(zoneID, certificateID string, options ZoneCustomSSLOptions) (ZoneCustomSSL, error) {
|
||||||
|
uri := "/zones/" + zoneID + "/custom_certificates/" + certificateID
|
||||||
|
res, err := api.makeRequest("PATCH", uri, options)
|
||||||
|
if err != nil {
|
||||||
|
return ZoneCustomSSL{}, errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
var r zoneCustomSSLResponse
|
||||||
|
if err := json.Unmarshal(res, &r); err != nil {
|
||||||
|
return ZoneCustomSSL{}, errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
return r.Result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ReprioritizeSSL allows you to change the priority (which is served for a given
|
||||||
|
// request) of custom SSL certificates associated with the given zone.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#custom-ssl-for-a-zone-re-prioritize-ssl-certificates
|
||||||
|
func (api *API) ReprioritizeSSL(zoneID string, p []ZoneCustomSSLPriority) ([]ZoneCustomSSL, error) {
|
||||||
|
uri := "/zones/" + zoneID + "/custom_certificates/prioritize"
|
||||||
|
params := struct {
|
||||||
|
Certificates []ZoneCustomSSLPriority `json:"certificates"`
|
||||||
|
}{
|
||||||
|
Certificates: p,
|
||||||
|
}
|
||||||
|
res, err := api.makeRequest("PUT", uri, params)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
var r zoneCustomSSLsResponse
|
||||||
|
if err := json.Unmarshal(res, &r); err != nil {
|
||||||
|
return nil, errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
return r.Result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeleteSSL deletes a custom SSL certificate from the given zone.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#custom-ssl-for-a-zone-delete-an-ssl-certificate
|
||||||
|
func (api *API) DeleteSSL(zoneID, certificateID string) error {
|
||||||
|
uri := "/zones/" + zoneID + "/custom_certificates/" + certificateID
|
||||||
|
if _, err := api.makeRequest("DELETE", uri, nil); err != nil {
|
||||||
|
return errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
113
vendor/github.com/cloudflare/cloudflare-go/user.go
generated
vendored
Normal file
113
vendor/github.com/cloudflare/cloudflare-go/user.go
generated
vendored
Normal file
|
@ -0,0 +1,113 @@
|
||||||
|
package cloudflare
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
// User describes a user account.
|
||||||
|
type User struct {
|
||||||
|
ID string `json:"id,omitempty"`
|
||||||
|
Email string `json:"email,omitempty"`
|
||||||
|
FirstName string `json:"first_name,omitempty"`
|
||||||
|
LastName string `json:"last_name,omitempty"`
|
||||||
|
Username string `json:"username,omitempty"`
|
||||||
|
Telephone string `json:"telephone,omitempty"`
|
||||||
|
Country string `json:"country,omitempty"`
|
||||||
|
Zipcode string `json:"zipcode,omitempty"`
|
||||||
|
CreatedOn *time.Time `json:"created_on,omitempty"`
|
||||||
|
ModifiedOn *time.Time `json:"modified_on,omitempty"`
|
||||||
|
APIKey string `json:"api_key,omitempty"`
|
||||||
|
TwoFA bool `json:"two_factor_authentication_enabled,omitempty"`
|
||||||
|
Betas []string `json:"betas,omitempty"`
|
||||||
|
Organizations []Organization `json:"organizations,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// UserResponse wraps a response containing User accounts.
|
||||||
|
type UserResponse struct {
|
||||||
|
Response
|
||||||
|
Result User `json:"result"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// userBillingProfileResponse wraps a response containing Billing Profile information.
|
||||||
|
type userBillingProfileResponse struct {
|
||||||
|
Response
|
||||||
|
Result UserBillingProfile
|
||||||
|
}
|
||||||
|
|
||||||
|
// UserBillingProfile contains Billing Profile information.
|
||||||
|
type UserBillingProfile struct {
|
||||||
|
ID string `json:"id,omitempty"`
|
||||||
|
FirstName string `json:"first_name,omitempty"`
|
||||||
|
LastName string `json:"last_name,omitempty"`
|
||||||
|
Address string `json:"address,omitempty"`
|
||||||
|
Address2 string `json:"address2,omitempty"`
|
||||||
|
Company string `json:"company,omitempty"`
|
||||||
|
City string `json:"city,omitempty"`
|
||||||
|
State string `json:"state,omitempty"`
|
||||||
|
ZipCode string `json:"zipcode,omitempty"`
|
||||||
|
Country string `json:"country,omitempty"`
|
||||||
|
Telephone string `json:"telephone,omitempty"`
|
||||||
|
CardNumber string `json:"card_number,omitempty"`
|
||||||
|
CardExpiryYear int `json:"card_expiry_year,omitempty"`
|
||||||
|
CardExpiryMonth int `json:"card_expiry_month,omitempty"`
|
||||||
|
VAT string `json:"vat,omitempty"`
|
||||||
|
CreatedOn *time.Time `json:"created_on,omitempty"`
|
||||||
|
EditedOn *time.Time `json:"edited_on,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// UserDetails provides information about the logged-in user.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#user-user-details
|
||||||
|
func (api *API) UserDetails() (User, error) {
|
||||||
|
var r UserResponse
|
||||||
|
res, err := api.makeRequest("GET", "/user", nil)
|
||||||
|
if err != nil {
|
||||||
|
return User{}, errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = json.Unmarshal(res, &r)
|
||||||
|
if err != nil {
|
||||||
|
return User{}, errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
|
||||||
|
return r.Result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateUser updates the properties of the given user.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#user-update-user
|
||||||
|
func (api *API) UpdateUser(user *User) (User, error) {
|
||||||
|
var r UserResponse
|
||||||
|
res, err := api.makeRequest("PATCH", "/user", user)
|
||||||
|
if err != nil {
|
||||||
|
return User{}, errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = json.Unmarshal(res, &r)
|
||||||
|
if err != nil {
|
||||||
|
return User{}, errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
|
||||||
|
return r.Result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// UserBillingProfile returns the billing profile of the user.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#user-billing-profile
|
||||||
|
func (api *API) UserBillingProfile() (UserBillingProfile, error) {
|
||||||
|
var r userBillingProfileResponse
|
||||||
|
res, err := api.makeRequest("GET", "/user/billing/profile", nil)
|
||||||
|
if err != nil {
|
||||||
|
return UserBillingProfile{}, errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = json.Unmarshal(res, &r)
|
||||||
|
if err != nil {
|
||||||
|
return UserBillingProfile{}, errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
|
||||||
|
return r.Result, nil
|
||||||
|
}
|
149
vendor/github.com/cloudflare/cloudflare-go/user_agent.go
generated
vendored
Normal file
149
vendor/github.com/cloudflare/cloudflare-go/user_agent.go
generated
vendored
Normal file
|
@ -0,0 +1,149 @@
|
||||||
|
package cloudflare
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"net/url"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
// UserAgentRule represents a User-Agent Block. These rules can be used to
|
||||||
|
// challenge, block or whitelist specific User-Agents for a given zone.
|
||||||
|
type UserAgentRule struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
Description string `json:"description"`
|
||||||
|
Mode string `json:"mode"`
|
||||||
|
Configuration UserAgentRuleConfig `json:"configuration"`
|
||||||
|
Paused bool `json:"paused"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// UserAgentRuleConfig represents a Zone Lockdown config, which comprises
|
||||||
|
// a Target ("ip" or "ip_range") and a Value (an IP address or IP+mask,
|
||||||
|
// respectively.)
|
||||||
|
type UserAgentRuleConfig ZoneLockdownConfig
|
||||||
|
|
||||||
|
// UserAgentRuleResponse represents a response from the Zone Lockdown endpoint.
|
||||||
|
type UserAgentRuleResponse struct {
|
||||||
|
Result UserAgentRule `json:"result"`
|
||||||
|
Response
|
||||||
|
ResultInfo `json:"result_info"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// UserAgentRuleListResponse represents a response from the List Zone Lockdown endpoint.
|
||||||
|
type UserAgentRuleListResponse struct {
|
||||||
|
Result []UserAgentRule `json:"result"`
|
||||||
|
Response
|
||||||
|
ResultInfo `json:"result_info"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreateUserAgentRule creates a User-Agent Block rule for the given zone ID.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#user-agent-blocking-rules-create-a-useragent-rule
|
||||||
|
func (api *API) CreateUserAgentRule(zoneID string, ld UserAgentRule) (*UserAgentRuleResponse, error) {
|
||||||
|
switch ld.Mode {
|
||||||
|
case "block", "challenge", "js_challenge", "whitelist":
|
||||||
|
break
|
||||||
|
default:
|
||||||
|
return nil, errors.New(`the User-Agent Block rule mode must be one of "block", "challenge", "js_challenge", "whitelist"`)
|
||||||
|
}
|
||||||
|
|
||||||
|
uri := "/zones/" + zoneID + "/firewall/ua_rules"
|
||||||
|
res, err := api.makeRequest("POST", uri, ld)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
|
||||||
|
response := &UserAgentRuleResponse{}
|
||||||
|
err = json.Unmarshal(res, &response)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
|
||||||
|
return response, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateUserAgentRule updates a User-Agent Block rule (based on the ID) for the given zone ID.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#user-agent-blocking-rules-update-useragent-rule
|
||||||
|
func (api *API) UpdateUserAgentRule(zoneID string, id string, ld UserAgentRule) (*UserAgentRuleResponse, error) {
|
||||||
|
uri := "/zones/" + zoneID + "/firewall/ua_rules/" + id
|
||||||
|
res, err := api.makeRequest("PUT", uri, ld)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
|
||||||
|
response := &UserAgentRuleResponse{}
|
||||||
|
err = json.Unmarshal(res, &response)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
|
||||||
|
return response, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeleteUserAgentRule deletes a User-Agent Block rule (based on the ID) for the given zone ID.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#user-agent-blocking-rules-delete-useragent-rule
|
||||||
|
func (api *API) DeleteUserAgentRule(zoneID string, id string) (*UserAgentRuleResponse, error) {
|
||||||
|
uri := "/zones/" + zoneID + "/firewall/ua_rules/" + id
|
||||||
|
res, err := api.makeRequest("DELETE", uri, nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
|
||||||
|
response := &UserAgentRuleResponse{}
|
||||||
|
err = json.Unmarshal(res, &response)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
|
||||||
|
return response, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// UserAgentRule retrieves a User-Agent Block rule (based on the ID) for the given zone ID.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#user-agent-blocking-rules-useragent-rule-details
|
||||||
|
func (api *API) UserAgentRule(zoneID string, id string) (*UserAgentRuleResponse, error) {
|
||||||
|
uri := "/zones/" + zoneID + "/firewall/ua_rules/" + id
|
||||||
|
res, err := api.makeRequest("GET", uri, nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
|
||||||
|
response := &UserAgentRuleResponse{}
|
||||||
|
err = json.Unmarshal(res, &response)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
|
||||||
|
return response, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ListUserAgentRules retrieves a list of User-Agent Block rules for a given zone ID by page number.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#user-agent-blocking-rules-list-useragent-rules
|
||||||
|
func (api *API) ListUserAgentRules(zoneID string, page int) (*UserAgentRuleListResponse, error) {
|
||||||
|
v := url.Values{}
|
||||||
|
if page <= 0 {
|
||||||
|
page = 1
|
||||||
|
}
|
||||||
|
|
||||||
|
v.Set("page", strconv.Itoa(page))
|
||||||
|
v.Set("per_page", strconv.Itoa(100))
|
||||||
|
query := "?" + v.Encode()
|
||||||
|
|
||||||
|
uri := "/zones/" + zoneID + "/firewall/ua_rules" + query
|
||||||
|
res, err := api.makeRequest("GET", uri, nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
|
||||||
|
response := &UserAgentRuleListResponse{}
|
||||||
|
err = json.Unmarshal(res, &response)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
|
||||||
|
return response, nil
|
||||||
|
}
|
125
vendor/github.com/cloudflare/cloudflare-go/virtualdns.go
generated
vendored
Normal file
125
vendor/github.com/cloudflare/cloudflare-go/virtualdns.go
generated
vendored
Normal file
|
@ -0,0 +1,125 @@
|
||||||
|
package cloudflare
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
// VirtualDNS represents a Virtual DNS configuration.
|
||||||
|
type VirtualDNS struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
OriginIPs []string `json:"origin_ips"`
|
||||||
|
VirtualDNSIPs []string `json:"virtual_dns_ips"`
|
||||||
|
MinimumCacheTTL uint `json:"minimum_cache_ttl"`
|
||||||
|
MaximumCacheTTL uint `json:"maximum_cache_ttl"`
|
||||||
|
DeprecateAnyRequests bool `json:"deprecate_any_requests"`
|
||||||
|
ModifiedOn string `json:"modified_on"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// VirtualDNSResponse represents a Virtual DNS response.
|
||||||
|
type VirtualDNSResponse struct {
|
||||||
|
Response
|
||||||
|
Result *VirtualDNS `json:"result"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// VirtualDNSListResponse represents an array of Virtual DNS responses.
|
||||||
|
type VirtualDNSListResponse struct {
|
||||||
|
Response
|
||||||
|
Result []*VirtualDNS `json:"result"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreateVirtualDNS creates a new Virtual DNS cluster.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#virtual-dns-users--create-a-virtual-dns-cluster
|
||||||
|
func (api *API) CreateVirtualDNS(v *VirtualDNS) (*VirtualDNS, error) {
|
||||||
|
res, err := api.makeRequest("POST", "/user/virtual_dns", v)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
|
||||||
|
response := &VirtualDNSResponse{}
|
||||||
|
err = json.Unmarshal(res, &response)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
|
||||||
|
return response.Result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// VirtualDNS fetches a single virtual DNS cluster.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#virtual-dns-users--get-a-virtual-dns-cluster
|
||||||
|
func (api *API) VirtualDNS(virtualDNSID string) (*VirtualDNS, error) {
|
||||||
|
uri := "/user/virtual_dns/" + virtualDNSID
|
||||||
|
res, err := api.makeRequest("GET", uri, nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
|
||||||
|
response := &VirtualDNSResponse{}
|
||||||
|
err = json.Unmarshal(res, &response)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
|
||||||
|
return response.Result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ListVirtualDNS lists the virtual DNS clusters associated with an account.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#virtual-dns-users--get-virtual-dns-clusters
|
||||||
|
func (api *API) ListVirtualDNS() ([]*VirtualDNS, error) {
|
||||||
|
res, err := api.makeRequest("GET", "/user/virtual_dns", nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
|
||||||
|
response := &VirtualDNSListResponse{}
|
||||||
|
err = json.Unmarshal(res, &response)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
|
||||||
|
return response.Result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateVirtualDNS updates a Virtual DNS cluster.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#virtual-dns-users--modify-a-virtual-dns-cluster
|
||||||
|
func (api *API) UpdateVirtualDNS(virtualDNSID string, vv VirtualDNS) error {
|
||||||
|
uri := "/user/virtual_dns/" + virtualDNSID
|
||||||
|
res, err := api.makeRequest("PUT", uri, vv)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
|
||||||
|
response := &VirtualDNSResponse{}
|
||||||
|
err = json.Unmarshal(res, &response)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeleteVirtualDNS deletes a Virtual DNS cluster. Note that this cannot be
|
||||||
|
// undone, and will stop all traffic to that cluster.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#virtual-dns-users--delete-a-virtual-dns-cluster
|
||||||
|
func (api *API) DeleteVirtualDNS(virtualDNSID string) error {
|
||||||
|
uri := "/user/virtual_dns/" + virtualDNSID
|
||||||
|
res, err := api.makeRequest("DELETE", uri, nil)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
|
||||||
|
response := &VirtualDNSResponse{}
|
||||||
|
err = json.Unmarshal(res, &response)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
97
vendor/github.com/cloudflare/cloudflare-go/waf.go
generated
vendored
Normal file
97
vendor/github.com/cloudflare/cloudflare-go/waf.go
generated
vendored
Normal file
|
@ -0,0 +1,97 @@
|
||||||
|
package cloudflare
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
// WAFPackage represents a WAF package configuration.
|
||||||
|
type WAFPackage struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
Description string `json:"description"`
|
||||||
|
ZoneID string `json:"zone_id"`
|
||||||
|
DetectionMode string `json:"detection_mode"`
|
||||||
|
Sensitivity string `json:"sensitivity"`
|
||||||
|
ActionMode string `json:"action_mode"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// WAFPackagesResponse represents the response from the WAF packages endpoint.
|
||||||
|
type WAFPackagesResponse struct {
|
||||||
|
Response
|
||||||
|
Result []WAFPackage `json:"result"`
|
||||||
|
ResultInfo ResultInfo `json:"result_info"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// WAFRule represents a WAF rule.
|
||||||
|
type WAFRule struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
Description string `json:"description"`
|
||||||
|
Priority string `json:"priority"`
|
||||||
|
PackageID string `json:"package_id"`
|
||||||
|
Group struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
} `json:"group"`
|
||||||
|
Mode string `json:"mode"`
|
||||||
|
DefaultMode string `json:"default_mode"`
|
||||||
|
AllowedModes []string `json:"allowed_modes"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// WAFRulesResponse represents the response from the WAF rule endpoint.
|
||||||
|
type WAFRulesResponse struct {
|
||||||
|
Response
|
||||||
|
Result []WAFRule `json:"result"`
|
||||||
|
ResultInfo ResultInfo `json:"result_info"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// ListWAFPackages returns a slice of the WAF packages for the given zone.
|
||||||
|
func (api *API) ListWAFPackages(zoneID string) ([]WAFPackage, error) {
|
||||||
|
var p WAFPackagesResponse
|
||||||
|
var packages []WAFPackage
|
||||||
|
var res []byte
|
||||||
|
var err error
|
||||||
|
uri := "/zones/" + zoneID + "/firewall/waf/packages"
|
||||||
|
res, err = api.makeRequest("GET", uri, nil)
|
||||||
|
if err != nil {
|
||||||
|
return []WAFPackage{}, errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
err = json.Unmarshal(res, &p)
|
||||||
|
if err != nil {
|
||||||
|
return []WAFPackage{}, errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
if !p.Success {
|
||||||
|
// TODO: Provide an actual error message instead of always returning nil
|
||||||
|
return []WAFPackage{}, err
|
||||||
|
}
|
||||||
|
for pi := range p.Result {
|
||||||
|
packages = append(packages, p.Result[pi])
|
||||||
|
}
|
||||||
|
return packages, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ListWAFRules returns a slice of the WAF rules for the given WAF package.
|
||||||
|
func (api *API) ListWAFRules(zoneID, packageID string) ([]WAFRule, error) {
|
||||||
|
var r WAFRulesResponse
|
||||||
|
var rules []WAFRule
|
||||||
|
var res []byte
|
||||||
|
var err error
|
||||||
|
uri := "/zones/" + zoneID + "/firewall/waf/packages/" + packageID + "/rules"
|
||||||
|
res, err = api.makeRequest("GET", uri, nil)
|
||||||
|
if err != nil {
|
||||||
|
return []WAFRule{}, errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
err = json.Unmarshal(res, &r)
|
||||||
|
if err != nil {
|
||||||
|
return []WAFRule{}, errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
if !r.Success {
|
||||||
|
// TODO: Provide an actual error message instead of always returning nil
|
||||||
|
return []WAFRule{}, err
|
||||||
|
}
|
||||||
|
for ri := range r.Result {
|
||||||
|
rules = append(rules, r.Result[ri])
|
||||||
|
}
|
||||||
|
return rules, nil
|
||||||
|
}
|
587
vendor/github.com/cloudflare/cloudflare-go/zone.go
generated
vendored
Normal file
587
vendor/github.com/cloudflare/cloudflare-go/zone.go
generated
vendored
Normal file
|
@ -0,0 +1,587 @@
|
||||||
|
package cloudflare
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"net/url"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Owner describes the resource owner.
|
||||||
|
type Owner struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
Email string `json:"email"`
|
||||||
|
OwnerType string `json:"owner_type"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Zone describes a Cloudflare zone.
|
||||||
|
type Zone struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
// DevMode contains the time in seconds until development expires (if
|
||||||
|
// positive) or since it expired (if negative). It will be 0 if never used.
|
||||||
|
DevMode int `json:"development_mode"`
|
||||||
|
OriginalNS []string `json:"original_name_servers"`
|
||||||
|
OriginalRegistrar string `json:"original_registrar"`
|
||||||
|
OriginalDNSHost string `json:"original_dnshost"`
|
||||||
|
CreatedOn time.Time `json:"created_on"`
|
||||||
|
ModifiedOn time.Time `json:"modified_on"`
|
||||||
|
NameServers []string `json:"name_servers"`
|
||||||
|
Owner Owner `json:"owner"`
|
||||||
|
Permissions []string `json:"permissions"`
|
||||||
|
Plan ZoneRatePlan `json:"plan"`
|
||||||
|
PlanPending ZoneRatePlan `json:"plan_pending,omitempty"`
|
||||||
|
Status string `json:"status"`
|
||||||
|
Paused bool `json:"paused"`
|
||||||
|
Type string `json:"type"`
|
||||||
|
Host struct {
|
||||||
|
Name string
|
||||||
|
Website string
|
||||||
|
} `json:"host"`
|
||||||
|
VanityNS []string `json:"vanity_name_servers"`
|
||||||
|
Betas []string `json:"betas"`
|
||||||
|
DeactReason string `json:"deactivation_reason"`
|
||||||
|
Meta ZoneMeta `json:"meta"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// ZoneMeta describes metadata about a zone.
|
||||||
|
type ZoneMeta struct {
|
||||||
|
// custom_certificate_quota is broken - sometimes it's a string, sometimes a number!
|
||||||
|
// CustCertQuota int `json:"custom_certificate_quota"`
|
||||||
|
PageRuleQuota int `json:"page_rule_quota"`
|
||||||
|
WildcardProxiable bool `json:"wildcard_proxiable"`
|
||||||
|
PhishingDetected bool `json:"phishing_detected"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// ZoneRatePlan contains the plan information for a zone.
|
||||||
|
type ZoneRatePlan struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
Name string `json:"name,omitempty"`
|
||||||
|
Price int `json:"price,omitempty"`
|
||||||
|
Currency string `json:"currency,omitempty"`
|
||||||
|
Duration int `json:"duration,omitempty"`
|
||||||
|
Frequency string `json:"frequency,omitempty"`
|
||||||
|
Components []zoneRatePlanComponents `json:"components,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type zoneRatePlanComponents struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
Default int `json:"Default"`
|
||||||
|
UnitPrice int `json:"unit_price"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// ZoneID contains only the zone ID.
|
||||||
|
type ZoneID struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// ZoneResponse represents the response from the Zone endpoint containing a single zone.
|
||||||
|
type ZoneResponse struct {
|
||||||
|
Response
|
||||||
|
Result Zone `json:"result"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// ZonesResponse represents the response from the Zone endpoint containing an array of zones.
|
||||||
|
type ZonesResponse struct {
|
||||||
|
Response
|
||||||
|
Result []Zone `json:"result"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// ZoneIDResponse represents the response from the Zone endpoint, containing only a zone ID.
|
||||||
|
type ZoneIDResponse struct {
|
||||||
|
Response
|
||||||
|
Result ZoneID `json:"result"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// AvailableZoneRatePlansResponse represents the response from the Available Rate Plans endpoint.
|
||||||
|
type AvailableZoneRatePlansResponse struct {
|
||||||
|
Response
|
||||||
|
Result []ZoneRatePlan `json:"result"`
|
||||||
|
ResultInfo
|
||||||
|
}
|
||||||
|
|
||||||
|
// ZoneRatePlanResponse represents the response from the Plan Details endpoint.
|
||||||
|
type ZoneRatePlanResponse struct {
|
||||||
|
Response
|
||||||
|
Result ZoneRatePlan `json:"result"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// ZoneSetting contains settings for a zone.
|
||||||
|
type ZoneSetting struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
Editable bool `json:"editable"`
|
||||||
|
ModifiedOn string `json:"modified_on"`
|
||||||
|
Value interface{} `json:"value"`
|
||||||
|
TimeRemaining int `json:"time_remaining"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// ZoneSettingResponse represents the response from the Zone Setting endpoint.
|
||||||
|
type ZoneSettingResponse struct {
|
||||||
|
Response
|
||||||
|
Result []ZoneSetting `json:"result"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// ZoneSSLSetting contains ssl setting for a zone.
|
||||||
|
type ZoneSSLSetting struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
Editable bool `json:"editable"`
|
||||||
|
ModifiedOn string `json:"modified_on"`
|
||||||
|
Value string `json:"value"`
|
||||||
|
CertificateStatus string `json:"certificate_status"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// ZoneSettingResponse represents the response from the Zone SSL Setting endpoint.
|
||||||
|
type ZoneSSLSettingResponse struct {
|
||||||
|
Response
|
||||||
|
Result ZoneSSLSetting `json:"result"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// ZoneAnalyticsData contains totals and timeseries analytics data for a zone.
|
||||||
|
type ZoneAnalyticsData struct {
|
||||||
|
Totals ZoneAnalytics `json:"totals"`
|
||||||
|
Timeseries []ZoneAnalytics `json:"timeseries"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// zoneAnalyticsDataResponse represents the response from the Zone Analytics Dashboard endpoint.
|
||||||
|
type zoneAnalyticsDataResponse struct {
|
||||||
|
Response
|
||||||
|
Result ZoneAnalyticsData `json:"result"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// ZoneAnalyticsColocation contains analytics data by datacenter.
|
||||||
|
type ZoneAnalyticsColocation struct {
|
||||||
|
ColocationID string `json:"colo_id"`
|
||||||
|
Timeseries []ZoneAnalytics `json:"timeseries"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// zoneAnalyticsColocationResponse represents the response from the Zone Analytics By Co-location endpoint.
|
||||||
|
type zoneAnalyticsColocationResponse struct {
|
||||||
|
Response
|
||||||
|
Result []ZoneAnalyticsColocation `json:"result"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// ZoneAnalytics contains analytics data for a zone.
|
||||||
|
type ZoneAnalytics struct {
|
||||||
|
Since time.Time `json:"since"`
|
||||||
|
Until time.Time `json:"until"`
|
||||||
|
Requests struct {
|
||||||
|
All int `json:"all"`
|
||||||
|
Cached int `json:"cached"`
|
||||||
|
Uncached int `json:"uncached"`
|
||||||
|
ContentType map[string]int `json:"content_type"`
|
||||||
|
Country map[string]int `json:"country"`
|
||||||
|
SSL struct {
|
||||||
|
Encrypted int `json:"encrypted"`
|
||||||
|
Unencrypted int `json:"unencrypted"`
|
||||||
|
} `json:"ssl"`
|
||||||
|
HTTPStatus map[string]int `json:"http_status"`
|
||||||
|
} `json:"requests"`
|
||||||
|
Bandwidth struct {
|
||||||
|
All int `json:"all"`
|
||||||
|
Cached int `json:"cached"`
|
||||||
|
Uncached int `json:"uncached"`
|
||||||
|
ContentType map[string]int `json:"content_type"`
|
||||||
|
Country map[string]int `json:"country"`
|
||||||
|
SSL struct {
|
||||||
|
Encrypted int `json:"encrypted"`
|
||||||
|
Unencrypted int `json:"unencrypted"`
|
||||||
|
} `json:"ssl"`
|
||||||
|
} `json:"bandwidth"`
|
||||||
|
Threats struct {
|
||||||
|
All int `json:"all"`
|
||||||
|
Country map[string]int `json:"country"`
|
||||||
|
Type map[string]int `json:"type"`
|
||||||
|
} `json:"threats"`
|
||||||
|
Pageviews struct {
|
||||||
|
All int `json:"all"`
|
||||||
|
SearchEngines map[string]int `json:"search_engines"`
|
||||||
|
} `json:"pageviews"`
|
||||||
|
Uniques struct {
|
||||||
|
All int `json:"all"`
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ZoneAnalyticsOptions represents the optional parameters in Zone Analytics
|
||||||
|
// endpoint requests.
|
||||||
|
type ZoneAnalyticsOptions struct {
|
||||||
|
Since *time.Time
|
||||||
|
Until *time.Time
|
||||||
|
Continuous *bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// PurgeCacheRequest represents the request format made to the purge endpoint.
|
||||||
|
type PurgeCacheRequest struct {
|
||||||
|
Everything bool `json:"purge_everything,omitempty"`
|
||||||
|
// Purge by filepath (exact match). Limit of 30
|
||||||
|
Files []string `json:"files,omitempty"`
|
||||||
|
// Purge by Tag (Enterprise only):
|
||||||
|
// https://support.cloudflare.com/hc/en-us/articles/206596608-How-to-Purge-Cache-Using-Cache-Tags-Enterprise-only-
|
||||||
|
Tags []string `json:"tags,omitempty"`
|
||||||
|
// Purge by hostname - e.g. "assets.example.com"
|
||||||
|
Hosts []string `json:"hosts,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// PurgeCacheResponse represents the response from the purge endpoint.
|
||||||
|
type PurgeCacheResponse struct {
|
||||||
|
Response
|
||||||
|
Result struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
} `json:"result"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// newZone describes a new zone.
|
||||||
|
type newZone struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
JumpStart bool `json:"jump_start"`
|
||||||
|
// We use a pointer to get a nil type when the field is empty.
|
||||||
|
// This allows us to completely omit this with json.Marshal().
|
||||||
|
Organization *Organization `json:"organization,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreateZone creates a zone on an account.
|
||||||
|
//
|
||||||
|
// Setting jumpstart to true will attempt to automatically scan for existing
|
||||||
|
// DNS records. Setting this to false will create the zone with no DNS records.
|
||||||
|
//
|
||||||
|
// If Organization is non-empty, it must have at least the ID field populated.
|
||||||
|
// This will add the new zone to the specified multi-user organization.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#zone-create-a-zone
|
||||||
|
func (api *API) CreateZone(name string, jumpstart bool, org Organization) (Zone, error) {
|
||||||
|
var newzone newZone
|
||||||
|
newzone.Name = name
|
||||||
|
newzone.JumpStart = jumpstart
|
||||||
|
if org.ID != "" {
|
||||||
|
newzone.Organization = &org
|
||||||
|
}
|
||||||
|
|
||||||
|
res, err := api.makeRequest("POST", "/zones", newzone)
|
||||||
|
if err != nil {
|
||||||
|
return Zone{}, errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
|
||||||
|
var r ZoneResponse
|
||||||
|
err = json.Unmarshal(res, &r)
|
||||||
|
if err != nil {
|
||||||
|
return Zone{}, errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
return r.Result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ZoneActivationCheck initiates another zone activation check for newly-created zones.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#zone-initiate-another-zone-activation-check
|
||||||
|
func (api *API) ZoneActivationCheck(zoneID string) (Response, error) {
|
||||||
|
res, err := api.makeRequest("PUT", "/zones/"+zoneID+"/activation_check", nil)
|
||||||
|
if err != nil {
|
||||||
|
return Response{}, errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
var r Response
|
||||||
|
err = json.Unmarshal(res, &r)
|
||||||
|
if err != nil {
|
||||||
|
return Response{}, errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
return r, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ListZones lists zones on an account. Optionally takes a list of zone names
|
||||||
|
// to filter against.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#zone-list-zones
|
||||||
|
func (api *API) ListZones(z ...string) ([]Zone, error) {
|
||||||
|
v := url.Values{}
|
||||||
|
var res []byte
|
||||||
|
var r ZonesResponse
|
||||||
|
var zones []Zone
|
||||||
|
var err error
|
||||||
|
if len(z) > 0 {
|
||||||
|
for _, zone := range z {
|
||||||
|
v.Set("name", zone)
|
||||||
|
res, err = api.makeRequest("GET", "/zones?"+v.Encode(), nil)
|
||||||
|
if err != nil {
|
||||||
|
return []Zone{}, errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
err = json.Unmarshal(res, &r)
|
||||||
|
if err != nil {
|
||||||
|
return []Zone{}, errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
if !r.Success {
|
||||||
|
// TODO: Provide an actual error message instead of always returning nil
|
||||||
|
return []Zone{}, err
|
||||||
|
}
|
||||||
|
for zi := range r.Result {
|
||||||
|
zones = append(zones, r.Result[zi])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// TODO: Paginate here. We only grab the first page of results.
|
||||||
|
// Could do this concurrently after the first request by creating a
|
||||||
|
// sync.WaitGroup or just a channel + workers.
|
||||||
|
res, err = api.makeRequest("GET", "/zones", nil)
|
||||||
|
if err != nil {
|
||||||
|
return []Zone{}, errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
err = json.Unmarshal(res, &r)
|
||||||
|
if err != nil {
|
||||||
|
return []Zone{}, errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
zones = r.Result
|
||||||
|
}
|
||||||
|
|
||||||
|
return zones, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ZoneDetails fetches information about a zone.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#zone-zone-details
|
||||||
|
func (api *API) ZoneDetails(zoneID string) (Zone, error) {
|
||||||
|
res, err := api.makeRequest("GET", "/zones/"+zoneID, nil)
|
||||||
|
if err != nil {
|
||||||
|
return Zone{}, errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
var r ZoneResponse
|
||||||
|
err = json.Unmarshal(res, &r)
|
||||||
|
if err != nil {
|
||||||
|
return Zone{}, errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
return r.Result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ZoneOptions is a subset of Zone, for editable options.
|
||||||
|
type ZoneOptions struct {
|
||||||
|
Paused *bool `json:"paused,omitempty"`
|
||||||
|
VanityNS []string `json:"vanity_name_servers,omitempty"`
|
||||||
|
Plan *ZoneRatePlan `json:"plan,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// ZoneSetPaused pauses Cloudflare service for the entire zone, sending all
|
||||||
|
// traffic direct to the origin.
|
||||||
|
func (api *API) ZoneSetPaused(zoneID string, paused bool) (Zone, error) {
|
||||||
|
zoneopts := ZoneOptions{Paused: &paused}
|
||||||
|
zone, err := api.EditZone(zoneID, zoneopts)
|
||||||
|
if err != nil {
|
||||||
|
return Zone{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return zone, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ZoneSetVanityNS sets custom nameservers for the zone.
|
||||||
|
// These names must be within the same zone.
|
||||||
|
func (api *API) ZoneSetVanityNS(zoneID string, ns []string) (Zone, error) {
|
||||||
|
zoneopts := ZoneOptions{VanityNS: ns}
|
||||||
|
zone, err := api.EditZone(zoneID, zoneopts)
|
||||||
|
if err != nil {
|
||||||
|
return Zone{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return zone, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ZoneSetRatePlan changes the zone plan.
|
||||||
|
func (api *API) ZoneSetRatePlan(zoneID string, plan ZoneRatePlan) (Zone, error) {
|
||||||
|
zoneopts := ZoneOptions{Plan: &plan}
|
||||||
|
zone, err := api.EditZone(zoneID, zoneopts)
|
||||||
|
if err != nil {
|
||||||
|
return Zone{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return zone, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// EditZone edits the given zone.
|
||||||
|
//
|
||||||
|
// This is usually called by ZoneSetPaused, ZoneSetVanityNS or ZoneSetPlan.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#zone-edit-zone-properties
|
||||||
|
func (api *API) EditZone(zoneID string, zoneOpts ZoneOptions) (Zone, error) {
|
||||||
|
res, err := api.makeRequest("PATCH", "/zones/"+zoneID, zoneOpts)
|
||||||
|
if err != nil {
|
||||||
|
return Zone{}, errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
var r ZoneResponse
|
||||||
|
err = json.Unmarshal(res, &r)
|
||||||
|
if err != nil {
|
||||||
|
return Zone{}, errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
|
||||||
|
return r.Result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// PurgeEverything purges the cache for the given zone.
|
||||||
|
//
|
||||||
|
// Note: this will substantially increase load on the origin server for that
|
||||||
|
// zone if there is a high cached vs. uncached request ratio.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#zone-purge-all-files
|
||||||
|
func (api *API) PurgeEverything(zoneID string) (PurgeCacheResponse, error) {
|
||||||
|
uri := "/zones/" + zoneID + "/purge_cache"
|
||||||
|
res, err := api.makeRequest("DELETE", uri, PurgeCacheRequest{true, nil, nil, nil})
|
||||||
|
if err != nil {
|
||||||
|
return PurgeCacheResponse{}, errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
var r PurgeCacheResponse
|
||||||
|
err = json.Unmarshal(res, &r)
|
||||||
|
if err != nil {
|
||||||
|
return PurgeCacheResponse{}, errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
return r, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// PurgeCache purges the cache using the given PurgeCacheRequest (zone/url/tag).
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#zone-purge-individual-files-by-url-and-cache-tags
|
||||||
|
func (api *API) PurgeCache(zoneID string, pcr PurgeCacheRequest) (PurgeCacheResponse, error) {
|
||||||
|
uri := "/zones/" + zoneID + "/purge_cache"
|
||||||
|
res, err := api.makeRequest("DELETE", uri, pcr)
|
||||||
|
if err != nil {
|
||||||
|
return PurgeCacheResponse{}, errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
var r PurgeCacheResponse
|
||||||
|
err = json.Unmarshal(res, &r)
|
||||||
|
if err != nil {
|
||||||
|
return PurgeCacheResponse{}, errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
return r, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeleteZone deletes the given zone.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#zone-delete-a-zone
|
||||||
|
func (api *API) DeleteZone(zoneID string) (ZoneID, error) {
|
||||||
|
res, err := api.makeRequest("DELETE", "/zones/"+zoneID, nil)
|
||||||
|
if err != nil {
|
||||||
|
return ZoneID{}, errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
var r ZoneIDResponse
|
||||||
|
err = json.Unmarshal(res, &r)
|
||||||
|
if err != nil {
|
||||||
|
return ZoneID{}, errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
return r.Result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// AvailableZoneRatePlans returns information about all plans available to the specified zone.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#zone-plan-available-plans
|
||||||
|
func (api *API) AvailableZoneRatePlans(zoneID string) ([]ZoneRatePlan, error) {
|
||||||
|
uri := "/zones/" + zoneID + "/available_rate_plans"
|
||||||
|
res, err := api.makeRequest("GET", uri, nil)
|
||||||
|
if err != nil {
|
||||||
|
return []ZoneRatePlan{}, errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
var r AvailableZoneRatePlansResponse
|
||||||
|
err = json.Unmarshal(res, &r)
|
||||||
|
if err != nil {
|
||||||
|
return []ZoneRatePlan{}, errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
return r.Result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// encode encodes non-nil fields into URL encoded form.
|
||||||
|
func (o ZoneAnalyticsOptions) encode() string {
|
||||||
|
v := url.Values{}
|
||||||
|
if o.Since != nil {
|
||||||
|
v.Set("since", (*o.Since).Format(time.RFC3339))
|
||||||
|
}
|
||||||
|
if o.Until != nil {
|
||||||
|
v.Set("until", (*o.Until).Format(time.RFC3339))
|
||||||
|
}
|
||||||
|
if o.Continuous != nil {
|
||||||
|
v.Set("continuous", fmt.Sprintf("%t", *o.Continuous))
|
||||||
|
}
|
||||||
|
return v.Encode()
|
||||||
|
}
|
||||||
|
|
||||||
|
// ZoneAnalyticsDashboard returns zone analytics information.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#zone-analytics-dashboard
|
||||||
|
func (api *API) ZoneAnalyticsDashboard(zoneID string, options ZoneAnalyticsOptions) (ZoneAnalyticsData, error) {
|
||||||
|
uri := "/zones/" + zoneID + "/analytics/dashboard" + "?" + options.encode()
|
||||||
|
res, err := api.makeRequest("GET", uri, nil)
|
||||||
|
if err != nil {
|
||||||
|
return ZoneAnalyticsData{}, errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
var r zoneAnalyticsDataResponse
|
||||||
|
err = json.Unmarshal(res, &r)
|
||||||
|
if err != nil {
|
||||||
|
return ZoneAnalyticsData{}, errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
return r.Result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ZoneAnalyticsByColocation returns zone analytics information by datacenter.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#zone-analytics-analytics-by-co-locations
|
||||||
|
func (api *API) ZoneAnalyticsByColocation(zoneID string, options ZoneAnalyticsOptions) ([]ZoneAnalyticsColocation, error) {
|
||||||
|
uri := "/zones/" + zoneID + "/analytics/colos" + "?" + options.encode()
|
||||||
|
res, err := api.makeRequest("GET", uri, nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
var r zoneAnalyticsColocationResponse
|
||||||
|
err = json.Unmarshal(res, &r)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
return r.Result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ZoneSettings returns all of the settings for a given zone.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#zone-settings-get-all-zone-settings
|
||||||
|
func (api *API) ZoneSettings(zoneID string) (*ZoneSettingResponse, error) {
|
||||||
|
uri := "/zones/" + zoneID + "/settings"
|
||||||
|
res, err := api.makeRequest("GET", uri, nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
|
||||||
|
response := &ZoneSettingResponse{}
|
||||||
|
err = json.Unmarshal(res, &response)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
|
||||||
|
return response, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateZoneSettings updates the settings for a given zone.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#zone-settings-edit-zone-settings-info
|
||||||
|
func (api *API) UpdateZoneSettings(zoneID string, settings []ZoneSetting) (*ZoneSettingResponse, error) {
|
||||||
|
uri := "/zones/" + zoneID + "/settings"
|
||||||
|
res, err := api.makeRequest("PATCH", uri, struct {
|
||||||
|
Items []ZoneSetting `json:"items"`
|
||||||
|
}{settings})
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
|
||||||
|
response := &ZoneSettingResponse{}
|
||||||
|
err = json.Unmarshal(res, &response)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
|
||||||
|
return response, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ZoneSSLSettings returns information about SSL setting to the specified zone.
|
||||||
|
//
|
||||||
|
// API reference: https://api.cloudflare.com/#zone-settings-get-ssl-setting
|
||||||
|
func (api *API) ZoneSSLSettings(zoneID string) (ZoneSSLSetting, error) {
|
||||||
|
uri := "/zones/" + zoneID + "/settings/ssl"
|
||||||
|
res, err := api.makeRequest("GET", uri, nil)
|
||||||
|
if err != nil {
|
||||||
|
return ZoneSSLSetting{}, errors.Wrap(err, errMakeRequestError)
|
||||||
|
}
|
||||||
|
var r ZoneSSLSettingResponse
|
||||||
|
err = json.Unmarshal(res, &r)
|
||||||
|
if err != nil {
|
||||||
|
return ZoneSSLSetting{}, errors.Wrap(err, errUnmarshalError)
|
||||||
|
}
|
||||||
|
return r.Result, nil
|
||||||
|
}
|
25
vendor/github.com/containous/flaeg/flaeg.go
generated
vendored
25
vendor/github.com/containous/flaeg/flaeg.go
generated
vendored
|
@ -396,6 +396,7 @@ type Command struct {
|
||||||
DefaultPointersConfig interface{} // TODO: case DefaultPointersConfig is nil
|
DefaultPointersConfig interface{} // TODO: case DefaultPointersConfig is nil
|
||||||
Run func() error
|
Run func() error
|
||||||
Metadata map[string]string
|
Metadata map[string]string
|
||||||
|
HideHelp bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// LoadWithCommand initializes config : struct fields given by reference, with args : arguments.
|
// LoadWithCommand initializes config : struct fields given by reference, with args : arguments.
|
||||||
|
@ -437,13 +438,16 @@ func PrintHelpWithCommand(flagMap map[string]reflect.StructField, defaultValMap
|
||||||
// Using POSXE STD : http://pubs.opengroup.org/onlinepubs/9699919799/
|
// Using POSXE STD : http://pubs.opengroup.org/onlinepubs/9699919799/
|
||||||
const helper = `{{if .ProgDescription}}{{.ProgDescription}}
|
const helper = `{{if .ProgDescription}}{{.ProgDescription}}
|
||||||
|
|
||||||
{{end}}Usage: {{.ProgName}} [--flag=flag_argument] [-f[flag_argument]] ... set flag_argument to flag(s)
|
{{end}}Usage: {{.ProgName}} [flags] <command> [<arguments>]
|
||||||
or: {{.ProgName}} [--flag[=true|false| ]] [-f[true|false| ]] ... set true/false to boolean flag(s)
|
|
||||||
|
Use "{{.ProgName}} <command> --help" for help on any command.
|
||||||
{{if .SubCommands}}
|
{{if .SubCommands}}
|
||||||
Available Commands:{{range $subCmdName, $subCmdDesc := .SubCommands}}
|
Commands:{{range $subCmdName, $subCmdDesc := .SubCommands}}
|
||||||
{{printf "\t%-50s %s" $subCmdName $subCmdDesc}}{{end}}
|
{{printf "\t%-50s %s" $subCmdName $subCmdDesc}}{{end}}
|
||||||
Use "{{.ProgName}} [command] --help" for more information about a command.
|
|
||||||
{{end}}
|
{{end}}
|
||||||
|
Flag's usage: {{.ProgName}} [--flag=flag_argument] [-f[flag_argument]] ... set flag_argument to flag(s)
|
||||||
|
or: {{.ProgName}} [--flag[=true|false| ]] [-f[true|false| ]] ... set true/false to boolean flag(s)
|
||||||
|
|
||||||
Flags:
|
Flags:
|
||||||
`
|
`
|
||||||
// Use a struct to give data to template
|
// Use a struct to give data to template
|
||||||
|
@ -453,13 +457,15 @@ Flags:
|
||||||
SubCommands map[string]string
|
SubCommands map[string]string
|
||||||
}
|
}
|
||||||
tempStruct := TempStruct{}
|
tempStruct := TempStruct{}
|
||||||
if cmd != nil {
|
if cmd != nil && !cmd.HideHelp {
|
||||||
tempStruct.ProgName = cmd.Name
|
tempStruct.ProgName = cmd.Name
|
||||||
tempStruct.ProgDescription = cmd.Description
|
tempStruct.ProgDescription = cmd.Description
|
||||||
tempStruct.SubCommands = map[string]string{}
|
tempStruct.SubCommands = map[string]string{}
|
||||||
if len(subCmd) > 1 && cmd == subCmd[0] {
|
if len(subCmd) > 1 && cmd == subCmd[0] {
|
||||||
for _, c := range subCmd[1:] {
|
for _, c := range subCmd[1:] {
|
||||||
tempStruct.SubCommands[c.Name] = c.Description
|
if !c.HideHelp {
|
||||||
|
tempStruct.SubCommands[c.Name] = c.Description
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -528,7 +534,7 @@ func printFlagsDescriptionsDefaultValues(flagMap map[string]reflect.StructField,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//add help flag
|
// add help flag
|
||||||
shortFlagsWithDash = append(shortFlagsWithDash, "-h,")
|
shortFlagsWithDash = append(shortFlagsWithDash, "-h,")
|
||||||
flagsWithDash = append(flagsWithDash, "--help")
|
flagsWithDash = append(flagsWithDash, "--help")
|
||||||
descriptions = append(descriptions, "Print Help (this message) and exit")
|
descriptions = append(descriptions, "Print Help (this message) and exit")
|
||||||
|
@ -536,6 +542,7 @@ func printFlagsDescriptionsDefaultValues(flagMap map[string]reflect.StructField,
|
||||||
|
|
||||||
return displayTab(output, shortFlagsWithDash, flagsWithDash, descriptions, defaultValues)
|
return displayTab(output, shortFlagsWithDash, flagsWithDash, descriptions, defaultValues)
|
||||||
}
|
}
|
||||||
|
|
||||||
func split(str string, width int) []string {
|
func split(str string, width int) []string {
|
||||||
if len(str) > width {
|
if len(str) > width {
|
||||||
index := strings.LastIndex(str[:width], " ")
|
index := strings.LastIndex(str[:width], " ")
|
||||||
|
@ -584,7 +591,7 @@ func PrintErrorWithCommand(err error, flagMap map[string]reflect.StructField, de
|
||||||
// a map of custom parsers could be use
|
// a map of custom parsers could be use
|
||||||
type Flaeg struct {
|
type Flaeg struct {
|
||||||
calledCommand *Command
|
calledCommand *Command
|
||||||
commands []*Command ///rootCommand is th fist one in this slice
|
commands []*Command // rootCommand is th fist one in this slice
|
||||||
args []string
|
args []string
|
||||||
commandArgs []string
|
commandArgs []string
|
||||||
customParsers map[reflect.Type]parse.Parser
|
customParsers map[reflect.Type]parse.Parser
|
||||||
|
@ -654,7 +661,7 @@ func (f *Flaeg) findCommandWithCommandArgs() (*Command, []string, error) {
|
||||||
commandName, f.commandArgs = splitArgs(f.args)
|
commandName, f.commandArgs = splitArgs(f.args)
|
||||||
if len(commandName) > 0 {
|
if len(commandName) > 0 {
|
||||||
for _, command := range f.commands {
|
for _, command := range f.commands {
|
||||||
if commandName == command.Name {
|
if commandName == command.Name && !command.HideHelp {
|
||||||
f.calledCommand = command
|
f.calledCommand = command
|
||||||
return f.calledCommand, f.commandArgs, nil
|
return f.calledCommand, f.commandArgs, nil
|
||||||
}
|
}
|
||||||
|
|
21
vendor/github.com/go-resty/resty/LICENSE
generated
vendored
Normal file
21
vendor/github.com/go-resty/resty/LICENSE
generated
vendored
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
The MIT License (MIT)
|
||||||
|
|
||||||
|
Copyright (c) 2015-2018 Jeevanandam M., https://myjeeva.com <jeeva@myjeeva.com>
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
900
vendor/github.com/go-resty/resty/client.go
generated
vendored
Normal file
900
vendor/github.com/go-resty/resty/client.go
generated
vendored
Normal file
|
@ -0,0 +1,900 @@
|
||||||
|
// Copyright (c) 2015-2018 Jeevanandam M (jeeva@myjeeva.com), All rights reserved.
|
||||||
|
// resty source code and usage is governed by a MIT style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package resty
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"compress/gzip"
|
||||||
|
"crypto/tls"
|
||||||
|
"crypto/x509"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"reflect"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// MethodGet HTTP method
|
||||||
|
MethodGet = "GET"
|
||||||
|
|
||||||
|
// MethodPost HTTP method
|
||||||
|
MethodPost = "POST"
|
||||||
|
|
||||||
|
// MethodPut HTTP method
|
||||||
|
MethodPut = "PUT"
|
||||||
|
|
||||||
|
// MethodDelete HTTP method
|
||||||
|
MethodDelete = "DELETE"
|
||||||
|
|
||||||
|
// MethodPatch HTTP method
|
||||||
|
MethodPatch = "PATCH"
|
||||||
|
|
||||||
|
// MethodHead HTTP method
|
||||||
|
MethodHead = "HEAD"
|
||||||
|
|
||||||
|
// MethodOptions HTTP method
|
||||||
|
MethodOptions = "OPTIONS"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
hdrUserAgentKey = http.CanonicalHeaderKey("User-Agent")
|
||||||
|
hdrAcceptKey = http.CanonicalHeaderKey("Accept")
|
||||||
|
hdrContentTypeKey = http.CanonicalHeaderKey("Content-Type")
|
||||||
|
hdrContentLengthKey = http.CanonicalHeaderKey("Content-Length")
|
||||||
|
hdrContentEncodingKey = http.CanonicalHeaderKey("Content-Encoding")
|
||||||
|
hdrAuthorizationKey = http.CanonicalHeaderKey("Authorization")
|
||||||
|
|
||||||
|
plainTextType = "text/plain; charset=utf-8"
|
||||||
|
jsonContentType = "application/json; charset=utf-8"
|
||||||
|
formContentType = "application/x-www-form-urlencoded"
|
||||||
|
|
||||||
|
jsonCheck = regexp.MustCompile(`(?i:(application|text)/(json|.*\+json)(;|$))`)
|
||||||
|
xmlCheck = regexp.MustCompile(`(?i:(application|text)/(xml|.*\+xml)(;|$))`)
|
||||||
|
|
||||||
|
hdrUserAgentValue = "go-resty/%s (https://github.com/go-resty/resty)"
|
||||||
|
bufPool = &sync.Pool{New: func() interface{} { return &bytes.Buffer{} }}
|
||||||
|
)
|
||||||
|
|
||||||
|
// Client type is used for HTTP/RESTful global values
|
||||||
|
// for all request raised from the client
|
||||||
|
type Client struct {
|
||||||
|
HostURL string
|
||||||
|
QueryParam url.Values
|
||||||
|
FormData url.Values
|
||||||
|
Header http.Header
|
||||||
|
UserInfo *User
|
||||||
|
Token string
|
||||||
|
Cookies []*http.Cookie
|
||||||
|
Error reflect.Type
|
||||||
|
Debug bool
|
||||||
|
DisableWarn bool
|
||||||
|
AllowGetMethodPayload bool
|
||||||
|
Log *log.Logger
|
||||||
|
RetryCount int
|
||||||
|
RetryWaitTime time.Duration
|
||||||
|
RetryMaxWaitTime time.Duration
|
||||||
|
RetryConditions []RetryConditionFunc
|
||||||
|
JSONMarshal func(v interface{}) ([]byte, error)
|
||||||
|
JSONUnmarshal func(data []byte, v interface{}) error
|
||||||
|
|
||||||
|
jsonEscapeHTML bool
|
||||||
|
httpClient *http.Client
|
||||||
|
setContentLength bool
|
||||||
|
isHTTPMode bool
|
||||||
|
outputDirectory string
|
||||||
|
scheme string
|
||||||
|
proxyURL *url.URL
|
||||||
|
closeConnection bool
|
||||||
|
notParseResponse bool
|
||||||
|
debugBodySizeLimit int64
|
||||||
|
logPrefix string
|
||||||
|
pathParams map[string]string
|
||||||
|
beforeRequest []func(*Client, *Request) error
|
||||||
|
udBeforeRequest []func(*Client, *Request) error
|
||||||
|
preReqHook func(*Client, *Request) error
|
||||||
|
afterResponse []func(*Client, *Response) error
|
||||||
|
}
|
||||||
|
|
||||||
|
// User type is to hold an username and password information
|
||||||
|
type User struct {
|
||||||
|
Username, Password string
|
||||||
|
}
|
||||||
|
|
||||||
|
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
|
||||||
|
// Client methods
|
||||||
|
//___________________________________
|
||||||
|
|
||||||
|
// SetHostURL method is to set Host URL in the client instance. It will be used with request
|
||||||
|
// raised from this client with relative URL
|
||||||
|
// // Setting HTTP address
|
||||||
|
// resty.SetHostURL("http://myjeeva.com")
|
||||||
|
//
|
||||||
|
// // Setting HTTPS address
|
||||||
|
// resty.SetHostURL("https://myjeeva.com")
|
||||||
|
//
|
||||||
|
func (c *Client) SetHostURL(url string) *Client {
|
||||||
|
c.HostURL = strings.TrimRight(url, "/")
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetHeader method sets a single header field and its value in the client instance.
|
||||||
|
// These headers will be applied to all requests raised from this client instance.
|
||||||
|
// Also it can be overridden at request level header options, see `resty.R().SetHeader`
|
||||||
|
// or `resty.R().SetHeaders`.
|
||||||
|
//
|
||||||
|
// Example: To set `Content-Type` and `Accept` as `application/json`
|
||||||
|
//
|
||||||
|
// resty.
|
||||||
|
// SetHeader("Content-Type", "application/json").
|
||||||
|
// SetHeader("Accept", "application/json")
|
||||||
|
//
|
||||||
|
func (c *Client) SetHeader(header, value string) *Client {
|
||||||
|
c.Header.Set(header, value)
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetHeaders method sets multiple headers field and its values at one go in the client instance.
|
||||||
|
// These headers will be applied to all requests raised from this client instance. Also it can be
|
||||||
|
// overridden at request level headers options, see `resty.R().SetHeaders` or `resty.R().SetHeader`.
|
||||||
|
//
|
||||||
|
// Example: To set `Content-Type` and `Accept` as `application/json`
|
||||||
|
//
|
||||||
|
// resty.SetHeaders(map[string]string{
|
||||||
|
// "Content-Type": "application/json",
|
||||||
|
// "Accept": "application/json",
|
||||||
|
// })
|
||||||
|
//
|
||||||
|
func (c *Client) SetHeaders(headers map[string]string) *Client {
|
||||||
|
for h, v := range headers {
|
||||||
|
c.Header.Set(h, v)
|
||||||
|
}
|
||||||
|
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetCookieJar method sets custom http.CookieJar in the resty client. Its way to override default.
|
||||||
|
// Example: sometimes we don't want to save cookies in api contacting, we can remove the default
|
||||||
|
// CookieJar in resty client.
|
||||||
|
//
|
||||||
|
// resty.SetCookieJar(nil)
|
||||||
|
//
|
||||||
|
func (c *Client) SetCookieJar(jar http.CookieJar) *Client {
|
||||||
|
c.httpClient.Jar = jar
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetCookie method appends a single cookie in the client instance.
|
||||||
|
// These cookies will be added to all the request raised from this client instance.
|
||||||
|
// resty.SetCookie(&http.Cookie{
|
||||||
|
// Name:"go-resty",
|
||||||
|
// Value:"This is cookie value",
|
||||||
|
// Path: "/",
|
||||||
|
// Domain: "sample.com",
|
||||||
|
// MaxAge: 36000,
|
||||||
|
// HttpOnly: true,
|
||||||
|
// Secure: false,
|
||||||
|
// })
|
||||||
|
//
|
||||||
|
func (c *Client) SetCookie(hc *http.Cookie) *Client {
|
||||||
|
c.Cookies = append(c.Cookies, hc)
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetCookies method sets an array of cookies in the client instance.
|
||||||
|
// These cookies will be added to all the request raised from this client instance.
|
||||||
|
// cookies := make([]*http.Cookie, 0)
|
||||||
|
//
|
||||||
|
// cookies = append(cookies, &http.Cookie{
|
||||||
|
// Name:"go-resty-1",
|
||||||
|
// Value:"This is cookie 1 value",
|
||||||
|
// Path: "/",
|
||||||
|
// Domain: "sample.com",
|
||||||
|
// MaxAge: 36000,
|
||||||
|
// HttpOnly: true,
|
||||||
|
// Secure: false,
|
||||||
|
// })
|
||||||
|
//
|
||||||
|
// cookies = append(cookies, &http.Cookie{
|
||||||
|
// Name:"go-resty-2",
|
||||||
|
// Value:"This is cookie 2 value",
|
||||||
|
// Path: "/",
|
||||||
|
// Domain: "sample.com",
|
||||||
|
// MaxAge: 36000,
|
||||||
|
// HttpOnly: true,
|
||||||
|
// Secure: false,
|
||||||
|
// })
|
||||||
|
//
|
||||||
|
// // Setting a cookies into resty
|
||||||
|
// resty.SetCookies(cookies)
|
||||||
|
//
|
||||||
|
func (c *Client) SetCookies(cs []*http.Cookie) *Client {
|
||||||
|
c.Cookies = append(c.Cookies, cs...)
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetQueryParam method sets single parameter and its value in the client instance.
|
||||||
|
// It will be formed as query string for the request. For example: `search=kitchen%20papers&size=large`
|
||||||
|
// in the URL after `?` mark. These query params will be added to all the request raised from
|
||||||
|
// this client instance. Also it can be overridden at request level Query Param options,
|
||||||
|
// see `resty.R().SetQueryParam` or `resty.R().SetQueryParams`.
|
||||||
|
// resty.
|
||||||
|
// SetQueryParam("search", "kitchen papers").
|
||||||
|
// SetQueryParam("size", "large")
|
||||||
|
//
|
||||||
|
func (c *Client) SetQueryParam(param, value string) *Client {
|
||||||
|
c.QueryParam.Set(param, value)
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetQueryParams method sets multiple parameters and their values at one go in the client instance.
|
||||||
|
// It will be formed as query string for the request. For example: `search=kitchen%20papers&size=large`
|
||||||
|
// in the URL after `?` mark. These query params will be added to all the request raised from this
|
||||||
|
// client instance. Also it can be overridden at request level Query Param options,
|
||||||
|
// see `resty.R().SetQueryParams` or `resty.R().SetQueryParam`.
|
||||||
|
// resty.SetQueryParams(map[string]string{
|
||||||
|
// "search": "kitchen papers",
|
||||||
|
// "size": "large",
|
||||||
|
// })
|
||||||
|
//
|
||||||
|
func (c *Client) SetQueryParams(params map[string]string) *Client {
|
||||||
|
for p, v := range params {
|
||||||
|
c.SetQueryParam(p, v)
|
||||||
|
}
|
||||||
|
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetFormData method sets Form parameters and their values in the client instance.
|
||||||
|
// It's applicable only HTTP method `POST` and `PUT` and requets content type would be set as
|
||||||
|
// `application/x-www-form-urlencoded`. These form data will be added to all the request raised from
|
||||||
|
// this client instance. Also it can be overridden at request level form data, see `resty.R().SetFormData`.
|
||||||
|
// resty.SetFormData(map[string]string{
|
||||||
|
// "access_token": "BC594900-518B-4F7E-AC75-BD37F019E08F",
|
||||||
|
// "user_id": "3455454545",
|
||||||
|
// })
|
||||||
|
//
|
||||||
|
func (c *Client) SetFormData(data map[string]string) *Client {
|
||||||
|
for k, v := range data {
|
||||||
|
c.FormData.Set(k, v)
|
||||||
|
}
|
||||||
|
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetBasicAuth method sets the basic authentication header in the HTTP request. Example:
|
||||||
|
// Authorization: Basic <base64-encoded-value>
|
||||||
|
//
|
||||||
|
// Example: To set the header for username "go-resty" and password "welcome"
|
||||||
|
// resty.SetBasicAuth("go-resty", "welcome")
|
||||||
|
//
|
||||||
|
// This basic auth information gets added to all the request rasied from this client instance.
|
||||||
|
// Also it can be overridden or set one at the request level is supported, see `resty.R().SetBasicAuth`.
|
||||||
|
//
|
||||||
|
func (c *Client) SetBasicAuth(username, password string) *Client {
|
||||||
|
c.UserInfo = &User{Username: username, Password: password}
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetAuthToken method sets bearer auth token header in the HTTP request. Example:
|
||||||
|
// Authorization: Bearer <auth-token-value-comes-here>
|
||||||
|
//
|
||||||
|
// Example: To set auth token BC594900518B4F7EAC75BD37F019E08FBC594900518B4F7EAC75BD37F019E08F
|
||||||
|
//
|
||||||
|
// resty.SetAuthToken("BC594900518B4F7EAC75BD37F019E08FBC594900518B4F7EAC75BD37F019E08F")
|
||||||
|
//
|
||||||
|
// This bearer auth token gets added to all the request rasied from this client instance.
|
||||||
|
// Also it can be overridden or set one at the request level is supported, see `resty.R().SetAuthToken`.
|
||||||
|
//
|
||||||
|
func (c *Client) SetAuthToken(token string) *Client {
|
||||||
|
c.Token = token
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
// R method creates a request instance, its used for Get, Post, Put, Delete, Patch, Head and Options.
|
||||||
|
func (c *Client) R() *Request {
|
||||||
|
r := &Request{
|
||||||
|
QueryParam: url.Values{},
|
||||||
|
FormData: url.Values{},
|
||||||
|
Header: http.Header{},
|
||||||
|
|
||||||
|
client: c,
|
||||||
|
multipartFiles: []*File{},
|
||||||
|
multipartFields: []*multipartField{},
|
||||||
|
pathParams: map[string]string{},
|
||||||
|
jsonEscapeHTML: true,
|
||||||
|
}
|
||||||
|
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewRequest is an alias for R(). Creates a request instance, its used for
|
||||||
|
// Get, Post, Put, Delete, Patch, Head and Options.
|
||||||
|
func (c *Client) NewRequest() *Request {
|
||||||
|
return c.R()
|
||||||
|
}
|
||||||
|
|
||||||
|
// OnBeforeRequest method appends request middleware into the before request chain.
|
||||||
|
// Its gets applied after default `go-resty` request middlewares and before request
|
||||||
|
// been sent from `go-resty` to host server.
|
||||||
|
// resty.OnBeforeRequest(func(c *resty.Client, r *resty.Request) error {
|
||||||
|
// // Now you have access to Client and Request instance
|
||||||
|
// // manipulate it as per your need
|
||||||
|
//
|
||||||
|
// return nil // if its success otherwise return error
|
||||||
|
// })
|
||||||
|
//
|
||||||
|
func (c *Client) OnBeforeRequest(m func(*Client, *Request) error) *Client {
|
||||||
|
c.udBeforeRequest = append(c.udBeforeRequest, m)
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
// OnAfterResponse method appends response middleware into the after response chain.
|
||||||
|
// Once we receive response from host server, default `go-resty` response middleware
|
||||||
|
// gets applied and then user assigened response middlewares applied.
|
||||||
|
// resty.OnAfterResponse(func(c *resty.Client, r *resty.Response) error {
|
||||||
|
// // Now you have access to Client and Response instance
|
||||||
|
// // manipulate it as per your need
|
||||||
|
//
|
||||||
|
// return nil // if its success otherwise return error
|
||||||
|
// })
|
||||||
|
//
|
||||||
|
func (c *Client) OnAfterResponse(m func(*Client, *Response) error) *Client {
|
||||||
|
c.afterResponse = append(c.afterResponse, m)
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetPreRequestHook method sets the given pre-request function into resty client.
|
||||||
|
// It is called right before the request is fired.
|
||||||
|
//
|
||||||
|
// Note: Only one pre-request hook can be registered. Use `resty.OnBeforeRequest` for mutilple.
|
||||||
|
func (c *Client) SetPreRequestHook(h func(*Client, *Request) error) *Client {
|
||||||
|
if c.preReqHook != nil {
|
||||||
|
c.Log.Printf("Overwriting an existing pre-request hook: %s", functionName(h))
|
||||||
|
}
|
||||||
|
c.preReqHook = h
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetDebug method enables the debug mode on `go-resty` client. Client logs details of every request and response.
|
||||||
|
// For `Request` it logs information such as HTTP verb, Relative URL path, Host, Headers, Body if it has one.
|
||||||
|
// For `Response` it logs information such as Status, Response Time, Headers, Body if it has one.
|
||||||
|
// resty.SetDebug(true)
|
||||||
|
//
|
||||||
|
func (c *Client) SetDebug(d bool) *Client {
|
||||||
|
c.Debug = d
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetDebugBodyLimit sets the maximum size for which the response body will be logged in debug mode.
|
||||||
|
// resty.SetDebugBodyLimit(1000000)
|
||||||
|
//
|
||||||
|
func (c *Client) SetDebugBodyLimit(sl int64) *Client {
|
||||||
|
c.debugBodySizeLimit = sl
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetDisableWarn method disables the warning message on `go-resty` client.
|
||||||
|
// For example: go-resty warns the user when BasicAuth used on HTTP mode.
|
||||||
|
// resty.SetDisableWarn(true)
|
||||||
|
//
|
||||||
|
func (c *Client) SetDisableWarn(d bool) *Client {
|
||||||
|
c.DisableWarn = d
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetAllowGetMethodPayload method allows the GET method with payload on `go-resty` client.
|
||||||
|
// For example: go-resty allows the user sends request with a payload on HTTP GET method.
|
||||||
|
// resty.SetAllowGetMethodPayload(true)
|
||||||
|
//
|
||||||
|
func (c *Client) SetAllowGetMethodPayload(a bool) *Client {
|
||||||
|
c.AllowGetMethodPayload = a
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetLogger method sets given writer for logging go-resty request and response details.
|
||||||
|
// Default is os.Stderr
|
||||||
|
// file, _ := os.OpenFile("/Users/jeeva/go-resty.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
|
||||||
|
//
|
||||||
|
// resty.SetLogger(file)
|
||||||
|
//
|
||||||
|
func (c *Client) SetLogger(w io.Writer) *Client {
|
||||||
|
c.Log = getLogger(w)
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetContentLength method enables the HTTP header `Content-Length` value for every request.
|
||||||
|
// By default go-resty won't set `Content-Length`.
|
||||||
|
// resty.SetContentLength(true)
|
||||||
|
//
|
||||||
|
// Also you have an option to enable for particular request. See `resty.R().SetContentLength`
|
||||||
|
//
|
||||||
|
func (c *Client) SetContentLength(l bool) *Client {
|
||||||
|
c.setContentLength = l
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetTimeout method sets timeout for request raised from client.
|
||||||
|
// resty.SetTimeout(time.Duration(1 * time.Minute))
|
||||||
|
//
|
||||||
|
func (c *Client) SetTimeout(timeout time.Duration) *Client {
|
||||||
|
c.httpClient.Timeout = timeout
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetError method is to register the global or client common `Error` object into go-resty.
|
||||||
|
// It is used for automatic unmarshalling if response status code is greater than 399 and
|
||||||
|
// content type either JSON or XML. Can be pointer or non-pointer.
|
||||||
|
// resty.SetError(&Error{})
|
||||||
|
// // OR
|
||||||
|
// resty.SetError(Error{})
|
||||||
|
//
|
||||||
|
func (c *Client) SetError(err interface{}) *Client {
|
||||||
|
c.Error = typeOf(err)
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetRedirectPolicy method sets the client redirect poilicy. go-resty provides ready to use
|
||||||
|
// redirect policies. Wanna create one for yourself refer `redirect.go`.
|
||||||
|
//
|
||||||
|
// resty.SetRedirectPolicy(FlexibleRedirectPolicy(20))
|
||||||
|
//
|
||||||
|
// // Need multiple redirect policies together
|
||||||
|
// resty.SetRedirectPolicy(FlexibleRedirectPolicy(20), DomainCheckRedirectPolicy("host1.com", "host2.net"))
|
||||||
|
//
|
||||||
|
func (c *Client) SetRedirectPolicy(policies ...interface{}) *Client {
|
||||||
|
for _, p := range policies {
|
||||||
|
if _, ok := p.(RedirectPolicy); !ok {
|
||||||
|
c.Log.Printf("ERORR: %v does not implement resty.RedirectPolicy (missing Apply method)",
|
||||||
|
functionName(p))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
c.httpClient.CheckRedirect = func(req *http.Request, via []*http.Request) error {
|
||||||
|
for _, p := range policies {
|
||||||
|
if err := p.(RedirectPolicy).Apply(req, via); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil // looks good, go ahead
|
||||||
|
}
|
||||||
|
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetRetryCount method enables retry on `go-resty` client and allows you
|
||||||
|
// to set no. of retry count. Resty uses a Backoff mechanism.
|
||||||
|
func (c *Client) SetRetryCount(count int) *Client {
|
||||||
|
c.RetryCount = count
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetRetryWaitTime method sets default wait time to sleep before retrying
|
||||||
|
// request.
|
||||||
|
// Default is 100 milliseconds.
|
||||||
|
func (c *Client) SetRetryWaitTime(waitTime time.Duration) *Client {
|
||||||
|
c.RetryWaitTime = waitTime
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetRetryMaxWaitTime method sets max wait time to sleep before retrying
|
||||||
|
// request.
|
||||||
|
// Default is 2 seconds.
|
||||||
|
func (c *Client) SetRetryMaxWaitTime(maxWaitTime time.Duration) *Client {
|
||||||
|
c.RetryMaxWaitTime = maxWaitTime
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
// AddRetryCondition method adds a retry condition function to array of functions
|
||||||
|
// that are checked to determine if the request is retried. The request will
|
||||||
|
// retry if any of the functions return true and error is nil.
|
||||||
|
func (c *Client) AddRetryCondition(condition RetryConditionFunc) *Client {
|
||||||
|
c.RetryConditions = append(c.RetryConditions, condition)
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetHTTPMode method sets go-resty mode to 'http'
|
||||||
|
func (c *Client) SetHTTPMode() *Client {
|
||||||
|
return c.SetMode("http")
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetRESTMode method sets go-resty mode to 'rest'
|
||||||
|
func (c *Client) SetRESTMode() *Client {
|
||||||
|
return c.SetMode("rest")
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetMode method sets go-resty client mode to given value such as 'http' & 'rest'.
|
||||||
|
// 'rest':
|
||||||
|
// - No Redirect
|
||||||
|
// - Automatic response unmarshal if it is JSON or XML
|
||||||
|
// 'http':
|
||||||
|
// - Up to 10 Redirects
|
||||||
|
// - No automatic unmarshall. Response will be treated as `response.String()`
|
||||||
|
//
|
||||||
|
// If you want more redirects, use FlexibleRedirectPolicy
|
||||||
|
// resty.SetRedirectPolicy(FlexibleRedirectPolicy(20))
|
||||||
|
//
|
||||||
|
func (c *Client) SetMode(mode string) *Client {
|
||||||
|
// HTTP
|
||||||
|
if mode == "http" {
|
||||||
|
c.isHTTPMode = true
|
||||||
|
c.SetRedirectPolicy(FlexibleRedirectPolicy(10))
|
||||||
|
c.afterResponse = []func(*Client, *Response) error{
|
||||||
|
responseLogger,
|
||||||
|
saveResponseIntoFile,
|
||||||
|
}
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
// RESTful
|
||||||
|
c.isHTTPMode = false
|
||||||
|
c.SetRedirectPolicy(NoRedirectPolicy())
|
||||||
|
c.afterResponse = []func(*Client, *Response) error{
|
||||||
|
responseLogger,
|
||||||
|
parseResponseBody,
|
||||||
|
saveResponseIntoFile,
|
||||||
|
}
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
// Mode method returns the current client mode. Typically its a "http" or "rest".
|
||||||
|
// Default is "rest"
|
||||||
|
func (c *Client) Mode() string {
|
||||||
|
if c.isHTTPMode {
|
||||||
|
return "http"
|
||||||
|
}
|
||||||
|
return "rest"
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetTLSClientConfig method sets TLSClientConfig for underling client Transport.
|
||||||
|
//
|
||||||
|
// Example:
|
||||||
|
// // One can set custom root-certificate. Refer: http://golang.org/pkg/crypto/tls/#example_Dial
|
||||||
|
// resty.SetTLSClientConfig(&tls.Config{ RootCAs: roots })
|
||||||
|
//
|
||||||
|
// // or One can disable security check (https)
|
||||||
|
// resty.SetTLSClientConfig(&tls.Config{ InsecureSkipVerify: true })
|
||||||
|
// Note: This method overwrites existing `TLSClientConfig`.
|
||||||
|
//
|
||||||
|
func (c *Client) SetTLSClientConfig(config *tls.Config) *Client {
|
||||||
|
transport, err := c.getTransport()
|
||||||
|
if err != nil {
|
||||||
|
c.Log.Printf("ERROR %v", err)
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
transport.TLSClientConfig = config
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetProxy method sets the Proxy URL and Port for resty client.
|
||||||
|
// resty.SetProxy("http://proxyserver:8888")
|
||||||
|
//
|
||||||
|
// Alternatives: At request level proxy, see `Request.SetProxy`. OR Without this `SetProxy` method,
|
||||||
|
// you can also set Proxy via environment variable. By default `Go` uses setting from `HTTP_PROXY`.
|
||||||
|
//
|
||||||
|
func (c *Client) SetProxy(proxyURL string) *Client {
|
||||||
|
transport, err := c.getTransport()
|
||||||
|
if err != nil {
|
||||||
|
c.Log.Printf("ERROR %v", err)
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
if pURL, err := url.Parse(proxyURL); err == nil {
|
||||||
|
c.proxyURL = pURL
|
||||||
|
transport.Proxy = http.ProxyURL(c.proxyURL)
|
||||||
|
} else {
|
||||||
|
c.Log.Printf("ERROR %v", err)
|
||||||
|
c.RemoveProxy()
|
||||||
|
}
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
// RemoveProxy method removes the proxy configuration from resty client
|
||||||
|
// resty.RemoveProxy()
|
||||||
|
//
|
||||||
|
func (c *Client) RemoveProxy() *Client {
|
||||||
|
transport, err := c.getTransport()
|
||||||
|
if err != nil {
|
||||||
|
c.Log.Printf("ERROR %v", err)
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
c.proxyURL = nil
|
||||||
|
transport.Proxy = nil
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetCertificates method helps to set client certificates into resty conveniently.
|
||||||
|
//
|
||||||
|
func (c *Client) SetCertificates(certs ...tls.Certificate) *Client {
|
||||||
|
config, err := c.getTLSConfig()
|
||||||
|
if err != nil {
|
||||||
|
c.Log.Printf("ERROR %v", err)
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
config.Certificates = append(config.Certificates, certs...)
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetRootCertificate method helps to add one or more root certificates into resty client
|
||||||
|
// resty.SetRootCertificate("/path/to/root/pemFile.pem")
|
||||||
|
//
|
||||||
|
func (c *Client) SetRootCertificate(pemFilePath string) *Client {
|
||||||
|
rootPemData, err := ioutil.ReadFile(pemFilePath)
|
||||||
|
if err != nil {
|
||||||
|
c.Log.Printf("ERROR %v", err)
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
config, err := c.getTLSConfig()
|
||||||
|
if err != nil {
|
||||||
|
c.Log.Printf("ERROR %v", err)
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
if config.RootCAs == nil {
|
||||||
|
config.RootCAs = x509.NewCertPool()
|
||||||
|
}
|
||||||
|
|
||||||
|
config.RootCAs.AppendCertsFromPEM(rootPemData)
|
||||||
|
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetOutputDirectory method sets output directory for saving HTTP response into file.
|
||||||
|
// If the output directory not exists then resty creates one. This setting is optional one,
|
||||||
|
// if you're planning using absoule path in `Request.SetOutput` and can used together.
|
||||||
|
// resty.SetOutputDirectory("/save/http/response/here")
|
||||||
|
//
|
||||||
|
func (c *Client) SetOutputDirectory(dirPath string) *Client {
|
||||||
|
c.outputDirectory = dirPath
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetTransport method sets custom `*http.Transport` or any `http.RoundTripper`
|
||||||
|
// compatible interface implementation in the resty client.
|
||||||
|
//
|
||||||
|
// NOTE:
|
||||||
|
//
|
||||||
|
// - If transport is not type of `*http.Transport` then you may not be able to
|
||||||
|
// take advantage of some of the `resty` client settings.
|
||||||
|
//
|
||||||
|
// - It overwrites the resty client transport instance and it's configurations.
|
||||||
|
//
|
||||||
|
// transport := &http.Transport{
|
||||||
|
// // somthing like Proxying to httptest.Server, etc...
|
||||||
|
// Proxy: func(req *http.Request) (*url.URL, error) {
|
||||||
|
// return url.Parse(server.URL)
|
||||||
|
// },
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// resty.SetTransport(transport)
|
||||||
|
//
|
||||||
|
func (c *Client) SetTransport(transport http.RoundTripper) *Client {
|
||||||
|
if transport != nil {
|
||||||
|
c.httpClient.Transport = transport
|
||||||
|
}
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetScheme method sets custom scheme in the resty client. It's way to override default.
|
||||||
|
// resty.SetScheme("http")
|
||||||
|
//
|
||||||
|
func (c *Client) SetScheme(scheme string) *Client {
|
||||||
|
if !IsStringEmpty(scheme) {
|
||||||
|
c.scheme = scheme
|
||||||
|
}
|
||||||
|
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetCloseConnection method sets variable `Close` in http request struct with the given
|
||||||
|
// value. More info: https://golang.org/src/net/http/request.go
|
||||||
|
func (c *Client) SetCloseConnection(close bool) *Client {
|
||||||
|
c.closeConnection = close
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetDoNotParseResponse method instructs `Resty` not to parse the response body automatically.
|
||||||
|
// Resty exposes the raw response body as `io.ReadCloser`. Also do not forget to close the body,
|
||||||
|
// otherwise you might get into connection leaks, no connection reuse.
|
||||||
|
//
|
||||||
|
// Please Note: Response middlewares are not applicable, if you use this option. Basically you have
|
||||||
|
// taken over the control of response parsing from `Resty`.
|
||||||
|
func (c *Client) SetDoNotParseResponse(parse bool) *Client {
|
||||||
|
c.notParseResponse = parse
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetLogPrefix method sets the Resty logger prefix value.
|
||||||
|
func (c *Client) SetLogPrefix(prefix string) *Client {
|
||||||
|
c.logPrefix = prefix
|
||||||
|
c.Log.SetPrefix(prefix)
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetPathParams method sets multiple URL path key-value pairs at one go in the
|
||||||
|
// resty client instance.
|
||||||
|
// resty.SetPathParams(map[string]string{
|
||||||
|
// "userId": "sample@sample.com",
|
||||||
|
// "subAccountId": "100002",
|
||||||
|
// })
|
||||||
|
//
|
||||||
|
// Result:
|
||||||
|
// URL - /v1/users/{userId}/{subAccountId}/details
|
||||||
|
// Composed URL - /v1/users/sample@sample.com/100002/details
|
||||||
|
// It replace the value of the key while composing request URL. Also it can be
|
||||||
|
// overridden at request level Path Params options, see `Request.SetPathParams`.
|
||||||
|
func (c *Client) SetPathParams(params map[string]string) *Client {
|
||||||
|
for p, v := range params {
|
||||||
|
c.pathParams[p] = v
|
||||||
|
}
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetJSONEscapeHTML method is to enable/disable the HTML escape on JSON marshal.
|
||||||
|
//
|
||||||
|
// NOTE: This option only applicable to standard JSON Marshaller.
|
||||||
|
func (c *Client) SetJSONEscapeHTML(b bool) *Client {
|
||||||
|
c.jsonEscapeHTML = b
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsProxySet method returns the true if proxy is set on client otherwise false.
|
||||||
|
func (c *Client) IsProxySet() bool {
|
||||||
|
return c.proxyURL != nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetClient method returns the current http.Client used by the resty client.
|
||||||
|
func (c *Client) GetClient() *http.Client {
|
||||||
|
return c.httpClient
|
||||||
|
}
|
||||||
|
|
||||||
|
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
|
||||||
|
// Client Unexported methods
|
||||||
|
//___________________________________
|
||||||
|
|
||||||
|
// executes the given `Request` object and returns response
|
||||||
|
func (c *Client) execute(req *Request) (*Response, error) {
|
||||||
|
defer releaseBuffer(req.bodyBuf)
|
||||||
|
// Apply Request middleware
|
||||||
|
var err error
|
||||||
|
|
||||||
|
// user defined on before request methods
|
||||||
|
// to modify the *resty.Request object
|
||||||
|
for _, f := range c.udBeforeRequest {
|
||||||
|
if err = f(c, req); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// resty middlewares
|
||||||
|
for _, f := range c.beforeRequest {
|
||||||
|
if err = f(c, req); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// call pre-request if defined
|
||||||
|
if c.preReqHook != nil {
|
||||||
|
if err = c.preReqHook(c, req); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if hostHeader := req.Header.Get("Host"); hostHeader != "" {
|
||||||
|
req.RawRequest.Host = hostHeader
|
||||||
|
}
|
||||||
|
|
||||||
|
req.Time = time.Now()
|
||||||
|
resp, err := c.httpClient.Do(req.RawRequest)
|
||||||
|
|
||||||
|
response := &Response{
|
||||||
|
Request: req,
|
||||||
|
RawResponse: resp,
|
||||||
|
receivedAt: time.Now(),
|
||||||
|
}
|
||||||
|
|
||||||
|
if err != nil || req.notParseResponse || c.notParseResponse {
|
||||||
|
return response, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if !req.isSaveResponse {
|
||||||
|
defer closeq(resp.Body)
|
||||||
|
body := resp.Body
|
||||||
|
|
||||||
|
// GitHub #142
|
||||||
|
if strings.EqualFold(resp.Header.Get(hdrContentEncodingKey), "gzip") && resp.ContentLength > 0 {
|
||||||
|
if _, ok := body.(*gzip.Reader); !ok {
|
||||||
|
body, err = gzip.NewReader(body)
|
||||||
|
if err != nil {
|
||||||
|
return response, err
|
||||||
|
}
|
||||||
|
defer closeq(body)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if response.body, err = ioutil.ReadAll(body); err != nil {
|
||||||
|
return response, err
|
||||||
|
}
|
||||||
|
|
||||||
|
response.size = int64(len(response.body))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Apply Response middleware
|
||||||
|
for _, f := range c.afterResponse {
|
||||||
|
if err = f(c, response); err != nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return response, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// enables a log prefix
|
||||||
|
func (c *Client) enableLogPrefix() {
|
||||||
|
c.Log.SetFlags(log.LstdFlags)
|
||||||
|
c.Log.SetPrefix(c.logPrefix)
|
||||||
|
}
|
||||||
|
|
||||||
|
// disables a log prefix
|
||||||
|
func (c *Client) disableLogPrefix() {
|
||||||
|
c.Log.SetFlags(0)
|
||||||
|
c.Log.SetPrefix("")
|
||||||
|
}
|
||||||
|
|
||||||
|
// getting TLS client config if not exists then create one
|
||||||
|
func (c *Client) getTLSConfig() (*tls.Config, error) {
|
||||||
|
transport, err := c.getTransport()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if transport.TLSClientConfig == nil {
|
||||||
|
transport.TLSClientConfig = &tls.Config{}
|
||||||
|
}
|
||||||
|
return transport.TLSClientConfig, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// returns `*http.Transport` currently in use or error
|
||||||
|
// in case currently used `transport` is not an `*http.Transport`
|
||||||
|
func (c *Client) getTransport() (*http.Transport, error) {
|
||||||
|
if c.httpClient.Transport == nil {
|
||||||
|
c.SetTransport(new(http.Transport))
|
||||||
|
}
|
||||||
|
|
||||||
|
if transport, ok := c.httpClient.Transport.(*http.Transport); ok {
|
||||||
|
return transport, nil
|
||||||
|
}
|
||||||
|
return nil, errors.New("current transport is not an *http.Transport instance")
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// File
|
||||||
|
//
|
||||||
|
|
||||||
|
// File represent file information for multipart request
|
||||||
|
type File struct {
|
||||||
|
Name string
|
||||||
|
ParamName string
|
||||||
|
io.Reader
|
||||||
|
}
|
||||||
|
|
||||||
|
// String returns string value of current file details
|
||||||
|
func (f *File) String() string {
|
||||||
|
return fmt.Sprintf("ParamName: %v; FileName: %v", f.ParamName, f.Name)
|
||||||
|
}
|
||||||
|
|
||||||
|
// multipartField represent custom data part for multipart request
|
||||||
|
type multipartField struct {
|
||||||
|
Param string
|
||||||
|
FileName string
|
||||||
|
ContentType string
|
||||||
|
io.Reader
|
||||||
|
}
|
328
vendor/github.com/go-resty/resty/default.go
generated
vendored
Normal file
328
vendor/github.com/go-resty/resty/default.go
generated
vendored
Normal file
|
@ -0,0 +1,328 @@
|
||||||
|
// Copyright (c) 2015-2018 Jeevanandam M (jeeva@myjeeva.com), All rights reserved.
|
||||||
|
// resty source code and usage is governed by a MIT style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package resty
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/tls"
|
||||||
|
"encoding/json"
|
||||||
|
"io"
|
||||||
|
"math"
|
||||||
|
"net/http"
|
||||||
|
"net/http/cookiejar"
|
||||||
|
"net/url"
|
||||||
|
"os"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"golang.org/x/net/publicsuffix"
|
||||||
|
)
|
||||||
|
|
||||||
|
// DefaultClient of resty
|
||||||
|
var DefaultClient *Client
|
||||||
|
|
||||||
|
// New method creates a new go-resty client.
|
||||||
|
func New() *Client {
|
||||||
|
cookieJar, _ := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
|
||||||
|
return createClient(&http.Client{Jar: cookieJar})
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewWithClient method create a new go-resty client with given `http.Client`.
|
||||||
|
func NewWithClient(hc *http.Client) *Client {
|
||||||
|
return createClient(hc)
|
||||||
|
}
|
||||||
|
|
||||||
|
// R creates a new resty request object, it is used form a HTTP/RESTful request
|
||||||
|
// such as GET, POST, PUT, DELETE, HEAD, PATCH and OPTIONS.
|
||||||
|
func R() *Request {
|
||||||
|
return DefaultClient.R()
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewRequest is an alias for R(). Creates a new resty request object, it is used form a HTTP/RESTful request
|
||||||
|
// such as GET, POST, PUT, DELETE, HEAD, PATCH and OPTIONS.
|
||||||
|
func NewRequest() *Request {
|
||||||
|
return R()
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetHostURL sets Host URL. See `Client.SetHostURL for more information.
|
||||||
|
func SetHostURL(url string) *Client {
|
||||||
|
return DefaultClient.SetHostURL(url)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetHeader sets single header. See `Client.SetHeader` for more information.
|
||||||
|
func SetHeader(header, value string) *Client {
|
||||||
|
return DefaultClient.SetHeader(header, value)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetHeaders sets multiple headers. See `Client.SetHeaders` for more information.
|
||||||
|
func SetHeaders(headers map[string]string) *Client {
|
||||||
|
return DefaultClient.SetHeaders(headers)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetCookieJar sets custom http.CookieJar. See `Client.SetCookieJar` for more information.
|
||||||
|
func SetCookieJar(jar http.CookieJar) *Client {
|
||||||
|
return DefaultClient.SetCookieJar(jar)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetCookie sets single cookie object. See `Client.SetCookie` for more information.
|
||||||
|
func SetCookie(hc *http.Cookie) *Client {
|
||||||
|
return DefaultClient.SetCookie(hc)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetCookies sets multiple cookie object. See `Client.SetCookies` for more information.
|
||||||
|
func SetCookies(cs []*http.Cookie) *Client {
|
||||||
|
return DefaultClient.SetCookies(cs)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetQueryParam method sets single parameter and its value. See `Client.SetQueryParam` for more information.
|
||||||
|
func SetQueryParam(param, value string) *Client {
|
||||||
|
return DefaultClient.SetQueryParam(param, value)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetQueryParams method sets multiple parameters and its value. See `Client.SetQueryParams` for more information.
|
||||||
|
func SetQueryParams(params map[string]string) *Client {
|
||||||
|
return DefaultClient.SetQueryParams(params)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetFormData method sets Form parameters and its values. See `Client.SetFormData` for more information.
|
||||||
|
func SetFormData(data map[string]string) *Client {
|
||||||
|
return DefaultClient.SetFormData(data)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetBasicAuth method sets the basic authentication header. See `Client.SetBasicAuth` for more information.
|
||||||
|
func SetBasicAuth(username, password string) *Client {
|
||||||
|
return DefaultClient.SetBasicAuth(username, password)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetAuthToken method sets bearer auth token header. See `Client.SetAuthToken` for more information.
|
||||||
|
func SetAuthToken(token string) *Client {
|
||||||
|
return DefaultClient.SetAuthToken(token)
|
||||||
|
}
|
||||||
|
|
||||||
|
// OnBeforeRequest method sets request middleware. See `Client.OnBeforeRequest` for more information.
|
||||||
|
func OnBeforeRequest(m func(*Client, *Request) error) *Client {
|
||||||
|
return DefaultClient.OnBeforeRequest(m)
|
||||||
|
}
|
||||||
|
|
||||||
|
// OnAfterResponse method sets response middleware. See `Client.OnAfterResponse` for more information.
|
||||||
|
func OnAfterResponse(m func(*Client, *Response) error) *Client {
|
||||||
|
return DefaultClient.OnAfterResponse(m)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetPreRequestHook method sets the pre-request hook. See `Client.SetPreRequestHook` for more information.
|
||||||
|
func SetPreRequestHook(h func(*Client, *Request) error) *Client {
|
||||||
|
return DefaultClient.SetPreRequestHook(h)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetDebug method enables the debug mode. See `Client.SetDebug` for more information.
|
||||||
|
func SetDebug(d bool) *Client {
|
||||||
|
return DefaultClient.SetDebug(d)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetDebugBodyLimit method sets the response body limit for debug mode. See `Client.SetDebugBodyLimit` for more information.
|
||||||
|
func SetDebugBodyLimit(sl int64) *Client {
|
||||||
|
return DefaultClient.SetDebugBodyLimit(sl)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetAllowGetMethodPayload method allows the GET method with payload. See `Client.SetAllowGetMethodPayload` for more information.
|
||||||
|
func SetAllowGetMethodPayload(a bool) *Client {
|
||||||
|
return DefaultClient.SetAllowGetMethodPayload(a)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetRetryCount method sets the retry count. See `Client.SetRetryCount` for more information.
|
||||||
|
func SetRetryCount(count int) *Client {
|
||||||
|
return DefaultClient.SetRetryCount(count)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetRetryWaitTime method sets the retry wait time. See `Client.SetRetryWaitTime` for more information.
|
||||||
|
func SetRetryWaitTime(waitTime time.Duration) *Client {
|
||||||
|
return DefaultClient.SetRetryWaitTime(waitTime)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetRetryMaxWaitTime method sets the retry max wait time. See `Client.SetRetryMaxWaitTime` for more information.
|
||||||
|
func SetRetryMaxWaitTime(maxWaitTime time.Duration) *Client {
|
||||||
|
return DefaultClient.SetRetryMaxWaitTime(maxWaitTime)
|
||||||
|
}
|
||||||
|
|
||||||
|
// AddRetryCondition method appends check function for retry. See `Client.AddRetryCondition` for more information.
|
||||||
|
func AddRetryCondition(condition RetryConditionFunc) *Client {
|
||||||
|
return DefaultClient.AddRetryCondition(condition)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetDisableWarn method disables warning comes from `go-resty` client. See `Client.SetDisableWarn` for more information.
|
||||||
|
func SetDisableWarn(d bool) *Client {
|
||||||
|
return DefaultClient.SetDisableWarn(d)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetLogger method sets given writer for logging. See `Client.SetLogger` for more information.
|
||||||
|
func SetLogger(w io.Writer) *Client {
|
||||||
|
return DefaultClient.SetLogger(w)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetContentLength method enables `Content-Length` value. See `Client.SetContentLength` for more information.
|
||||||
|
func SetContentLength(l bool) *Client {
|
||||||
|
return DefaultClient.SetContentLength(l)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetError method is to register the global or client common `Error` object. See `Client.SetError` for more information.
|
||||||
|
func SetError(err interface{}) *Client {
|
||||||
|
return DefaultClient.SetError(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetRedirectPolicy method sets the client redirect poilicy. See `Client.SetRedirectPolicy` for more information.
|
||||||
|
func SetRedirectPolicy(policies ...interface{}) *Client {
|
||||||
|
return DefaultClient.SetRedirectPolicy(policies...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetHTTPMode method sets go-resty mode into HTTP. See `Client.SetMode` for more information.
|
||||||
|
func SetHTTPMode() *Client {
|
||||||
|
return DefaultClient.SetHTTPMode()
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetRESTMode method sets go-resty mode into RESTful. See `Client.SetMode` for more information.
|
||||||
|
func SetRESTMode() *Client {
|
||||||
|
return DefaultClient.SetRESTMode()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Mode method returns the current client mode. See `Client.Mode` for more information.
|
||||||
|
func Mode() string {
|
||||||
|
return DefaultClient.Mode()
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetTLSClientConfig method sets TLSClientConfig for underling client Transport. See `Client.SetTLSClientConfig` for more information.
|
||||||
|
func SetTLSClientConfig(config *tls.Config) *Client {
|
||||||
|
return DefaultClient.SetTLSClientConfig(config)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetTimeout method sets timeout for request. See `Client.SetTimeout` for more information.
|
||||||
|
func SetTimeout(timeout time.Duration) *Client {
|
||||||
|
return DefaultClient.SetTimeout(timeout)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetProxy method sets Proxy for request. See `Client.SetProxy` for more information.
|
||||||
|
func SetProxy(proxyURL string) *Client {
|
||||||
|
return DefaultClient.SetProxy(proxyURL)
|
||||||
|
}
|
||||||
|
|
||||||
|
// RemoveProxy method removes the proxy configuration. See `Client.RemoveProxy` for more information.
|
||||||
|
func RemoveProxy() *Client {
|
||||||
|
return DefaultClient.RemoveProxy()
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetCertificates method helps to set client certificates into resty conveniently.
|
||||||
|
// See `Client.SetCertificates` for more information and example.
|
||||||
|
func SetCertificates(certs ...tls.Certificate) *Client {
|
||||||
|
return DefaultClient.SetCertificates(certs...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetRootCertificate method helps to add one or more root certificates into resty client.
|
||||||
|
// See `Client.SetRootCertificate` for more information.
|
||||||
|
func SetRootCertificate(pemFilePath string) *Client {
|
||||||
|
return DefaultClient.SetRootCertificate(pemFilePath)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetOutputDirectory method sets output directory. See `Client.SetOutputDirectory` for more information.
|
||||||
|
func SetOutputDirectory(dirPath string) *Client {
|
||||||
|
return DefaultClient.SetOutputDirectory(dirPath)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetTransport method sets custom `*http.Transport` or any `http.RoundTripper`
|
||||||
|
// compatible interface implementation in the resty client.
|
||||||
|
// See `Client.SetTransport` for more information.
|
||||||
|
func SetTransport(transport http.RoundTripper) *Client {
|
||||||
|
return DefaultClient.SetTransport(transport)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetScheme method sets custom scheme in the resty client.
|
||||||
|
// See `Client.SetScheme` for more information.
|
||||||
|
func SetScheme(scheme string) *Client {
|
||||||
|
return DefaultClient.SetScheme(scheme)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetCloseConnection method sets close connection value in the resty client.
|
||||||
|
// See `Client.SetCloseConnection` for more information.
|
||||||
|
func SetCloseConnection(close bool) *Client {
|
||||||
|
return DefaultClient.SetCloseConnection(close)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetDoNotParseResponse method instructs `Resty` not to parse the response body automatically.
|
||||||
|
// See `Client.SetDoNotParseResponse` for more information.
|
||||||
|
func SetDoNotParseResponse(parse bool) *Client {
|
||||||
|
return DefaultClient.SetDoNotParseResponse(parse)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetPathParams method sets the Request path parameter key-value pairs. See
|
||||||
|
// `Client.SetPathParams` for more information.
|
||||||
|
func SetPathParams(params map[string]string) *Client {
|
||||||
|
return DefaultClient.SetPathParams(params)
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsProxySet method returns the true if proxy is set on client otherwise false.
|
||||||
|
// See `Client.IsProxySet` for more information.
|
||||||
|
func IsProxySet() bool {
|
||||||
|
return DefaultClient.IsProxySet()
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetClient method returns the current `http.Client` used by the default resty client.
|
||||||
|
func GetClient() *http.Client {
|
||||||
|
return DefaultClient.httpClient
|
||||||
|
}
|
||||||
|
|
||||||
|
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
|
||||||
|
// Unexported methods
|
||||||
|
//___________________________________
|
||||||
|
|
||||||
|
func createClient(hc *http.Client) *Client {
|
||||||
|
c := &Client{
|
||||||
|
HostURL: "",
|
||||||
|
QueryParam: url.Values{},
|
||||||
|
FormData: url.Values{},
|
||||||
|
Header: http.Header{},
|
||||||
|
UserInfo: nil,
|
||||||
|
Token: "",
|
||||||
|
Cookies: make([]*http.Cookie, 0),
|
||||||
|
Debug: false,
|
||||||
|
Log: getLogger(os.Stderr),
|
||||||
|
RetryCount: 0,
|
||||||
|
RetryWaitTime: defaultWaitTime,
|
||||||
|
RetryMaxWaitTime: defaultMaxWaitTime,
|
||||||
|
JSONMarshal: json.Marshal,
|
||||||
|
JSONUnmarshal: json.Unmarshal,
|
||||||
|
jsonEscapeHTML: true,
|
||||||
|
httpClient: hc,
|
||||||
|
debugBodySizeLimit: math.MaxInt32,
|
||||||
|
pathParams: make(map[string]string),
|
||||||
|
}
|
||||||
|
|
||||||
|
// Log Prefix
|
||||||
|
c.SetLogPrefix("RESTY ")
|
||||||
|
|
||||||
|
// Default redirect policy
|
||||||
|
c.SetRedirectPolicy(NoRedirectPolicy())
|
||||||
|
|
||||||
|
// default before request middlewares
|
||||||
|
c.beforeRequest = []func(*Client, *Request) error{
|
||||||
|
parseRequestURL,
|
||||||
|
parseRequestHeader,
|
||||||
|
parseRequestBody,
|
||||||
|
createHTTPRequest,
|
||||||
|
addCredentials,
|
||||||
|
requestLogger,
|
||||||
|
}
|
||||||
|
|
||||||
|
// user defined request middlewares
|
||||||
|
c.udBeforeRequest = []func(*Client, *Request) error{}
|
||||||
|
|
||||||
|
// default after response middlewares
|
||||||
|
c.afterResponse = []func(*Client, *Response) error{
|
||||||
|
responseLogger,
|
||||||
|
parseResponseBody,
|
||||||
|
saveResponseIntoFile,
|
||||||
|
}
|
||||||
|
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
DefaultClient = New()
|
||||||
|
}
|
453
vendor/github.com/go-resty/resty/middleware.go
generated
vendored
Normal file
453
vendor/github.com/go-resty/resty/middleware.go
generated
vendored
Normal file
|
@ -0,0 +1,453 @@
|
||||||
|
// Copyright (c) 2015-2018 Jeevanandam M (jeeva@myjeeva.com), All rights reserved.
|
||||||
|
// resty source code and usage is governed by a MIT style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package resty
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/xml"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"mime/multipart"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"reflect"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
|
||||||
|
// Request Middleware(s)
|
||||||
|
//___________________________________
|
||||||
|
|
||||||
|
func parseRequestURL(c *Client, r *Request) error {
|
||||||
|
// GitHub #103 Path Params
|
||||||
|
if len(r.pathParams) > 0 {
|
||||||
|
for p, v := range r.pathParams {
|
||||||
|
r.URL = strings.Replace(r.URL, "{"+p+"}", v, -1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(c.pathParams) > 0 {
|
||||||
|
for p, v := range c.pathParams {
|
||||||
|
r.URL = strings.Replace(r.URL, "{"+p+"}", v, -1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parsing request URL
|
||||||
|
reqURL, err := url.Parse(r.URL)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// If Request.URL is relative path then added c.HostURL into
|
||||||
|
// the request URL otherwise Request.URL will be used as-is
|
||||||
|
if !reqURL.IsAbs() {
|
||||||
|
r.URL = reqURL.String()
|
||||||
|
if len(r.URL) > 0 && r.URL[0] != '/' {
|
||||||
|
r.URL = "/" + r.URL
|
||||||
|
}
|
||||||
|
|
||||||
|
reqURL, err = url.Parse(c.HostURL + r.URL)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Adding Query Param
|
||||||
|
query := make(url.Values)
|
||||||
|
for k, v := range c.QueryParam {
|
||||||
|
for _, iv := range v {
|
||||||
|
query.Add(k, iv)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for k, v := range r.QueryParam {
|
||||||
|
// remove query param from client level by key
|
||||||
|
// since overrides happens for that key in the request
|
||||||
|
query.Del(k)
|
||||||
|
|
||||||
|
for _, iv := range v {
|
||||||
|
query.Add(k, iv)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// GitHub #123 Preserve query string order partially.
|
||||||
|
// Since not feasible in `SetQuery*` resty methods, because
|
||||||
|
// standard package `url.Encode(...)` sorts the query params
|
||||||
|
// alphabetically
|
||||||
|
if len(query) > 0 {
|
||||||
|
if IsStringEmpty(reqURL.RawQuery) {
|
||||||
|
reqURL.RawQuery = query.Encode()
|
||||||
|
} else {
|
||||||
|
reqURL.RawQuery = reqURL.RawQuery + "&" + query.Encode()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
r.URL = reqURL.String()
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseRequestHeader(c *Client, r *Request) error {
|
||||||
|
hdr := make(http.Header)
|
||||||
|
for k := range c.Header {
|
||||||
|
hdr[k] = append(hdr[k], c.Header[k]...)
|
||||||
|
}
|
||||||
|
|
||||||
|
for k := range r.Header {
|
||||||
|
hdr.Del(k)
|
||||||
|
hdr[k] = append(hdr[k], r.Header[k]...)
|
||||||
|
}
|
||||||
|
|
||||||
|
if IsStringEmpty(hdr.Get(hdrUserAgentKey)) {
|
||||||
|
hdr.Set(hdrUserAgentKey, fmt.Sprintf(hdrUserAgentValue, Version))
|
||||||
|
}
|
||||||
|
|
||||||
|
ct := hdr.Get(hdrContentTypeKey)
|
||||||
|
if IsStringEmpty(hdr.Get(hdrAcceptKey)) && !IsStringEmpty(ct) &&
|
||||||
|
(IsJSONType(ct) || IsXMLType(ct)) {
|
||||||
|
hdr.Set(hdrAcceptKey, hdr.Get(hdrContentTypeKey))
|
||||||
|
}
|
||||||
|
|
||||||
|
r.Header = hdr
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseRequestBody(c *Client, r *Request) (err error) {
|
||||||
|
if isPayloadSupported(r.Method, c.AllowGetMethodPayload) {
|
||||||
|
// Handling Multipart
|
||||||
|
if r.isMultiPart && !(r.Method == MethodPatch) {
|
||||||
|
if err = handleMultipart(c, r); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
goto CL
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handling Form Data
|
||||||
|
if len(c.FormData) > 0 || len(r.FormData) > 0 {
|
||||||
|
handleFormData(c, r)
|
||||||
|
|
||||||
|
goto CL
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handling Request body
|
||||||
|
if r.Body != nil {
|
||||||
|
handleContentType(c, r)
|
||||||
|
|
||||||
|
if err = handleRequestBody(c, r); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
CL:
|
||||||
|
// by default resty won't set content length, you can if you want to :)
|
||||||
|
if (c.setContentLength || r.setContentLength) && r.bodyBuf != nil {
|
||||||
|
r.Header.Set(hdrContentLengthKey, fmt.Sprintf("%d", r.bodyBuf.Len()))
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func createHTTPRequest(c *Client, r *Request) (err error) {
|
||||||
|
if r.bodyBuf == nil {
|
||||||
|
if reader, ok := r.Body.(io.Reader); ok {
|
||||||
|
r.RawRequest, err = http.NewRequest(r.Method, r.URL, reader)
|
||||||
|
} else {
|
||||||
|
r.RawRequest, err = http.NewRequest(r.Method, r.URL, nil)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
r.RawRequest, err = http.NewRequest(r.Method, r.URL, r.bodyBuf)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Assign close connection option
|
||||||
|
r.RawRequest.Close = c.closeConnection
|
||||||
|
|
||||||
|
// Add headers into http request
|
||||||
|
r.RawRequest.Header = r.Header
|
||||||
|
|
||||||
|
// Add cookies into http request
|
||||||
|
for _, cookie := range c.Cookies {
|
||||||
|
r.RawRequest.AddCookie(cookie)
|
||||||
|
}
|
||||||
|
|
||||||
|
// it's for non-http scheme option
|
||||||
|
if r.RawRequest.URL != nil && r.RawRequest.URL.Scheme == "" {
|
||||||
|
r.RawRequest.URL.Scheme = c.scheme
|
||||||
|
r.RawRequest.URL.Host = r.URL
|
||||||
|
}
|
||||||
|
|
||||||
|
// Use context if it was specified
|
||||||
|
r.addContextIfAvailable()
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func addCredentials(c *Client, r *Request) error {
|
||||||
|
var isBasicAuth bool
|
||||||
|
// Basic Auth
|
||||||
|
if r.UserInfo != nil { // takes precedence
|
||||||
|
r.RawRequest.SetBasicAuth(r.UserInfo.Username, r.UserInfo.Password)
|
||||||
|
isBasicAuth = true
|
||||||
|
} else if c.UserInfo != nil {
|
||||||
|
r.RawRequest.SetBasicAuth(c.UserInfo.Username, c.UserInfo.Password)
|
||||||
|
isBasicAuth = true
|
||||||
|
}
|
||||||
|
|
||||||
|
if !c.DisableWarn {
|
||||||
|
if isBasicAuth && !strings.HasPrefix(r.URL, "https") {
|
||||||
|
c.Log.Println("WARNING - Using Basic Auth in HTTP mode is not secure.")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Token Auth
|
||||||
|
if !IsStringEmpty(r.Token) { // takes precedence
|
||||||
|
r.RawRequest.Header.Set(hdrAuthorizationKey, "Bearer "+r.Token)
|
||||||
|
} else if !IsStringEmpty(c.Token) {
|
||||||
|
r.RawRequest.Header.Set(hdrAuthorizationKey, "Bearer "+c.Token)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func requestLogger(c *Client, r *Request) error {
|
||||||
|
if c.Debug {
|
||||||
|
rr := r.RawRequest
|
||||||
|
reqLog := "\n---------------------- REQUEST LOG -----------------------\n" +
|
||||||
|
fmt.Sprintf("%s %s %s\n", r.Method, rr.URL.RequestURI(), rr.Proto) +
|
||||||
|
fmt.Sprintf("HOST : %s\n", rr.URL.Host) +
|
||||||
|
fmt.Sprintf("HEADERS:\n") +
|
||||||
|
composeHeaders(rr.Header) + "\n" +
|
||||||
|
fmt.Sprintf("BODY :\n%v\n", r.fmtBodyString()) +
|
||||||
|
"----------------------------------------------------------\n"
|
||||||
|
|
||||||
|
c.Log.Print(reqLog)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
|
||||||
|
// Response Middleware(s)
|
||||||
|
//___________________________________
|
||||||
|
|
||||||
|
func responseLogger(c *Client, res *Response) error {
|
||||||
|
if c.Debug {
|
||||||
|
resLog := "\n---------------------- RESPONSE LOG -----------------------\n" +
|
||||||
|
fmt.Sprintf("STATUS : %s\n", res.Status()) +
|
||||||
|
fmt.Sprintf("RECEIVED AT : %v\n", res.ReceivedAt().Format(time.RFC3339Nano)) +
|
||||||
|
fmt.Sprintf("RESPONSE TIME : %v\n", res.Time()) +
|
||||||
|
"HEADERS:\n" +
|
||||||
|
composeHeaders(res.Header()) + "\n"
|
||||||
|
|
||||||
|
if res.Request.isSaveResponse {
|
||||||
|
resLog += fmt.Sprintf("BODY :\n***** RESPONSE WRITTEN INTO FILE *****\n")
|
||||||
|
} else {
|
||||||
|
resLog += fmt.Sprintf("BODY :\n%v\n", res.fmtBodyString(c.debugBodySizeLimit))
|
||||||
|
}
|
||||||
|
resLog += "----------------------------------------------------------\n"
|
||||||
|
|
||||||
|
c.Log.Print(resLog)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseResponseBody(c *Client, res *Response) (err error) {
|
||||||
|
// Handles only JSON or XML content type
|
||||||
|
ct := firstNonEmpty(res.Header().Get(hdrContentTypeKey), res.Request.fallbackContentType)
|
||||||
|
if IsJSONType(ct) || IsXMLType(ct) {
|
||||||
|
// HTTP status code > 199 and < 300, considered as Result
|
||||||
|
if res.IsSuccess() {
|
||||||
|
if res.Request.Result != nil {
|
||||||
|
err = Unmarshalc(c, ct, res.body, res.Request.Result)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// HTTP status code > 399, considered as Error
|
||||||
|
if res.IsError() {
|
||||||
|
// global error interface
|
||||||
|
if res.Request.Error == nil && c.Error != nil {
|
||||||
|
res.Request.Error = reflect.New(c.Error).Interface()
|
||||||
|
}
|
||||||
|
|
||||||
|
if res.Request.Error != nil {
|
||||||
|
err = Unmarshalc(c, ct, res.body, res.Request.Error)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleMultipart(c *Client, r *Request) (err error) {
|
||||||
|
r.bodyBuf = acquireBuffer()
|
||||||
|
w := multipart.NewWriter(r.bodyBuf)
|
||||||
|
|
||||||
|
for k, v := range c.FormData {
|
||||||
|
for _, iv := range v {
|
||||||
|
if err = w.WriteField(k, iv); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for k, v := range r.FormData {
|
||||||
|
for _, iv := range v {
|
||||||
|
if strings.HasPrefix(k, "@") { // file
|
||||||
|
err = addFile(w, k[1:], iv)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
} else { // form value
|
||||||
|
if err = w.WriteField(k, iv); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// #21 - adding io.Reader support
|
||||||
|
if len(r.multipartFiles) > 0 {
|
||||||
|
for _, f := range r.multipartFiles {
|
||||||
|
err = addFileReader(w, f)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// GitHub #130 adding multipart field support with content type
|
||||||
|
if len(r.multipartFields) > 0 {
|
||||||
|
for _, mf := range r.multipartFields {
|
||||||
|
if err = addMultipartFormField(w, mf); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
r.Header.Set(hdrContentTypeKey, w.FormDataContentType())
|
||||||
|
err = w.Close()
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleFormData(c *Client, r *Request) {
|
||||||
|
formData := url.Values{}
|
||||||
|
|
||||||
|
for k, v := range c.FormData {
|
||||||
|
for _, iv := range v {
|
||||||
|
formData.Add(k, iv)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for k, v := range r.FormData {
|
||||||
|
// remove form data field from client level by key
|
||||||
|
// since overrides happens for that key in the request
|
||||||
|
formData.Del(k)
|
||||||
|
|
||||||
|
for _, iv := range v {
|
||||||
|
formData.Add(k, iv)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
r.bodyBuf = bytes.NewBuffer([]byte(formData.Encode()))
|
||||||
|
r.Header.Set(hdrContentTypeKey, formContentType)
|
||||||
|
r.isFormData = true
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleContentType(c *Client, r *Request) {
|
||||||
|
contentType := r.Header.Get(hdrContentTypeKey)
|
||||||
|
if IsStringEmpty(contentType) {
|
||||||
|
contentType = DetectContentType(r.Body)
|
||||||
|
r.Header.Set(hdrContentTypeKey, contentType)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleRequestBody(c *Client, r *Request) (err error) {
|
||||||
|
var bodyBytes []byte
|
||||||
|
contentType := r.Header.Get(hdrContentTypeKey)
|
||||||
|
kind := kindOf(r.Body)
|
||||||
|
r.bodyBuf = nil
|
||||||
|
|
||||||
|
if reader, ok := r.Body.(io.Reader); ok {
|
||||||
|
if c.setContentLength || r.setContentLength { // keep backward compability
|
||||||
|
r.bodyBuf = acquireBuffer()
|
||||||
|
_, err = r.bodyBuf.ReadFrom(reader)
|
||||||
|
r.Body = nil
|
||||||
|
} else {
|
||||||
|
// Otherwise buffer less processing for `io.Reader`, sounds good.
|
||||||
|
return
|
||||||
|
}
|
||||||
|
} else if b, ok := r.Body.([]byte); ok {
|
||||||
|
bodyBytes = b
|
||||||
|
} else if s, ok := r.Body.(string); ok {
|
||||||
|
bodyBytes = []byte(s)
|
||||||
|
} else if IsJSONType(contentType) &&
|
||||||
|
(kind == reflect.Struct || kind == reflect.Map || kind == reflect.Slice) {
|
||||||
|
bodyBytes, err = jsonMarshal(c, r, r.Body)
|
||||||
|
} else if IsXMLType(contentType) && (kind == reflect.Struct) {
|
||||||
|
bodyBytes, err = xml.Marshal(r.Body)
|
||||||
|
}
|
||||||
|
|
||||||
|
if bodyBytes == nil && r.bodyBuf == nil {
|
||||||
|
err = errors.New("unsupported 'Body' type/value")
|
||||||
|
}
|
||||||
|
|
||||||
|
// if any errors during body bytes handling, return it
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// []byte into Buffer
|
||||||
|
if bodyBytes != nil && r.bodyBuf == nil {
|
||||||
|
r.bodyBuf = acquireBuffer()
|
||||||
|
_, _ = r.bodyBuf.Write(bodyBytes)
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func saveResponseIntoFile(c *Client, res *Response) error {
|
||||||
|
if res.Request.isSaveResponse {
|
||||||
|
file := ""
|
||||||
|
|
||||||
|
if len(c.outputDirectory) > 0 && !filepath.IsAbs(res.Request.outputFile) {
|
||||||
|
file += c.outputDirectory + string(filepath.Separator)
|
||||||
|
}
|
||||||
|
|
||||||
|
file = filepath.Clean(file + res.Request.outputFile)
|
||||||
|
if err := createDirectory(filepath.Dir(file)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
outFile, err := os.Create(file)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer closeq(outFile)
|
||||||
|
|
||||||
|
// io.Copy reads maximum 32kb size, it is perfect for large file download too
|
||||||
|
defer closeq(res.RawResponse.Body)
|
||||||
|
|
||||||
|
written, err := io.Copy(outFile, res.RawResponse.Body)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
res.size = written
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
99
vendor/github.com/go-resty/resty/redirect.go
generated
vendored
Normal file
99
vendor/github.com/go-resty/resty/redirect.go
generated
vendored
Normal file
|
@ -0,0 +1,99 @@
|
||||||
|
// Copyright (c) 2015-2018 Jeevanandam M (jeeva@myjeeva.com), All rights reserved.
|
||||||
|
// resty source code and usage is governed by a MIT style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package resty
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"net"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type (
|
||||||
|
// RedirectPolicy to regulate the redirects in the resty client.
|
||||||
|
// Objects implementing the RedirectPolicy interface can be registered as
|
||||||
|
//
|
||||||
|
// Apply function should return nil to continue the redirect jounery, otherwise
|
||||||
|
// return error to stop the redirect.
|
||||||
|
RedirectPolicy interface {
|
||||||
|
Apply(req *http.Request, via []*http.Request) error
|
||||||
|
}
|
||||||
|
|
||||||
|
// The RedirectPolicyFunc type is an adapter to allow the use of ordinary functions as RedirectPolicy.
|
||||||
|
// If f is a function with the appropriate signature, RedirectPolicyFunc(f) is a RedirectPolicy object that calls f.
|
||||||
|
RedirectPolicyFunc func(*http.Request, []*http.Request) error
|
||||||
|
)
|
||||||
|
|
||||||
|
// Apply calls f(req, via).
|
||||||
|
func (f RedirectPolicyFunc) Apply(req *http.Request, via []*http.Request) error {
|
||||||
|
return f(req, via)
|
||||||
|
}
|
||||||
|
|
||||||
|
// NoRedirectPolicy is used to disable redirects in the HTTP client
|
||||||
|
// resty.SetRedirectPolicy(NoRedirectPolicy())
|
||||||
|
func NoRedirectPolicy() RedirectPolicy {
|
||||||
|
return RedirectPolicyFunc(func(req *http.Request, via []*http.Request) error {
|
||||||
|
return errors.New("auto redirect is disabled")
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// FlexibleRedirectPolicy is convenient method to create No of redirect policy for HTTP client.
|
||||||
|
// resty.SetRedirectPolicy(FlexibleRedirectPolicy(20))
|
||||||
|
func FlexibleRedirectPolicy(noOfRedirect int) RedirectPolicy {
|
||||||
|
return RedirectPolicyFunc(func(req *http.Request, via []*http.Request) error {
|
||||||
|
if len(via) >= noOfRedirect {
|
||||||
|
return fmt.Errorf("stopped after %d redirects", noOfRedirect)
|
||||||
|
}
|
||||||
|
|
||||||
|
checkHostAndAddHeaders(req, via[0])
|
||||||
|
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// DomainCheckRedirectPolicy is convenient method to define domain name redirect rule in resty client.
|
||||||
|
// Redirect is allowed for only mentioned host in the policy.
|
||||||
|
// resty.SetRedirectPolicy(DomainCheckRedirectPolicy("host1.com", "host2.org", "host3.net"))
|
||||||
|
func DomainCheckRedirectPolicy(hostnames ...string) RedirectPolicy {
|
||||||
|
hosts := make(map[string]bool)
|
||||||
|
for _, h := range hostnames {
|
||||||
|
hosts[strings.ToLower(h)] = true
|
||||||
|
}
|
||||||
|
|
||||||
|
fn := RedirectPolicyFunc(func(req *http.Request, via []*http.Request) error {
|
||||||
|
if ok := hosts[getHostname(req.URL.Host)]; !ok {
|
||||||
|
return errors.New("redirect is not allowed as per DomainCheckRedirectPolicy")
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
|
return fn
|
||||||
|
}
|
||||||
|
|
||||||
|
func getHostname(host string) (hostname string) {
|
||||||
|
if strings.Index(host, ":") > 0 {
|
||||||
|
host, _, _ = net.SplitHostPort(host)
|
||||||
|
}
|
||||||
|
hostname = strings.ToLower(host)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// By default Golang will not redirect request headers
|
||||||
|
// after go throughing various discussion comments from thread
|
||||||
|
// https://github.com/golang/go/issues/4800
|
||||||
|
// go-resty will add all the headers during a redirect for the same host
|
||||||
|
func checkHostAndAddHeaders(cur *http.Request, pre *http.Request) {
|
||||||
|
curHostname := getHostname(cur.URL.Host)
|
||||||
|
preHostname := getHostname(pre.URL.Host)
|
||||||
|
if strings.EqualFold(curHostname, preHostname) {
|
||||||
|
for key, val := range pre.Header {
|
||||||
|
cur.Header[key] = val
|
||||||
|
}
|
||||||
|
} else { // only library User-Agent header is added
|
||||||
|
cur.Header.Set(hdrUserAgentKey, fmt.Sprintf(hdrUserAgentValue, Version))
|
||||||
|
}
|
||||||
|
}
|
566
vendor/github.com/go-resty/resty/request.go
generated
vendored
Normal file
566
vendor/github.com/go-resty/resty/request.go
generated
vendored
Normal file
|
@ -0,0 +1,566 @@
|
||||||
|
// Copyright (c) 2015-2018 Jeevanandam M (jeeva@myjeeva.com), All rights reserved.
|
||||||
|
// resty source code and usage is governed by a MIT style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package resty
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/base64"
|
||||||
|
"encoding/json"
|
||||||
|
"encoding/xml"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net"
|
||||||
|
"net/url"
|
||||||
|
"reflect"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// SRVRecord holds the data to query the SRV record for the following service
|
||||||
|
type SRVRecord struct {
|
||||||
|
Service string
|
||||||
|
Domain string
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetHeader method is to set a single header field and its value in the current request.
|
||||||
|
// Example: To set `Content-Type` and `Accept` as `application/json`.
|
||||||
|
// resty.R().
|
||||||
|
// SetHeader("Content-Type", "application/json").
|
||||||
|
// SetHeader("Accept", "application/json")
|
||||||
|
//
|
||||||
|
// Also you can override header value, which was set at client instance level.
|
||||||
|
//
|
||||||
|
func (r *Request) SetHeader(header, value string) *Request {
|
||||||
|
r.Header.Set(header, value)
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetHeaders method sets multiple headers field and its values at one go in the current request.
|
||||||
|
// Example: To set `Content-Type` and `Accept` as `application/json`
|
||||||
|
//
|
||||||
|
// resty.R().
|
||||||
|
// SetHeaders(map[string]string{
|
||||||
|
// "Content-Type": "application/json",
|
||||||
|
// "Accept": "application/json",
|
||||||
|
// })
|
||||||
|
// Also you can override header value, which was set at client instance level.
|
||||||
|
//
|
||||||
|
func (r *Request) SetHeaders(headers map[string]string) *Request {
|
||||||
|
for h, v := range headers {
|
||||||
|
r.SetHeader(h, v)
|
||||||
|
}
|
||||||
|
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetQueryParam method sets single parameter and its value in the current request.
|
||||||
|
// It will be formed as query string for the request.
|
||||||
|
// Example: `search=kitchen%20papers&size=large` in the URL after `?` mark.
|
||||||
|
// resty.R().
|
||||||
|
// SetQueryParam("search", "kitchen papers").
|
||||||
|
// SetQueryParam("size", "large")
|
||||||
|
// Also you can override query params value, which was set at client instance level
|
||||||
|
//
|
||||||
|
func (r *Request) SetQueryParam(param, value string) *Request {
|
||||||
|
r.QueryParam.Set(param, value)
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetQueryParams method sets multiple parameters and its values at one go in the current request.
|
||||||
|
// It will be formed as query string for the request.
|
||||||
|
// Example: `search=kitchen%20papers&size=large` in the URL after `?` mark.
|
||||||
|
// resty.R().
|
||||||
|
// SetQueryParams(map[string]string{
|
||||||
|
// "search": "kitchen papers",
|
||||||
|
// "size": "large",
|
||||||
|
// })
|
||||||
|
// Also you can override query params value, which was set at client instance level
|
||||||
|
//
|
||||||
|
func (r *Request) SetQueryParams(params map[string]string) *Request {
|
||||||
|
for p, v := range params {
|
||||||
|
r.SetQueryParam(p, v)
|
||||||
|
}
|
||||||
|
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetMultiValueQueryParams method appends multiple parameters with multi-value
|
||||||
|
// at one go in the current request. It will be formed as query string for the request.
|
||||||
|
// Example: `status=pending&status=approved&status=open` in the URL after `?` mark.
|
||||||
|
// resty.R().
|
||||||
|
// SetMultiValueQueryParams(url.Values{
|
||||||
|
// "status": []string{"pending", "approved", "open"},
|
||||||
|
// })
|
||||||
|
// Also you can override query params value, which was set at client instance level
|
||||||
|
//
|
||||||
|
func (r *Request) SetMultiValueQueryParams(params url.Values) *Request {
|
||||||
|
for p, v := range params {
|
||||||
|
for _, pv := range v {
|
||||||
|
r.QueryParam.Add(p, pv)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetQueryString method provides ability to use string as an input to set URL query string for the request.
|
||||||
|
//
|
||||||
|
// Using String as an input
|
||||||
|
// resty.R().
|
||||||
|
// SetQueryString("productId=232&template=fresh-sample&cat=resty&source=google&kw=buy a lot more")
|
||||||
|
//
|
||||||
|
func (r *Request) SetQueryString(query string) *Request {
|
||||||
|
params, err := url.ParseQuery(strings.TrimSpace(query))
|
||||||
|
if err == nil {
|
||||||
|
for p, v := range params {
|
||||||
|
for _, pv := range v {
|
||||||
|
r.QueryParam.Add(p, pv)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
r.client.Log.Printf("ERROR %v", err)
|
||||||
|
}
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetFormData method sets Form parameters and their values in the current request.
|
||||||
|
// It's applicable only HTTP method `POST` and `PUT` and requests content type would be set as
|
||||||
|
// `application/x-www-form-urlencoded`.
|
||||||
|
// resty.R().
|
||||||
|
// SetFormData(map[string]string{
|
||||||
|
// "access_token": "BC594900-518B-4F7E-AC75-BD37F019E08F",
|
||||||
|
// "user_id": "3455454545",
|
||||||
|
// })
|
||||||
|
// Also you can override form data value, which was set at client instance level
|
||||||
|
//
|
||||||
|
func (r *Request) SetFormData(data map[string]string) *Request {
|
||||||
|
for k, v := range data {
|
||||||
|
r.FormData.Set(k, v)
|
||||||
|
}
|
||||||
|
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetMultiValueFormData method appends multiple form parameters with multi-value
|
||||||
|
// at one go in the current request.
|
||||||
|
// resty.R().
|
||||||
|
// SetMultiValueFormData(url.Values{
|
||||||
|
// "search_criteria": []string{"book", "glass", "pencil"},
|
||||||
|
// })
|
||||||
|
// Also you can override form data value, which was set at client instance level
|
||||||
|
//
|
||||||
|
func (r *Request) SetMultiValueFormData(params url.Values) *Request {
|
||||||
|
for k, v := range params {
|
||||||
|
for _, kv := range v {
|
||||||
|
r.FormData.Add(k, kv)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetBody method sets the request body for the request. It supports various realtime need easy.
|
||||||
|
// We can say its quite handy or powerful. Supported request body data types is `string`, `[]byte`,
|
||||||
|
// `struct` and `map`. Body value can be pointer or non-pointer. Automatic marshalling
|
||||||
|
// for JSON and XML content type, if it is `struct` or `map`.
|
||||||
|
//
|
||||||
|
// Example:
|
||||||
|
//
|
||||||
|
// Struct as a body input, based on content type, it will be marshalled.
|
||||||
|
// resty.R().
|
||||||
|
// SetBody(User{
|
||||||
|
// Username: "jeeva@myjeeva.com",
|
||||||
|
// Password: "welcome2resty",
|
||||||
|
// })
|
||||||
|
//
|
||||||
|
// Map as a body input, based on content type, it will be marshalled.
|
||||||
|
// resty.R().
|
||||||
|
// SetBody(map[string]interface{}{
|
||||||
|
// "username": "jeeva@myjeeva.com",
|
||||||
|
// "password": "welcome2resty",
|
||||||
|
// "address": &Address{
|
||||||
|
// Address1: "1111 This is my street",
|
||||||
|
// Address2: "Apt 201",
|
||||||
|
// City: "My City",
|
||||||
|
// State: "My State",
|
||||||
|
// ZipCode: 00000,
|
||||||
|
// },
|
||||||
|
// })
|
||||||
|
//
|
||||||
|
// String as a body input. Suitable for any need as a string input.
|
||||||
|
// resty.R().
|
||||||
|
// SetBody(`{
|
||||||
|
// "username": "jeeva@getrightcare.com",
|
||||||
|
// "password": "admin"
|
||||||
|
// }`)
|
||||||
|
//
|
||||||
|
// []byte as a body input. Suitable for raw request such as file upload, serialize & deserialize, etc.
|
||||||
|
// resty.R().
|
||||||
|
// SetBody([]byte("This is my raw request, sent as-is"))
|
||||||
|
//
|
||||||
|
func (r *Request) SetBody(body interface{}) *Request {
|
||||||
|
r.Body = body
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetResult method is to register the response `Result` object for automatic unmarshalling in the RESTful mode
|
||||||
|
// if response status code is between 200 and 299 and content type either JSON or XML.
|
||||||
|
//
|
||||||
|
// Note: Result object can be pointer or non-pointer.
|
||||||
|
// resty.R().SetResult(&AuthToken{})
|
||||||
|
// // OR
|
||||||
|
// resty.R().SetResult(AuthToken{})
|
||||||
|
//
|
||||||
|
// Accessing a result value
|
||||||
|
// response.Result().(*AuthToken)
|
||||||
|
//
|
||||||
|
func (r *Request) SetResult(res interface{}) *Request {
|
||||||
|
r.Result = getPointer(res)
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetError method is to register the request `Error` object for automatic unmarshalling in the RESTful mode
|
||||||
|
// if response status code is greater than 399 and content type either JSON or XML.
|
||||||
|
//
|
||||||
|
// Note: Error object can be pointer or non-pointer.
|
||||||
|
// resty.R().SetError(&AuthError{})
|
||||||
|
// // OR
|
||||||
|
// resty.R().SetError(AuthError{})
|
||||||
|
//
|
||||||
|
// Accessing a error value
|
||||||
|
// response.Error().(*AuthError)
|
||||||
|
//
|
||||||
|
func (r *Request) SetError(err interface{}) *Request {
|
||||||
|
r.Error = getPointer(err)
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetFile method is to set single file field name and its path for multipart upload.
|
||||||
|
// resty.R().
|
||||||
|
// SetFile("my_file", "/Users/jeeva/Gas Bill - Sep.pdf")
|
||||||
|
//
|
||||||
|
func (r *Request) SetFile(param, filePath string) *Request {
|
||||||
|
r.isMultiPart = true
|
||||||
|
r.FormData.Set("@"+param, filePath)
|
||||||
|
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetFiles method is to set multiple file field name and its path for multipart upload.
|
||||||
|
// resty.R().
|
||||||
|
// SetFiles(map[string]string{
|
||||||
|
// "my_file1": "/Users/jeeva/Gas Bill - Sep.pdf",
|
||||||
|
// "my_file2": "/Users/jeeva/Electricity Bill - Sep.pdf",
|
||||||
|
// "my_file3": "/Users/jeeva/Water Bill - Sep.pdf",
|
||||||
|
// })
|
||||||
|
//
|
||||||
|
func (r *Request) SetFiles(files map[string]string) *Request {
|
||||||
|
r.isMultiPart = true
|
||||||
|
|
||||||
|
for f, fp := range files {
|
||||||
|
r.FormData.Set("@"+f, fp)
|
||||||
|
}
|
||||||
|
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetFileReader method is to set single file using io.Reader for multipart upload.
|
||||||
|
// resty.R().
|
||||||
|
// SetFileReader("profile_img", "my-profile-img.png", bytes.NewReader(profileImgBytes)).
|
||||||
|
// SetFileReader("notes", "user-notes.txt", bytes.NewReader(notesBytes))
|
||||||
|
//
|
||||||
|
func (r *Request) SetFileReader(param, fileName string, reader io.Reader) *Request {
|
||||||
|
r.isMultiPart = true
|
||||||
|
|
||||||
|
r.multipartFiles = append(r.multipartFiles, &File{
|
||||||
|
Name: fileName,
|
||||||
|
ParamName: param,
|
||||||
|
Reader: reader,
|
||||||
|
})
|
||||||
|
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetMultipartField method is to set custom data using io.Reader for multipart upload.
|
||||||
|
func (r *Request) SetMultipartField(param, fileName, contentType string, reader io.Reader) *Request {
|
||||||
|
r.isMultiPart = true
|
||||||
|
|
||||||
|
r.multipartFields = append(r.multipartFields, &multipartField{
|
||||||
|
Param: param,
|
||||||
|
FileName: fileName,
|
||||||
|
ContentType: contentType,
|
||||||
|
Reader: reader,
|
||||||
|
})
|
||||||
|
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetContentLength method sets the HTTP header `Content-Length` value for current request.
|
||||||
|
// By default go-resty won't set `Content-Length`. Also you have an option to enable for every
|
||||||
|
// request. See `resty.SetContentLength`
|
||||||
|
// resty.R().SetContentLength(true)
|
||||||
|
//
|
||||||
|
func (r *Request) SetContentLength(l bool) *Request {
|
||||||
|
r.setContentLength = true
|
||||||
|
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetBasicAuth method sets the basic authentication header in the current HTTP request.
|
||||||
|
// For Header example:
|
||||||
|
// Authorization: Basic <base64-encoded-value>
|
||||||
|
//
|
||||||
|
// To set the header for username "go-resty" and password "welcome"
|
||||||
|
// resty.R().SetBasicAuth("go-resty", "welcome")
|
||||||
|
//
|
||||||
|
// This method overrides the credentials set by method `resty.SetBasicAuth`.
|
||||||
|
//
|
||||||
|
func (r *Request) SetBasicAuth(username, password string) *Request {
|
||||||
|
r.UserInfo = &User{Username: username, Password: password}
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetAuthToken method sets bearer auth token header in the current HTTP request. Header example:
|
||||||
|
// Authorization: Bearer <auth-token-value-comes-here>
|
||||||
|
//
|
||||||
|
// Example: To set auth token BC594900518B4F7EAC75BD37F019E08FBC594900518B4F7EAC75BD37F019E08F
|
||||||
|
//
|
||||||
|
// resty.R().SetAuthToken("BC594900518B4F7EAC75BD37F019E08FBC594900518B4F7EAC75BD37F019E08F")
|
||||||
|
//
|
||||||
|
// This method overrides the Auth token set by method `resty.SetAuthToken`.
|
||||||
|
//
|
||||||
|
func (r *Request) SetAuthToken(token string) *Request {
|
||||||
|
r.Token = token
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetOutput method sets the output file for current HTTP request. Current HTTP response will be
|
||||||
|
// saved into given file. It is similar to `curl -o` flag. Absolute path or relative path can be used.
|
||||||
|
// If is it relative path then output file goes under the output directory, as mentioned
|
||||||
|
// in the `Client.SetOutputDirectory`.
|
||||||
|
// resty.R().
|
||||||
|
// SetOutput("/Users/jeeva/Downloads/ReplyWithHeader-v5.1-beta.zip").
|
||||||
|
// Get("http://bit.ly/1LouEKr")
|
||||||
|
//
|
||||||
|
// Note: In this scenario `Response.Body` might be nil.
|
||||||
|
func (r *Request) SetOutput(file string) *Request {
|
||||||
|
r.outputFile = file
|
||||||
|
r.isSaveResponse = true
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetSRV method sets the details to query the service SRV record and execute the
|
||||||
|
// request.
|
||||||
|
// resty.R().
|
||||||
|
// SetSRV(SRVRecord{"web", "testservice.com"}).
|
||||||
|
// Get("/get")
|
||||||
|
func (r *Request) SetSRV(srv *SRVRecord) *Request {
|
||||||
|
r.SRV = srv
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetDoNotParseResponse method instructs `Resty` not to parse the response body automatically.
|
||||||
|
// Resty exposes the raw response body as `io.ReadCloser`. Also do not forget to close the body,
|
||||||
|
// otherwise you might get into connection leaks, no connection reuse.
|
||||||
|
//
|
||||||
|
// Please Note: Response middlewares are not applicable, if you use this option. Basically you have
|
||||||
|
// taken over the control of response parsing from `Resty`.
|
||||||
|
func (r *Request) SetDoNotParseResponse(parse bool) *Request {
|
||||||
|
r.notParseResponse = parse
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetPathParams method sets multiple URL path key-value pairs at one go in the
|
||||||
|
// resty current request instance.
|
||||||
|
// resty.R().SetPathParams(map[string]string{
|
||||||
|
// "userId": "sample@sample.com",
|
||||||
|
// "subAccountId": "100002",
|
||||||
|
// })
|
||||||
|
//
|
||||||
|
// Result:
|
||||||
|
// URL - /v1/users/{userId}/{subAccountId}/details
|
||||||
|
// Composed URL - /v1/users/sample@sample.com/100002/details
|
||||||
|
// It replace the value of the key while composing request URL. Also you can
|
||||||
|
// override Path Params value, which was set at client instance level.
|
||||||
|
func (r *Request) SetPathParams(params map[string]string) *Request {
|
||||||
|
for p, v := range params {
|
||||||
|
r.pathParams[p] = v
|
||||||
|
}
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
// ExpectContentType method allows to provide fallback `Content-Type` for automatic unmarshalling
|
||||||
|
// when `Content-Type` response header is unavailable.
|
||||||
|
func (r *Request) ExpectContentType(contentType string) *Request {
|
||||||
|
r.fallbackContentType = contentType
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetJSONEscapeHTML method is to enable/disable the HTML escape on JSON marshal.
|
||||||
|
//
|
||||||
|
// NOTE: This option only applicable to standard JSON Marshaller.
|
||||||
|
func (r *Request) SetJSONEscapeHTML(b bool) *Request {
|
||||||
|
r.jsonEscapeHTML = b
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
|
||||||
|
// HTTP verb method starts here
|
||||||
|
//___________________________________
|
||||||
|
|
||||||
|
// Get method does GET HTTP request. It's defined in section 4.3.1 of RFC7231.
|
||||||
|
func (r *Request) Get(url string) (*Response, error) {
|
||||||
|
return r.Execute(MethodGet, url)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Head method does HEAD HTTP request. It's defined in section 4.3.2 of RFC7231.
|
||||||
|
func (r *Request) Head(url string) (*Response, error) {
|
||||||
|
return r.Execute(MethodHead, url)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Post method does POST HTTP request. It's defined in section 4.3.3 of RFC7231.
|
||||||
|
func (r *Request) Post(url string) (*Response, error) {
|
||||||
|
return r.Execute(MethodPost, url)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Put method does PUT HTTP request. It's defined in section 4.3.4 of RFC7231.
|
||||||
|
func (r *Request) Put(url string) (*Response, error) {
|
||||||
|
return r.Execute(MethodPut, url)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete method does DELETE HTTP request. It's defined in section 4.3.5 of RFC7231.
|
||||||
|
func (r *Request) Delete(url string) (*Response, error) {
|
||||||
|
return r.Execute(MethodDelete, url)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Options method does OPTIONS HTTP request. It's defined in section 4.3.7 of RFC7231.
|
||||||
|
func (r *Request) Options(url string) (*Response, error) {
|
||||||
|
return r.Execute(MethodOptions, url)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Patch method does PATCH HTTP request. It's defined in section 2 of RFC5789.
|
||||||
|
func (r *Request) Patch(url string) (*Response, error) {
|
||||||
|
return r.Execute(MethodPatch, url)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Execute method performs the HTTP request with given HTTP method and URL
|
||||||
|
// for current `Request`.
|
||||||
|
// resp, err := resty.R().Execute(resty.GET, "http://httpbin.org/get")
|
||||||
|
//
|
||||||
|
func (r *Request) Execute(method, url string) (*Response, error) {
|
||||||
|
var addrs []*net.SRV
|
||||||
|
var err error
|
||||||
|
|
||||||
|
if r.isMultiPart && !(method == MethodPost || method == MethodPut) {
|
||||||
|
return nil, fmt.Errorf("multipart content is not allowed in HTTP verb [%v]", method)
|
||||||
|
}
|
||||||
|
|
||||||
|
if r.SRV != nil {
|
||||||
|
_, addrs, err = net.LookupSRV(r.SRV.Service, "tcp", r.SRV.Domain)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
r.Method = method
|
||||||
|
r.URL = r.selectAddr(addrs, url, 0)
|
||||||
|
|
||||||
|
if r.client.RetryCount == 0 {
|
||||||
|
return r.client.execute(r)
|
||||||
|
}
|
||||||
|
|
||||||
|
var resp *Response
|
||||||
|
attempt := 0
|
||||||
|
_ = Backoff(
|
||||||
|
func() (*Response, error) {
|
||||||
|
attempt++
|
||||||
|
|
||||||
|
r.URL = r.selectAddr(addrs, url, attempt)
|
||||||
|
|
||||||
|
resp, err = r.client.execute(r)
|
||||||
|
if err != nil {
|
||||||
|
r.client.Log.Printf("ERROR %v, Attempt %v", err, attempt)
|
||||||
|
if r.isContextCancelledIfAvailable() {
|
||||||
|
// stop Backoff from retrying request if request has been
|
||||||
|
// canceled by context
|
||||||
|
return resp, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return resp, err
|
||||||
|
},
|
||||||
|
Retries(r.client.RetryCount),
|
||||||
|
WaitTime(r.client.RetryWaitTime),
|
||||||
|
MaxWaitTime(r.client.RetryMaxWaitTime),
|
||||||
|
RetryConditions(r.client.RetryConditions),
|
||||||
|
)
|
||||||
|
|
||||||
|
return resp, err
|
||||||
|
}
|
||||||
|
|
||||||
|
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
|
||||||
|
// Request Unexported methods
|
||||||
|
//___________________________________
|
||||||
|
|
||||||
|
func (r *Request) fmtBodyString() (body string) {
|
||||||
|
body = "***** NO CONTENT *****"
|
||||||
|
if isPayloadSupported(r.Method, r.client.AllowGetMethodPayload) {
|
||||||
|
if _, ok := r.Body.(io.Reader); ok {
|
||||||
|
body = "***** BODY IS io.Reader *****"
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// multipart or form-data
|
||||||
|
if r.isMultiPart || r.isFormData {
|
||||||
|
body = r.bodyBuf.String()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// request body data
|
||||||
|
if r.Body == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var prtBodyBytes []byte
|
||||||
|
var err error
|
||||||
|
|
||||||
|
contentType := r.Header.Get(hdrContentTypeKey)
|
||||||
|
kind := kindOf(r.Body)
|
||||||
|
if canJSONMarshal(contentType, kind) {
|
||||||
|
prtBodyBytes, err = json.MarshalIndent(&r.Body, "", " ")
|
||||||
|
} else if IsXMLType(contentType) && (kind == reflect.Struct) {
|
||||||
|
prtBodyBytes, err = xml.MarshalIndent(&r.Body, "", " ")
|
||||||
|
} else if b, ok := r.Body.(string); ok {
|
||||||
|
if IsJSONType(contentType) {
|
||||||
|
bodyBytes := []byte(b)
|
||||||
|
out := acquireBuffer()
|
||||||
|
defer releaseBuffer(out)
|
||||||
|
if err = json.Indent(out, bodyBytes, "", " "); err == nil {
|
||||||
|
prtBodyBytes = out.Bytes()
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
body = b
|
||||||
|
return
|
||||||
|
}
|
||||||
|
} else if b, ok := r.Body.([]byte); ok {
|
||||||
|
body = base64.StdEncoding.EncodeToString(b)
|
||||||
|
}
|
||||||
|
|
||||||
|
if prtBodyBytes != nil && err == nil {
|
||||||
|
body = string(prtBodyBytes)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Request) selectAddr(addrs []*net.SRV, path string, attempt int) string {
|
||||||
|
if addrs == nil {
|
||||||
|
return path
|
||||||
|
}
|
||||||
|
|
||||||
|
idx := attempt % len(addrs)
|
||||||
|
domain := strings.TrimRight(addrs[idx].Target, ".")
|
||||||
|
path = strings.TrimLeft(path, "/")
|
||||||
|
|
||||||
|
return fmt.Sprintf("%s://%s:%d/%s", r.client.scheme, domain, addrs[idx].Port, path)
|
||||||
|
}
|
63
vendor/github.com/go-resty/resty/request16.go
generated
vendored
Normal file
63
vendor/github.com/go-resty/resty/request16.go
generated
vendored
Normal file
|
@ -0,0 +1,63 @@
|
||||||
|
// +build !go1.7
|
||||||
|
|
||||||
|
// Copyright (c) 2015-2018 Jeevanandam M (jeeva@myjeeva.com)
|
||||||
|
// 2016 Andrew Grigorev (https://github.com/ei-grad)
|
||||||
|
// All rights reserved.
|
||||||
|
// resty source code and usage is governed by a MIT style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package resty
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Request type is used to compose and send individual request from client
|
||||||
|
// go-resty is provide option override client level settings such as
|
||||||
|
// Auth Token, Basic Auth credentials, Header, Query Param, Form Data, Error object
|
||||||
|
// and also you can add more options for that particular request
|
||||||
|
type Request struct {
|
||||||
|
URL string
|
||||||
|
Method string
|
||||||
|
Token string
|
||||||
|
QueryParam url.Values
|
||||||
|
FormData url.Values
|
||||||
|
Header http.Header
|
||||||
|
Time time.Time
|
||||||
|
Body interface{}
|
||||||
|
Result interface{}
|
||||||
|
Error interface{}
|
||||||
|
RawRequest *http.Request
|
||||||
|
SRV *SRVRecord
|
||||||
|
UserInfo *User
|
||||||
|
|
||||||
|
isMultiPart bool
|
||||||
|
isFormData bool
|
||||||
|
setContentLength bool
|
||||||
|
isSaveResponse bool
|
||||||
|
notParseResponse bool
|
||||||
|
jsonEscapeHTML bool
|
||||||
|
outputFile string
|
||||||
|
fallbackContentType string
|
||||||
|
pathParams map[string]string
|
||||||
|
client *Client
|
||||||
|
bodyBuf *bytes.Buffer
|
||||||
|
multipartFiles []*File
|
||||||
|
multipartFields []*multipartField
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Request) addContextIfAvailable() {
|
||||||
|
// nothing to do for golang<1.7
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Request) isContextCancelledIfAvailable() bool {
|
||||||
|
// just always return false golang<1.7
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// for !go1.7
|
||||||
|
var noescapeJSONMarshal = json.Marshal
|
87
vendor/github.com/go-resty/resty/request17.go
generated
vendored
Normal file
87
vendor/github.com/go-resty/resty/request17.go
generated
vendored
Normal file
|
@ -0,0 +1,87 @@
|
||||||
|
// +build go1.7 go1.8
|
||||||
|
|
||||||
|
// Copyright (c) 2015-2018 Jeevanandam M (jeeva@myjeeva.com)
|
||||||
|
// 2016 Andrew Grigorev (https://github.com/ei-grad)
|
||||||
|
// All rights reserved.
|
||||||
|
// resty source code and usage is governed by a MIT style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package resty
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Request type is used to compose and send individual request from client
|
||||||
|
// go-resty is provide option override client level settings such as
|
||||||
|
// Auth Token, Basic Auth credentials, Header, Query Param, Form Data, Error object
|
||||||
|
// and also you can add more options for that particular request
|
||||||
|
type Request struct {
|
||||||
|
URL string
|
||||||
|
Method string
|
||||||
|
Token string
|
||||||
|
QueryParam url.Values
|
||||||
|
FormData url.Values
|
||||||
|
Header http.Header
|
||||||
|
Time time.Time
|
||||||
|
Body interface{}
|
||||||
|
Result interface{}
|
||||||
|
Error interface{}
|
||||||
|
RawRequest *http.Request
|
||||||
|
SRV *SRVRecord
|
||||||
|
UserInfo *User
|
||||||
|
|
||||||
|
isMultiPart bool
|
||||||
|
isFormData bool
|
||||||
|
setContentLength bool
|
||||||
|
isSaveResponse bool
|
||||||
|
notParseResponse bool
|
||||||
|
jsonEscapeHTML bool
|
||||||
|
outputFile string
|
||||||
|
fallbackContentType string
|
||||||
|
ctx context.Context
|
||||||
|
pathParams map[string]string
|
||||||
|
client *Client
|
||||||
|
bodyBuf *bytes.Buffer
|
||||||
|
multipartFiles []*File
|
||||||
|
multipartFields []*multipartField
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetContext method sets the context.Context for current Request. It allows
|
||||||
|
// to interrupt the request execution if ctx.Done() channel is closed.
|
||||||
|
// See https://blog.golang.org/context article and the "context" package
|
||||||
|
// documentation.
|
||||||
|
func (r *Request) SetContext(ctx context.Context) *Request {
|
||||||
|
r.ctx = ctx
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Request) addContextIfAvailable() {
|
||||||
|
if r.ctx != nil {
|
||||||
|
r.RawRequest = r.RawRequest.WithContext(r.ctx)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Request) isContextCancelledIfAvailable() bool {
|
||||||
|
if r.ctx != nil {
|
||||||
|
if r.ctx.Err() != nil {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// for go1.7+
|
||||||
|
var noescapeJSONMarshal = func(v interface{}) ([]byte, error) {
|
||||||
|
buf := acquireBuffer()
|
||||||
|
defer releaseBuffer(buf)
|
||||||
|
encoder := json.NewEncoder(buf)
|
||||||
|
encoder.SetEscapeHTML(false)
|
||||||
|
err := encoder.Encode(v)
|
||||||
|
return buf.Bytes(), err
|
||||||
|
}
|
150
vendor/github.com/go-resty/resty/response.go
generated
vendored
Normal file
150
vendor/github.com/go-resty/resty/response.go
generated
vendored
Normal file
|
@ -0,0 +1,150 @@
|
||||||
|
// Copyright (c) 2015-2018 Jeevanandam M (jeeva@myjeeva.com), All rights reserved.
|
||||||
|
// resty source code and usage is governed by a MIT style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package resty
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Response is an object represents executed request and its values.
|
||||||
|
type Response struct {
|
||||||
|
Request *Request
|
||||||
|
RawResponse *http.Response
|
||||||
|
|
||||||
|
body []byte
|
||||||
|
size int64
|
||||||
|
receivedAt time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
// Body method returns HTTP response as []byte array for the executed request.
|
||||||
|
// Note: `Response.Body` might be nil, if `Request.SetOutput` is used.
|
||||||
|
func (r *Response) Body() []byte {
|
||||||
|
if r.RawResponse == nil {
|
||||||
|
return []byte{}
|
||||||
|
}
|
||||||
|
return r.body
|
||||||
|
}
|
||||||
|
|
||||||
|
// Status method returns the HTTP status string for the executed request.
|
||||||
|
// Example: 200 OK
|
||||||
|
func (r *Response) Status() string {
|
||||||
|
if r.RawResponse == nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
return r.RawResponse.Status
|
||||||
|
}
|
||||||
|
|
||||||
|
// StatusCode method returns the HTTP status code for the executed request.
|
||||||
|
// Example: 200
|
||||||
|
func (r *Response) StatusCode() int {
|
||||||
|
if r.RawResponse == nil {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
return r.RawResponse.StatusCode
|
||||||
|
}
|
||||||
|
|
||||||
|
// Result method returns the response value as an object if it has one
|
||||||
|
func (r *Response) Result() interface{} {
|
||||||
|
return r.Request.Result
|
||||||
|
}
|
||||||
|
|
||||||
|
// Error method returns the error object if it has one
|
||||||
|
func (r *Response) Error() interface{} {
|
||||||
|
return r.Request.Error
|
||||||
|
}
|
||||||
|
|
||||||
|
// Header method returns the response headers
|
||||||
|
func (r *Response) Header() http.Header {
|
||||||
|
if r.RawResponse == nil {
|
||||||
|
return http.Header{}
|
||||||
|
}
|
||||||
|
|
||||||
|
return r.RawResponse.Header
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cookies method to access all the response cookies
|
||||||
|
func (r *Response) Cookies() []*http.Cookie {
|
||||||
|
if r.RawResponse == nil {
|
||||||
|
return make([]*http.Cookie, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
return r.RawResponse.Cookies()
|
||||||
|
}
|
||||||
|
|
||||||
|
// String method returns the body of the server response as String.
|
||||||
|
func (r *Response) String() string {
|
||||||
|
if r.body == nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
return strings.TrimSpace(string(r.body))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Time method returns the time of HTTP response time that from request we sent and received a request.
|
||||||
|
// See `response.ReceivedAt` to know when client recevied response and see `response.Request.Time` to know
|
||||||
|
// when client sent a request.
|
||||||
|
func (r *Response) Time() time.Duration {
|
||||||
|
return r.receivedAt.Sub(r.Request.Time)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ReceivedAt method returns when response got recevied from server for the request.
|
||||||
|
func (r *Response) ReceivedAt() time.Time {
|
||||||
|
return r.receivedAt
|
||||||
|
}
|
||||||
|
|
||||||
|
// Size method returns the HTTP response size in bytes. Ya, you can relay on HTTP `Content-Length` header,
|
||||||
|
// however it won't be good for chucked transfer/compressed response. Since Resty calculates response size
|
||||||
|
// at the client end. You will get actual size of the http response.
|
||||||
|
func (r *Response) Size() int64 {
|
||||||
|
return r.size
|
||||||
|
}
|
||||||
|
|
||||||
|
// RawBody method exposes the HTTP raw response body. Use this method in-conjunction with `SetDoNotParseResponse`
|
||||||
|
// option otherwise you get an error as `read err: http: read on closed response body`.
|
||||||
|
//
|
||||||
|
// Do not forget to close the body, otherwise you might get into connection leaks, no connection reuse.
|
||||||
|
// Basically you have taken over the control of response parsing from `Resty`.
|
||||||
|
func (r *Response) RawBody() io.ReadCloser {
|
||||||
|
if r.RawResponse == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return r.RawResponse.Body
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsSuccess method returns true if HTTP status code >= 200 and <= 299 otherwise false.
|
||||||
|
func (r *Response) IsSuccess() bool {
|
||||||
|
return r.StatusCode() > 199 && r.StatusCode() < 300
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsError method returns true if HTTP status code >= 400 otherwise false.
|
||||||
|
func (r *Response) IsError() bool {
|
||||||
|
return r.StatusCode() > 399
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Response) fmtBodyString(sl int64) string {
|
||||||
|
if r.body != nil {
|
||||||
|
if int64(len(r.body)) > sl {
|
||||||
|
return fmt.Sprintf("***** RESPONSE TOO LARGE (size - %d) *****", len(r.body))
|
||||||
|
}
|
||||||
|
ct := r.Header().Get(hdrContentTypeKey)
|
||||||
|
if IsJSONType(ct) {
|
||||||
|
out := acquireBuffer()
|
||||||
|
defer releaseBuffer(out)
|
||||||
|
if err := json.Indent(out, r.body, "", " "); err == nil {
|
||||||
|
return out.String()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return r.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
return "***** NO CONTENT *****"
|
||||||
|
}
|
9
vendor/github.com/go-resty/resty/resty.go
generated
vendored
Normal file
9
vendor/github.com/go-resty/resty/resty.go
generated
vendored
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
// Copyright (c) 2015-2018 Jeevanandam M (jeeva@myjeeva.com), All rights reserved.
|
||||||
|
// resty source code and usage is governed by a MIT style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
// Package resty provides simple HTTP and REST client for Go inspired by Ruby rest-client.
|
||||||
|
package resty
|
||||||
|
|
||||||
|
// Version # of resty
|
||||||
|
const Version = "1.9.1"
|
114
vendor/github.com/go-resty/resty/retry.go
generated
vendored
Normal file
114
vendor/github.com/go-resty/resty/retry.go
generated
vendored
Normal file
|
@ -0,0 +1,114 @@
|
||||||
|
// Copyright (c) 2015-2018 Jeevanandam M (jeeva@myjeeva.com), All rights reserved.
|
||||||
|
// resty source code and usage is governed by a MIT style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package resty
|
||||||
|
|
||||||
|
import (
|
||||||
|
"math"
|
||||||
|
"math/rand"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
defaultMaxRetries = 3
|
||||||
|
defaultWaitTime = time.Duration(100) * time.Millisecond
|
||||||
|
defaultMaxWaitTime = time.Duration(2000) * time.Millisecond
|
||||||
|
)
|
||||||
|
|
||||||
|
type (
|
||||||
|
// Option is to create convenient retry options like wait time, max retries, etc.
|
||||||
|
Option func(*Options)
|
||||||
|
|
||||||
|
// RetryConditionFunc type is for retry condition function
|
||||||
|
RetryConditionFunc func(*Response) (bool, error)
|
||||||
|
|
||||||
|
// Options to hold go-resty retry values
|
||||||
|
Options struct {
|
||||||
|
maxRetries int
|
||||||
|
waitTime time.Duration
|
||||||
|
maxWaitTime time.Duration
|
||||||
|
retryConditions []RetryConditionFunc
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
// Retries sets the max number of retries
|
||||||
|
func Retries(value int) Option {
|
||||||
|
return func(o *Options) {
|
||||||
|
o.maxRetries = value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// WaitTime sets the default wait time to sleep between requests
|
||||||
|
func WaitTime(value time.Duration) Option {
|
||||||
|
return func(o *Options) {
|
||||||
|
o.waitTime = value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// MaxWaitTime sets the max wait time to sleep between requests
|
||||||
|
func MaxWaitTime(value time.Duration) Option {
|
||||||
|
return func(o *Options) {
|
||||||
|
o.maxWaitTime = value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// RetryConditions sets the conditions that will be checked for retry.
|
||||||
|
func RetryConditions(conditions []RetryConditionFunc) Option {
|
||||||
|
return func(o *Options) {
|
||||||
|
o.retryConditions = conditions
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Backoff retries with increasing timeout duration up until X amount of retries
|
||||||
|
// (Default is 3 attempts, Override with option Retries(n))
|
||||||
|
func Backoff(operation func() (*Response, error), options ...Option) error {
|
||||||
|
// Defaults
|
||||||
|
opts := Options{
|
||||||
|
maxRetries: defaultMaxRetries,
|
||||||
|
waitTime: defaultWaitTime,
|
||||||
|
maxWaitTime: defaultMaxWaitTime,
|
||||||
|
retryConditions: []RetryConditionFunc{},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, o := range options {
|
||||||
|
o(&opts)
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
resp *Response
|
||||||
|
err error
|
||||||
|
)
|
||||||
|
base := float64(opts.waitTime) // Time to wait between each attempt
|
||||||
|
capLevel := float64(opts.maxWaitTime) // Maximum amount of wait time for the retry
|
||||||
|
for attempt := 0; attempt < opts.maxRetries; attempt++ {
|
||||||
|
resp, err = operation()
|
||||||
|
|
||||||
|
var needsRetry bool
|
||||||
|
var conditionErr error
|
||||||
|
for _, condition := range opts.retryConditions {
|
||||||
|
needsRetry, conditionErr = condition(resp)
|
||||||
|
if needsRetry || conditionErr != nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If the operation returned no error, there was no condition satisfied and
|
||||||
|
// there was no error caused by the conditional functions.
|
||||||
|
if err == nil && !needsRetry && conditionErr == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
// Adding capped exponential backup with jitter
|
||||||
|
// See the following article...
|
||||||
|
// http://www.awsarchitectureblog.com/2015/03/backoff.html
|
||||||
|
temp := math.Min(capLevel, base*math.Exp2(float64(attempt)))
|
||||||
|
sleepDuration := time.Duration(int(temp/2) + rand.Intn(int(temp/2)))
|
||||||
|
|
||||||
|
if sleepDuration < opts.waitTime {
|
||||||
|
sleepDuration = opts.waitTime
|
||||||
|
}
|
||||||
|
time.Sleep(sleepDuration)
|
||||||
|
}
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
253
vendor/github.com/go-resty/resty/util.go
generated
vendored
Normal file
253
vendor/github.com/go-resty/resty/util.go
generated
vendored
Normal file
|
@ -0,0 +1,253 @@
|
||||||
|
// Copyright (c) 2015-2018 Jeevanandam M (jeeva@myjeeva.com), All rights reserved.
|
||||||
|
// resty source code and usage is governed by a MIT style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package resty
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"encoding/xml"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"log"
|
||||||
|
"mime/multipart"
|
||||||
|
"net/http"
|
||||||
|
"net/textproto"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"reflect"
|
||||||
|
"runtime"
|
||||||
|
"sort"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
|
||||||
|
// Package Helper methods
|
||||||
|
//___________________________________
|
||||||
|
|
||||||
|
// IsStringEmpty method tells whether given string is empty or not
|
||||||
|
func IsStringEmpty(str string) bool {
|
||||||
|
return len(strings.TrimSpace(str)) == 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// DetectContentType method is used to figure out `Request.Body` content type for request header
|
||||||
|
func DetectContentType(body interface{}) string {
|
||||||
|
contentType := plainTextType
|
||||||
|
kind := kindOf(body)
|
||||||
|
switch kind {
|
||||||
|
case reflect.Struct, reflect.Map:
|
||||||
|
contentType = jsonContentType
|
||||||
|
case reflect.String:
|
||||||
|
contentType = plainTextType
|
||||||
|
default:
|
||||||
|
if b, ok := body.([]byte); ok {
|
||||||
|
contentType = http.DetectContentType(b)
|
||||||
|
} else if kind == reflect.Slice {
|
||||||
|
contentType = jsonContentType
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return contentType
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsJSONType method is to check JSON content type or not
|
||||||
|
func IsJSONType(ct string) bool {
|
||||||
|
return jsonCheck.MatchString(ct)
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsXMLType method is to check XML content type or not
|
||||||
|
func IsXMLType(ct string) bool {
|
||||||
|
return xmlCheck.MatchString(ct)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unmarshal content into object from JSON or XML
|
||||||
|
// Deprecated: kept for backward compatibility
|
||||||
|
func Unmarshal(ct string, b []byte, d interface{}) (err error) {
|
||||||
|
if IsJSONType(ct) {
|
||||||
|
err = json.Unmarshal(b, d)
|
||||||
|
} else if IsXMLType(ct) {
|
||||||
|
err = xml.Unmarshal(b, d)
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unmarshalc content into object from JSON or XML
|
||||||
|
func Unmarshalc(c *Client, ct string, b []byte, d interface{}) (err error) {
|
||||||
|
if IsJSONType(ct) {
|
||||||
|
err = c.JSONUnmarshal(b, d)
|
||||||
|
} else if IsXMLType(ct) {
|
||||||
|
err = xml.Unmarshal(b, d)
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// way to disable the HTML escape as opt-in
|
||||||
|
func jsonMarshal(c *Client, r *Request, d interface{}) ([]byte, error) {
|
||||||
|
if !r.jsonEscapeHTML {
|
||||||
|
return noescapeJSONMarshal(d)
|
||||||
|
} else if !c.jsonEscapeHTML {
|
||||||
|
return noescapeJSONMarshal(d)
|
||||||
|
}
|
||||||
|
return c.JSONMarshal(d)
|
||||||
|
}
|
||||||
|
|
||||||
|
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
|
||||||
|
// Package Unexported methods
|
||||||
|
//___________________________________
|
||||||
|
|
||||||
|
func firstNonEmpty(v ...string) string {
|
||||||
|
for _, s := range v {
|
||||||
|
if !IsStringEmpty(s) {
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func getLogger(w io.Writer) *log.Logger {
|
||||||
|
return log.New(w, "RESTY ", log.LstdFlags)
|
||||||
|
}
|
||||||
|
|
||||||
|
var quoteEscaper = strings.NewReplacer("\\", "\\\\", `"`, "\\\"")
|
||||||
|
|
||||||
|
func escapeQuotes(s string) string {
|
||||||
|
return quoteEscaper.Replace(s)
|
||||||
|
}
|
||||||
|
|
||||||
|
func createMultipartHeader(param, fileName, contentType string) textproto.MIMEHeader {
|
||||||
|
hdr := make(textproto.MIMEHeader)
|
||||||
|
hdr.Set("Content-Disposition", fmt.Sprintf(`form-data; name="%s"; filename="%s"`,
|
||||||
|
escapeQuotes(param), escapeQuotes(fileName)))
|
||||||
|
hdr.Set("Content-Type", contentType)
|
||||||
|
return hdr
|
||||||
|
}
|
||||||
|
|
||||||
|
func addMultipartFormField(w *multipart.Writer, mf *multipartField) error {
|
||||||
|
partWriter, err := w.CreatePart(createMultipartHeader(mf.Param, mf.FileName, mf.ContentType))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = io.Copy(partWriter, mf.Reader)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func writeMultipartFormFile(w *multipart.Writer, fieldName, fileName string, r io.Reader) error {
|
||||||
|
// Auto detect actual multipart content type
|
||||||
|
cbuf := make([]byte, 512)
|
||||||
|
size, err := r.Read(cbuf)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
partWriter, err := w.CreatePart(createMultipartHeader(fieldName, fileName, http.DetectContentType(cbuf)))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err = partWriter.Write(cbuf[:size]); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = io.Copy(partWriter, r)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func addFile(w *multipart.Writer, fieldName, path string) error {
|
||||||
|
file, err := os.Open(path)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer closeq(file)
|
||||||
|
return writeMultipartFormFile(w, fieldName, filepath.Base(path), file)
|
||||||
|
}
|
||||||
|
|
||||||
|
func addFileReader(w *multipart.Writer, f *File) error {
|
||||||
|
return writeMultipartFormFile(w, f.ParamName, f.Name, f.Reader)
|
||||||
|
}
|
||||||
|
|
||||||
|
func getPointer(v interface{}) interface{} {
|
||||||
|
vv := valueOf(v)
|
||||||
|
if vv.Kind() == reflect.Ptr {
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
return reflect.New(vv.Type()).Interface()
|
||||||
|
}
|
||||||
|
|
||||||
|
func isPayloadSupported(m string, allowMethodGet bool) bool {
|
||||||
|
return !(m == MethodHead || m == MethodOptions || (m == MethodGet && !allowMethodGet))
|
||||||
|
}
|
||||||
|
|
||||||
|
func typeOf(i interface{}) reflect.Type {
|
||||||
|
return indirect(valueOf(i)).Type()
|
||||||
|
}
|
||||||
|
|
||||||
|
func valueOf(i interface{}) reflect.Value {
|
||||||
|
return reflect.ValueOf(i)
|
||||||
|
}
|
||||||
|
|
||||||
|
func indirect(v reflect.Value) reflect.Value {
|
||||||
|
return reflect.Indirect(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
func kindOf(v interface{}) reflect.Kind {
|
||||||
|
return typeOf(v).Kind()
|
||||||
|
}
|
||||||
|
|
||||||
|
func createDirectory(dir string) (err error) {
|
||||||
|
if _, err = os.Stat(dir); err != nil {
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
if err = os.MkdirAll(dir, 0755); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func canJSONMarshal(contentType string, kind reflect.Kind) bool {
|
||||||
|
return IsJSONType(contentType) && (kind == reflect.Struct || kind == reflect.Map)
|
||||||
|
}
|
||||||
|
|
||||||
|
func functionName(i interface{}) string {
|
||||||
|
return runtime.FuncForPC(reflect.ValueOf(i).Pointer()).Name()
|
||||||
|
}
|
||||||
|
|
||||||
|
func acquireBuffer() *bytes.Buffer {
|
||||||
|
return bufPool.Get().(*bytes.Buffer)
|
||||||
|
}
|
||||||
|
|
||||||
|
func releaseBuffer(buf *bytes.Buffer) {
|
||||||
|
if buf != nil {
|
||||||
|
buf.Reset()
|
||||||
|
bufPool.Put(buf)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func closeq(v interface{}) {
|
||||||
|
if c, ok := v.(io.Closer); ok {
|
||||||
|
sliently(c.Close())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func sliently(_ ...interface{}) {}
|
||||||
|
|
||||||
|
func composeHeaders(hdrs http.Header) string {
|
||||||
|
var str []string
|
||||||
|
for _, k := range sortHeaderKeys(hdrs) {
|
||||||
|
str = append(str, fmt.Sprintf("%25s: %s", k, strings.Join(hdrs[k], ", ")))
|
||||||
|
}
|
||||||
|
return strings.Join(str, "\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
func sortHeaderKeys(hdrs http.Header) []string {
|
||||||
|
var keys []string
|
||||||
|
for key := range hdrs {
|
||||||
|
keys = append(keys, key)
|
||||||
|
}
|
||||||
|
sort.Strings(keys)
|
||||||
|
return keys
|
||||||
|
}
|
21
vendor/github.com/linode/linodego/LICENSE
generated
vendored
Normal file
21
vendor/github.com/linode/linodego/LICENSE
generated
vendored
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
MIT License
|
||||||
|
|
||||||
|
Copyright (c) 2017 Christopher "Chief" Najewicz
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
44
vendor/github.com/linode/linodego/account.go
generated
vendored
Normal file
44
vendor/github.com/linode/linodego/account.go
generated
vendored
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
package linodego
|
||||||
|
|
||||||
|
import "context"
|
||||||
|
|
||||||
|
// Account associated with the token in use
|
||||||
|
type Account struct {
|
||||||
|
FirstName string `json:"first_name"`
|
||||||
|
LastName string `json:"last_name"`
|
||||||
|
Email string `json:"email"`
|
||||||
|
Company string `json:"company"`
|
||||||
|
Address1 string `json:"address1"`
|
||||||
|
Address2 string `json:"address2"`
|
||||||
|
Balance float32 `json:"balance"`
|
||||||
|
City string `json:"city"`
|
||||||
|
State string `json:"state"`
|
||||||
|
Zip string `json:"zip"`
|
||||||
|
Country string `json:"country"`
|
||||||
|
TaxID string `json:"tax_id"`
|
||||||
|
CreditCard *CreditCard `json:"credit_card"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreditCard information associated with the Account.
|
||||||
|
type CreditCard struct {
|
||||||
|
LastFour string `json:"last_four"`
|
||||||
|
Expiry string `json:"expiry"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// fixDates converts JSON timestamps to Go time.Time values
|
||||||
|
func (v *Account) fixDates() *Account {
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetAccount gets the contact and billing information related to the Account
|
||||||
|
func (c *Client) GetAccount(ctx context.Context) (*Account, error) {
|
||||||
|
e, err := c.Account.Endpoint()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
r, err := coupleAPIErrors(c.R(ctx).SetResult(&Account{}).Get(e))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return r.Result().(*Account).fixDates(), nil
|
||||||
|
}
|
277
vendor/github.com/linode/linodego/account_events.go
generated
vendored
Normal file
277
vendor/github.com/linode/linodego/account_events.go
generated
vendored
Normal file
|
@ -0,0 +1,277 @@
|
||||||
|
package linodego
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Event represents an action taken on the Account.
|
||||||
|
type Event struct {
|
||||||
|
CreatedStr string `json:"created"`
|
||||||
|
|
||||||
|
// The unique ID of this Event.
|
||||||
|
ID int `json:"id"`
|
||||||
|
|
||||||
|
// Current status of the Event, Enum: "failed" "finished" "notification" "scheduled" "started"
|
||||||
|
Status EventStatus `json:"status"`
|
||||||
|
|
||||||
|
// The action that caused this Event. New actions may be added in the future.
|
||||||
|
Action EventAction `json:"action"`
|
||||||
|
|
||||||
|
// A percentage estimating the amount of time remaining for an Event. Returns null for notification events.
|
||||||
|
PercentComplete int `json:"percent_complete"`
|
||||||
|
|
||||||
|
// The rate of completion of the Event. Only some Events will return rate; for example, migration and resize Events.
|
||||||
|
Rate *string `json:"rate"`
|
||||||
|
|
||||||
|
// If this Event has been read.
|
||||||
|
Read bool `json:"read"`
|
||||||
|
|
||||||
|
// If this Event has been seen.
|
||||||
|
Seen bool `json:"seen"`
|
||||||
|
|
||||||
|
// The estimated time remaining until the completion of this Event. This value is only returned for in-progress events.
|
||||||
|
TimeRemainingMsg json.RawMessage `json:"time_remaining"`
|
||||||
|
TimeRemaining *int `json:"-"`
|
||||||
|
|
||||||
|
// The username of the User who caused the Event.
|
||||||
|
Username string `json:"username"`
|
||||||
|
|
||||||
|
// Detailed information about the Event's entity, including ID, type, label, and URL used to access it.
|
||||||
|
Entity *EventEntity `json:"entity"`
|
||||||
|
|
||||||
|
// When this Event was created.
|
||||||
|
Created *time.Time `json:"-"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// EventAction constants start with Action and include all known Linode API Event Actions.
|
||||||
|
type EventAction string
|
||||||
|
|
||||||
|
// EventAction constants represent the actions that cause an Event. New actions may be added in the future.
|
||||||
|
const (
|
||||||
|
ActionBackupsEnable EventAction = "backups_enable"
|
||||||
|
ActionBackupsCancel EventAction = "backups_cancel"
|
||||||
|
ActionBackupsRestore EventAction = "backups_restore"
|
||||||
|
ActionCommunityQuestionReply EventAction = "community_question_reply"
|
||||||
|
ActionCreateCardUpdated EventAction = "credit_card_updated"
|
||||||
|
ActionDiskCreate EventAction = "disk_create"
|
||||||
|
ActionDiskDelete EventAction = "disk_delete"
|
||||||
|
ActionDiskDuplicate EventAction = "disk_duplicate"
|
||||||
|
ActionDiskImagize EventAction = "disk_imagize"
|
||||||
|
ActionDiskResize EventAction = "disk_resize"
|
||||||
|
ActionDNSRecordCreate EventAction = "dns_record_create"
|
||||||
|
ActionDNSRecordDelete EventAction = "dns_record_delete"
|
||||||
|
ActionDNSZoneCreate EventAction = "dns_zone_create"
|
||||||
|
ActionDNSZoneDelete EventAction = "dns_zone_delete"
|
||||||
|
ActionImageDelete EventAction = "image_delete"
|
||||||
|
ActionLinodeAddIP EventAction = "linode_addip"
|
||||||
|
ActionLinodeBoot EventAction = "linode_boot"
|
||||||
|
ActionLinodeClone EventAction = "linode_clone"
|
||||||
|
ActionLinodeCreate EventAction = "linode_create"
|
||||||
|
ActionLinodeDelete EventAction = "linode_delete"
|
||||||
|
ActionLinodeDeleteIP EventAction = "linode_deleteip"
|
||||||
|
ActionLinodeMigrate EventAction = "linode_migrate"
|
||||||
|
ActionLinodeMutate EventAction = "linode_mutate"
|
||||||
|
ActionLinodeReboot EventAction = "linode_reboot"
|
||||||
|
ActionLinodeRebuild EventAction = "linode_rebuild"
|
||||||
|
ActionLinodeResize EventAction = "linode_resize"
|
||||||
|
ActionLinodeShutdown EventAction = "linode_shutdown"
|
||||||
|
ActionLinodeSnapshot EventAction = "linode_snapshot"
|
||||||
|
ActionLongviewClientCreate EventAction = "longviewclient_create"
|
||||||
|
ActionLongviewClientDelete EventAction = "longviewclient_delete"
|
||||||
|
ActionManagedDisabled EventAction = "managed_disabled"
|
||||||
|
ActionManagedEnabled EventAction = "managed_enabled"
|
||||||
|
ActionManagedServiceCreate EventAction = "managed_service_create"
|
||||||
|
ActionManagedServiceDelete EventAction = "managed_service_delete"
|
||||||
|
ActionNodebalancerCreate EventAction = "nodebalancer_create"
|
||||||
|
ActionNodebalancerDelete EventAction = "nodebalancer_delete"
|
||||||
|
ActionNodebalancerConfigCreate EventAction = "nodebalancer_config_create"
|
||||||
|
ActionNodebalancerConfigDelete EventAction = "nodebalancer_config_delete"
|
||||||
|
ActionPasswordReset EventAction = "password_reset"
|
||||||
|
ActionPaymentSubmitted EventAction = "payment_submitted"
|
||||||
|
ActionStackScriptCreate EventAction = "stackscript_create"
|
||||||
|
ActionStackScriptDelete EventAction = "stackscript_delete"
|
||||||
|
ActionStackScriptPublicize EventAction = "stackscript_publicize"
|
||||||
|
ActionStackScriptRevise EventAction = "stackscript_revise"
|
||||||
|
ActionTFADisabled EventAction = "tfa_disabled"
|
||||||
|
ActionTFAEnabled EventAction = "tfa_enabled"
|
||||||
|
ActionTicketAttachmentUpload EventAction = "ticket_attachment_upload"
|
||||||
|
ActionTicketCreate EventAction = "ticket_create"
|
||||||
|
ActionTicketReply EventAction = "ticket_reply"
|
||||||
|
ActionVolumeAttach EventAction = "volume_attach"
|
||||||
|
ActionVolumeClone EventAction = "volume_clone"
|
||||||
|
ActionVolumeCreate EventAction = "volume_create"
|
||||||
|
ActionVolumeDelte EventAction = "volume_delete"
|
||||||
|
ActionVolumeDetach EventAction = "volume_detach"
|
||||||
|
ActionVolumeResize EventAction = "volume_resize"
|
||||||
|
)
|
||||||
|
|
||||||
|
// EntityType constants start with Entity and include Linode API Event Entity Types
|
||||||
|
type EntityType string
|
||||||
|
|
||||||
|
// EntityType contants are the entities an Event can be related to
|
||||||
|
const (
|
||||||
|
EntityLinode EntityType = "linode"
|
||||||
|
EntityDisk EntityType = "disk"
|
||||||
|
)
|
||||||
|
|
||||||
|
// EventStatus constants start with Event and include Linode API Event Status values
|
||||||
|
type EventStatus string
|
||||||
|
|
||||||
|
// EventStatus constants reflect the current status of an Event
|
||||||
|
const (
|
||||||
|
EventFailed EventStatus = "failed"
|
||||||
|
EventFinished EventStatus = "finished"
|
||||||
|
EventNotification EventStatus = "notification"
|
||||||
|
EventScheduled EventStatus = "scheduled"
|
||||||
|
EventStarted EventStatus = "started"
|
||||||
|
)
|
||||||
|
|
||||||
|
// EventEntity provides detailed information about the Event's
|
||||||
|
// associated entity, including ID, Type, Label, and a URL that
|
||||||
|
// can be used to access it.
|
||||||
|
type EventEntity struct {
|
||||||
|
// ID may be a string or int, it depends on the EntityType
|
||||||
|
ID interface{} `json:"id"`
|
||||||
|
Label string `json:"label"`
|
||||||
|
Type EntityType `json:"type"`
|
||||||
|
URL string `json:"url"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// EventsPagedResponse represents a paginated Events API response
|
||||||
|
type EventsPagedResponse struct {
|
||||||
|
*PageOptions
|
||||||
|
Data []Event `json:"data"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// endpoint gets the endpoint URL for Event
|
||||||
|
func (EventsPagedResponse) endpoint(c *Client) string {
|
||||||
|
endpoint, err := c.Events.Endpoint()
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return endpoint
|
||||||
|
}
|
||||||
|
|
||||||
|
// endpointWithID gets the endpoint URL for a specific Event
|
||||||
|
func (e Event) endpointWithID(c *Client) string {
|
||||||
|
endpoint, err := c.Events.Endpoint()
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
endpoint = fmt.Sprintf("%s/%d", endpoint, e.ID)
|
||||||
|
return endpoint
|
||||||
|
}
|
||||||
|
|
||||||
|
// appendData appends Events when processing paginated Event responses
|
||||||
|
func (resp *EventsPagedResponse) appendData(r *EventsPagedResponse) {
|
||||||
|
resp.Data = append(resp.Data, r.Data...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ListEvents gets a collection of Event objects representing actions taken
|
||||||
|
// on the Account. The Events returned depend on the token grants and the grants
|
||||||
|
// of the associated user.
|
||||||
|
func (c *Client) ListEvents(ctx context.Context, opts *ListOptions) ([]Event, error) {
|
||||||
|
response := EventsPagedResponse{}
|
||||||
|
err := c.listHelper(ctx, &response, opts)
|
||||||
|
for i := range response.Data {
|
||||||
|
response.Data[i].fixDates()
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return response.Data, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetEvent gets the Event with the Event ID
|
||||||
|
func (c *Client) GetEvent(ctx context.Context, id int) (*Event, error) {
|
||||||
|
e, err := c.Events.Endpoint()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
e = fmt.Sprintf("%s/%d", e, id)
|
||||||
|
r, err := c.R(ctx).SetResult(&Event{}).Get(e)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return r.Result().(*Event).fixDates(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// fixDates converts JSON timestamps to Go time.Time values
|
||||||
|
func (e *Event) fixDates() *Event {
|
||||||
|
e.Created, _ = parseDates(e.CreatedStr)
|
||||||
|
e.TimeRemaining = unmarshalTimeRemaining(e.TimeRemainingMsg)
|
||||||
|
return e
|
||||||
|
}
|
||||||
|
|
||||||
|
// MarkEventRead marks a single Event as read.
|
||||||
|
func (c *Client) MarkEventRead(ctx context.Context, event *Event) error {
|
||||||
|
e := event.endpointWithID(c)
|
||||||
|
e = fmt.Sprintf("%s/read", e)
|
||||||
|
|
||||||
|
_, err := coupleAPIErrors(c.R(ctx).Post(e))
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// MarkEventsSeen marks all Events up to and including this Event by ID as seen.
|
||||||
|
func (c *Client) MarkEventsSeen(ctx context.Context, event *Event) error {
|
||||||
|
e := event.endpointWithID(c)
|
||||||
|
e = fmt.Sprintf("%s/seen", e)
|
||||||
|
|
||||||
|
_, err := coupleAPIErrors(c.R(ctx).Post(e))
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func unmarshalTimeRemaining(m json.RawMessage) *int {
|
||||||
|
jsonBytes, err := m.MarshalJSON()
|
||||||
|
if err != nil {
|
||||||
|
panic(jsonBytes)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(jsonBytes) == 4 && string(jsonBytes) == "null" {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var timeStr string
|
||||||
|
if err := json.Unmarshal(jsonBytes, &timeStr); err == nil && len(timeStr) > 0 {
|
||||||
|
if dur, err := durationToSeconds(timeStr); err != nil {
|
||||||
|
panic(err)
|
||||||
|
} else {
|
||||||
|
return &dur
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
var intPtr int
|
||||||
|
if err := json.Unmarshal(jsonBytes, &intPtr); err == nil {
|
||||||
|
return &intPtr
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Println("[WARN] Unexpected unmarshalTimeRemaining value: ", jsonBytes)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// durationToSeconds takes a hh:mm:ss string and returns the number of seconds
|
||||||
|
func durationToSeconds(s string) (int, error) {
|
||||||
|
multipliers := [3]int{60 * 60, 60, 1}
|
||||||
|
segs := strings.Split(s, ":")
|
||||||
|
if len(segs) > len(multipliers) {
|
||||||
|
return 0, fmt.Errorf("too many ':' separators in time duration: %s", s)
|
||||||
|
}
|
||||||
|
var d int
|
||||||
|
l := len(segs)
|
||||||
|
for i := 0; i < l; i++ {
|
||||||
|
m, err := strconv.Atoi(segs[i])
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
d += m * multipliers[i+len(multipliers)-l]
|
||||||
|
}
|
||||||
|
return d, nil
|
||||||
|
}
|
125
vendor/github.com/linode/linodego/account_invoices.go
generated
vendored
Normal file
125
vendor/github.com/linode/linodego/account_invoices.go
generated
vendored
Normal file
|
@ -0,0 +1,125 @@
|
||||||
|
package linodego
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Invoice structs reflect an invoice for billable activity on the account.
|
||||||
|
type Invoice struct {
|
||||||
|
DateStr string `json:"date"`
|
||||||
|
|
||||||
|
ID int `json:"id"`
|
||||||
|
Label string `json:"label"`
|
||||||
|
Total float32 `json:"total"`
|
||||||
|
Date *time.Time `json:"-"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// InvoiceItem structs reflect an single billable activity associate with an Invoice
|
||||||
|
type InvoiceItem struct {
|
||||||
|
FromStr string `json:"from"`
|
||||||
|
ToStr string `json:"to"`
|
||||||
|
|
||||||
|
Label string `json:"label"`
|
||||||
|
Type string `json:"type"`
|
||||||
|
UnitPrice int `json:"unitprice"`
|
||||||
|
Quantity int `json:"quantity"`
|
||||||
|
Amount float32 `json:"amount"`
|
||||||
|
From *time.Time `json:"-"`
|
||||||
|
To *time.Time `json:"-"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// InvoicesPagedResponse represents a paginated Invoice API response
|
||||||
|
type InvoicesPagedResponse struct {
|
||||||
|
*PageOptions
|
||||||
|
Data []Invoice `json:"data"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// endpoint gets the endpoint URL for Invoice
|
||||||
|
func (InvoicesPagedResponse) endpoint(c *Client) string {
|
||||||
|
endpoint, err := c.Invoices.Endpoint()
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return endpoint
|
||||||
|
}
|
||||||
|
|
||||||
|
// appendData appends Invoices when processing paginated Invoice responses
|
||||||
|
func (resp *InvoicesPagedResponse) appendData(r *InvoicesPagedResponse) {
|
||||||
|
resp.Data = append(resp.Data, r.Data...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ListInvoices gets a paginated list of Invoices against the Account
|
||||||
|
func (c *Client) ListInvoices(ctx context.Context, opts *ListOptions) ([]Invoice, error) {
|
||||||
|
response := InvoicesPagedResponse{}
|
||||||
|
err := c.listHelper(ctx, &response, opts)
|
||||||
|
for i := range response.Data {
|
||||||
|
response.Data[i].fixDates()
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return response.Data, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// fixDates converts JSON timestamps to Go time.Time values
|
||||||
|
func (v *Invoice) fixDates() *Invoice {
|
||||||
|
v.Date, _ = parseDates(v.DateStr)
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
|
||||||
|
// fixDates converts JSON timestamps to Go time.Time values
|
||||||
|
func (v *InvoiceItem) fixDates() *InvoiceItem {
|
||||||
|
v.From, _ = parseDates(v.FromStr)
|
||||||
|
v.To, _ = parseDates(v.ToStr)
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetInvoice gets the a single Invoice matching the provided ID
|
||||||
|
func (c *Client) GetInvoice(ctx context.Context, id int) (*Invoice, error) {
|
||||||
|
e, err := c.Invoices.Endpoint()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
e = fmt.Sprintf("%s/%d", e, id)
|
||||||
|
r, err := coupleAPIErrors(c.R(ctx).SetResult(&Invoice{}).Get(e))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return r.Result().(*Invoice).fixDates(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// InvoiceItemsPagedResponse represents a paginated Invoice Item API response
|
||||||
|
type InvoiceItemsPagedResponse struct {
|
||||||
|
*PageOptions
|
||||||
|
Data []InvoiceItem `json:"data"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// endpointWithID gets the endpoint URL for InvoiceItems associated with a specific Invoice
|
||||||
|
func (InvoiceItemsPagedResponse) endpointWithID(c *Client, id int) string {
|
||||||
|
endpoint, err := c.InvoiceItems.endpointWithID(id)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return endpoint
|
||||||
|
}
|
||||||
|
|
||||||
|
// appendData appends InvoiceItems when processing paginated Invoice Item responses
|
||||||
|
func (resp *InvoiceItemsPagedResponse) appendData(r *InvoiceItemsPagedResponse) {
|
||||||
|
resp.Data = append(resp.Data, r.Data...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ListInvoiceItems gets the invoice items associated with a specific Invoice
|
||||||
|
func (c *Client) ListInvoiceItems(ctx context.Context, id int, opts *ListOptions) ([]InvoiceItem, error) {
|
||||||
|
response := InvoiceItemsPagedResponse{}
|
||||||
|
err := c.listHelperWithID(ctx, &response, id, opts)
|
||||||
|
for i := range response.Data {
|
||||||
|
response.Data[i].fixDates()
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return response.Data, nil
|
||||||
|
}
|
73
vendor/github.com/linode/linodego/account_notifications.go
generated
vendored
Normal file
73
vendor/github.com/linode/linodego/account_notifications.go
generated
vendored
Normal file
|
@ -0,0 +1,73 @@
|
||||||
|
package linodego
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Notification represents a notification on an Account
|
||||||
|
type Notification struct {
|
||||||
|
UntilStr string `json:"until"`
|
||||||
|
WhenStr string `json:"when"`
|
||||||
|
|
||||||
|
Label string `json:"label"`
|
||||||
|
Message string `json:"message"`
|
||||||
|
Type string `json:"type"`
|
||||||
|
Severity string `json:"severity"`
|
||||||
|
Entity *NotificationEntity `json:"entity"`
|
||||||
|
Until *time.Time `json:"-"`
|
||||||
|
When *time.Time `json:"-"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// NotificationEntity adds detailed information about the Notification.
|
||||||
|
// This could refer to the ticket that triggered the notification, for example.
|
||||||
|
type NotificationEntity struct {
|
||||||
|
ID int `json:"id"`
|
||||||
|
Label string `json:"label"`
|
||||||
|
Type string `json:"type"`
|
||||||
|
URL string `json:"url"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// NotificationsPagedResponse represents a paginated Notifications API response
|
||||||
|
type NotificationsPagedResponse struct {
|
||||||
|
*PageOptions
|
||||||
|
Data []Notification `json:"data"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// endpoint gets the endpoint URL for Notification
|
||||||
|
func (NotificationsPagedResponse) endpoint(c *Client) string {
|
||||||
|
endpoint, err := c.Notifications.Endpoint()
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return endpoint
|
||||||
|
}
|
||||||
|
|
||||||
|
// appendData appends Notifications when processing paginated Notification responses
|
||||||
|
func (resp *NotificationsPagedResponse) appendData(r *NotificationsPagedResponse) {
|
||||||
|
resp.Data = append(resp.Data, r.Data...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ListNotifications gets a collection of Notification objects representing important,
|
||||||
|
// often time-sensitive items related to the Account. An account cannot interact directly with
|
||||||
|
// Notifications, and a Notification will disappear when the circumstances causing it
|
||||||
|
// have been resolved. For example, if the account has an important Ticket open, a response
|
||||||
|
// to the Ticket will dismiss the Notification.
|
||||||
|
func (c *Client) ListNotifications(ctx context.Context, opts *ListOptions) ([]Notification, error) {
|
||||||
|
response := NotificationsPagedResponse{}
|
||||||
|
err := c.listHelper(ctx, &response, opts)
|
||||||
|
for i := range response.Data {
|
||||||
|
response.Data[i].fixDates()
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return response.Data, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// fixDates converts JSON timestamps to Go time.Time values
|
||||||
|
func (v *Notification) fixDates() *Notification {
|
||||||
|
v.Until, _ = parseDates(v.UntilStr)
|
||||||
|
v.When, _ = parseDates(v.WhenStr)
|
||||||
|
return v
|
||||||
|
}
|
201
vendor/github.com/linode/linodego/client.go
generated
vendored
Normal file
201
vendor/github.com/linode/linodego/client.go
generated
vendored
Normal file
|
@ -0,0 +1,201 @@
|
||||||
|
package linodego
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"github.com/go-resty/resty"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// APIHost Linode API hostname
|
||||||
|
APIHost = "api.linode.com"
|
||||||
|
// APIVersion Linode API version
|
||||||
|
APIVersion = "v4"
|
||||||
|
// APIProto connect to API with http(s)
|
||||||
|
APIProto = "https"
|
||||||
|
// Version of linodego
|
||||||
|
Version = "0.5.1"
|
||||||
|
// APIEnvVar environment var to check for API token
|
||||||
|
APIEnvVar = "LINODE_TOKEN"
|
||||||
|
// APISecondsPerPoll how frequently to poll for new Events
|
||||||
|
APISecondsPerPoll = 10
|
||||||
|
)
|
||||||
|
|
||||||
|
// DefaultUserAgent is the default User-Agent sent in HTTP request headers
|
||||||
|
const DefaultUserAgent = "linodego " + Version + " https://github.com/linode/linodego"
|
||||||
|
|
||||||
|
var envDebug = false
|
||||||
|
|
||||||
|
// Client is a wrapper around the Resty client
|
||||||
|
type Client struct {
|
||||||
|
resty *resty.Client
|
||||||
|
userAgent string
|
||||||
|
resources map[string]*Resource
|
||||||
|
debug bool
|
||||||
|
|
||||||
|
Images *Resource
|
||||||
|
InstanceDisks *Resource
|
||||||
|
InstanceConfigs *Resource
|
||||||
|
InstanceSnapshots *Resource
|
||||||
|
InstanceIPs *Resource
|
||||||
|
InstanceVolumes *Resource
|
||||||
|
Instances *Resource
|
||||||
|
IPAddresses *Resource
|
||||||
|
IPv6Pools *Resource
|
||||||
|
IPv6Ranges *Resource
|
||||||
|
Regions *Resource
|
||||||
|
StackScripts *Resource
|
||||||
|
Volumes *Resource
|
||||||
|
Kernels *Resource
|
||||||
|
Types *Resource
|
||||||
|
Domains *Resource
|
||||||
|
DomainRecords *Resource
|
||||||
|
Longview *Resource
|
||||||
|
LongviewClients *Resource
|
||||||
|
LongviewSubscriptions *Resource
|
||||||
|
NodeBalancers *Resource
|
||||||
|
NodeBalancerConfigs *Resource
|
||||||
|
NodeBalancerNodes *Resource
|
||||||
|
SSHKeys *Resource
|
||||||
|
Tickets *Resource
|
||||||
|
Account *Resource
|
||||||
|
Invoices *Resource
|
||||||
|
InvoiceItems *Resource
|
||||||
|
Events *Resource
|
||||||
|
Notifications *Resource
|
||||||
|
Profile *Resource
|
||||||
|
Managed *Resource
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
// Wether or not we will enable Resty debugging output
|
||||||
|
if apiDebug, ok := os.LookupEnv("LINODE_DEBUG"); ok {
|
||||||
|
if parsed, err := strconv.ParseBool(apiDebug); err == nil {
|
||||||
|
envDebug = parsed
|
||||||
|
log.Println("[INFO] LINODE_DEBUG being set to", envDebug)
|
||||||
|
} else {
|
||||||
|
log.Println("[WARN] LINODE_DEBUG should be an integer, 0 or 1")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetUserAgent sets a custom user-agent for HTTP requests
|
||||||
|
func (c *Client) SetUserAgent(ua string) *Client {
|
||||||
|
c.userAgent = ua
|
||||||
|
c.resty.SetHeader("User-Agent", c.userAgent)
|
||||||
|
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
// R wraps resty's R method
|
||||||
|
func (c *Client) R(ctx context.Context) *resty.Request {
|
||||||
|
return c.resty.R().
|
||||||
|
ExpectContentType("application/json").
|
||||||
|
SetHeader("Content-Type", "application/json").
|
||||||
|
SetContext(ctx).
|
||||||
|
SetError(APIError{})
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetDebug sets the debug on resty's client
|
||||||
|
func (c *Client) SetDebug(debug bool) *Client {
|
||||||
|
c.debug = debug
|
||||||
|
c.resty.SetDebug(debug)
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetBaseURL sets the base URL of the Linode v4 API (https://api.linode.com/v4)
|
||||||
|
func (c *Client) SetBaseURL(url string) *Client {
|
||||||
|
c.resty.SetHostURL(url)
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
// Resource looks up a resource by name
|
||||||
|
func (c Client) Resource(resourceName string) *Resource {
|
||||||
|
selectedResource, ok := c.resources[resourceName]
|
||||||
|
if !ok {
|
||||||
|
log.Fatalf("Could not find resource named '%s', exiting.", resourceName)
|
||||||
|
}
|
||||||
|
return selectedResource
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewClient factory to create new Client struct
|
||||||
|
func NewClient(hc *http.Client) (client Client) {
|
||||||
|
restyClient := resty.NewWithClient(hc)
|
||||||
|
client.resty = restyClient
|
||||||
|
client.SetUserAgent(DefaultUserAgent)
|
||||||
|
client.SetBaseURL(fmt.Sprintf("%s://%s/%s", APIProto, APIHost, APIVersion))
|
||||||
|
|
||||||
|
resources := map[string]*Resource{
|
||||||
|
stackscriptsName: NewResource(&client, stackscriptsName, stackscriptsEndpoint, false, Stackscript{}, StackscriptsPagedResponse{}),
|
||||||
|
imagesName: NewResource(&client, imagesName, imagesEndpoint, false, Image{}, ImagesPagedResponse{}),
|
||||||
|
instancesName: NewResource(&client, instancesName, instancesEndpoint, false, Instance{}, InstancesPagedResponse{}),
|
||||||
|
instanceDisksName: NewResource(&client, instanceDisksName, instanceDisksEndpoint, true, InstanceDisk{}, InstanceDisksPagedResponse{}),
|
||||||
|
instanceConfigsName: NewResource(&client, instanceConfigsName, instanceConfigsEndpoint, true, InstanceConfig{}, InstanceConfigsPagedResponse{}),
|
||||||
|
instanceSnapshotsName: NewResource(&client, instanceSnapshotsName, instanceSnapshotsEndpoint, true, InstanceSnapshot{}, nil),
|
||||||
|
instanceIPsName: NewResource(&client, instanceIPsName, instanceIPsEndpoint, true, InstanceIP{}, nil), // really?
|
||||||
|
instanceVolumesName: NewResource(&client, instanceVolumesName, instanceVolumesEndpoint, true, nil, InstanceVolumesPagedResponse{}), // really?
|
||||||
|
ipaddressesName: NewResource(&client, ipaddressesName, ipaddressesEndpoint, false, nil, IPAddressesPagedResponse{}), // really?
|
||||||
|
ipv6poolsName: NewResource(&client, ipv6poolsName, ipv6poolsEndpoint, false, nil, IPv6PoolsPagedResponse{}), // really?
|
||||||
|
ipv6rangesName: NewResource(&client, ipv6rangesName, ipv6rangesEndpoint, false, IPv6Range{}, IPv6RangesPagedResponse{}),
|
||||||
|
regionsName: NewResource(&client, regionsName, regionsEndpoint, false, Region{}, RegionsPagedResponse{}),
|
||||||
|
volumesName: NewResource(&client, volumesName, volumesEndpoint, false, Volume{}, VolumesPagedResponse{}),
|
||||||
|
kernelsName: NewResource(&client, kernelsName, kernelsEndpoint, false, LinodeKernel{}, LinodeKernelsPagedResponse{}),
|
||||||
|
typesName: NewResource(&client, typesName, typesEndpoint, false, LinodeType{}, LinodeTypesPagedResponse{}),
|
||||||
|
domainsName: NewResource(&client, domainsName, domainsEndpoint, false, Domain{}, DomainsPagedResponse{}),
|
||||||
|
domainRecordsName: NewResource(&client, domainRecordsName, domainRecordsEndpoint, true, DomainRecord{}, DomainRecordsPagedResponse{}),
|
||||||
|
longviewName: NewResource(&client, longviewName, longviewEndpoint, false, nil, nil), // really?
|
||||||
|
longviewclientsName: NewResource(&client, longviewclientsName, longviewclientsEndpoint, false, LongviewClient{}, LongviewClientsPagedResponse{}),
|
||||||
|
longviewsubscriptionsName: NewResource(&client, longviewsubscriptionsName, longviewsubscriptionsEndpoint, false, LongviewSubscription{}, LongviewSubscriptionsPagedResponse{}),
|
||||||
|
nodebalancersName: NewResource(&client, nodebalancersName, nodebalancersEndpoint, false, NodeBalancer{}, NodeBalancerConfigsPagedResponse{}),
|
||||||
|
nodebalancerconfigsName: NewResource(&client, nodebalancerconfigsName, nodebalancerconfigsEndpoint, true, NodeBalancerConfig{}, NodeBalancerConfigsPagedResponse{}),
|
||||||
|
nodebalancernodesName: NewResource(&client, nodebalancernodesName, nodebalancernodesEndpoint, true, NodeBalancerNode{}, NodeBalancerNodesPagedResponse{}),
|
||||||
|
sshkeysName: NewResource(&client, sshkeysName, sshkeysEndpoint, false, SSHKey{}, SSHKeysPagedResponse{}),
|
||||||
|
ticketsName: NewResource(&client, ticketsName, ticketsEndpoint, false, Ticket{}, TicketsPagedResponse{}),
|
||||||
|
accountName: NewResource(&client, accountName, accountEndpoint, false, Account{}, nil), // really?
|
||||||
|
eventsName: NewResource(&client, eventsName, eventsEndpoint, false, Event{}, EventsPagedResponse{}),
|
||||||
|
invoicesName: NewResource(&client, invoicesName, invoicesEndpoint, false, Invoice{}, InvoicesPagedResponse{}),
|
||||||
|
invoiceItemsName: NewResource(&client, invoiceItemsName, invoiceItemsEndpoint, true, InvoiceItem{}, InvoiceItemsPagedResponse{}),
|
||||||
|
profileName: NewResource(&client, profileName, profileEndpoint, false, nil, nil), // really?
|
||||||
|
managedName: NewResource(&client, managedName, managedEndpoint, false, nil, nil), // really?
|
||||||
|
}
|
||||||
|
|
||||||
|
client.resources = resources
|
||||||
|
|
||||||
|
client.SetDebug(envDebug)
|
||||||
|
client.Images = resources[imagesName]
|
||||||
|
client.StackScripts = resources[stackscriptsName]
|
||||||
|
client.Instances = resources[instancesName]
|
||||||
|
client.Regions = resources[regionsName]
|
||||||
|
client.InstanceDisks = resources[instanceDisksName]
|
||||||
|
client.InstanceConfigs = resources[instanceConfigsName]
|
||||||
|
client.InstanceSnapshots = resources[instanceSnapshotsName]
|
||||||
|
client.InstanceIPs = resources[instanceIPsName]
|
||||||
|
client.InstanceVolumes = resources[instanceVolumesName]
|
||||||
|
client.IPAddresses = resources[ipaddressesName]
|
||||||
|
client.IPv6Pools = resources[ipv6poolsName]
|
||||||
|
client.IPv6Ranges = resources[ipv6rangesName]
|
||||||
|
client.Volumes = resources[volumesName]
|
||||||
|
client.Kernels = resources[kernelsName]
|
||||||
|
client.Types = resources[typesName]
|
||||||
|
client.Domains = resources[domainsName]
|
||||||
|
client.DomainRecords = resources[domainRecordsName]
|
||||||
|
client.Longview = resources[longviewName]
|
||||||
|
client.LongviewSubscriptions = resources[longviewsubscriptionsName]
|
||||||
|
client.NodeBalancers = resources[nodebalancersName]
|
||||||
|
client.NodeBalancerConfigs = resources[nodebalancerconfigsName]
|
||||||
|
client.NodeBalancerNodes = resources[nodebalancernodesName]
|
||||||
|
client.SSHKeys = resources[sshkeysName]
|
||||||
|
client.Tickets = resources[ticketsName]
|
||||||
|
client.Account = resources[accountName]
|
||||||
|
client.Events = resources[eventsName]
|
||||||
|
client.Invoices = resources[invoicesName]
|
||||||
|
client.Profile = resources[profileName]
|
||||||
|
client.Managed = resources[managedName]
|
||||||
|
return
|
||||||
|
}
|
211
vendor/github.com/linode/linodego/domain_records.go
generated
vendored
Normal file
211
vendor/github.com/linode/linodego/domain_records.go
generated
vendored
Normal file
|
@ -0,0 +1,211 @@
|
||||||
|
package linodego
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
// DomainRecord represents a DomainRecord object
|
||||||
|
type DomainRecord struct {
|
||||||
|
ID int `json:"id"`
|
||||||
|
Type DomainRecordType `json:"type"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
Target string `json:"target"`
|
||||||
|
Priority int `json:"priority"`
|
||||||
|
Weight int `json:"weight"`
|
||||||
|
Port int `json:"port"`
|
||||||
|
Service *string `json:"service"`
|
||||||
|
Protocol *string `json:"protocol"`
|
||||||
|
TTLSec int `json:"ttl_sec"`
|
||||||
|
Tag *string `json:"tag"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// DomainRecordCreateOptions fields are those accepted by CreateDomainRecord
|
||||||
|
type DomainRecordCreateOptions struct {
|
||||||
|
Type DomainRecordType `json:"type"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
Target string `json:"target"`
|
||||||
|
Priority *int `json:"priority,omitempty"`
|
||||||
|
Weight *int `json:"weight,omitempty"`
|
||||||
|
Port *int `json:"port,omitempty"`
|
||||||
|
Service *string `json:"service,omitempty"`
|
||||||
|
Protocol *string `json:"protocol,omitempty"`
|
||||||
|
TTLSec int `json:"ttl_sec,omitempty"` // 0 is not accepted by Linode, so can be omitted
|
||||||
|
Tag *string `json:"tag,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// DomainRecordUpdateOptions fields are those accepted by UpdateDomainRecord
|
||||||
|
type DomainRecordUpdateOptions struct {
|
||||||
|
Type DomainRecordType `json:"type,omitempty"`
|
||||||
|
Name string `json:"name,omitempty"`
|
||||||
|
Target string `json:"target,omitempty"`
|
||||||
|
Priority *int `json:"priority,omitempty"` // 0 is valid, so omit only nil values
|
||||||
|
Weight *int `json:"weight,omitempty"` // 0 is valid, so omit only nil values
|
||||||
|
Port *int `json:"port,omitempty"` // 0 is valid to spec, so omit only nil values
|
||||||
|
Service *string `json:"service,omitempty"`
|
||||||
|
Protocol *string `json:"protocol,omitempty"`
|
||||||
|
TTLSec int `json:"ttl_sec,omitempty"` // 0 is not accepted by Linode, so can be omitted
|
||||||
|
Tag *string `json:"tag,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// DomainRecordType constants start with RecordType and include Linode API Domain Record Types
|
||||||
|
type DomainRecordType string
|
||||||
|
|
||||||
|
// DomainRecordType contants are the DNS record types a DomainRecord can assign
|
||||||
|
const (
|
||||||
|
RecordTypeA DomainRecordType = "A"
|
||||||
|
RecordTypeAAAA DomainRecordType = "AAAA"
|
||||||
|
RecordTypeNS DomainRecordType = "NS"
|
||||||
|
RecordTypeMX DomainRecordType = "MX"
|
||||||
|
RecordTypeCNAME DomainRecordType = "CNAME"
|
||||||
|
RecordTypeTXT DomainRecordType = "TXT"
|
||||||
|
RecordTypeSRV DomainRecordType = "SRV"
|
||||||
|
RecordTypePTR DomainRecordType = "PTR"
|
||||||
|
RecordTypeCAA DomainRecordType = "CAA"
|
||||||
|
)
|
||||||
|
|
||||||
|
// GetUpdateOptions converts a DomainRecord to DomainRecordUpdateOptions for use in UpdateDomainRecord
|
||||||
|
func (d DomainRecord) GetUpdateOptions() (du DomainRecordUpdateOptions) {
|
||||||
|
du.Type = d.Type
|
||||||
|
du.Name = d.Name
|
||||||
|
du.Target = d.Target
|
||||||
|
du.Priority = copyInt(&d.Priority)
|
||||||
|
du.Weight = copyInt(&d.Weight)
|
||||||
|
du.Port = copyInt(&d.Port)
|
||||||
|
du.Service = copyString(d.Service)
|
||||||
|
du.Protocol = copyString(d.Protocol)
|
||||||
|
du.TTLSec = d.TTLSec
|
||||||
|
du.Tag = copyString(d.Tag)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func copyInt(iPtr *int) *int {
|
||||||
|
if iPtr == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
var t = *iPtr
|
||||||
|
return &t
|
||||||
|
}
|
||||||
|
|
||||||
|
func copyString(sPtr *string) *string {
|
||||||
|
if sPtr == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
var t = *sPtr
|
||||||
|
return &t
|
||||||
|
}
|
||||||
|
|
||||||
|
// DomainRecordsPagedResponse represents a paginated DomainRecord API response
|
||||||
|
type DomainRecordsPagedResponse struct {
|
||||||
|
*PageOptions
|
||||||
|
Data []DomainRecord `json:"data"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// endpoint gets the endpoint URL for InstanceConfig
|
||||||
|
func (DomainRecordsPagedResponse) endpointWithID(c *Client, id int) string {
|
||||||
|
endpoint, err := c.DomainRecords.endpointWithID(id)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return endpoint
|
||||||
|
}
|
||||||
|
|
||||||
|
// appendData appends DomainRecords when processing paginated DomainRecord responses
|
||||||
|
func (resp *DomainRecordsPagedResponse) appendData(r *DomainRecordsPagedResponse) {
|
||||||
|
resp.Data = append(resp.Data, r.Data...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ListDomainRecords lists DomainRecords
|
||||||
|
func (c *Client) ListDomainRecords(ctx context.Context, domainID int, opts *ListOptions) ([]DomainRecord, error) {
|
||||||
|
response := DomainRecordsPagedResponse{}
|
||||||
|
err := c.listHelperWithID(ctx, &response, domainID, opts)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return response.Data, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// fixDates converts JSON timestamps to Go time.Time values
|
||||||
|
func (d *DomainRecord) fixDates() *DomainRecord {
|
||||||
|
return d
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetDomainRecord gets the domainrecord with the provided ID
|
||||||
|
func (c *Client) GetDomainRecord(ctx context.Context, domainID int, id int) (*DomainRecord, error) {
|
||||||
|
e, err := c.DomainRecords.endpointWithID(domainID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
e = fmt.Sprintf("%s/%d", e, id)
|
||||||
|
r, err := coupleAPIErrors(c.R(ctx).SetResult(&DomainRecord{}).Get(e))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return r.Result().(*DomainRecord), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreateDomainRecord creates a DomainRecord
|
||||||
|
func (c *Client) CreateDomainRecord(ctx context.Context, domainID int, domainrecord DomainRecordCreateOptions) (*DomainRecord, error) {
|
||||||
|
var body string
|
||||||
|
e, err := c.DomainRecords.endpointWithID(domainID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
req := c.R(ctx).SetResult(&DomainRecord{})
|
||||||
|
|
||||||
|
bodyData, err := json.Marshal(domainrecord)
|
||||||
|
if err != nil {
|
||||||
|
return nil, NewError(err)
|
||||||
|
}
|
||||||
|
body = string(bodyData)
|
||||||
|
|
||||||
|
r, err := coupleAPIErrors(req.
|
||||||
|
SetBody(body).
|
||||||
|
Post(e))
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return r.Result().(*DomainRecord).fixDates(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateDomainRecord updates the DomainRecord with the specified id
|
||||||
|
func (c *Client) UpdateDomainRecord(ctx context.Context, domainID int, id int, domainrecord DomainRecordUpdateOptions) (*DomainRecord, error) {
|
||||||
|
var body string
|
||||||
|
e, err := c.DomainRecords.endpointWithID(domainID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
e = fmt.Sprintf("%s/%d", e, id)
|
||||||
|
|
||||||
|
req := c.R(ctx).SetResult(&DomainRecord{})
|
||||||
|
|
||||||
|
if bodyData, err := json.Marshal(domainrecord); err == nil {
|
||||||
|
body = string(bodyData)
|
||||||
|
} else {
|
||||||
|
return nil, NewError(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
r, err := coupleAPIErrors(req.
|
||||||
|
SetBody(body).
|
||||||
|
Put(e))
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return r.Result().(*DomainRecord).fixDates(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeleteDomainRecord deletes the DomainRecord with the specified id
|
||||||
|
func (c *Client) DeleteDomainRecord(ctx context.Context, domainID int, id int) error {
|
||||||
|
e, err := c.DomainRecords.endpointWithID(domainID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
e = fmt.Sprintf("%s/%d", e, id)
|
||||||
|
|
||||||
|
_, err = coupleAPIErrors(c.R(ctx).Delete(e))
|
||||||
|
return err
|
||||||
|
}
|
285
vendor/github.com/linode/linodego/domains.go
generated
vendored
Normal file
285
vendor/github.com/linode/linodego/domains.go
generated
vendored
Normal file
|
@ -0,0 +1,285 @@
|
||||||
|
package linodego
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Domain represents a Domain object
|
||||||
|
type Domain struct {
|
||||||
|
// This Domain's unique ID
|
||||||
|
ID int `json:"id"`
|
||||||
|
|
||||||
|
// The domain this Domain represents. These must be unique in our system; you cannot have two Domains representing the same domain.
|
||||||
|
Domain string `json:"domain"`
|
||||||
|
|
||||||
|
// If this Domain represents the authoritative source of information for the domain it describes, or if it is a read-only copy of a master (also called a slave).
|
||||||
|
Type DomainType `json:"type"` // Enum:"master" "slave"
|
||||||
|
|
||||||
|
// Deprecated: The group this Domain belongs to. This is for display purposes only.
|
||||||
|
Group string `json:"group"`
|
||||||
|
|
||||||
|
// Used to control whether this Domain is currently being rendered.
|
||||||
|
Status DomainStatus `json:"status"` // Enum:"disabled" "active" "edit_mode" "has_errors"
|
||||||
|
|
||||||
|
// A description for this Domain. This is for display purposes only.
|
||||||
|
Description string `json:"description"`
|
||||||
|
|
||||||
|
// Start of Authority email address. This is required for master Domains.
|
||||||
|
SOAEmail string `json:"soa_email"`
|
||||||
|
|
||||||
|
// The interval, in seconds, at which a failed refresh should be retried.
|
||||||
|
// Valid values are 300, 3600, 7200, 14400, 28800, 57600, 86400, 172800, 345600, 604800, 1209600, and 2419200 - any other value will be rounded to the nearest valid value.
|
||||||
|
RetrySec int `json:"retry_sec"`
|
||||||
|
|
||||||
|
// The IP addresses representing the master DNS for this Domain.
|
||||||
|
MasterIPs []string `json:"master_ips"`
|
||||||
|
|
||||||
|
// The list of IPs that may perform a zone transfer for this Domain. This is potentially dangerous, and should be set to an empty list unless you intend to use it.
|
||||||
|
AXfrIPs []string `json:"axfr_ips"`
|
||||||
|
|
||||||
|
// The amount of time in seconds that may pass before this Domain is no longer authoritative. Valid values are 300, 3600, 7200, 14400, 28800, 57600, 86400, 172800, 345600, 604800, 1209600, and 2419200 - any other value will be rounded to the nearest valid value.
|
||||||
|
ExpireSec int `json:"expire_sec"`
|
||||||
|
|
||||||
|
// The amount of time in seconds before this Domain should be refreshed. Valid values are 300, 3600, 7200, 14400, 28800, 57600, 86400, 172800, 345600, 604800, 1209600, and 2419200 - any other value will be rounded to the nearest valid value.
|
||||||
|
RefreshSec int `json:"refresh_sec"`
|
||||||
|
|
||||||
|
// "Time to Live" - the amount of time in seconds that this Domain's records may be cached by resolvers or other domain servers. Valid values are 300, 3600, 7200, 14400, 28800, 57600, 86400, 172800, 345600, 604800, 1209600, and 2419200 - any other value will be rounded to the nearest valid value.
|
||||||
|
TTLSec int `json:"ttl_sec"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// DomainCreateOptions fields are those accepted by CreateDomain
|
||||||
|
type DomainCreateOptions struct {
|
||||||
|
// The domain this Domain represents. These must be unique in our system; you cannot have two Domains representing the same domain.
|
||||||
|
Domain string `json:"domain"`
|
||||||
|
|
||||||
|
// If this Domain represents the authoritative source of information for the domain it describes, or if it is a read-only copy of a master (also called a slave).
|
||||||
|
// Enum:"master" "slave"
|
||||||
|
Type DomainType `json:"type"`
|
||||||
|
|
||||||
|
// Deprecated: The group this Domain belongs to. This is for display purposes only.
|
||||||
|
Group string `json:"group,omitempty"`
|
||||||
|
|
||||||
|
// Used to control whether this Domain is currently being rendered.
|
||||||
|
// Enum:"disabled" "active" "edit_mode" "has_errors"
|
||||||
|
Status DomainStatus `json:"status,omitempty"`
|
||||||
|
|
||||||
|
// A description for this Domain. This is for display purposes only.
|
||||||
|
Description string `json:"description,omitempty"`
|
||||||
|
|
||||||
|
// Start of Authority email address. This is required for master Domains.
|
||||||
|
SOAEmail string `json:"soa_email,omitempty"`
|
||||||
|
|
||||||
|
// The interval, in seconds, at which a failed refresh should be retried.
|
||||||
|
// Valid values are 300, 3600, 7200, 14400, 28800, 57600, 86400, 172800, 345600, 604800, 1209600, and 2419200 - any other value will be rounded to the nearest valid value.
|
||||||
|
RetrySec int `json:"retry_sec,omitempty"`
|
||||||
|
|
||||||
|
// The IP addresses representing the master DNS for this Domain.
|
||||||
|
MasterIPs []string `json:"master_ips,omitempty"`
|
||||||
|
|
||||||
|
// The list of IPs that may perform a zone transfer for this Domain. This is potentially dangerous, and should be set to an empty list unless you intend to use it.
|
||||||
|
AXfrIPs []string `json:"axfr_ips,omitempty"`
|
||||||
|
|
||||||
|
// The amount of time in seconds that may pass before this Domain is no longer authoritative. Valid values are 300, 3600, 7200, 14400, 28800, 57600, 86400, 172800, 345600, 604800, 1209600, and 2419200 - any other value will be rounded to the nearest valid value.
|
||||||
|
ExpireSec int `json:"expire_sec,omitempty"`
|
||||||
|
|
||||||
|
// The amount of time in seconds before this Domain should be refreshed. Valid values are 300, 3600, 7200, 14400, 28800, 57600, 86400, 172800, 345600, 604800, 1209600, and 2419200 - any other value will be rounded to the nearest valid value.
|
||||||
|
RefreshSec int `json:"refresh_sec,omitempty"`
|
||||||
|
|
||||||
|
// "Time to Live" - the amount of time in seconds that this Domain's records may be cached by resolvers or other domain servers. Valid values are 300, 3600, 7200, 14400, 28800, 57600, 86400, 172800, 345600, 604800, 1209600, and 2419200 - any other value will be rounded to the nearest valid value.
|
||||||
|
TTLSec int `json:"ttl_sec,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// DomainUpdateOptions converts a Domain to DomainUpdateOptions for use in UpdateDomain
|
||||||
|
type DomainUpdateOptions struct {
|
||||||
|
// The domain this Domain represents. These must be unique in our system; you cannot have two Domains representing the same domain.
|
||||||
|
Domain string `json:"domain,omitempty"`
|
||||||
|
|
||||||
|
// If this Domain represents the authoritative source of information for the domain it describes, or if it is a read-only copy of a master (also called a slave).
|
||||||
|
// Enum:"master" "slave"
|
||||||
|
Type DomainType `json:"type,omitempty"`
|
||||||
|
|
||||||
|
// Deprecated: The group this Domain belongs to. This is for display purposes only.
|
||||||
|
Group string `json:"group,omitempty"`
|
||||||
|
|
||||||
|
// Used to control whether this Domain is currently being rendered.
|
||||||
|
// Enum:"disabled" "active" "edit_mode" "has_errors"
|
||||||
|
Status DomainStatus `json:"status,omitempty"`
|
||||||
|
|
||||||
|
// A description for this Domain. This is for display purposes only.
|
||||||
|
Description string `json:"description,omitempty"`
|
||||||
|
|
||||||
|
// Start of Authority email address. This is required for master Domains.
|
||||||
|
SOAEmail string `json:"soa_email,omitempty"`
|
||||||
|
|
||||||
|
// The interval, in seconds, at which a failed refresh should be retried.
|
||||||
|
// Valid values are 300, 3600, 7200, 14400, 28800, 57600, 86400, 172800, 345600, 604800, 1209600, and 2419200 - any other value will be rounded to the nearest valid value.
|
||||||
|
RetrySec int `json:"retry_sec,omitempty"`
|
||||||
|
|
||||||
|
// The IP addresses representing the master DNS for this Domain.
|
||||||
|
MasterIPs []string `json:"master_ips,omitempty"`
|
||||||
|
|
||||||
|
// The list of IPs that may perform a zone transfer for this Domain. This is potentially dangerous, and should be set to an empty list unless you intend to use it.
|
||||||
|
AXfrIPs []string `json:"axfr_ips,omitempty"`
|
||||||
|
|
||||||
|
// The amount of time in seconds that may pass before this Domain is no longer authoritative. Valid values are 300, 3600, 7200, 14400, 28800, 57600, 86400, 172800, 345600, 604800, 1209600, and 2419200 - any other value will be rounded to the nearest valid value.
|
||||||
|
ExpireSec int `json:"expire_sec,omitempty"`
|
||||||
|
|
||||||
|
// The amount of time in seconds before this Domain should be refreshed. Valid values are 300, 3600, 7200, 14400, 28800, 57600, 86400, 172800, 345600, 604800, 1209600, and 2419200 - any other value will be rounded to the nearest valid value.
|
||||||
|
RefreshSec int `json:"refresh_sec,omitempty"`
|
||||||
|
|
||||||
|
// "Time to Live" - the amount of time in seconds that this Domain's records may be cached by resolvers or other domain servers. Valid values are 300, 3600, 7200, 14400, 28800, 57600, 86400, 172800, 345600, 604800, 1209600, and 2419200 - any other value will be rounded to the nearest valid value.
|
||||||
|
TTLSec int `json:"ttl_sec,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// DomainType constants start with DomainType and include Linode API Domain Type values
|
||||||
|
type DomainType string
|
||||||
|
|
||||||
|
// DomainType constants reflect the DNS zone type of a Domain
|
||||||
|
const (
|
||||||
|
DomainTypeMaster DomainType = "master"
|
||||||
|
DomainTypeSlave DomainType = "slave"
|
||||||
|
)
|
||||||
|
|
||||||
|
// DomainStatus constants start with DomainStatus and include Linode API Domain Status values
|
||||||
|
type DomainStatus string
|
||||||
|
|
||||||
|
// DomainStatus constants reflect the current status of a Domain
|
||||||
|
const (
|
||||||
|
DomainStatusDisabled DomainStatus = "disabled"
|
||||||
|
DomainStatusActive DomainStatus = "active"
|
||||||
|
DomainStatusEditMode DomainStatus = "edit_mode"
|
||||||
|
DomainStatusHasErrors DomainStatus = "has_errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
// GetUpdateOptions converts a Domain to DomainUpdateOptions for use in UpdateDomain
|
||||||
|
func (d Domain) GetUpdateOptions() (du DomainUpdateOptions) {
|
||||||
|
du.Domain = d.Domain
|
||||||
|
du.Type = d.Type
|
||||||
|
du.Group = d.Group
|
||||||
|
du.Status = d.Status
|
||||||
|
du.Description = d.Description
|
||||||
|
du.SOAEmail = d.SOAEmail
|
||||||
|
du.RetrySec = d.RetrySec
|
||||||
|
du.MasterIPs = d.MasterIPs
|
||||||
|
du.AXfrIPs = d.AXfrIPs
|
||||||
|
du.ExpireSec = d.ExpireSec
|
||||||
|
du.RefreshSec = d.RefreshSec
|
||||||
|
du.TTLSec = d.TTLSec
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// DomainsPagedResponse represents a paginated Domain API response
|
||||||
|
type DomainsPagedResponse struct {
|
||||||
|
*PageOptions
|
||||||
|
Data []Domain `json:"data"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// endpoint gets the endpoint URL for Domain
|
||||||
|
func (DomainsPagedResponse) endpoint(c *Client) string {
|
||||||
|
endpoint, err := c.Domains.Endpoint()
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return endpoint
|
||||||
|
}
|
||||||
|
|
||||||
|
// appendData appends Domains when processing paginated Domain responses
|
||||||
|
func (resp *DomainsPagedResponse) appendData(r *DomainsPagedResponse) {
|
||||||
|
resp.Data = append(resp.Data, r.Data...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ListDomains lists Domains
|
||||||
|
func (c *Client) ListDomains(ctx context.Context, opts *ListOptions) ([]Domain, error) {
|
||||||
|
response := DomainsPagedResponse{}
|
||||||
|
err := c.listHelper(ctx, &response, opts)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return response.Data, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// fixDates converts JSON timestamps to Go time.Time values
|
||||||
|
func (d *Domain) fixDates() *Domain {
|
||||||
|
return d
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetDomain gets the domain with the provided ID
|
||||||
|
func (c *Client) GetDomain(ctx context.Context, id int) (*Domain, error) {
|
||||||
|
e, err := c.Domains.Endpoint()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
e = fmt.Sprintf("%s/%d", e, id)
|
||||||
|
r, err := coupleAPIErrors(c.R(ctx).SetResult(&Domain{}).Get(e))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return r.Result().(*Domain).fixDates(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreateDomain creates a Domain
|
||||||
|
func (c *Client) CreateDomain(ctx context.Context, domain DomainCreateOptions) (*Domain, error) {
|
||||||
|
var body string
|
||||||
|
e, err := c.Domains.Endpoint()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
req := c.R(ctx).SetResult(&Domain{})
|
||||||
|
|
||||||
|
bodyData, err := json.Marshal(domain)
|
||||||
|
if err != nil {
|
||||||
|
return nil, NewError(err)
|
||||||
|
}
|
||||||
|
body = string(bodyData)
|
||||||
|
|
||||||
|
r, err := coupleAPIErrors(req.
|
||||||
|
SetBody(body).
|
||||||
|
Post(e))
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return r.Result().(*Domain).fixDates(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateDomain updates the Domain with the specified id
|
||||||
|
func (c *Client) UpdateDomain(ctx context.Context, id int, domain DomainUpdateOptions) (*Domain, error) {
|
||||||
|
var body string
|
||||||
|
e, err := c.Domains.Endpoint()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
e = fmt.Sprintf("%s/%d", e, id)
|
||||||
|
|
||||||
|
req := c.R(ctx).SetResult(&Domain{})
|
||||||
|
|
||||||
|
if bodyData, err := json.Marshal(domain); err == nil {
|
||||||
|
body = string(bodyData)
|
||||||
|
} else {
|
||||||
|
return nil, NewError(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
r, err := coupleAPIErrors(req.
|
||||||
|
SetBody(body).
|
||||||
|
Put(e))
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return r.Result().(*Domain).fixDates(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeleteDomain deletes the Domain with the specified id
|
||||||
|
func (c *Client) DeleteDomain(ctx context.Context, id int) error {
|
||||||
|
e, err := c.Domains.Endpoint()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
e = fmt.Sprintf("%s/%d", e, id)
|
||||||
|
|
||||||
|
_, err = coupleAPIErrors(c.R(ctx).Delete(e))
|
||||||
|
return err
|
||||||
|
}
|
109
vendor/github.com/linode/linodego/errors.go
generated
vendored
Normal file
109
vendor/github.com/linode/linodego/errors.go
generated
vendored
Normal file
|
@ -0,0 +1,109 @@
|
||||||
|
package linodego
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/go-resty/resty"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// ErrorFromString is the Code identifying Errors created by string types
|
||||||
|
ErrorFromString = 1
|
||||||
|
// ErrorFromError is the Code identifying Errors created by error types
|
||||||
|
ErrorFromError = 2
|
||||||
|
// ErrorFromStringer is the Code identifying Errors created by fmt.Stringer types
|
||||||
|
ErrorFromStringer = 3
|
||||||
|
)
|
||||||
|
|
||||||
|
// Error wraps the LinodeGo error with the relevant http.Response
|
||||||
|
type Error struct {
|
||||||
|
Response *http.Response
|
||||||
|
Code int
|
||||||
|
Message string
|
||||||
|
}
|
||||||
|
|
||||||
|
// APIErrorReason is an individual invalid request message returned by the Linode API
|
||||||
|
type APIErrorReason struct {
|
||||||
|
Reason string `json:"reason"`
|
||||||
|
Field string `json:"field"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r APIErrorReason) Error() string {
|
||||||
|
if len(r.Field) == 0 {
|
||||||
|
return r.Reason
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("[%s] %s", r.Field, r.Reason)
|
||||||
|
}
|
||||||
|
|
||||||
|
// APIError is the error-set returned by the Linode API when presented with an invalid request
|
||||||
|
type APIError struct {
|
||||||
|
Errors []APIErrorReason `json:"errors"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func coupleAPIErrors(r *resty.Response, err error) (*resty.Response, error) {
|
||||||
|
if err != nil {
|
||||||
|
return nil, NewError(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if r.Error() != nil {
|
||||||
|
apiError, ok := r.Error().(*APIError)
|
||||||
|
if !ok || (ok && len(apiError.Errors) == 0) {
|
||||||
|
return r, nil
|
||||||
|
}
|
||||||
|
return nil, NewError(r)
|
||||||
|
}
|
||||||
|
|
||||||
|
return r, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e APIError) Error() string {
|
||||||
|
var x []string
|
||||||
|
for _, msg := range e.Errors {
|
||||||
|
x = append(x, msg.Error())
|
||||||
|
}
|
||||||
|
return strings.Join(x, "; ")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g Error) Error() string {
|
||||||
|
return fmt.Sprintf("[%03d] %s", g.Code, g.Message)
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewError creates a linodego.Error with a Code identifying the source err type,
|
||||||
|
// - ErrorFromString (1) from a string
|
||||||
|
// - ErrorFromError (2) for an error
|
||||||
|
// - ErrorFromStringer (3) for a Stringer
|
||||||
|
// - HTTP Status Codes (100-600) for a resty.Response object
|
||||||
|
func NewError(err interface{}) *Error {
|
||||||
|
if err == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
switch e := err.(type) {
|
||||||
|
case *Error:
|
||||||
|
return e
|
||||||
|
case *resty.Response:
|
||||||
|
apiError, ok := e.Error().(*APIError)
|
||||||
|
|
||||||
|
if !ok {
|
||||||
|
log.Fatalln("Unexpected Resty Error Response")
|
||||||
|
}
|
||||||
|
|
||||||
|
return &Error{
|
||||||
|
Code: e.RawResponse.StatusCode,
|
||||||
|
Message: apiError.Error(),
|
||||||
|
Response: e.RawResponse,
|
||||||
|
}
|
||||||
|
case error:
|
||||||
|
return &Error{Code: ErrorFromError, Message: e.Error()}
|
||||||
|
case string:
|
||||||
|
return &Error{Code: ErrorFromString, Message: e}
|
||||||
|
case fmt.Stringer:
|
||||||
|
return &Error{Code: ErrorFromStringer, Message: e.String()}
|
||||||
|
default:
|
||||||
|
log.Fatalln("Unsupported type to linodego.NewError")
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
163
vendor/github.com/linode/linodego/images.go
generated
vendored
Normal file
163
vendor/github.com/linode/linodego/images.go
generated
vendored
Normal file
|
@ -0,0 +1,163 @@
|
||||||
|
package linodego
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Image represents a deployable Image object for use with Linode Instances
|
||||||
|
type Image struct {
|
||||||
|
CreatedStr string `json:"created"`
|
||||||
|
UpdatedStr string `json:"updated"`
|
||||||
|
ID string `json:"id"`
|
||||||
|
CreatedBy string `json:"created_by"`
|
||||||
|
Label string `json:"label"`
|
||||||
|
Description string `json:"description"`
|
||||||
|
Type string `json:"type"`
|
||||||
|
Vendor string `json:"vendor"`
|
||||||
|
Size int `json:"size"`
|
||||||
|
IsPublic bool `json:"is_public"`
|
||||||
|
Deprecated bool `json:"deprecated"`
|
||||||
|
|
||||||
|
Created *time.Time `json:"-"`
|
||||||
|
Updated *time.Time `json:"-"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// ImageCreateOptions fields are those accepted by CreateImage
|
||||||
|
type ImageCreateOptions struct {
|
||||||
|
DiskID int `json:"disk_id"`
|
||||||
|
Label string `json:"label"`
|
||||||
|
Description string `json:"description,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// ImageUpdateOptions fields are those accepted by UpdateImage
|
||||||
|
type ImageUpdateOptions struct {
|
||||||
|
Label string `json:"label,omitempty"`
|
||||||
|
Description *string `json:"description,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *Image) fixDates() *Image {
|
||||||
|
i.Created, _ = parseDates(i.CreatedStr)
|
||||||
|
i.Updated, _ = parseDates(i.UpdatedStr)
|
||||||
|
return i
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetUpdateOptions converts an Image to ImageUpdateOptions for use in UpdateImage
|
||||||
|
func (i Image) GetUpdateOptions() (iu ImageUpdateOptions) {
|
||||||
|
iu.Label = i.Label
|
||||||
|
iu.Description = copyString(&i.Description)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// ImagesPagedResponse represents a linode API response for listing of images
|
||||||
|
type ImagesPagedResponse struct {
|
||||||
|
*PageOptions
|
||||||
|
Data []Image `json:"data"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ImagesPagedResponse) endpoint(c *Client) string {
|
||||||
|
endpoint, err := c.Images.Endpoint()
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return endpoint
|
||||||
|
}
|
||||||
|
|
||||||
|
func (resp *ImagesPagedResponse) appendData(r *ImagesPagedResponse) {
|
||||||
|
resp.Data = append(resp.Data, r.Data...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ListImages lists Images
|
||||||
|
func (c *Client) ListImages(ctx context.Context, opts *ListOptions) ([]Image, error) {
|
||||||
|
response := ImagesPagedResponse{}
|
||||||
|
err := c.listHelper(ctx, &response, opts)
|
||||||
|
for i := range response.Data {
|
||||||
|
response.Data[i].fixDates()
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return response.Data, nil
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetImage gets the Image with the provided ID
|
||||||
|
func (c *Client) GetImage(ctx context.Context, id string) (*Image, error) {
|
||||||
|
e, err := c.Images.Endpoint()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
e = fmt.Sprintf("%s/%s", e, id)
|
||||||
|
r, err := coupleAPIErrors(c.Images.R(ctx).Get(e))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return r.Result().(*Image), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreateImage creates a Image
|
||||||
|
func (c *Client) CreateImage(ctx context.Context, createOpts ImageCreateOptions) (*Image, error) {
|
||||||
|
var body string
|
||||||
|
e, err := c.Images.Endpoint()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
req := c.R(ctx).SetResult(&Image{})
|
||||||
|
|
||||||
|
if bodyData, err := json.Marshal(createOpts); err == nil {
|
||||||
|
body = string(bodyData)
|
||||||
|
} else {
|
||||||
|
return nil, NewError(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
r, err := coupleAPIErrors(req.
|
||||||
|
SetBody(body).
|
||||||
|
Post(e))
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return r.Result().(*Image).fixDates(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateImage updates the Image with the specified id
|
||||||
|
func (c *Client) UpdateImage(ctx context.Context, id string, updateOpts ImageUpdateOptions) (*Image, error) {
|
||||||
|
var body string
|
||||||
|
e, err := c.Images.Endpoint()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
e = fmt.Sprintf("%s/%s", e, id)
|
||||||
|
|
||||||
|
req := c.R(ctx).SetResult(&Image{})
|
||||||
|
|
||||||
|
if bodyData, err := json.Marshal(updateOpts); err == nil {
|
||||||
|
body = string(bodyData)
|
||||||
|
} else {
|
||||||
|
return nil, NewError(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
r, err := coupleAPIErrors(req.
|
||||||
|
SetBody(body).
|
||||||
|
Put(e))
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return r.Result().(*Image).fixDates(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeleteImage deletes the Image with the specified id
|
||||||
|
func (c *Client) DeleteImage(ctx context.Context, id string) error {
|
||||||
|
e, err := c.Images.Endpoint()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
e = fmt.Sprintf("%s/%s", e, id)
|
||||||
|
|
||||||
|
_, err = coupleAPIErrors(c.R(ctx).Delete(e))
|
||||||
|
return err
|
||||||
|
}
|
246
vendor/github.com/linode/linodego/instance_configs.go
generated
vendored
Normal file
246
vendor/github.com/linode/linodego/instance_configs.go
generated
vendored
Normal file
|
@ -0,0 +1,246 @@
|
||||||
|
package linodego
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// InstanceConfig represents all of the settings that control the boot and run configuration of a Linode Instance
|
||||||
|
type InstanceConfig struct {
|
||||||
|
CreatedStr string `json:"created"`
|
||||||
|
UpdatedStr string `json:"updated"`
|
||||||
|
|
||||||
|
ID int `json:"id"`
|
||||||
|
Label string `json:"label"`
|
||||||
|
Comments string `json:"comments"`
|
||||||
|
Devices *InstanceConfigDeviceMap `json:"devices"`
|
||||||
|
Helpers *InstanceConfigHelpers `json:"helpers"`
|
||||||
|
MemoryLimit int `json:"memory_limit"`
|
||||||
|
Kernel string `json:"kernel"`
|
||||||
|
InitRD *int `json:"init_rd"`
|
||||||
|
RootDevice string `json:"root_device"`
|
||||||
|
RunLevel string `json:"run_level"`
|
||||||
|
VirtMode string `json:"virt_mode"`
|
||||||
|
Created *time.Time `json:"-"`
|
||||||
|
Updated *time.Time `json:"-"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// InstanceConfigDevice contains either the DiskID or VolumeID assigned to a Config Device
|
||||||
|
type InstanceConfigDevice struct {
|
||||||
|
DiskID int `json:"disk_id,omitempty"`
|
||||||
|
VolumeID int `json:"volume_id,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// InstanceConfigDeviceMap contains SDA-SDH InstanceConfigDevice settings
|
||||||
|
type InstanceConfigDeviceMap struct {
|
||||||
|
SDA *InstanceConfigDevice `json:"sda,omitempty"`
|
||||||
|
SDB *InstanceConfigDevice `json:"sdb,omitempty"`
|
||||||
|
SDC *InstanceConfigDevice `json:"sdc,omitempty"`
|
||||||
|
SDD *InstanceConfigDevice `json:"sdd,omitempty"`
|
||||||
|
SDE *InstanceConfigDevice `json:"sde,omitempty"`
|
||||||
|
SDF *InstanceConfigDevice `json:"sdf,omitempty"`
|
||||||
|
SDG *InstanceConfigDevice `json:"sdg,omitempty"`
|
||||||
|
SDH *InstanceConfigDevice `json:"sdh,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// InstanceConfigHelpers are Instance Config options that control Linux distribution specific tweaks
|
||||||
|
type InstanceConfigHelpers struct {
|
||||||
|
UpdateDBDisabled bool `json:"updatedb_disabled"`
|
||||||
|
Distro bool `json:"distro"`
|
||||||
|
ModulesDep bool `json:"modules_dep"`
|
||||||
|
Network bool `json:"network"`
|
||||||
|
DevTmpFsAutomount bool `json:"devtmpfs_automount"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// InstanceConfigsPagedResponse represents a paginated InstanceConfig API response
|
||||||
|
type InstanceConfigsPagedResponse struct {
|
||||||
|
*PageOptions
|
||||||
|
Data []InstanceConfig `json:"data"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// InstanceConfigCreateOptions are InstanceConfig settings that can be used at creation
|
||||||
|
type InstanceConfigCreateOptions struct {
|
||||||
|
Label string `json:"label,omitempty"`
|
||||||
|
Comments string `json:"comments,omitempty"`
|
||||||
|
Devices InstanceConfigDeviceMap `json:"devices"`
|
||||||
|
Helpers *InstanceConfigHelpers `json:"helpers,omitempty"`
|
||||||
|
MemoryLimit int `json:"memory_limit,omitempty"`
|
||||||
|
Kernel string `json:"kernel,omitempty"`
|
||||||
|
InitRD int `json:"init_rd,omitempty"`
|
||||||
|
RootDevice *string `json:"root_device,omitempty"`
|
||||||
|
RunLevel string `json:"run_level,omitempty"`
|
||||||
|
VirtMode string `json:"virt_mode,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// InstanceConfigUpdateOptions are InstanceConfig settings that can be used in updates
|
||||||
|
type InstanceConfigUpdateOptions struct {
|
||||||
|
Label string `json:"label,omitempty"`
|
||||||
|
Comments string `json:"comments"`
|
||||||
|
Devices *InstanceConfigDeviceMap `json:"devices,omitempty"`
|
||||||
|
Helpers *InstanceConfigHelpers `json:"helpers,omitempty"`
|
||||||
|
// MemoryLimit 0 means unlimitted, this is not omitted
|
||||||
|
MemoryLimit int `json:"memory_limit"`
|
||||||
|
Kernel string `json:"kernel,omitempty"`
|
||||||
|
// InitRD is nullable, permit the sending of null
|
||||||
|
InitRD *int `json:"init_rd"`
|
||||||
|
RootDevice string `json:"root_device,omitempty"`
|
||||||
|
RunLevel string `json:"run_level,omitempty"`
|
||||||
|
VirtMode string `json:"virt_mode,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetCreateOptions converts a InstanceConfig to InstanceConfigCreateOptions for use in CreateInstanceConfig
|
||||||
|
func (i InstanceConfig) GetCreateOptions() InstanceConfigCreateOptions {
|
||||||
|
initrd := 0
|
||||||
|
if i.InitRD != nil {
|
||||||
|
initrd = *i.InitRD
|
||||||
|
}
|
||||||
|
return InstanceConfigCreateOptions{
|
||||||
|
Label: i.Label,
|
||||||
|
Comments: i.Comments,
|
||||||
|
Devices: *i.Devices,
|
||||||
|
Helpers: i.Helpers,
|
||||||
|
MemoryLimit: i.MemoryLimit,
|
||||||
|
Kernel: i.Kernel,
|
||||||
|
InitRD: initrd,
|
||||||
|
RootDevice: copyString(&i.RootDevice),
|
||||||
|
RunLevel: i.RunLevel,
|
||||||
|
VirtMode: i.VirtMode,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetUpdateOptions converts a InstanceConfig to InstanceConfigUpdateOptions for use in UpdateInstanceConfig
|
||||||
|
func (i InstanceConfig) GetUpdateOptions() InstanceConfigUpdateOptions {
|
||||||
|
return InstanceConfigUpdateOptions{
|
||||||
|
Label: i.Label,
|
||||||
|
Comments: i.Comments,
|
||||||
|
Devices: i.Devices,
|
||||||
|
Helpers: i.Helpers,
|
||||||
|
MemoryLimit: i.MemoryLimit,
|
||||||
|
Kernel: i.Kernel,
|
||||||
|
InitRD: copyInt(i.InitRD),
|
||||||
|
RootDevice: i.RootDevice,
|
||||||
|
RunLevel: i.RunLevel,
|
||||||
|
VirtMode: i.VirtMode,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// endpointWithID gets the endpoint URL for InstanceConfigs of a given Instance
|
||||||
|
func (InstanceConfigsPagedResponse) endpointWithID(c *Client, id int) string {
|
||||||
|
endpoint, err := c.InstanceConfigs.endpointWithID(id)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return endpoint
|
||||||
|
}
|
||||||
|
|
||||||
|
// appendData appends InstanceConfigs when processing paginated InstanceConfig responses
|
||||||
|
func (resp *InstanceConfigsPagedResponse) appendData(r *InstanceConfigsPagedResponse) {
|
||||||
|
resp.Data = append(resp.Data, r.Data...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ListInstanceConfigs lists InstanceConfigs
|
||||||
|
func (c *Client) ListInstanceConfigs(ctx context.Context, linodeID int, opts *ListOptions) ([]InstanceConfig, error) {
|
||||||
|
response := InstanceConfigsPagedResponse{}
|
||||||
|
err := c.listHelperWithID(ctx, &response, linodeID, opts)
|
||||||
|
for i := range response.Data {
|
||||||
|
response.Data[i].fixDates()
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return response.Data, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// fixDates converts JSON timestamps to Go time.Time values
|
||||||
|
func (i *InstanceConfig) fixDates() *InstanceConfig {
|
||||||
|
i.Created, _ = parseDates(i.CreatedStr)
|
||||||
|
i.Updated, _ = parseDates(i.UpdatedStr)
|
||||||
|
return i
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetInstanceConfig gets the template with the provided ID
|
||||||
|
func (c *Client) GetInstanceConfig(ctx context.Context, linodeID int, configID int) (*InstanceConfig, error) {
|
||||||
|
e, err := c.InstanceConfigs.endpointWithID(linodeID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
e = fmt.Sprintf("%s/%d", e, configID)
|
||||||
|
r, err := coupleAPIErrors(c.R(ctx).SetResult(&InstanceConfig{}).Get(e))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return r.Result().(*InstanceConfig).fixDates(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreateInstanceConfig creates a new InstanceConfig for the given Instance
|
||||||
|
func (c *Client) CreateInstanceConfig(ctx context.Context, linodeID int, createOpts InstanceConfigCreateOptions) (*InstanceConfig, error) {
|
||||||
|
var body string
|
||||||
|
e, err := c.InstanceConfigs.endpointWithID(linodeID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
req := c.R(ctx).SetResult(&InstanceConfig{})
|
||||||
|
|
||||||
|
if bodyData, err := json.Marshal(createOpts); err == nil {
|
||||||
|
body = string(bodyData)
|
||||||
|
} else {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
r, err := coupleAPIErrors(req.
|
||||||
|
SetBody(body).
|
||||||
|
Post(e))
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return r.Result().(*InstanceConfig).fixDates(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateInstanceConfig update an InstanceConfig for the given Instance
|
||||||
|
func (c *Client) UpdateInstanceConfig(ctx context.Context, linodeID int, configID int, updateOpts InstanceConfigUpdateOptions) (*InstanceConfig, error) {
|
||||||
|
var body string
|
||||||
|
e, err := c.InstanceConfigs.endpointWithID(linodeID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
e = fmt.Sprintf("%s/%d", e, configID)
|
||||||
|
req := c.R(ctx).SetResult(&InstanceConfig{})
|
||||||
|
|
||||||
|
if bodyData, err := json.Marshal(updateOpts); err == nil {
|
||||||
|
body = string(bodyData)
|
||||||
|
} else {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
r, err := coupleAPIErrors(req.
|
||||||
|
SetBody(body).
|
||||||
|
Put(e))
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return r.Result().(*InstanceConfig).fixDates(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// RenameInstanceConfig renames an InstanceConfig
|
||||||
|
func (c *Client) RenameInstanceConfig(ctx context.Context, linodeID int, configID int, label string) (*InstanceConfig, error) {
|
||||||
|
return c.UpdateInstanceConfig(ctx, linodeID, configID, InstanceConfigUpdateOptions{Label: label})
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeleteInstanceConfig deletes a Linode InstanceConfig
|
||||||
|
func (c *Client) DeleteInstanceConfig(ctx context.Context, linodeID int, configID int) error {
|
||||||
|
e, err := c.InstanceConfigs.endpointWithID(linodeID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
e = fmt.Sprintf("%s/%d", e, configID)
|
||||||
|
|
||||||
|
_, err = coupleAPIErrors(c.R(ctx).Delete(e))
|
||||||
|
return err
|
||||||
|
}
|
217
vendor/github.com/linode/linodego/instance_disks.go
generated
vendored
Normal file
217
vendor/github.com/linode/linodego/instance_disks.go
generated
vendored
Normal file
|
@ -0,0 +1,217 @@
|
||||||
|
package linodego
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// InstanceDisk represents an Instance Disk object
|
||||||
|
type InstanceDisk struct {
|
||||||
|
CreatedStr string `json:"created"`
|
||||||
|
UpdatedStr string `json:"updated"`
|
||||||
|
|
||||||
|
ID int `json:"id"`
|
||||||
|
Label string `json:"label"`
|
||||||
|
Status string `json:"status"`
|
||||||
|
Size int `json:"size"`
|
||||||
|
Filesystem DiskFilesystem `json:"filesystem"`
|
||||||
|
Created time.Time `json:"-"`
|
||||||
|
Updated time.Time `json:"-"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// DiskFilesystem constants start with Filesystem and include Linode API Filesystems
|
||||||
|
type DiskFilesystem string
|
||||||
|
|
||||||
|
// DiskFilesystem constants represent the filesystems types an Instance Disk may use
|
||||||
|
const (
|
||||||
|
FilesystemRaw DiskFilesystem = "raw"
|
||||||
|
FilesystemSwap DiskFilesystem = "swap"
|
||||||
|
FilesystemExt3 DiskFilesystem = "ext3"
|
||||||
|
FilesystemExt4 DiskFilesystem = "ext4"
|
||||||
|
FilesystemInitrd DiskFilesystem = "initrd"
|
||||||
|
)
|
||||||
|
|
||||||
|
// InstanceDisksPagedResponse represents a paginated InstanceDisk API response
|
||||||
|
type InstanceDisksPagedResponse struct {
|
||||||
|
*PageOptions
|
||||||
|
Data []InstanceDisk `json:"data"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// InstanceDiskCreateOptions are InstanceDisk settings that can be used at creation
|
||||||
|
type InstanceDiskCreateOptions struct {
|
||||||
|
Label string `json:"label"`
|
||||||
|
Size int `json:"size"`
|
||||||
|
|
||||||
|
// Image is optional, but requires RootPass if provided
|
||||||
|
Image string `json:"image,omitempty"`
|
||||||
|
RootPass string `json:"root_pass,omitempty"`
|
||||||
|
|
||||||
|
Filesystem string `json:"filesystem,omitempty"`
|
||||||
|
AuthorizedKeys []string `json:"authorized_keys,omitempty"`
|
||||||
|
AuthorizedUsers []string `json:"authorized_users,omitempty"`
|
||||||
|
ReadOnly bool `json:"read_only,omitempty"`
|
||||||
|
StackscriptID int `json:"stackscript_id,omitempty"`
|
||||||
|
StackscriptData map[string]string `json:"stackscript_data,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// InstanceDiskUpdateOptions are InstanceDisk settings that can be used in updates
|
||||||
|
type InstanceDiskUpdateOptions struct {
|
||||||
|
Label string `json:"label"`
|
||||||
|
ReadOnly bool `json:"read_only"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// endpointWithID gets the endpoint URL for InstanceDisks of a given Instance
|
||||||
|
func (InstanceDisksPagedResponse) endpointWithID(c *Client, id int) string {
|
||||||
|
endpoint, err := c.InstanceDisks.endpointWithID(id)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return endpoint
|
||||||
|
}
|
||||||
|
|
||||||
|
// appendData appends InstanceDisks when processing paginated InstanceDisk responses
|
||||||
|
func (resp *InstanceDisksPagedResponse) appendData(r *InstanceDisksPagedResponse) {
|
||||||
|
resp.Data = append(resp.Data, r.Data...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ListInstanceDisks lists InstanceDisks
|
||||||
|
func (c *Client) ListInstanceDisks(ctx context.Context, linodeID int, opts *ListOptions) ([]InstanceDisk, error) {
|
||||||
|
response := InstanceDisksPagedResponse{}
|
||||||
|
err := c.listHelperWithID(ctx, &response, linodeID, opts)
|
||||||
|
for i := range response.Data {
|
||||||
|
response.Data[i].fixDates()
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return response.Data, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// fixDates converts JSON timestamps to Go time.Time values
|
||||||
|
func (v *InstanceDisk) fixDates() *InstanceDisk {
|
||||||
|
if created, err := parseDates(v.CreatedStr); err == nil {
|
||||||
|
v.Created = *created
|
||||||
|
}
|
||||||
|
if updated, err := parseDates(v.UpdatedStr); err == nil {
|
||||||
|
v.Updated = *updated
|
||||||
|
}
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetInstanceDisk gets the template with the provided ID
|
||||||
|
func (c *Client) GetInstanceDisk(ctx context.Context, linodeID int, configID int) (*InstanceDisk, error) {
|
||||||
|
e, err := c.InstanceDisks.endpointWithID(linodeID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
e = fmt.Sprintf("%s/%d", e, configID)
|
||||||
|
r, err := coupleAPIErrors(c.R(ctx).SetResult(&InstanceDisk{}).Get(e))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return r.Result().(*InstanceDisk).fixDates(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreateInstanceDisk creates a new InstanceDisk for the given Instance
|
||||||
|
func (c *Client) CreateInstanceDisk(ctx context.Context, linodeID int, createOpts InstanceDiskCreateOptions) (*InstanceDisk, error) {
|
||||||
|
var body string
|
||||||
|
e, err := c.InstanceDisks.endpointWithID(linodeID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
req := c.R(ctx).SetResult(&InstanceDisk{})
|
||||||
|
|
||||||
|
if bodyData, err := json.Marshal(createOpts); err == nil {
|
||||||
|
body = string(bodyData)
|
||||||
|
} else {
|
||||||
|
return nil, NewError(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
r, err := coupleAPIErrors(req.
|
||||||
|
SetBody(body).
|
||||||
|
Post(e))
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return r.Result().(*InstanceDisk).fixDates(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateInstanceDisk creates a new InstanceDisk for the given Instance
|
||||||
|
func (c *Client) UpdateInstanceDisk(ctx context.Context, linodeID int, diskID int, updateOpts InstanceDiskUpdateOptions) (*InstanceDisk, error) {
|
||||||
|
var body string
|
||||||
|
e, err := c.InstanceDisks.endpointWithID(linodeID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
e = fmt.Sprintf("%s/%d", e, diskID)
|
||||||
|
|
||||||
|
req := c.R(ctx).SetResult(&InstanceDisk{})
|
||||||
|
|
||||||
|
if bodyData, err := json.Marshal(updateOpts); err == nil {
|
||||||
|
body = string(bodyData)
|
||||||
|
} else {
|
||||||
|
return nil, NewError(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
r, err := coupleAPIErrors(req.
|
||||||
|
SetBody(body).
|
||||||
|
Put(e))
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return r.Result().(*InstanceDisk).fixDates(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// RenameInstanceDisk renames an InstanceDisk
|
||||||
|
func (c *Client) RenameInstanceDisk(ctx context.Context, linodeID int, diskID int, label string) (*InstanceDisk, error) {
|
||||||
|
return c.UpdateInstanceDisk(ctx, linodeID, diskID, InstanceDiskUpdateOptions{Label: label})
|
||||||
|
}
|
||||||
|
|
||||||
|
// ResizeInstanceDisk resizes the size of the Instance disk
|
||||||
|
func (c *Client) ResizeInstanceDisk(ctx context.Context, linodeID int, diskID int, size int) (*InstanceDisk, error) {
|
||||||
|
var body string
|
||||||
|
e, err := c.InstanceDisks.endpointWithID(linodeID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
e = fmt.Sprintf("%s/%d", e, diskID)
|
||||||
|
|
||||||
|
req := c.R(ctx).SetResult(&InstanceDisk{})
|
||||||
|
updateOpts := map[string]interface{}{
|
||||||
|
"size": size,
|
||||||
|
}
|
||||||
|
|
||||||
|
if bodyData, err := json.Marshal(updateOpts); err == nil {
|
||||||
|
body = string(bodyData)
|
||||||
|
} else {
|
||||||
|
return nil, NewError(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
r, err := coupleAPIErrors(req.
|
||||||
|
SetBody(body).
|
||||||
|
Post(e))
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return r.Result().(*InstanceDisk).fixDates(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeleteInstanceDisk deletes a Linode Instance Disk
|
||||||
|
func (c *Client) DeleteInstanceDisk(ctx context.Context, linodeID int, diskID int) error {
|
||||||
|
e, err := c.InstanceDisks.endpointWithID(linodeID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
e = fmt.Sprintf("%s/%d", e, diskID)
|
||||||
|
|
||||||
|
_, err = coupleAPIErrors(c.R(ctx).Delete(e))
|
||||||
|
return err
|
||||||
|
}
|
106
vendor/github.com/linode/linodego/instance_ips.go
generated
vendored
Normal file
106
vendor/github.com/linode/linodego/instance_ips.go
generated
vendored
Normal file
|
@ -0,0 +1,106 @@
|
||||||
|
package linodego
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
// InstanceIPAddressResponse contains the IPv4 and IPv6 details for an Instance
|
||||||
|
type InstanceIPAddressResponse struct {
|
||||||
|
IPv4 *InstanceIPv4Response `json:"ipv4"`
|
||||||
|
IPv6 *InstanceIPv6Response `json:"ipv6"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// InstanceIPv4Response contains the details of all IPv4 addresses associated with an Instance
|
||||||
|
type InstanceIPv4Response struct {
|
||||||
|
Public []*InstanceIP `json:"public"`
|
||||||
|
Private []*InstanceIP `json:"private"`
|
||||||
|
Shared []*InstanceIP `json:"shared"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// InstanceIP represents an Instance IP with additional DNS and networking details
|
||||||
|
type InstanceIP struct {
|
||||||
|
Address string `json:"address"`
|
||||||
|
Gateway string `json:"gateway"`
|
||||||
|
SubnetMask string `json:"subnet_mask"`
|
||||||
|
Prefix int `json:"prefix"`
|
||||||
|
Type string `json:"type"`
|
||||||
|
Public bool `json:"public"`
|
||||||
|
RDNS string `json:"rdns"`
|
||||||
|
LinodeID int `json:"linode_id"`
|
||||||
|
Region string `json:"region"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// InstanceIPv6Response contains the IPv6 addresses and ranges for an Instance
|
||||||
|
type InstanceIPv6Response struct {
|
||||||
|
LinkLocal *InstanceIP `json:"link_local"`
|
||||||
|
SLAAC *InstanceIP `json:"slaac"`
|
||||||
|
Global []*IPv6Range `json:"global"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// IPv6Range represents a range of IPv6 addresses routed to a single Linode in a given Region
|
||||||
|
type IPv6Range struct {
|
||||||
|
Range string `json:"range"`
|
||||||
|
Region string `json:"region"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetInstanceIPAddresses gets the IPAddresses for a Linode instance
|
||||||
|
func (c *Client) GetInstanceIPAddresses(ctx context.Context, linodeID int) (*InstanceIPAddressResponse, error) {
|
||||||
|
e, err := c.InstanceIPs.endpointWithID(linodeID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
r, err := coupleAPIErrors(c.R(ctx).SetResult(&InstanceIPAddressResponse{}).Get(e))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return r.Result().(*InstanceIPAddressResponse), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetInstanceIPAddress gets the IPAddress for a Linode instance matching a supplied IP address
|
||||||
|
func (c *Client) GetInstanceIPAddress(ctx context.Context, linodeID int, ipaddress string) (*InstanceIP, error) {
|
||||||
|
e, err := c.InstanceIPs.endpointWithID(linodeID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
e = fmt.Sprintf("%s/%s", e, ipaddress)
|
||||||
|
r, err := coupleAPIErrors(c.R(ctx).SetResult(&InstanceIP{}).Get(e))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return r.Result().(*InstanceIP), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// AddInstanceIPAddress adds a public or private IP to a Linode instance
|
||||||
|
func (c *Client) AddInstanceIPAddress(ctx context.Context, linodeID int, public bool) (*InstanceIP, error) {
|
||||||
|
var body string
|
||||||
|
e, err := c.InstanceIPs.endpointWithID(linodeID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
req := c.R(ctx).SetResult(&InstanceIP{})
|
||||||
|
|
||||||
|
instanceipRequest := struct {
|
||||||
|
Type string `json:"type"`
|
||||||
|
Public bool `json:"public"`
|
||||||
|
}{"ipv4", public}
|
||||||
|
|
||||||
|
if bodyData, err := json.Marshal(instanceipRequest); err == nil {
|
||||||
|
body = string(bodyData)
|
||||||
|
} else {
|
||||||
|
return nil, NewError(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
r, err := coupleAPIErrors(req.
|
||||||
|
SetHeader("Content-Type", "application/json").
|
||||||
|
SetBody(body).
|
||||||
|
Post(e))
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return r.Result().(*InstanceIP), nil
|
||||||
|
}
|
188
vendor/github.com/linode/linodego/instance_snapshots.go
generated
vendored
Normal file
188
vendor/github.com/linode/linodego/instance_snapshots.go
generated
vendored
Normal file
|
@ -0,0 +1,188 @@
|
||||||
|
package linodego
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// InstanceBackupsResponse response struct for backup snapshot
|
||||||
|
type InstanceBackupsResponse struct {
|
||||||
|
Automatic []*InstanceSnapshot `json:"automatic"`
|
||||||
|
Snapshot *InstanceBackupSnapshotResponse `json:"snapshot"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// InstanceBackupSnapshotResponse fields are those representing Instance Backup Snapshots
|
||||||
|
type InstanceBackupSnapshotResponse struct {
|
||||||
|
Current *InstanceSnapshot `json:"current"`
|
||||||
|
InProgress *InstanceSnapshot `json:"in_progress"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// RestoreInstanceOptions fields are those accepted by InstanceRestore
|
||||||
|
type RestoreInstanceOptions struct {
|
||||||
|
LinodeID int `json:"linode_id"`
|
||||||
|
Overwrite bool `json:"overwrite"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// InstanceSnapshot represents a linode backup snapshot
|
||||||
|
type InstanceSnapshot struct {
|
||||||
|
CreatedStr string `json:"created"`
|
||||||
|
UpdatedStr string `json:"updated"`
|
||||||
|
FinishedStr string `json:"finished"`
|
||||||
|
|
||||||
|
ID int `json:"id"`
|
||||||
|
Label string `json:"label"`
|
||||||
|
Status InstanceSnapshotStatus `json:"status"`
|
||||||
|
Type string `json:"type"`
|
||||||
|
Created *time.Time `json:"-"`
|
||||||
|
Updated *time.Time `json:"-"`
|
||||||
|
Finished *time.Time `json:"-"`
|
||||||
|
Configs []string `json:"configs"`
|
||||||
|
Disks []*InstanceSnapshotDisk `json:"disks"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// InstanceSnapshotDisk fields represent the source disk of a Snapshot
|
||||||
|
type InstanceSnapshotDisk struct {
|
||||||
|
Label string `json:"label"`
|
||||||
|
Size int `json:"size"`
|
||||||
|
Filesystem string `json:"filesystem"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// InstanceSnapshotStatus constants start with Snapshot and include Linode API Instance Backup Snapshot status values
|
||||||
|
type InstanceSnapshotStatus string
|
||||||
|
|
||||||
|
// InstanceSnapshotStatus constants reflect the current status of an Instance Snapshot
|
||||||
|
var (
|
||||||
|
SnapshotPaused InstanceSnapshotStatus = "paused"
|
||||||
|
SnapshotPending InstanceSnapshotStatus = "pending"
|
||||||
|
SnapshotRunning InstanceSnapshotStatus = "running"
|
||||||
|
SnapshotNeedsPostProcessing InstanceSnapshotStatus = "needsPostProcessing"
|
||||||
|
SnapshotSuccessful InstanceSnapshotStatus = "successful"
|
||||||
|
SnapshotFailed InstanceSnapshotStatus = "failed"
|
||||||
|
SnapshotUserAborted InstanceSnapshotStatus = "userAborted"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (l *InstanceSnapshot) fixDates() *InstanceSnapshot {
|
||||||
|
l.Created, _ = parseDates(l.CreatedStr)
|
||||||
|
l.Updated, _ = parseDates(l.UpdatedStr)
|
||||||
|
l.Finished, _ = parseDates(l.FinishedStr)
|
||||||
|
return l
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetInstanceSnapshot gets the snapshot with the provided ID
|
||||||
|
func (c *Client) GetInstanceSnapshot(ctx context.Context, linodeID int, snapshotID int) (*InstanceSnapshot, error) {
|
||||||
|
e, err := c.InstanceSnapshots.endpointWithID(linodeID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
e = fmt.Sprintf("%s/%d", e, snapshotID)
|
||||||
|
r, err := coupleAPIErrors(c.R(ctx).SetResult(&InstanceSnapshot{}).Get(e))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return r.Result().(*InstanceSnapshot).fixDates(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreateInstanceSnapshot Creates or Replaces the snapshot Backup of a Linode. If a previous snapshot exists for this Linode, it will be deleted.
|
||||||
|
func (c *Client) CreateInstanceSnapshot(ctx context.Context, linodeID int, label string) (*InstanceSnapshot, error) {
|
||||||
|
o, err := json.Marshal(map[string]string{"label": label})
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
body := string(o)
|
||||||
|
e, err := c.InstanceSnapshots.endpointWithID(linodeID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
r, err := coupleAPIErrors(c.R(ctx).
|
||||||
|
SetBody(body).
|
||||||
|
SetResult(&InstanceSnapshot{}).
|
||||||
|
Post(e))
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return r.Result().(*InstanceSnapshot).fixDates(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetInstanceBackups gets the Instance's available Backups.
|
||||||
|
// This is not called ListInstanceBackups because a single object is returned, matching the API response.
|
||||||
|
func (c *Client) GetInstanceBackups(ctx context.Context, linodeID int) (*InstanceBackupsResponse, error) {
|
||||||
|
e, err := c.InstanceSnapshots.endpointWithID(linodeID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
r, err := coupleAPIErrors(c.R(ctx).
|
||||||
|
SetResult(&InstanceBackupsResponse{}).
|
||||||
|
Get(e))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return r.Result().(*InstanceBackupsResponse).fixDates(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// EnableInstanceBackups Enables backups for the specified Linode.
|
||||||
|
func (c *Client) EnableInstanceBackups(ctx context.Context, linodeID int) error {
|
||||||
|
e, err := c.InstanceSnapshots.endpointWithID(linodeID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
e = fmt.Sprintf("%s/enable", e)
|
||||||
|
|
||||||
|
_, err = coupleAPIErrors(c.R(ctx).Post(e))
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// CancelInstanceBackups Cancels backups for the specified Linode.
|
||||||
|
func (c *Client) CancelInstanceBackups(ctx context.Context, linodeID int) error {
|
||||||
|
e, err := c.InstanceSnapshots.endpointWithID(linodeID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
e = fmt.Sprintf("%s/cancel", e)
|
||||||
|
|
||||||
|
_, err = coupleAPIErrors(c.R(ctx).Post(e))
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// RestoreInstanceBackup Restores a Linode's Backup to the specified Linode.
|
||||||
|
func (c *Client) RestoreInstanceBackup(ctx context.Context, linodeID int, backupID int, opts RestoreInstanceOptions) error {
|
||||||
|
o, err := json.Marshal(opts)
|
||||||
|
if err != nil {
|
||||||
|
return NewError(err)
|
||||||
|
}
|
||||||
|
body := string(o)
|
||||||
|
e, err := c.InstanceSnapshots.endpointWithID(linodeID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
e = fmt.Sprintf("%s/%d/restore", e, backupID)
|
||||||
|
|
||||||
|
_, err = coupleAPIErrors(c.R(ctx).SetBody(body).Post(e))
|
||||||
|
|
||||||
|
return err
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *InstanceBackupSnapshotResponse) fixDates() *InstanceBackupSnapshotResponse {
|
||||||
|
if l.Current != nil {
|
||||||
|
l.Current.fixDates()
|
||||||
|
}
|
||||||
|
if l.InProgress != nil {
|
||||||
|
l.InProgress.fixDates()
|
||||||
|
}
|
||||||
|
return l
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *InstanceBackupsResponse) fixDates() *InstanceBackupsResponse {
|
||||||
|
for i := range l.Automatic {
|
||||||
|
l.Automatic[i].fixDates()
|
||||||
|
}
|
||||||
|
if l.Snapshot != nil {
|
||||||
|
l.Snapshot.fixDates()
|
||||||
|
}
|
||||||
|
return l
|
||||||
|
}
|
38
vendor/github.com/linode/linodego/instance_volumes.go
generated
vendored
Normal file
38
vendor/github.com/linode/linodego/instance_volumes.go
generated
vendored
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
package linodego
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
)
|
||||||
|
|
||||||
|
// InstanceVolumesPagedResponse represents a paginated InstanceVolume API response
|
||||||
|
type InstanceVolumesPagedResponse struct {
|
||||||
|
*PageOptions
|
||||||
|
Data []Volume `json:"data"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// endpoint gets the endpoint URL for InstanceVolume
|
||||||
|
func (InstanceVolumesPagedResponse) endpointWithID(c *Client, id int) string {
|
||||||
|
endpoint, err := c.InstanceVolumes.endpointWithID(id)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return endpoint
|
||||||
|
}
|
||||||
|
|
||||||
|
// appendData appends InstanceVolumes when processing paginated InstanceVolume responses
|
||||||
|
func (resp *InstanceVolumesPagedResponse) appendData(r *InstanceVolumesPagedResponse) {
|
||||||
|
resp.Data = append(resp.Data, r.Data...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ListInstanceVolumes lists InstanceVolumes
|
||||||
|
func (c *Client) ListInstanceVolumes(ctx context.Context, linodeID int, opts *ListOptions) ([]Volume, error) {
|
||||||
|
response := InstanceVolumesPagedResponse{}
|
||||||
|
err := c.listHelperWithID(ctx, &response, linodeID, opts)
|
||||||
|
for i := range response.Data {
|
||||||
|
response.Data[i].fixDates()
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return response.Data, nil
|
||||||
|
}
|
Some files were not shown because too many files have changed in this diff Show more
Loading…
Reference in a new issue