Skip to content
This repository has been archived by the owner on Dec 21, 2023. It is now read-only.

Commit

Permalink
Add v2 construction functions
Browse files Browse the repository at this point in the history
Signed-off-by: Arthur Pitman <[email protected]>
  • Loading branch information
arthurpitman committed Jun 2, 2022
1 parent f2d23c7 commit 3fbb0ed
Show file tree
Hide file tree
Showing 13 changed files with 140 additions and 176 deletions.
7 changes: 2 additions & 5 deletions pkg/api/utils/v2/apiUtils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package v2
import (
"context"
"net/http"
"strings"

"github.com/keptn/go-utils/pkg/api/models"
"github.com/keptn/go-utils/pkg/common/httputils"
)

const v1EventPath = "/v1/event"
Expand Down Expand Up @@ -61,7 +61,6 @@ type APIInterface interface {
GetMetadata(ctx context.Context, opts APIGetMetadataOptions) (*models.Metadata, *models.Error)
}

// APIHandler handles projects
type APIHandler struct {
BaseURL string
AuthToken string
Expand All @@ -71,10 +70,8 @@ type APIHandler struct {
}

func createAuthenticatedAPIHandler(baseURL string, authToken string, authHeader string, httpClient *http.Client, scheme string) *APIHandler {
baseURL = strings.TrimPrefix(baseURL, "http://")
baseURL = strings.TrimPrefix(baseURL, "https://")
return &APIHandler{
BaseURL: baseURL,
BaseURL: httputils.TrimHTTPScheme(baseURL),
AuthHeader: authHeader,
AuthToken: authToken,
HTTPClient: httpClient,
Expand Down
29 changes: 12 additions & 17 deletions pkg/api/utils/v2/authUtils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package v2
import (
"context"
"net/http"
"strings"

"github.com/keptn/go-utils/pkg/api/models"
"github.com/keptn/go-utils/pkg/common/httputils"
)

// AuthAuthenticateOptions are options for AuthInterface.Authenticate().
Expand All @@ -16,7 +16,6 @@ type AuthInterface interface {
Authenticate(ctx context.Context, opts AuthAuthenticateOptions) (*models.EventContext, *models.Error)
}

// AuthHandler handles projects
type AuthHandler struct {
BaseURL string
AuthToken string
Expand All @@ -27,25 +26,21 @@ type AuthHandler struct {

// NewAuthHandler returns a new AuthHandler
func NewAuthHandler(baseURL string) *AuthHandler {
if strings.Contains(baseURL, "https://") {
baseURL = strings.TrimPrefix(baseURL, "https://")
} else if strings.Contains(baseURL, "http://") {
baseURL = strings.TrimPrefix(baseURL, "http://")
}
return &AuthHandler{
BaseURL: baseURL,
AuthHeader: "",
AuthToken: "",
HTTPClient: &http.Client{Transport: wrapOtelTransport(getClientTransport(nil))},
Scheme: "http",
}
return NewAuthHandlerWithHTTPClient(baseURL, &http.Client{Transport: wrapOtelTransport(getClientTransport(nil))})
}

// NewAuthHandlerWithHTTPClient returns a new AuthHandler using the specified http.Client
func NewAuthHandlerWithHTTPClient(baseURL string, httpClient *http.Client) *AuthHandler {
return createAuthHandler(baseURL, "", "", httpClient, "http")
}

func createAuthenticatedAuthHandler(baseURL string, authToken string, authHeader string, httpClient *http.Client, scheme string) *AuthHandler {
baseURL = strings.TrimPrefix(baseURL, "http://")
baseURL = strings.TrimPrefix(baseURL, "https://")
return createAuthHandler(baseURL, authToken, authHeader, httpClient, scheme)
}

func createAuthHandler(baseURL string, authToken string, authHeader string, httpClient *http.Client, scheme string) *AuthHandler {
return &AuthHandler{
BaseURL: baseURL,
BaseURL: httputils.TrimHTTPScheme(baseURL),
AuthHeader: authHeader,
AuthToken: authToken,
HTTPClient: httpClient,
Expand Down
2 changes: 1 addition & 1 deletion pkg/api/utils/v2/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func New(baseURL string, options ...func(*APISet)) (*APISet, error) {
as.authHandler = createAuthenticatedAuthHandler(baseURL, as.apiToken, as.authHeader, as.httpClient, as.scheme)
as.logHandler = createAuthenticatedLogHandler(baseURL, as.apiToken, as.authHeader, as.httpClient, as.scheme)
as.eventHandler = createAuthenticatedEventHandler(baseURL, as.apiToken, as.authHeader, as.httpClient, as.scheme)
as.projectHandler = createAuthProjectHandler(baseURL, as.apiToken, as.authHeader, as.httpClient, as.scheme)
as.projectHandler = createAuthenticatedProjectHandler(baseURL, as.apiToken, as.authHeader, as.httpClient, as.scheme)
as.resourceHandler = createAuthenticatedResourceHandler(baseURL, as.apiToken, as.authHeader, as.httpClient, as.scheme)
as.secretHandler = createAuthenticatedSecretHandler(baseURL, as.apiToken, as.authHeader, as.httpClient, as.scheme)
as.sequenceControlHandler = createAuthenticatedSequenceControlHandler(baseURL, as.apiToken, as.authHeader, as.httpClient, as.scheme)
Expand Down
28 changes: 12 additions & 16 deletions pkg/api/utils/v2/eventUtils.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"time"

"github.com/keptn/go-utils/pkg/api/models"
"github.com/keptn/go-utils/pkg/common/httputils"
)

// EventsGetEventsOptions are options for EventsInterface.GetEvents().
Expand All @@ -27,7 +28,6 @@ type EventsInterface interface {
GetEventsWithRetry(ctx context.Context, filter *EventFilter, maxRetries int, retrySleepTime time.Duration, opts EventsGetEventsWithRetryOptions) ([]*models.KeptnContextExtendedCE, error)
}

// EventHandler handles services
type EventHandler struct {
BaseURL string
AuthToken string
Expand All @@ -51,32 +51,28 @@ type EventFilter struct {

// NewEventHandler returns a new EventHandler
func NewEventHandler(baseURL string) *EventHandler {
if strings.Contains(baseURL, "https://") {
baseURL = strings.TrimPrefix(baseURL, "https://")
} else if strings.Contains(baseURL, "http://") {
baseURL = strings.TrimPrefix(baseURL, "http://")
}
return &EventHandler{
BaseURL: baseURL,
AuthHeader: "",
AuthToken: "",
HTTPClient: &http.Client{Transport: wrapOtelTransport(getClientTransport(nil))},
Scheme: "http",
}
return NewEventHandlerWithHTTPClient(baseURL, &http.Client{Transport: wrapOtelTransport(getClientTransport(nil))})
}

// NewEventHandlerWithHTTPClient returns a new EventHandler using the specified http.Client
func NewEventHandlerWithHTTPClient(baseURL string, httpClient *http.Client) *EventHandler {
return createEventHandler(baseURL, "", "", httpClient, "http")
}

const mongodbDatastoreServiceBaseUrl = "mongodb-datastore"

func createAuthenticatedEventHandler(baseURL string, authToken string, authHeader string, httpClient *http.Client, scheme string) *EventHandler {
baseURL = strings.TrimPrefix(baseURL, "http://")
baseURL = strings.TrimPrefix(baseURL, "https://")
baseURL = strings.TrimRight(baseURL, "/")
if !strings.HasSuffix(baseURL, mongodbDatastoreServiceBaseUrl) {
baseURL += "/" + mongodbDatastoreServiceBaseUrl
}

return createEventHandler(baseURL, authToken, authHeader, httpClient, scheme)
}

func createEventHandler(baseURL string, authToken string, authHeader string, httpClient *http.Client, scheme string) *EventHandler {
return &EventHandler{
BaseURL: baseURL,
BaseURL: httputils.TrimHTTPScheme(baseURL),
AuthHeader: authHeader,
AuthToken: authToken,
HTTPClient: httpClient,
Expand Down
31 changes: 13 additions & 18 deletions pkg/api/utils/v2/logUtils.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/benbjohnson/clock"
"github.com/keptn/go-utils/pkg/api/models"
"github.com/keptn/go-utils/pkg/common/httputils"
)

const v1LogPath = "/v1/log"
Expand Down Expand Up @@ -63,34 +64,28 @@ type LogHandler struct {
lock sync.Mutex
}

// NewLogHandler returns a new LogHandler
func NewLogHandler(baseURL string) *LogHandler {
if strings.Contains(baseURL, "https://") {
baseURL = strings.TrimPrefix(baseURL, "https://")
} else if strings.Contains(baseURL, "http://") {
baseURL = strings.TrimPrefix(baseURL, "http://")
}
return &LogHandler{
BaseURL: baseURL,
AuthHeader: "",
AuthToken: "",
HTTPClient: &http.Client{Transport: getClientTransport(nil)},
Scheme: "http",
LogCache: []models.LogEntry{},
TheClock: clock.New(),
SyncInterval: defaultSyncInterval,
}
return NewLogHandlerWithHTTPClient(baseURL, &http.Client{Transport: getClientTransport(nil)})
}

// NewLogHandlerWithHTTPClient returns a new LogHandler that uses the specified http.Client
func NewLogHandlerWithHTTPClient(baseURL string, httpClient *http.Client) *LogHandler {
return createLogHandler(baseURL, "", "", httpClient, "http")
}

func createAuthenticatedLogHandler(baseURL string, authToken string, authHeader string, httpClient *http.Client, scheme string) *LogHandler {
baseURL = strings.TrimPrefix(baseURL, "http://")
baseURL = strings.TrimPrefix(baseURL, "https://")
baseURL = strings.TrimRight(baseURL, "/")
if !strings.HasSuffix(baseURL, shipyardControllerBaseURL) {
baseURL += "/" + shipyardControllerBaseURL
}

return createLogHandler(baseURL, authToken, authHeader, httpClient, scheme)
}

func createLogHandler(baseURL string, authToken string, authHeader string, httpClient *http.Client, scheme string) *LogHandler {
return &LogHandler{
BaseURL: baseURL,
BaseURL: httputils.TrimHTTPScheme(baseURL),
AuthHeader: authHeader,
AuthToken: authToken,
HTTPClient: httpClient,
Expand Down
25 changes: 12 additions & 13 deletions pkg/api/utils/v2/projectUtils.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,27 +57,26 @@ type ProjectHandler struct {

// NewProjectHandler returns a new ProjectHandler which sends all requests directly to the configuration-service
func NewProjectHandler(baseURL string) *ProjectHandler {
baseURL = httputils.TrimHTTPScheme(baseURL)
return &ProjectHandler{
BaseURL: baseURL,
AuthHeader: "",
AuthToken: "",
HTTPClient: &http.Client{Transport: wrapOtelTransport(getClientTransport(nil))},
Scheme: "http",
}
return NewProjectHandlerWithHTTPClient(baseURL, &http.Client{Transport: wrapOtelTransport(getClientTransport(nil))})
}

func createAuthProjectHandler(baseURL string, authToken string, authHeader string, httpClient *http.Client, scheme string) *ProjectHandler {
baseURL = strings.TrimPrefix(baseURL, "http://")
baseURL = strings.TrimPrefix(baseURL, "https://")
baseURL = strings.TrimRight(baseURL, "/")
// NewProjectHandlerWithHTTPClient returns a new ProjectHandler which sends all requests directly to the configuration-service using the specified http.Client
func NewProjectHandlerWithHTTPClient(baseURL string, httpClient *http.Client) *ProjectHandler {
return createProjectHandler(baseURL, "", "", httpClient, "http")
}

func createAuthenticatedProjectHandler(baseURL string, authToken string, authHeader string, httpClient *http.Client, scheme string) *ProjectHandler {
baseURL = strings.TrimRight(baseURL, "/")
if !strings.HasSuffix(baseURL, shipyardControllerBaseURL) {
baseURL += "/" + shipyardControllerBaseURL
}

return createProjectHandler(baseURL, authToken, authHeader, httpClient, scheme)
}

func createProjectHandler(baseURL string, authToken string, authHeader string, httpClient *http.Client, scheme string) *ProjectHandler {
return &ProjectHandler{
BaseURL: baseURL,
BaseURL: httputils.TrimHTTPScheme(baseURL),
AuthHeader: authHeader,
AuthToken: authToken,
HTTPClient: httpClient,
Expand Down
28 changes: 13 additions & 15 deletions pkg/api/utils/v2/resourceUtils.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"strings"

"github.com/keptn/go-utils/pkg/api/models"
"github.com/keptn/go-utils/pkg/common/httputils"
)

const pathToResource = "/resource"
Expand Down Expand Up @@ -216,29 +217,26 @@ func (r *resourceRequest) FromJSON(b []byte) error {

// NewResourceHandler returns a new ResourceHandler which sends all requests directly to the configuration-service
func NewResourceHandler(baseURL string) *ResourceHandler {
if strings.Contains(baseURL, "https://") {
baseURL = strings.TrimPrefix(baseURL, "https://")
} else if strings.Contains(baseURL, "http://") {
baseURL = strings.TrimPrefix(baseURL, "http://")
}
return &ResourceHandler{
BaseURL: baseURL,
AuthHeader: "",
AuthToken: "",
HTTPClient: &http.Client{Transport: wrapOtelTransport(getClientTransport(nil))},
Scheme: "http",
}
return NewResourceHandlerWithHTTPClient(baseURL, &http.Client{Transport: wrapOtelTransport(getClientTransport(nil))})
}

// NewResourceHandlerWithHTTPClient returns a new ResourceHandler which sends all requests directly to the configuration-service using the specified http.Client
func NewResourceHandlerWithHTTPClient(baseURL string, httpClient *http.Client) *ResourceHandler {
return createResourceHandler(baseURL, "", "", httpClient, "http")
}

func createAuthenticatedResourceHandler(baseURL string, authToken string, authHeader string, httpClient *http.Client, scheme string) *ResourceHandler {
baseURL = strings.TrimPrefix(baseURL, "http://")
baseURL = strings.TrimPrefix(baseURL, "https://")
baseURL = strings.TrimRight(baseURL, "/")
if !strings.HasSuffix(baseURL, configurationServiceBaseURL) {
baseURL += "/" + configurationServiceBaseURL
}

return createResourceHandler(baseURL, authToken, authHeader, httpClient, scheme)
}

func createResourceHandler(baseURL string, authToken string, authHeader string, httpClient *http.Client, scheme string) *ResourceHandler {
return &ResourceHandler{
BaseURL: baseURL,
BaseURL: httputils.TrimHTTPScheme(baseURL),
AuthHeader: authHeader,
AuthToken: authToken,
HTTPClient: httpClient,
Expand Down
31 changes: 13 additions & 18 deletions pkg/api/utils/v2/secretUtils.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"

"github.com/keptn/go-utils/pkg/api/models"
"github.com/keptn/go-utils/pkg/common/httputils"
)

const secretServiceBaseURL = "secrets"
Expand Down Expand Up @@ -39,7 +40,7 @@ type SecretsInterface interface {
GetSecrets(ctx context.Context, opts SecretsGetSecretsOptions) (*models.GetSecretsResponse, error)
}

// SecretHandler handles services
// SecretHandler handles secrets
type SecretHandler struct {
BaseURL string
AuthToken string
Expand All @@ -50,32 +51,26 @@ type SecretHandler struct {

// NewSecretHandler returns a new SecretHandler which sends all requests directly to the secret-service
func NewSecretHandler(baseURL string) *SecretHandler {
if strings.Contains(baseURL, "https://") {
baseURL = strings.TrimPrefix(baseURL, "https://")
} else if strings.Contains(baseURL, "http://") {
baseURL = strings.TrimPrefix(baseURL, "http://")
}
return &SecretHandler{
BaseURL: baseURL,
AuthHeader: "",
AuthToken: "",
HTTPClient: &http.Client{Transport: wrapOtelTransport(getClientTransport(nil))},
Scheme: "http",
}
return NewSecretHandlerWithHTTPClient(baseURL, &http.Client{Transport: wrapOtelTransport(getClientTransport(nil))})
}

func createAuthenticatedSecretHandler(baseURL string, authToken string, authHeader string, httpClient *http.Client, scheme string) *SecretHandler {
baseURL = strings.TrimPrefix(baseURL, "http://")
baseURL = strings.TrimPrefix(baseURL, "https://")
// NewSecretHandlerWithHTTPClient returns a new SecretHandler which sends all requests directly to the secret-service using the specified http.Client
func NewSecretHandlerWithHTTPClient(baseURL string, httpClient *http.Client) *SecretHandler {
return createSecretHandler(baseURL, "", "", httpClient, "http")
}

func createAuthenticatedSecretHandler(baseURL string, authToken string, authHeader string, httpClient *http.Client, scheme string) *SecretHandler {
baseURL = strings.TrimRight(baseURL, "/")

if !strings.HasSuffix(baseURL, secretServiceBaseURL) {
baseURL += "/" + secretServiceBaseURL
}

return createSecretHandler(baseURL, authToken, authHeader, httpClient, scheme)
}

func createSecretHandler(baseURL string, authToken string, authHeader string, httpClient *http.Client, scheme string) *SecretHandler {
return &SecretHandler{
BaseURL: baseURL,
BaseURL: httputils.TrimHTTPScheme(baseURL),
AuthHeader: authHeader,
AuthToken: authToken,
HTTPClient: httpClient,
Expand Down
Loading

0 comments on commit 3fbb0ed

Please sign in to comment.