From 0650f7701c35cfd296d85e6e224fab6770028b09 Mon Sep 17 00:00:00 2001 From: Florian Bacher Date: Fri, 3 Jun 2022 10:30:35 +0200 Subject: [PATCH] fix: Set the path properly for calls to api-service (#471) --- pkg/api/utils/apiUtils.go | 20 ++++++++--------- pkg/api/utils/apiUtils_test.go | 40 ++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 10 deletions(-) create mode 100644 pkg/api/utils/apiUtils_test.go diff --git a/pkg/api/utils/apiUtils.go b/pkg/api/utils/apiUtils.go index 84e6ee98..c5f32b46 100644 --- a/pkg/api/utils/apiUtils.go +++ b/pkg/api/utils/apiUtils.go @@ -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 { @@ -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 { @@ -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 +} diff --git a/pkg/api/utils/apiUtils_test.go b/pkg/api/utils/apiUtils_test.go new file mode 100644 index 00000000..242dd9f9 --- /dev/null +++ b/pkg/api/utils/apiUtils_test.go @@ -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()") + }) + } +}