-
Notifications
You must be signed in to change notification settings - Fork 8
/
ip_provider.go
79 lines (68 loc) · 2.44 KB
/
ip_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
79
package oracle
import (
"fmt"
"github.com/oracle/oci-go-sdk/v48/functions"
"io/ioutil"
"net/http"
"net/url"
"github.com/oracle/oci-go-sdk/v48/common"
"github.com/oracle/oci-go-sdk/v48/common/auth"
"github.com/fnproject/fn_go/provider"
)
func NewIPProvider(configSource provider.ConfigSource, passphraseSource provider.PassPhraseSource) (provider.Provider, error) {
// Set OCI SDK to use IMDS to fetch region info (second-level domain etc.)
common.EnableInstanceMetadataServiceLookup()
configProvider, err := auth.InstancePrincipalConfigurationProvider()
if err != nil {
return nil, err
}
compartmentID := configSource.GetString(CfgCompartmentID)
if compartmentID == "" {
// Get the local compartment ID from the metadata endpoint
resp, err := http.DefaultClient.Get(CompartmentMetadata)
if err != nil {
return nil, fmt.Errorf("problem fetching compartment OCID from metadata endpoint %s", err)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("problem fetching compartment OCID from metadata endpoint %s", err)
}
compartmentID = string(body)
}
ociClient, err := functions.NewFunctionsManagementClientWithConfigurationProvider(configProvider)
if err != nil {
return nil, err
}
ociClient.UserAgent = fmt.Sprintf("%s %s", userAgentPrefixIp, ociClient.UserAgent)
disableCerts := configSource.GetBool(CfgDisableCerts)
if disableCerts {
c := ociClient.HTTPClient.(*http.Client)
c.Transport = InsecureRoundTripper(c.Transport)
}
// If we have an explicit api-url configured then use that, otherwise let OCI client compute the url from the standard
// production url template and the configured region from environment.
cfgApiUrl := configSource.GetString(provider.CfgFnAPIURL)
var apiUrl *url.URL
if cfgApiUrl != "" {
apiUrl, err = provider.CanonicalFnAPIUrl(cfgApiUrl)
if err != nil {
return nil, err
}
ociClient.Host = apiUrl.String()
} else {
// Even if URL is computed by OCI SDK itself, we still populate FnApiUrl in the Provider for compatibility's sake
apiUrl, err = provider.CanonicalFnAPIUrl(ociClient.Host)
if err != nil {
return nil, err
}
}
return &OracleProvider{
FnApiUrl: apiUrl,
Signer: common.DefaultRequestSigner(configProvider),
Interceptor: nil,
DisableCerts: disableCerts,
CompartmentID: compartmentID,
ConfigurationProvider: configProvider,
ociClient: ociClient,
}, nil
}