Created
March 4, 2020 21:18
-
-
Save yohanb/d9f4b5967290b3da913c6eb972b78fb9 to your computer and use it in GitHub Desktop.
Pulumi policy validation to advise usage of certain compute families.
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
import { ResourceValidationPolicy, ResourceValidationArgs } from "@pulumi/policy"; | |
const advisedComputeFamilies = [ | |
"Standard_DS?\d{1,2}_v2", | |
"Standard_DS?\d{1,2}_v3"]; | |
const escapeRegex = (expression: string): string => expression.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'); | |
const isAzureVirtualMachineResource = (args: ResourceValidationArgs): boolean => args.type.startsWith("azure:compute/virtualMachine"); | |
const isInAdvisedComputeFamily = (args: ResourceValidationArgs): boolean => { | |
advisedComputeFamilies.forEach(fam => { | |
const expression = escapeRegex(fam); | |
const matches = new RegExp(expression).test(args.props.vmSize); | |
if (!matches) { | |
return false; | |
} | |
}) | |
return true; | |
}; | |
const computeFamilyPolicy: ResourceValidationPolicy = { | |
name: "advisory-compute-families", | |
description: "Virtual machine should be in advised families.", | |
enforcementLevel: "advisory", | |
validateResource: (args, reportViolation) => { | |
if (isAzureVirtualMachineResource(args) && !isInAdvisedComputeFamily(args) | |
) { | |
reportViolation( | |
`Virtual machine is advised to be in the following families: '${advisedComputeFamilies.join(',')}'.` | |
); | |
} | |
} | |
}; | |
export default computeFamilyPolicy; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment