This repository has been archived by the owner on Dec 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
initializer.go
47 lines (40 loc) · 1.64 KB
/
initializer.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
package api
import (
"fmt"
keptnapi "github.com/keptn/go-utils/pkg/api/utils"
"github.com/keptn/go-utils/pkg/common/apiutils"
"github.com/keptn/go-utils/pkg/sdk/internal/config"
"net/http"
"net/url"
"strings"
)
// Initializer implements both methods of creating a new keptn API with internal or remote execution plane
type Initializer struct {
Remote func(baseURL string, options ...func(*keptnapi.APISet)) (*keptnapi.APISet, error)
Internal func(client *http.Client, apiMappings ...apiutils.InClusterAPIMappings) (*apiutils.InternalAPISet, error)
}
func CreateKeptnAPI(httpClient *http.Client, env config.EnvConfig) (keptnapi.KeptnInterface, error) {
return createAPI(httpClient, env, Initializer{keptnapi.New, apiutils.NewInternal})
}
func createAPI(httpClient *http.Client, env config.EnvConfig, apiInit Initializer) (keptnapi.KeptnInterface, error) {
if httpClient == nil {
httpClient = &http.Client{}
}
if env.PubSubConnectionType() == config.ConnectionTypeHTTP {
scheme := "http"
parsed, err := url.ParseRequestURI(env.KeptnAPIEndpoint)
if err != nil {
return nil, err
}
// accepts either "" or http
if parsed.Scheme == "" || !strings.HasPrefix(parsed.Scheme, "http") {
return nil, fmt.Errorf("invalid scheme for keptn endpoint, %s is not http or https", env.KeptnAPIEndpoint)
}
if strings.HasPrefix(parsed.Scheme, "http") {
// if no value is assigned to the endpoint than we keep the default scheme
scheme = parsed.Scheme
}
return apiInit.Remote(env.KeptnAPIEndpoint, keptnapi.WithScheme(scheme), keptnapi.WithHTTPClient(httpClient), keptnapi.WithAuthToken(env.KeptnAPIToken))
}
return apiInit.Internal(httpClient)
}