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

Commit

Permalink
fix: Fixed wrong paths for apiutils (#469)
Browse files Browse the repository at this point in the history
* fix: Fixed wrong paths for apiutils

Signed-off-by: warber <[email protected]>

* Fixed wrong paths for apiutils

Signed-off-by: warber <[email protected]>

* Fixed wrong paths for apiutils

Signed-off-by: warber <[email protected]>

* Fixed wrong paths for apiutils

Signed-off-by: warber <[email protected]>

* review

Signed-off-by: warber <[email protected]>

* review

Signed-off-by: warber <[email protected]>
  • Loading branch information
warber authored Jun 1, 2022
1 parent 2052e14 commit 623e06c
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions pkg/api/utils/apiUtils.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ func NewAuthenticatedAPIHandler(baseURL string, authToken string, authHeader str
func createAuthenticatedAPIHandler(baseURL string, authToken string, authHeader string, httpClient *http.Client, scheme string) *APIHandler {
baseURL = strings.TrimPrefix(baseURL, "http://")
baseURL = strings.TrimPrefix(baseURL, "https://")
if !strings.HasSuffix(baseURL, shipyardControllerBaseURL) {
baseURL += "/" + shipyardControllerBaseURL
}

return &APIHandler{
BaseURL: baseURL,
AuthHeader: authHeader,
Expand Down Expand Up @@ -71,11 +75,17 @@ 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"
}

bodyStr, err := event.ToJSON()
if err != nil {
return nil, buildErrorResponse(err.Error())
}
return postWithEventContext(a.Scheme+"://"+a.getBaseURL()+v1EventPath, bodyStr, a)
return postWithEventContext(a.Scheme+"://"+baseURL+v1EventPath, bodyStr, a)
}

// TriggerEvaluation triggers a new evaluation
Expand All @@ -84,7 +94,7 @@ func (a *APIHandler) TriggerEvaluation(project, stage, service string, evaluatio
if err != nil {
return nil, buildErrorResponse(err.Error())
}
return postWithEventContext(a.Scheme+"://"+a.getBaseURL()+"/"+shipyardControllerBaseURL+v1ProjectPath+"/"+project+pathToStage+"/"+stage+pathToService+"/"+service+"/evaluation", bodyStr, a)
return postWithEventContext(a.Scheme+"://"+a.getBaseURL()+v1ProjectPath+"/"+project+pathToStage+"/"+stage+pathToService+"/"+service+"/evaluation", bodyStr, a)
}

// CreateProject creates a new project
Expand All @@ -93,7 +103,7 @@ func (a *APIHandler) CreateProject(project models.CreateProject) (string, *model
if err != nil {
return "", buildErrorResponse(err.Error())
}
return post(a.Scheme+"://"+a.getBaseURL()+"/"+shipyardControllerBaseURL+v1ProjectPath, bodyStr, a)
return post(a.Scheme+"://"+a.getBaseURL()+v1ProjectPath, bodyStr, a)
}

// UpdateProject updates project
Expand All @@ -102,12 +112,12 @@ func (a *APIHandler) UpdateProject(project models.CreateProject) (string, *model
if err != nil {
return "", buildErrorResponse(err.Error())
}
return put(a.Scheme+"://"+a.getBaseURL()+"/"+shipyardControllerBaseURL+v1ProjectPath, bodyStr, a)
return put(a.Scheme+"://"+a.getBaseURL()+v1ProjectPath, bodyStr, a)
}

// DeleteProject deletes a project
func (a *APIHandler) DeleteProject(project models.Project) (*models.DeleteProjectResponse, *models.Error) {
resp, err := delete(a.Scheme+"://"+a.getBaseURL()+"/"+shipyardControllerBaseURL+v1ProjectPath+"/"+project.ProjectName, a)
resp, err := delete(a.Scheme+"://"+a.getBaseURL()+v1ProjectPath+"/"+project.ProjectName, a)
if err != nil {
return nil, err
}
Expand All @@ -127,12 +137,12 @@ func (a *APIHandler) CreateService(project string, service models.CreateService)
if err != nil {
return "", buildErrorResponse(err.Error())
}
return post(a.Scheme+"://"+a.getBaseURL()+"/"+shipyardControllerBaseURL+v1ProjectPath+"/"+project+pathToService, bodyStr, a)
return post(a.Scheme+"://"+a.getBaseURL()+v1ProjectPath+"/"+project+pathToService, bodyStr, a)
}

// DeleteProject deletes a project
func (a *APIHandler) DeleteService(project, service string) (*models.DeleteServiceResponse, *models.Error) {
resp, err := delete(a.Scheme+"://"+a.getBaseURL()+"/"+shipyardControllerBaseURL+v1ProjectPath+"/"+project+pathToService+"/"+service, a)
resp, err := delete(a.Scheme+"://"+a.getBaseURL()+v1ProjectPath+"/"+project+pathToService+"/"+service, a)
if err != nil {
return nil, err
}
Expand All @@ -148,7 +158,13 @@ func (a *APIHandler) DeleteService(project, service string) (*models.DeleteServi

// GetMetadata retrieve keptn MetaData information
func (a *APIHandler) GetMetadata() (*models.Metadata, *models.Error) {
req, err := http.NewRequest("GET", a.Scheme+"://"+a.getBaseURL()+v1MetadataPath, nil)
baseURL := a.getBaseURL()
if strings.HasSuffix(baseURL, "/"+shipyardControllerBaseURL) {
baseURL = strings.TrimSuffix(a.getBaseURL(), "/"+shipyardControllerBaseURL)
baseURL += "/api"
}

req, err := http.NewRequest("GET", a.Scheme+"://"+baseURL+v1MetadataPath, nil)
if err != nil {
return nil, buildErrorResponse(err.Error())
}
Expand Down

0 comments on commit 623e06c

Please sign in to comment.