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

feat: Add v2.KeptnInterface that adds context.Context support to api.KeptnInterface #449

Merged
merged 61 commits into from
Jun 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
61 commits
Select commit Hold shift + click to select a range
7ebcc69
Add `context.Context` to request methods
arthurpitman Apr 4, 2022
e83fcdc
Add get method
arthurpitman Apr 4, 2022
04691e5
Use get methods
arthurpitman Apr 7, 2022
2bc1f2f
Add context to `APIV1Interface`
arthurpitman Apr 7, 2022
0398bc5
Add context to `AuthV1Interface`
arthurpitman Apr 7, 2022
0d43bd9
Add context to `EventsV1Interface`
arthurpitman Apr 7, 2022
eb1b4f1
Add context to `LogsV1Interface`
arthurpitman Apr 7, 2022
199fa04
Add context to `ProjectsV1Interface`
arthurpitman Apr 14, 2022
80ee98b
Add initial context support to `ResourcesV1Interface`
arthurpitman Apr 14, 2022
75d6875
Add context to `ResourcesV1Interface`
arthurpitman Apr 19, 2022
4e3979e
Add context to `SecretsV1Interface`
arthurpitman Apr 19, 2022
8ecb4bd
Add context to `SequencesV1Interface`
arthurpitman Apr 19, 2022
4e882c4
Add context to `ServicesV1Interface`
arthurpitman Apr 19, 2022
379aeef
Add context to `ShipyardControlV1Interface`
arthurpitman Apr 19, 2022
ddea8ee
Add context to `StagesV1Interface`
arthurpitman Apr 19, 2022
340a256
Add context to `UniformV1Interface`
arthurpitman Apr 19, 2022
8d2893e
Add context to `Keptn` and `KeptnBase`
arthurpitman Apr 26, 2022
1b644d4
Fix comments
arthurpitman Apr 26, 2022
62fa95d
Introduce APIV2Interface
arthurpitman May 6, 2022
c511097
Second option: APIV2Interface with `...WithContext` methods
arthurpitman May 6, 2022
a27f916
Create `v2` as duplicate of `api` package
arthurpitman May 10, 2022
fe384f9
Fix `v2.AuthInterface`
arthurpitman May 10, 2022
31903f1
Fix `v2.EventsInterface`
arthurpitman May 10, 2022
d5350f1
Fix `v2.LogsInterface`
arthurpitman May 10, 2022
c390f3a
Fix `v2.ProjectsInterface`
arthurpitman May 10, 2022
177e0c4
Fix `v2.ResourcesInterface`
arthurpitman May 10, 2022
523d30b
Fix `v2.SecretsInterface`
arthurpitman May 10, 2022
f995f61
Fix `v2.SequencesInterface`
arthurpitman May 10, 2022
6e308a7
Fix `v2.ServicesInterface`
arthurpitman May 10, 2022
cdf2a25
Fix `v2.ShipyardControlInterface`
arthurpitman May 10, 2022
5ece0f1
Fix `v2.StagesInterface`
arthurpitman May 10, 2022
990ac0c
Fix `v2.UniformInterface`
arthurpitman May 10, 2022
ca49659
Fix `v2.Client`
arthurpitman May 10, 2022
42be6e3
APIHandler: v1 embeds v2
arthurpitman May 31, 2022
e315cbc
AuthHandler: v1 embeds v2
arthurpitman May 31, 2022
923a93d
EventHandler: v1 embeds v2
arthurpitman May 31, 2022
ca30f63
LogHandler: v1 embeds v2
arthurpitman May 31, 2022
28fda4f
ProjectHandler: v1 embeds v2
arthurpitman May 31, 2022
94bce5c
SecretHandler: v1 embeds v2
arthurpitman May 31, 2022
57667b0
SequenceControlHandler: v1 embeds v2
arthurpitman May 31, 2022
a27461c
ServiceHandler: v1 embeds v2
arthurpitman May 31, 2022
bd86694
ShipyardControllerHandler: v1 embeds v2
arthurpitman May 31, 2022
1219166
StageHandler: v1 embeds v2
arthurpitman May 31, 2022
0e31d6e
UniformHandler: v1 embeds v2
arthurpitman May 31, 2022
f0cd12d
Revert "Add context to `Keptn` and `KeptnBase`"
arthurpitman May 31, 2022
0320167
Remove redundant test
arthurpitman May 31, 2022
540a1f8
Revert `ResourcesV1Interface`
arthurpitman May 31, 2022
fdce402
Rename and export implementation methods
arthurpitman Jun 1, 2022
65f9564
Fix options for `v2.ResourceHandler` methods
arthurpitman Jun 1, 2022
534acb6
ResourceHandler: v1 embeds v2
arthurpitman Jun 1, 2022
4c398d1
Remove redundant test
arthurpitman Jun 1, 2022
d71e1ec
Add construction functions
arthurpitman Jun 1, 2022
92ae5d9
Harmonize construction functions
arthurpitman Jun 2, 2022
b8929e1
Remove deprecated functions from v2
arthurpitman Jun 2, 2022
3ea9b5c
Add v2 construction functions
arthurpitman Jun 2, 2022
9a505e6
Polish v2 handlers
arthurpitman Jun 8, 2022
80183ee
Add missing functions for APIHandler
arthurpitman Jun 9, 2022
4eb9756
Merge branch 'master' into feature/add-context-to-api-set
arthurpitman Jun 20, 2022
6c057c6
added missing api handler in GetMetadata()
bacherfl Jun 21, 2022
46689a2
added alias for v2.ResourceNotFoundError to stay backwards compatible
bacherfl Jun 21, 2022
920b501
fix: Make sure all api handlers are set when using v2 api handlers in…
bacherfl Jun 23, 2022
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
163 changes: 104 additions & 59 deletions pkg/api/utils/apiServiceUtils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package api

