This repository has been archived by the owner on Oct 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(api): refactor scan estimation status api object (#1089)
* refactor(api): refactor scan estimation status api object * refactor(api): rename statuses, change description for them * refactor(api): change some descriptions * refactor(api): fix selector for GET request
- Loading branch information
1 parent
8f7aff3
commit 52aac54
Showing
8 changed files
with
541 additions
and
263 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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 |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package models | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
) | ||
|
||
var scanEstimationStatusStateTransitions = map[ScanEstimationStatusState][]ScanEstimationStatusState{ | ||
ScanEstimationStatusStatePending: { | ||
ScanEstimationStatusStateDiscovered, | ||
ScanEstimationStatusStateAborted, | ||
ScanEstimationStatusStateFailed, | ||
ScanEstimationStatusStateDone, | ||
}, | ||
ScanEstimationStatusStateDiscovered: { | ||
ScanEstimationStatusStateInProgress, | ||
ScanEstimationStatusStateAborted, | ||
ScanEstimationStatusStateFailed, | ||
}, | ||
ScanEstimationStatusStateInProgress: { | ||
ScanEstimationStatusStateAborted, | ||
ScanEstimationStatusStateFailed, | ||
ScanEstimationStatusStateDone, | ||
}, | ||
ScanEstimationStatusStateAborted: { | ||
ScanEstimationStatusStateFailed, | ||
}, | ||
} | ||
|
||
var scanEstimationStatusReasonMapping = map[ScanEstimationStatusState][]ScanEstimationStatusReason{ | ||
ScanEstimationStatusStatePending: { | ||
ScanEstimationStatusReasonCreated, | ||
}, | ||
ScanEstimationStatusStateDiscovered: { | ||
ScanEstimationStatusReasonSuccessfulDiscovery, | ||
}, | ||
ScanEstimationStatusStateInProgress: { | ||
ScanEstimationStatusReasonRunning, | ||
}, | ||
ScanEstimationStatusStateAborted: { | ||
ScanEstimationStatusReasonCancellation, | ||
}, | ||
ScanEstimationStatusStateFailed: { | ||
ScanEstimationStatusReasonAborted, | ||
ScanEstimationStatusReasonError, | ||
ScanEstimationStatusReasonTimeout, | ||
}, | ||
ScanEstimationStatusStateDone: { | ||
ScanEstimationStatusReasonNothingToEstimate, | ||
ScanEstimationStatusReasonSuccess, | ||
}, | ||
} | ||
|
||
func NewScanEstimationStatus(s ScanEstimationStatusState, r ScanEstimationStatusReason, m *string) *ScanEstimationStatus { | ||
return &ScanEstimationStatus{ | ||
State: s, | ||
Reason: r, | ||
Message: m, | ||
LastTransitionTime: time.Now(), | ||
} | ||
} | ||
|
||
func (a *ScanEstimationStatus) Equals(b ScanEstimationStatus) bool { | ||
if a.Message == nil && b.Message != nil { | ||
return false | ||
} | ||
if b.Message == nil && a.Message != nil { | ||
return false | ||
} | ||
if a.Message == nil && b.Message == nil { | ||
return a.State == b.State && a.Reason == b.Reason | ||
} | ||
|
||
return a.State == b.State && a.Reason == b.Reason && *a.Message == *b.Message | ||
} | ||
|
||
func (a *ScanEstimationStatus) isValidStatusTransition(b ScanEstimationStatus) error { | ||
transitions := scanEstimationStatusStateTransitions[a.State] | ||
for _, transition := range transitions { | ||
if transition == b.State { | ||
return nil | ||
} | ||
} | ||
|
||
return fmt.Errorf("invalid transition: from=%s to=%s", a.State, b.State) | ||
} | ||
|
||
func (a *ScanEstimationStatus) isValidReason() error { | ||
reasons := scanEstimationStatusReasonMapping[a.State] | ||
for _, reason := range reasons { | ||
if reason == a.Reason { | ||
return nil | ||
} | ||
} | ||
|
||
return fmt.Errorf("invalid reason for state: state=%s reason=%s", a.State, a.Reason) | ||
} | ||
|
||
func (a *ScanEstimationStatus) IsValidTransition(b ScanEstimationStatus) error { | ||
if a.Equals(b) { | ||
return nil | ||
} | ||
|
||
if err := b.isValidReason(); err != nil { | ||
return err | ||
} | ||
|
||
if err := a.isValidStatusTransition(b); err != nil { | ||
return err | ||
} | ||
|
||
return 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.