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

fix: Set the path properly for calls to api-service #471

Merged
merged 1 commit into from
Jun 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions pkg/api/utils/apiUtils.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,7 @@ func (a *APIHandler) getHTTPClient() *http.Client {

// SendEvent sends an event to Keptn
func (a *APIHandler) SendEvent(event models.KeptnContextExtendedCE) (*models.EventContext, *models.Error) {
baseURL := a.getBaseURL()
if strings.HasSuffix(baseURL, "/"+shipyardControllerBaseURL) {
baseURL = strings.TrimSuffix(a.getBaseURL(), "/"+shipyardControllerBaseURL)
baseURL += "/api"
}
baseURL := a.getAPIServicePath()

bodyStr, err := event.ToJSON()
if err != nil {
Expand Down Expand Up @@ -158,11 +154,7 @@ func (a *APIHandler) DeleteService(project, service string) (*models.DeleteServi

// GetMetadata retrieve keptn MetaData information
func (a *APIHandler) GetMetadata() (*models.Metadata, *models.Error) {
baseURL := a.getBaseURL()
if strings.HasSuffix(baseURL, "/"+shipyardControllerBaseURL) {
baseURL = strings.TrimSuffix(a.getBaseURL(), "/"+shipyardControllerBaseURL)
baseURL += "/api"
}
baseURL := a.getAPIServicePath()

req, err := http.NewRequest("GET", a.Scheme+"://"+baseURL+v1MetadataPath, nil)
if err != nil {
Expand Down Expand Up @@ -198,3 +190,11 @@ func (a *APIHandler) GetMetadata() (*models.Metadata, *models.Error) {

return nil, handleErrStatusCode(resp.StatusCode, body)
}

func (a *APIHandler) getAPIServicePath() string {
baseURL := a.getBaseURL()
if strings.HasSuffix(baseURL, "/"+shipyardControllerBaseURL) {
baseURL = strings.TrimSuffix(a.getBaseURL(), "/"+shipyardControllerBaseURL)
}
return baseURL
}
40 changes: 40 additions & 0 deletions pkg/api/utils/apiUtils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package api

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestAPIHandler_getAPIServicePath(t *testing.T) {
type fields struct {
BaseURL string
}
tests := []struct {
name string
fields fields
want string
}{
{
name: "remove controlPlane path suffix",
fields: fields{
BaseURL: "my-api.sh/api/controlPlane",
},
want: "my-api.sh/api",
},
{
name: "don't modify anything for internal API calls",
fields: fields{
BaseURL: "api-service",
},
want: "api-service",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
a := &APIHandler{
BaseURL: tt.fields.BaseURL,
}
assert.Equalf(t, tt.want, a.getAPIServicePath(), "getAPIServicePath()")
})
}
}