import (
"bytes"
"context"
"crypto/tls"
"fmt"
"io/ioutil"
Expand Down Expand Up @@ -61,11 +62,66 @@ func getClientTransport(rt http.RoundTripper) http.RoundTripper {
return tr
}
return rt
}

func getAndExpectOK(ctx context.Context, uri string, api APIService) ([]byte, *models.Error) {
body, statusCode, status, err := get(ctx, uri, api)
if err != nil {
return nil, err
}

if statusCode == 200 {
return body, nil
}

if len(body) > 0 {
return nil, handleErrStatusCode(statusCode, body)
}

return nil, buildErrorResponse(fmt.Sprintf("Received unexpected response: %d %s", statusCode, status))
}

func putWithEventContext(uri string, data []byte, api APIService) (*models.EventContext, *models.Error) {
req, err := http.NewRequest("PUT", uri, bytes.NewBuffer(data))
func getAndExpectSuccess(ctx context.Context, uri string, api APIService) ([]byte, *models.Error) {
body, statusCode, status, err := get(ctx, uri, api)
if err != nil {
return nil, err
}

if statusCode >= 200 && statusCode < 300 {
return body, nil
}

if len(body) > 0 {
return nil, handleErrStatusCode(statusCode, body)
}

return nil, buildErrorResponse(fmt.Sprintf("Received unexpected response: %d %s", statusCode, status))
}

func get(ctx context.Context, uri string, api APIService) ([]byte, int, string, *models.Error) {
req, err := http.NewRequestWithContext(ctx, "GET", uri, nil)
if err != nil {
return nil, 0, "", buildErrorResponse(err.Error())
}
req.Header.Set("Content-Type", "application/json")
addAuthHeader(req, api)

resp, err := api.getHTTPClient().Do(req)
if err != nil {
return nil, 0, "", buildErrorResponse(err.Error())
}
defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, 0, "", buildErrorResponse(err.Error())
}

return body, resp.StatusCode, resp.Status, nil
}

func putWithEventContext(ctx context.Context, uri string, data []byte, api APIService) (*models.EventContext, *models.Error) {
req, err := http.NewRequestWithContext(ctx, "PUT", uri, bytes.NewBuffer(data))
if err != nil {
return nil, buildErrorResponse(err.Error())
}
Expand All @@ -84,20 +140,21 @@ func putWithEventContext(uri string, data []byte, api APIService) (*models.Event
}

if resp.StatusCode >= 200 && resp.StatusCode <= 204 {
if len(body) > 0 {
eventContext := &models.EventContext{}
if err = eventContext.FromJSON(body); err != nil {
// failed to parse json
return nil, buildErrorResponse(err.Error() + "\n" + "-----DETAILS-----" + string(body))
}

if eventContext.KeptnContext != nil {
fmt.Println("ID of Keptn context: " + *eventContext.KeptnContext)
}
return eventContext, nil
if len(body) == 0 {
return nil, nil
}

eventContext := &models.EventContext{}

if err = eventContext.FromJSON(body); err != nil {
// failed to parse json
return nil, buildErrorResponse(err.Error() + "\n" + "-----DETAILS-----" + string(body))
}

return nil, nil
if eventContext.KeptnContext != nil {
fmt.Println("ID of Keptn context: " + *eventContext.KeptnContext)
}
return eventContext, nil
}

if len(body) > 0 {
Expand All @@ -107,8 +164,8 @@ func putWithEventContext(uri string, data []byte, api APIService) (*models.Event
return nil, buildErrorResponse(fmt.Sprintf("Received unexpected response: %d %s", resp.StatusCode, resp.Status))
}

func put(uri string, data []byte, api APIService) (string, *models.Error) {
req, err := http.NewRequest("PUT", uri, bytes.NewBuffer(data))
func put(ctx context.Context, uri string, data []byte, api APIService) (string, *models.Error) {
req, err := http.NewRequestWithContext(ctx, "PUT", uri, bytes.NewBuffer(data))
if err != nil {
return "", buildErrorResponse(err.Error())
}
Expand All @@ -127,11 +184,7 @@ func put(uri string, data []byte, api APIService) (string, *models.Error) {
}

if resp.StatusCode >= 200 && resp.StatusCode <= 204 {
if len(body) > 0 {
return string(body), nil
}

return "", nil
return string(body), nil
}

if len(body) > 0 {
Expand All @@ -141,8 +194,8 @@ func put(uri string, data []byte, api APIService) (string, *models.Error) {
return "", buildErrorResponse(fmt.Sprintf("Received unexpected response: %d %s", resp.StatusCode, resp.Status))
}

func postWithEventContext(uri string, data []byte, api APIService) (*models.EventContext, *models.Error) {
req, err := http.NewRequest("POST", uri, bytes.NewBuffer(data))
func postWithEventContext(ctx context.Context, uri string, data []byte, api APIService) (*models.EventContext, *models.Error) {
req, err := http.NewRequestWithContext(ctx, "POST", uri, bytes.NewBuffer(data))
if err != nil {
return nil, buildErrorResponse(err.Error())
}
Expand All @@ -161,20 +214,20 @@ func postWithEventContext(uri string, data []byte, api APIService) (*models.Even
}

if resp.StatusCode >= 200 && resp.StatusCode <= 204 {
if len(body) > 0 {
eventContext := &models.EventContext{}
if err = eventContext.FromJSON(body); err != nil {
// failed to parse json
return nil, buildErrorResponse(err.Error() + "\n" + "-----DETAILS-----" + string(body))
}

if eventContext.KeptnContext != nil {
fmt.Println("ID of Keptn context: " + *eventContext.KeptnContext)
}
return eventContext, nil
if len(body) == 0 {
return nil, nil
}

eventContext := &models.EventContext{}
if err = eventContext.FromJSON(body); err != nil {
// failed to parse json
return nil, buildErrorResponse(err.Error() + "\n" + "-----DETAILS-----" + string(body))
}

return nil, nil
if eventContext.KeptnContext != nil {
fmt.Println("ID of Keptn context: " + *eventContext.KeptnContext)
}
return eventContext, nil
}

if len(body) > 0 {
Expand All @@ -184,8 +237,8 @@ func postWithEventContext(uri string, data []byte, api APIService) (*models.Even
return nil, buildErrorResponse(fmt.Sprintf("Received unexpected response: %d %s", resp.StatusCode, resp.Status))
}

func post(uri string, data []byte, api APIService) (string, *models.Error) {
req, err := http.NewRequest("POST", uri, bytes.NewBuffer(data))
func post(ctx context.Context, uri string, data []byte, api APIService) (string, *models.Error) {
req, err := http.NewRequestWithContext(ctx, "POST", uri, bytes.NewBuffer(data))
if err != nil {
return "", buildErrorResponse(err.Error())
}
Expand All @@ -204,11 +257,7 @@ func post(uri string, data []byte, api APIService) (string, *models.Error) {
}

if resp.StatusCode >= 200 && resp.StatusCode <= 204 {
if len(body) > 0 {
return string(body), nil
}

return "", nil
return string(body), nil
}

if len(body) > 0 {
Expand All @@ -218,8 +267,8 @@ func post(uri string, data []byte, api APIService) (string, *models.Error) {
return "", buildErrorResponse(fmt.Sprintf("Received unexpected response: %d %s", resp.StatusCode, resp.Status))
}

func deleteWithEventContext(uri string, api APIService) (*models.EventContext, *models.Error) {
req, err := http.NewRequest("DELETE", uri, nil)
func deleteWithEventContext(ctx context.Context, uri string, api APIService) (*models.EventContext, *models.Error) {
req, err := http.NewRequestWithContext(ctx, "DELETE", uri, nil)
if err != nil {
return nil, buildErrorResponse(err.Error())
}
Expand All @@ -238,23 +287,23 @@ func deleteWithEventContext(uri string, api APIService) (*models.EventContext, *
}

if resp.StatusCode >= 200 && resp.StatusCode < 300 {
if len(body) > 0 {
eventContext := &models.EventContext{}
if err = eventContext.FromJSON(body); err != nil {
// failed to parse json
return nil, buildErrorResponse(err.Error() + "\n" + "-----DETAILS-----" + string(body))
}
return eventContext, nil
if len(body) == 0 {
return nil, nil
}

return nil, nil
eventContext := &models.EventContext{}
if err = eventContext.FromJSON(body); err != nil {
// failed to parse json
return nil, buildErrorResponse(err.Error() + "\n" + "-----DETAILS-----" + string(body))
}
return eventContext, nil
}

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

func delete(uri string, api APIService) (string, *models.Error) {
req, err := http.NewRequest("DELETE", uri, nil)
func delete(ctx context.Context, uri string, api APIService) (string, *models.Error) {
req, err := http.NewRequestWithContext(ctx, "DELETE", uri, nil)
if err != nil {
return "", buildErrorResponse(err.Error())
}
Expand All @@ -273,11 +322,7 @@ func delete(uri string, api APIService) (string, *models.Error) {
}

if resp.StatusCode >= 200 && resp.StatusCode < 300 {
if len(body) > 0 {
return string(body), nil
}

return "", nil
return string(body), nil
}

return "", handleErrStatusCode(resp.StatusCode, body)
Expand Down
Loading