-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathprovider.go
78 lines (64 loc) · 2.28 KB
/
provider.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package provider
import (
"fmt"
"net/http"
"net/url"
"strings"
"github.com/fnproject/fn_go/client/version"
"github.com/fnproject/fn_go/clientv2"
)
// ProviderFunc constructs a provider
type ProviderFunc func(config ConfigSource, source PassPhraseSource) (Provider, error)
//Providers describes a set of providers
type Providers struct {
Providers map[string]ProviderFunc
}
// Register adds a named provider to a configuration
func (c *Providers) Register(name string, pf ProviderFunc) {
if len(c.Providers) == 0 {
c.Providers = make(map[string]ProviderFunc)
}
c.Providers[name] = pf
}
// Provider creates API clients for Fn calls adding any required middleware
type Provider interface {
// APIURL returns the current API URL base to use with this provider
APIURL() *url.URL
// WrapCallTransport adds any request signing or auth to an existing round tripper for calls
WrapCallTransport(http.RoundTripper) http.RoundTripper
APIClientv2() *clientv2.Fn
VersionClient() *version.Client
}
// CanonicalFnAPIUrl canonicalises an *FN_API_URL to a default value
func CanonicalFnAPIUrl(urlStr string) (*url.URL, error) {
if !strings.Contains(urlStr, "://") {
urlStr = fmt.Sprint("http://", urlStr)
}
parseUrl, err := url.Parse(urlStr)
if err != nil {
return nil, fmt.Errorf("unparsable FN API Url: %s. Error: %s", urlStr, err)
}
if parseUrl.Port() == "" {
if parseUrl.Scheme == "http" {
parseUrl.Host = fmt.Sprint(parseUrl.Host, ":80")
}
if parseUrl.Scheme == "https" {
parseUrl.Host = fmt.Sprint(parseUrl.Host, ":443")
}
}
//Remove /v1 from any paths here internal URL is now base URL
if strings.HasSuffix(parseUrl.Path, "/v1") {
parseUrl.Path = strings.TrimSuffix(parseUrl.Path, "v1")
} else if strings.HasSuffix(parseUrl.Path, "/v1/") {
parseUrl.Path = strings.TrimSuffix(parseUrl.Path, "v1/")
}
return parseUrl, nil
}
//ProviderFromConfig returns the provider corresponding to a given identifier populated with configuration from source - if a passphrase is required then it is request from phraseSource
func (c *Providers) ProviderFromConfig(id string, source ConfigSource, phraseSource PassPhraseSource) (Provider, error) {
p, ok := c.Providers[id]
if !ok {
return nil, fmt.Errorf("No provider with id '%s' is registered", id)
}
return p(source, phraseSource)
}