diff --git a/api/util/util.go b/api/util/util.go index 2fea024..3dcf474 100644 --- a/api/util/util.go +++ b/api/util/util.go @@ -67,3 +67,13 @@ func FixActivationActionStatus(action *types.Action) *types.Action { Message: action.Message, } } + +// Contains checks if the given value is in the slice. +func Contains[T comparable](slice []T, value T) bool { + for _, v := range slice { + if value == v { + return true + } + } + return false +} diff --git a/api/util/util_test.go b/api/util/util_test.go index 1a50f3f..7384c1f 100644 --- a/api/util/util_test.go +++ b/api/util/util_test.go @@ -104,3 +104,13 @@ func TestFixActivationActionStatus(t *testing.T) { } } + +func TestContains(t *testing.T) { + assert.True(t, Contains([]int{2, 5, 9}, 2)) + assert.False(t, Contains([]int{2, -3, 9}, 3)) + assert.False(t, Contains([]int{}, 3)) + assert.True(t, Contains([]string{"", "text", "string"}, "text")) + assert.False(t, Contains([]string{"", "text", "string"}, "number")) + assert.True(t, Contains([]types.CommandType{types.CommandActivate, types.CommandDownload, types.CommandUpdate}, types.CommandUpdate)) + assert.False(t, Contains([]types.CommandType{types.CommandActivate, types.CommandDownload, types.CommandUpdate}, types.CommandRollback)) +} diff --git a/updatem/orchestration/update_orchestrator_apply.go b/updatem/orchestration/update_orchestrator_apply.go index feb1e19..f1708f9 100644 --- a/updatem/orchestration/update_orchestrator_apply.go +++ b/updatem/orchestration/update_orchestrator_apply.go @@ -15,11 +15,11 @@ package orchestration import ( "context" "fmt" - "slices" "time" "github.com/eclipse-kanto/update-manager/api" "github.com/eclipse-kanto/update-manager/api/types" + "github.com/eclipse-kanto/update-manager/api/util" "github.com/eclipse-kanto/update-manager/logger" ) @@ -111,7 +111,7 @@ func handleCommandSignal(ctx context.Context, command types.CommandType, orchest executeCommand := func(statuses ...types.StatusType) { for domain, domainStatus := range orchestrator.operation.domains { - if slices.Contains(statuses, domainStatus) { + if util.Contains(statuses, domainStatus) { orchestrator.command(ctx, orchestrator.operation.activityID, domain, command) } } @@ -134,7 +134,7 @@ func handleCommandSignal(ctx context.Context, command types.CommandType, orchest } func (orchestrator *updateOrchestrator) getOwnerConsent(ctx context.Context, command types.CommandType) error { - if command == "" || !slices.Contains(orchestrator.cfg.OwnerConsentCommands, command) { + if command == "" || !util.Contains(orchestrator.cfg.OwnerConsentCommands, command) { return nil } if command == types.CommandRollback || command == types.CommandCleanup {