IGNORE/Generate-CfnDsl.ps1


$RES_PROP_TYPES = @{
    String = 'string'
    Object = 'object'
    Array  = 'array'
    Number = 'int'
}

$cfnSchema = ConvertFrom-Json ([System.IO.File]::ReadAllText("$PSScriptRoot\CloudFormationV1.schema"))
$resDefs = @{}



foreach ($resType in ($cfnSchema.'root-schema-object'.properties.Resources.'child-schemas' | gm -MemberType NoteProperty | select -First 1)) {
    $resName = $resType.Name
    $resDef = $cfnSchema.'root-schema-object'.properties.Resources.'child-schemas'.($resName)
    if (-not ($resName -match '^AWS::(.+)::(.+)$')) {
        Write-Warning "Resource [$resName] does not match expected naming convention; SKIPPING"
        continue
    }



    $funName = "CfnRes-$($Matches[1])-$($Matches[2])"
    $resDescription  = $resDef.description
    $resDeletePolicy = $resDef.properties.DeletionPolicy.'allowed-values'
    $resMetaData     = $resDef.properties.Metadata

    $paramsHelp = ""
    $paramsDef = ""
    $resPropNames = ($resDef.properties.Properties.properties | gm -MemberType NoteProperty).Name
    foreach ($resPropName in $resPropNames) {
        $resProp = $resDef.properties.Properties.properties.$resPropName
        $resPropType    = $RES_PROP_TYPES[$resProp.type]

        if (-not $resPropType) {
            Write-Warning "Unknown property type of [$($resProp.type)] for [$resPropName]; SKIPPING"
            continue
        }

        $resPropSubtype = $resProp.'array-type'
        $resPropRefType = $resProp.'resource-ref-type'
        $resPropValid   = $resProp.'allowed-values'
        $resPropProps   = ($resProp | gm -MemberType NoteProperty).Name

        if ($resPropValid) {
            $paramsDef += " [ValidateSet('" + [string]::Join("','", $resPropValid) + "')]`n"
        }
        $paramsDef += " [$resPropType]`$$resPropName, #$resPropProps `n`n"

        $paramsHelp += @"
.PARAMETER $($resPropName)
$($resProp.description)`n
"@

    }

    if ($resDeletePolicy) {
        $paramsDef += " [ValidateSet('$([string]::Join(`"','`", $resDeletePolicy))')]`n"
        $paramsDef += " [string]`$DeletePolicy,`n"
    }
    if ($resMetaData) {
        $paramsDef += " [hashtable]`$MetaData,`n"
    }
    $paramsDef += " [array]`$DependsOn`n"

    @"
function $funName {
<#
.SYNOPSIS
$($resDescription)
$($paramsHelp)
#>
  param(
$paramsDef
  )
"@

"}"
}