Skip to content

Commit

Permalink
AB#94 Use global list of types that are safe to apply in validator
Browse files Browse the repository at this point in the history
  • Loading branch information
giovannibaratta committed Dec 8, 2023
1 parent 635e803 commit 61cb602
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 5 deletions.
19 changes: 14 additions & 5 deletions core/libs/service/src/approval/safe-to-apply-mode.use-case.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,20 @@ export class SafeToApplyModeUseCase {
...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)
const isTypeSafeToApply =
configuration.global.safeToApplyItems
?.map(it => it.providerType)
.includes(pair[0].providerType) ?? false

return (
// If type is in the safe-list, there is no need to check the actions
!isTypeSafeToApply &&
// 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.
!areAllItemsIncluded(
safeActionsForResource,
mapDiffTypeToActions(pair[0].diffType)
)
)
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,4 +283,52 @@ describe("SafeToApplyModeUseCase", () => {
expect(result).toBe(true)
})
})

it("should return false if the plan contains resources of type that are safe to apply", async () => {
// Given
const resourceType: string = "aws_s3_bucket"
const resourceName: string = "my_bucket"
const resourceAddress: string = "aws_s3_bucket.my_bucket"

const terraformEntity: TerraformEntity = {
entityInfo: {
internalType: "plain_resource",
providerType: resourceType,
userProvidedName: resourceName
},
decorator: {
type: "no_decorator"
}
}

const diffFromPlan: TerraformDiff = {
fullyQualifiedAddress: resourceAddress,
userProvidedName: resourceName,
providerType: resourceType,
diffType: "create"
}

const diffsEntityPairs: [TerraformDiff, TerraformEntity][] = [
[diffFromPlan, terraformEntity]
]

const configuration = mockConfiguration({
global: {
safeToApplyItems: [
{
providerType: resourceType
}
]
}
})

// When
const result = await useCase.isApprovalRequired({
configuration,
diffsEntityPairs
})

// Expect
expect(result).toBe(false)
})
})

0 comments on commit 61cb602

Please sign in to comment.