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): asset scan estimation statuses (#1074)
* refactor(api): asset scan estimation statuses * refactor(api): add guards to endpoints, fix unit test, change db schema * fix(api): undefined variable * fix(api): gofumpt controller * fix(api): gofmt file * refactor(api): dont return pointer from GetStatus functions * refactor(api): remove manually from aborted description * refactor(api): rerun oapicodegen
- Loading branch information
1 parent
8099c85
commit 8eb8eb5
Showing
15 changed files
with
491 additions
and
295 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package models | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
) | ||
|
||
var assetScanEstimationStatusStateTransitions = map[AssetScanEstimationStatusState][]AssetScanEstimationStatusState{ | ||
AssetScanEstimationStatusStatePending: { | ||
AssetScanEstimationStatusStateAborted, | ||
AssetScanEstimationStatusStateDone, | ||
AssetScanEstimationStatusStateFailed, | ||
}, | ||
AssetScanEstimationStatusStateFailed: { | ||
AssetScanEstimationStatusStateAborted, | ||
}, | ||
} | ||
|
||
var assetScanEstimationStatusReasonMapping = map[AssetScanEstimationStatusState][]AssetScanEstimationStatusReason{ | ||
AssetScanEstimationStatusStatePending: { | ||
AssetScanEstimationStatusReasonCreated, | ||
}, | ||
AssetScanEstimationStatusStateAborted: { | ||
AssetScanEstimationStatusReasonCancellation, | ||
}, | ||
AssetScanEstimationStatusStateFailed: { | ||
AssetScanEstimationStatusReasonAborted, | ||
AssetScanEstimationStatusReasonError, | ||
}, | ||
AssetScanEstimationStatusStateDone: { | ||
AssetScanEstimationStatusReasonSuccess, | ||
}, | ||
} | ||
|
||
func NewAssetScanEstimationStatus(s AssetScanEstimationStatusState, r AssetScanEstimationStatusReason, m *string) *AssetScanEstimationStatus { | ||
return &AssetScanEstimationStatus{ | ||
State: s, | ||
Reason: r, | ||
Message: m, | ||
LastTransitionTime: time.Now(), | ||
} | ||
} | ||
|
||
func (a *AssetScanEstimationStatus) Equals(b AssetScanEstimationStatus) 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 *AssetScanEstimationStatus) isValidStatusTransition(b AssetScanEstimationStatus) error { | ||
transitions := assetScanEstimationStatusStateTransitions[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 *AssetScanEstimationStatus) isValidReason() error { | ||
reasons := assetScanEstimationStatusReasonMapping[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 *AssetScanEstimationStatus) IsValidTransition(b AssetScanEstimationStatus) 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
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
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.