This repository has been archived by the owner on Jan 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Use correct URLs in Keptn API clients (#783)
* Introduce `keptn.ClientFactory` Signed-off-by: Arthur Pitman <[email protected]> * Cleanup environment variable variable names Signed-off-by: Arthur Pitman <[email protected]> * Move code to keptn package Signed-off-by: Arthur Pitman <[email protected]> * Unexport functions Signed-off-by: Arthur Pitman <[email protected]> * Revert back to `APIClient` for creating services Signed-off-by: Arthur Pitman <[email protected]> * Linting Signed-off-by: Arthur Pitman <[email protected]>
- Loading branch information
1 parent
15c1132
commit 479d2f2
Showing
9 changed files
with
216 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package keptn | ||
|
||
import ( | ||
"net/http" | ||
|
||
api "github.com/keptn/go-utils/pkg/api/utils" | ||
) | ||
|
||
// ClientFactoryInterface provides a factories for clients. | ||
type ClientFactoryInterface interface { | ||
CreateEventClient() EventClientInterface | ||
CreateResourceClient() ResourceClientInterface | ||
CreateServiceClient() ServiceClientInterface | ||
CreateUniformClient() UniformClientInterface | ||
} | ||
|
||
// ClientFactory is an implementation of ClientFactoryInterface. | ||
type ClientFactory struct { | ||
} | ||
|
||
// NewClientFactory creates a new ClientFactory. | ||
func NewClientFactory() *ClientFactory { | ||
return &ClientFactory{} | ||
} | ||
|
||
// CreateEventClient creates an EventClientInterface. | ||
func (c *ClientFactory) CreateEventClient() EventClientInterface { | ||
return NewEventClient(api.NewEventHandler(getDatastoreURL())) | ||
} | ||
|
||
// CreateResourceClient creates a ResourceClientInterface. | ||
func (c *ClientFactory) CreateResourceClient() ResourceClientInterface { | ||
return NewResourceClient(api.NewResourceHandler(getConfigurationServiceURL())) | ||
} | ||
|
||
// CreateServiceClient creates a ServiceClientInterface. | ||
func (c *ClientFactory) CreateServiceClient() ServiceClientInterface { | ||
return NewServiceClient( | ||
api.NewServiceHandler(getShipyardControllerURL()), | ||
&http.Client{}) | ||
} | ||
|
||
// CreateUniformClient creates a UniformClientInterface. | ||
func (c *ClientFactory) CreateUniformClient() UniformClientInterface { | ||
return NewUniformClient(api.NewUniformHandler(getShipyardControllerURL())) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package keptn | ||
|
||
import "github.com/keptn/go-utils/pkg/lib/keptn" | ||
|
||
const shipyardControllerURLEnvironmentVariableName = "SHIPYARD_CONTROLLER" | ||
const configurationServiceEnvironmentVariableName = "CONFIGURATION_SERVICE" | ||
const datastoreEnvironmentVariableName = "DATASTORE" | ||
|
||
const defaultShipyardControllerURL = "http://shipyard-controller:8080" | ||
|
||
// getConfigurationServiceURL Returns the endpoint to the configuration-service. | ||
func getConfigurationServiceURL() string { | ||
return getKeptnServiceURL(configurationServiceEnvironmentVariableName, keptn.ConfigurationServiceURL) | ||
} | ||
|
||
// getDatastoreURL Returns the endpoint to the datastore. | ||
func getDatastoreURL() string { | ||
return getKeptnServiceURL(datastoreEnvironmentVariableName, keptn.DatastoreURL) | ||
} | ||
|
||
// getShipyardControllerURL Returns the endpoint to the shipyard-controller. | ||
func getShipyardControllerURL() string { | ||
return getKeptnServiceURL(shipyardControllerURLEnvironmentVariableName, defaultShipyardControllerURL) | ||
} | ||
|
||
func getKeptnServiceURL(serviceName, defaultURL string) string { | ||
url, err := keptn.GetServiceEndpoint(serviceName) | ||
if err != nil { | ||
return defaultURL | ||
} | ||
return url.String() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package keptn | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
|
||
"github.com/keptn-contrib/dynatrace-service/internal/rest" | ||
) | ||
|
||
// APIClientInterface provides methods for accessing a Keptn API. | ||
type APIClientInterface interface { | ||
// Post performs a post request and returns a validated response or an error. | ||
Post(apiPath string, body []byte) ([]byte, error) | ||
} | ||
|
||
// APIClient is an implementation of APIClientInterface using a rest.ClientInterface. | ||
type APIClient struct { | ||
restClient rest.ClientInterface | ||
} | ||
|
||
// NewAPIClient creates a new APIClient. | ||
func NewAPIClient(client rest.ClientInterface) *APIClient { | ||
return &APIClient{ | ||
restClient: client, | ||
} | ||
} | ||
|
||
// Post performs a post request and returns a validated response or an error. | ||
func (c *APIClient) Post(apiPath string, body []byte) ([]byte, error) { | ||
body, status, url, err := c.restClient.Post(apiPath, body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return validateResponse(body, status, url) | ||
} | ||
|
||
// genericAPIErrorDTO will support multiple Keptn API errors | ||
type genericAPIErrorDTO struct { | ||
Code int `json:"code"` | ||
ErrorCode int `json:"errorCode"` | ||
Message string `json:"message"` | ||
} | ||
|
||
func (e *genericAPIErrorDTO) status() int { | ||
if e.Code != 0 { | ||
return e.Code | ||
} | ||
|
||
return e.ErrorCode | ||
} | ||
|
||
// APIError respresents an error returned from a Keptn API. | ||
type APIError struct { | ||
status int | ||
message string | ||
url string | ||
} | ||
|
||
// Error returns a string representation of the APIError. | ||
func (e *APIError) Error() string { | ||
return fmt.Sprintf("Keptn API error (%d): %s - URL: %s", e.status, e.message, e.url) | ||
} | ||
|
||
func validateResponse(body []byte, status int, url string) ([]byte, error) { | ||
if status < 200 || status >= 300 { | ||
// try to get the error information | ||
keptnAPIError := &genericAPIErrorDTO{} | ||
err := json.Unmarshal(body, keptnAPIError) | ||
if err != nil { | ||
return body, &APIError{ | ||
status: status, | ||
message: string(body), | ||
url: url, | ||
} | ||
} | ||
|
||
return nil, &APIError{ | ||
status: keptnAPIError.status(), | ||
message: keptnAPIError.Message, | ||
url: url, | ||
} | ||
} | ||
|
||
return body, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.