-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AB#98 refactor: extract different modes from ApprovalService
- Loading branch information
1 parent
4dec128
commit 9ce27a8
Showing
5 changed files
with
143 additions
and
100 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
59 changes: 59 additions & 0 deletions
59
core/libs/service/src/approval/require-approval-mode.use-case.ts
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,59 @@ | ||
import {Configuration} from "@libs/domain/configuration/configuration" | ||
import { | ||
DiffType, | ||
TerraformDiff, | ||
mapDiffTypeToActions | ||
} from "@libs/domain/terraform/diffs" | ||
import { | ||
TerraformEntity, | ||
isDiffActionIncludedInEntityDecorator, | ||
printShortTerraformEntity | ||
} from "@libs/domain/terraform/resource" | ||
import {Injectable, Logger} from "@nestjs/common" | ||
|
||
@Injectable() | ||
export class RequireApprovalModeUseCase { | ||
isApprovalRequired(data: { | ||
configuration: Configuration | ||
diffsEntityPairs: [TerraformDiff, TerraformEntity][] | ||
}): boolean { | ||
const {diffsEntityPairs, configuration} = data | ||
|
||
// From all the diffs, keep only the ones that requires approval | ||
const resourcesThatRequiredApproval = diffsEntityPairs.filter( | ||
pair => | ||
// Verify first if one of the action to achieve the diffType is in the list of actions that | ||
// always require approval. If this is the case the resource requires approval. | ||
this.doesContainActionThatAlwaysRequireApproval( | ||
configuration, | ||
pair[0].diffType | ||
) || | ||
// If no match is found check is there is a specific decorator associated to the resource. | ||
(pair[1].decorator.type === "manual_approval" && | ||
isDiffActionIncludedInEntityDecorator(pair[1].decorator, pair[0])) | ||
) | ||
|
||
Logger.log( | ||
`Found ${resourcesThatRequiredApproval.length} resource(s) that require approval:` | ||
) | ||
resourcesThatRequiredApproval.forEach(it => | ||
Logger.log(`- ${printShortTerraformEntity(it[1])}`) | ||
) | ||
|
||
return resourcesThatRequiredApproval.length > 0 | ||
} | ||
|
||
private doesContainActionThatAlwaysRequireApproval( | ||
configuration: Configuration, | ||
diffType: DiffType | ||
): boolean { | ||
const actionaThatAlwaysRequireApproval = | ||
configuration.global.requireApprovalActions | ||
const actions = mapDiffTypeToActions(diffType) | ||
|
||
return ( | ||
actionaThatAlwaysRequireApproval !== undefined && | ||
actions.some(it => actionaThatAlwaysRequireApproval.includes(it)) | ||
) | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
core/libs/service/src/approval/safe-to-apply-mode.use-case.ts
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,54 @@ | ||
import {Configuration} from "@libs/domain/configuration/configuration" | ||
import {getSafeToApplyActionsFromDecorator} from "@libs/domain/terraform/approval" | ||
import { | ||
Action, | ||
TerraformDiff, | ||
mapDiffTypeToActions | ||
} from "@libs/domain/terraform/diffs" | ||
import { | ||
TerraformEntity, | ||
printShortTerraformEntity | ||
} from "@libs/domain/terraform/resource" | ||
import {Injectable, Logger} from "@nestjs/common" | ||
|
||
@Injectable() | ||
export class SafeToApplyModeUseCase { | ||
isApprovalRequired(data: { | ||
configuration: Configuration | ||
diffsEntityPairs: [TerraformDiff, TerraformEntity][] | ||
}): boolean { | ||
const {diffsEntityPairs, configuration} = data | ||
|
||
// From all the diffs, remove all the ones that are safe to apply | ||
const resourcesThatAreNotSafeToApply = diffsEntityPairs.filter(pair => { | ||
// Merge the safe to apply actions defined at the global level and the ones defined at the resource level | ||
const safeActionsForResource: Action[] = [ | ||
...(configuration.global.safeToApplyActions ?? []), | ||
...getSafeToApplyActionsFromDecorator(pair[1].decorator) | ||
] | ||
|
||
// It all the actions that will be perfomed to apply the plan are not included in the safe-list, | ||
// it means that there is a potential unsafe action for the resource and we need to ask for approval. | ||
return !areAllItemsIncluded( | ||
safeActionsForResource, | ||
mapDiffTypeToActions(pair[0].diffType) | ||
) | ||
}) | ||
|
||
Logger.log( | ||
`Found ${resourcesThatAreNotSafeToApply.length} resource(s) that are not safe to apply:` | ||
) | ||
resourcesThatAreNotSafeToApply.forEach(it => | ||
Logger.log(`- ${printShortTerraformEntity(it[1])}`) | ||
) | ||
|
||
return resourcesThatAreNotSafeToApply.length > 0 | ||
} | ||
} | ||
|
||
function areAllItemsIncluded<T>( | ||
items: ReadonlyArray<T>, | ||
itemsToCheck: ReadonlyArray<T> | ||
): boolean { | ||
return itemsToCheck.every(it => items.includes(it)) | ||
} |
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