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

Commit

Permalink
fix: Set the path properly for calls to api-service (#470)
Browse files Browse the repository at this point in the history
* fix: Set the path properly for calls to api-service

Signed-off-by: Florian Bacher <florian.bacher@dynatrace.com>

* added test

Signed-off-by: Florian Bacher <florian.bacher@dynatrace.com>

* removed debug output

Signed-off-by: Florian Bacher <florian.bacher@dynatrace.com>
  • Loading branch information
bacherfl committed Jun 3, 2022
1 parent 669fb3d commit a3c50ce
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 10 deletions.
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()")
})
}
}

0 comments on commit a3c50ce

Please sign in to comment.