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
feat: Use keptn/go-utils to access Keptn APIs #767
Merged
arthurpitman
merged 21 commits into
master
from
feature/754/use-go-utils-for-keptn-apis
Apr 12, 2022
Merged
Changes from 7 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
da8b5ec
Use goutils APIs in UniformClient
arthurpitman 5431667
Add test for `GetIntegrationIDByName`
arthurpitman b6f1b2c
Use goutils APIs in ServiceClient
arthurpitman ff183e8
Use goutils APIs in ResourceClient and ConfigResourceClient
arthurpitman 71525ce
Remove unused LocalResourceClient
arthurpitman 16a9993
Use goutils APIs in EventClient
arthurpitman c66e7ae
Remove unused APIClient
arthurpitman 0163882
Linting
arthurpitman e7b5355
Method for creating integrations in test
arthurpitman d079738
Remove redundant `EventClientBaseInterface`
arthurpitman 9408229
Wrap error
arthurpitman 4c13909
Remove `keptn` from filenames
arthurpitman efe2b0a
Renaming types
arthurpitman 88c48ac
Rename files
arthurpitman 3ff8b54
Fix error check
arthurpitman a3b3d15
Remove redundant check
arthurpitman 1a10843
Shorten error messages
arthurpitman f7b14ac
Improve problem ID parsing
arthurpitman c3bd94a
Rename constant
arthurpitman ed26f1e
Extract function for getting problem ID from labels
arthurpitman 9fc9cda
Extract problem ID from URL fragment and add test
arthurpitman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,11 +5,13 @@ import ( | |
"fmt" | ||
|
||
cloudevents "github.com/cloudevents/sdk-go/v2" | ||
api "github.com/keptn/go-utils/pkg/api/utils" | ||
keptnevents "github.com/keptn/go-utils/pkg/lib" | ||
keptnv2 "github.com/keptn/go-utils/pkg/lib/v0_2_0" | ||
log "github.com/sirupsen/logrus" | ||
|
||
"github.com/keptn-contrib/dynatrace-service/internal/adapter" | ||
"github.com/keptn-contrib/dynatrace-service/internal/common" | ||
"github.com/keptn-contrib/dynatrace-service/internal/config" | ||
"github.com/keptn-contrib/dynatrace-service/internal/credentials" | ||
"github.com/keptn-contrib/dynatrace-service/internal/deployment" | ||
|
@@ -24,18 +26,23 @@ type DynatraceEventHandler interface { | |
HandleEvent() error | ||
} | ||
|
||
func NewEventHandler(event cloudevents.Event) DynatraceEventHandler { | ||
eventHandler, err := getEventHandler(event) | ||
func NewEventHandler(event cloudevents.Event) (DynatraceEventHandler, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [golint] reported by reviewdog 🐶 |
||
keptnAPISet, err := api.New(common.GetShipyardControllerURL()) | ||
if err != nil { | ||
return nil, fmt.Errorf("could not create Keptn API set: %w", err) | ||
} | ||
|
||
eventHandler, err := getEventHandler(event, *keptnAPISet) | ||
if err != nil { | ||
err = fmt.Errorf("cannot handle event: %w", err) | ||
log.Error(err.Error()) | ||
return NewErrorHandler(err, event) | ||
return NewErrorHandler(err, event, keptn.NewUniformClient(keptnAPISet.UniformV1())), nil | ||
} | ||
|
||
return eventHandler | ||
return eventHandler, nil | ||
} | ||
|
||
func getEventHandler(event cloudevents.Event) (DynatraceEventHandler, error) { | ||
func getEventHandler(event cloudevents.Event, keptnAPISet api.APISet) (DynatraceEventHandler, error) { | ||
log.WithField("eventType", event.Type()).Debug("Received event") | ||
|
||
keptnEvent, err := getEventAdapter(event) | ||
|
@@ -52,7 +59,7 @@ func getEventHandler(event cloudevents.Event) (DynatraceEventHandler, error) { | |
return nil, errors.New("event has no project") | ||
} | ||
|
||
dynatraceConfigGetter := config.NewDynatraceConfigGetter(keptn.NewDefaultResourceClient()) | ||
dynatraceConfigGetter := config.NewDynatraceConfigGetter(keptn.NewResourceClient(keptn.NewConfigResourceClient(keptnAPISet.ResourcesV1()))) | ||
dynatraceConfig, err := dynatraceConfigGetter.GetDynatraceConfig(keptnEvent) | ||
if err != nil { | ||
return nil, fmt.Errorf("could not get configuration: %w", err) | ||
|
@@ -77,29 +84,29 @@ func getEventHandler(event cloudevents.Event) (DynatraceEventHandler, error) { | |
|
||
switch aType := keptnEvent.(type) { | ||
case *monitoring.ConfigureMonitoringAdapter: | ||
return monitoring.NewConfigureMonitoringEventHandler(keptnEvent.(*monitoring.ConfigureMonitoringAdapter), dtClient, kClient, keptn.NewDefaultResourceClient(), keptn.NewDefaultServiceClient()), nil | ||
return monitoring.NewConfigureMonitoringEventHandler(keptnEvent.(*monitoring.ConfigureMonitoringAdapter), dtClient, kClient, keptn.NewResourceClient(keptn.NewConfigResourceClient(keptnAPISet.ResourcesV1())), keptn.NewServiceClient(keptnAPISet.ServicesV1(), keptnAPISet.APIV1())), nil | ||
case *problem.ProblemAdapter: | ||
return problem.NewProblemEventHandler(keptnEvent.(*problem.ProblemAdapter), kClient), nil | ||
case *problem.ActionTriggeredAdapter: | ||
return problem.NewActionTriggeredEventHandler(keptnEvent.(*problem.ActionTriggeredAdapter), dtClient, keptn.NewDefaultEventClient(), dynatraceConfig.AttachRules), nil | ||
return problem.NewActionTriggeredEventHandler(keptnEvent.(*problem.ActionTriggeredAdapter), dtClient, keptn.NewEventClient(keptn.NewEventClientBase(keptnAPISet.EventsV1())), dynatraceConfig.AttachRules), nil | ||
case *problem.ActionStartedAdapter: | ||
return problem.NewActionStartedEventHandler(keptnEvent.(*problem.ActionStartedAdapter), dtClient, keptn.NewDefaultEventClient()), nil | ||
return problem.NewActionStartedEventHandler(keptnEvent.(*problem.ActionStartedAdapter), dtClient, keptn.NewEventClient(keptn.NewEventClientBase(keptnAPISet.EventsV1()))), nil | ||
case *problem.ActionFinishedAdapter: | ||
return problem.NewActionFinishedEventHandler(keptnEvent.(*problem.ActionFinishedAdapter), dtClient, keptn.NewDefaultEventClient(), dynatraceConfig.AttachRules), nil | ||
return problem.NewActionFinishedEventHandler(keptnEvent.(*problem.ActionFinishedAdapter), dtClient, keptn.NewEventClient(keptn.NewEventClientBase(keptnAPISet.EventsV1())), dynatraceConfig.AttachRules), nil | ||
case *sli.GetSLITriggeredAdapter: | ||
return sli.NewGetSLITriggeredHandler(keptnEvent.(*sli.GetSLITriggeredAdapter), dtClient, kClient, keptn.NewDefaultResourceClient(), dynatraceConfig.DtCreds, dynatraceConfig.Dashboard), nil | ||
return sli.NewGetSLITriggeredHandler(keptnEvent.(*sli.GetSLITriggeredAdapter), dtClient, kClient, keptn.NewResourceClient(keptn.NewConfigResourceClient(keptnAPISet.ResourcesV1())), dynatraceConfig.DtCreds, dynatraceConfig.Dashboard), nil | ||
case *deployment.DeploymentFinishedAdapter: | ||
return deployment.NewDeploymentFinishedEventHandler(keptnEvent.(*deployment.DeploymentFinishedAdapter), dtClient, keptn.NewDefaultEventClient(), dynatraceConfig.AttachRules), nil | ||
return deployment.NewDeploymentFinishedEventHandler(keptnEvent.(*deployment.DeploymentFinishedAdapter), dtClient, keptn.NewEventClient(keptn.NewEventClientBase(keptnAPISet.EventsV1())), dynatraceConfig.AttachRules), nil | ||
case *deployment.TestTriggeredAdapter: | ||
return deployment.NewTestTriggeredEventHandler(keptnEvent.(*deployment.TestTriggeredAdapter), dtClient, keptn.NewDefaultEventClient(), dynatraceConfig.AttachRules), nil | ||
return deployment.NewTestTriggeredEventHandler(keptnEvent.(*deployment.TestTriggeredAdapter), dtClient, keptn.NewEventClient(keptn.NewEventClientBase(keptnAPISet.EventsV1())), dynatraceConfig.AttachRules), nil | ||
case *deployment.TestFinishedAdapter: | ||
return deployment.NewTestFinishedEventHandler(keptnEvent.(*deployment.TestFinishedAdapter), dtClient, keptn.NewDefaultEventClient(), dynatraceConfig.AttachRules), nil | ||
return deployment.NewTestFinishedEventHandler(keptnEvent.(*deployment.TestFinishedAdapter), dtClient, keptn.NewEventClient(keptn.NewEventClientBase(keptnAPISet.EventsV1())), dynatraceConfig.AttachRules), nil | ||
case *deployment.EvaluationFinishedAdapter: | ||
return deployment.NewEvaluationFinishedEventHandler(keptnEvent.(*deployment.EvaluationFinishedAdapter), dtClient, keptn.NewDefaultEventClient(), dynatraceConfig.AttachRules), nil | ||
return deployment.NewEvaluationFinishedEventHandler(keptnEvent.(*deployment.EvaluationFinishedAdapter), dtClient, keptn.NewEventClient(keptn.NewEventClientBase(keptnAPISet.EventsV1())), dynatraceConfig.AttachRules), nil | ||
case *deployment.ReleaseTriggeredAdapter: | ||
return deployment.NewReleaseTriggeredEventHandler(keptnEvent.(*deployment.ReleaseTriggeredAdapter), dtClient, keptn.NewDefaultEventClient(), dynatraceConfig.AttachRules), nil | ||
return deployment.NewReleaseTriggeredEventHandler(keptnEvent.(*deployment.ReleaseTriggeredAdapter), dtClient, keptn.NewEventClient(keptn.NewEventClientBase(keptnAPISet.EventsV1())), dynatraceConfig.AttachRules), nil | ||
default: | ||
return NewErrorHandler(fmt.Errorf("this should not have happened, we are missing an implementation for: %T", aType), event), nil | ||
return NewErrorHandler(fmt.Errorf("this should not have happened, we are missing an implementation for: %T", aType), event, keptn.NewUniformClient(keptnAPISet.UniformV1())), nil | ||
} | ||
} | ||
|
||
|
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -3,28 +3,28 @@ package keptn | |
import ( | ||
"errors" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/keptn-contrib/dynatrace-service/internal/adapter" | ||
"github.com/keptn-contrib/dynatrace-service/internal/common" | ||
"github.com/keptn/go-utils/pkg/api/models" | ||
keptnapi "github.com/keptn/go-utils/pkg/api/utils" | ||
keptncommon "github.com/keptn/go-utils/pkg/lib" | ||
keptnv2 "github.com/keptn/go-utils/pkg/lib/v0_2_0" | ||
log "github.com/sirupsen/logrus" | ||
"os" | ||
"strings" | ||
) | ||
|
||
type EventClientBaseInterface interface { | ||
GetEvents(filter *keptnapi.EventFilter) ([]*models.KeptnContextExtendedCE, error) | ||
} | ||
|
||
type EventClientBase struct { | ||
client *keptnapi.EventHandler | ||
client keptnapi.EventsV1Interface | ||
} | ||
|
||
func NewEventClientBase() *EventClientBase { | ||
func NewEventClientBase(client keptnapi.EventsV1Interface) *EventClientBase { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [golint] reported by reviewdog 🐶 |
||
return &EventClientBase{ | ||
client: keptnapi.NewEventHandler(os.Getenv("DATASTORE")), | ||
client: client, | ||
} | ||
} | ||
|
||
|
@@ -55,11 +55,6 @@ func NewEventClient(client EventClientBaseInterface) *EventClient { | |
} | ||
} | ||
|
||
func NewDefaultEventClient() *EventClient { | ||
return NewEventClient( | ||
NewEventClientBase()) | ||
} | ||
|
||
// IsPartOfRemediation checks whether the evaluation.finished event is part of a remediation task sequence | ||
func (c *EventClient) IsPartOfRemediation(event adapter.EventContentAdapter) (bool, error) { | ||
events, err := c.client.GetEvents( | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[golint] reported by reviewdog 🐶
exported function NewErrorHandler should have comment or be unexported