diff --git a/samples/ia4ct.ps1 b/samples/ia4ct.ps1 new file mode 100755 index 0000000..0693d00 --- /dev/null +++ b/samples/ia4ct.ps1 @@ -0,0 +1,74 @@ +#! /usr/local/bin/pwsh +[CmdletBinding()] +param ( + # This is the s3 path to the template file we will reference in the manifest file + [Parameter()] + [string] + $templatePath = "templates/linux-bastion.template", + [Parameter()] + [string] + $manifestPath = "temp/ps_manifest.yaml", + [Parameter()] + [string] + $verboseManifest = $true +) +import-module powershell-yaml + +# Read Yaml file +$cfn = Get-Content $templatePath | ConvertFrom-Yaml + +class cfnParameter { + [String]$pname + [String]$default + [String]$type + [String]$description + [String]$allowedValues + [String]$allowedPattern + [String]$constraintDescription +} +$parms = @() +foreach ($key in $cfn.Parameters.keys) { + $ctParm = New-Object cfnParameter + $ctParm.pname = $key + $ctParm.default = $cfn.Parameters[$key].default + $ctParm.type = $cfn.Parameters[$key].type + $ctParm.description = $cfn.Parameters[$key].description + $ctParm.allowedValues = $cfn.Parameters[$key].allowedValues + $ctParm.allowedPattern = $cfn.Parameters[$key].allowedPattern + $ctParm.ConstraintDescription = $cfn.Parameters[$key].ConstraintDescription + $parms += $ctParm +} +#$parms +$manifestPath= '../temp/ps_manifest.yaml' +Set-Content -Path $manifestPath -Value '---' +#Add-Content -Path temp/manifest.yaml -Value '---' +Add-Content -Path $manifestPath -Value 'region: [The region where Customization for Control Tower is deployed]' +Add-Content -Path $manifestPath -Value 'version: 2021-03-15' +Add-Content -Path $manifestPath -Value 'resources:' +Add-Content -Path $manifestPath -Value ' - name: [The name for this deployment]' +Add-Content -Path $manifestPath -Value " description: $($cfn.description)" +Add-Content -Path $manifestPath -Value ' resource_file: [The s3 path where the template is located.]' +Add-Content -Path $manifestPath -Value ' parameters:' +$parms = $parms | Sort-Object -Property pname +foreach ($parm in $parms) { + if ($verboseManifest -eq $true) { + if ($parm.description) { Add-Content -Path $manifestPath -Value " # Description: $($parm.description)" } + if ($parm.allowedPattern) { Add-Content -Path $manifestPath -Value " # AllowedPattern: $($parm.allowedPattern)" } + if ($parm.allowedValues) { Add-Content -Path $manifestPath -Value " # AllowedValues: $($parm.allowedValues)" } + if ($parm.constraintDescription) { Add-Content -Path $manifestPath -Value " # ConstraintDescription: $($parm.constraintDescription)" } + if ($parm.MaxLength) { Add-Content -Path $manifestPath -Value " # MaxLength: $($parm.MaxLength)" } + if ($parm.MaxValue) { Add-Content -Path $manifestPath -Value " # MaxValue: $($parm.MaxValue)" } + if ($parm.MinLength) { Add-Content -Path $manifestPath -Value " # MinLength: $($parm.MinLength)" } + if ($parm.MinValue) { Add-Content -Path $manifestPath -Value " # MinValue: $($parm.MinValue)" } + if ($parm.NoEcho) { Add-Content -Path $manifestPath -Value " # NoEcho: $($parm.NoEcho)" } + if ($parm.type) { Add-Content -Path $manifestPath -Value " # Type: $($parm.type)" } + } + Add-Content -Path $manifestPath -Value " - parameter_key: $($parm.pname)" + Add-Content -Path $manifestPath -Value " parameter_value: $($parm.default)" +} +Add-Content -Path $manifestPath -Value ' deploy_method: stack_set' +Add-Content -Path $manifestPath -Value ' deployment_targets: stack_set' +Add-Content -Path $manifestPath -Value ' organizational_units:' +Add-Content -Path $manifestPath -Value ' - [Enter your Organizational Unit]' +Add-Content -Path $manifestPath -Value ' regions:' +Add-Content -Path $manifestPath -Value ' - [The region where you wish to deploy this workload]' diff --git a/samples/ia4ct.py b/samples/ia4ct.py new file mode 100644 index 0000000..6665def --- /dev/null +++ b/samples/ia4ct.py @@ -0,0 +1,88 @@ +#!/usr/bin/env python +import sys +import argparse +from typing import DefaultDict +import yaml +from yaml.error import YAMLError + +parser = argparse.ArgumentParser() + +parser.add_argument("path", nargs='?', default="templates/linux-bastion.template", help="Provide a path to the template file", type=str) +parser.add_argument("outPath", nargs='?', default="temp/py_manifest.yaml", help="Provide a destination path for the output file", type=str) +parser.add_argument("-v", "--verboseManifest", help="Include help comments in the manifest", action="store_true") +args = parser.parse_args() +# print(args.path) +# print(args.verboseManifest) + +class CtParameter(yaml.YAMLObject): + yaml_tag = u'!Parameters' + def __init__(self, name): + self.name = name + pass + def method(self, arg): + return True + +try: + yaml.add_multi_constructor('!', lambda loader, suffix, node: None) + # Read Yaml file + cfn = yaml.full_load(open(args.path, 'r')) + # Container for each parameter object + parameters = [] + # Get data for each parameter + for n in cfn['Parameters']: + ctParm = CtParameter(n) + #ctParm.name = n + for i in cfn['Parameters'][n]: + setattr(ctParm, i, cfn['Parameters'][n][i]) + # Append the parameter data to the list + parameters.append(ctParm) + + # Create the manifest file and write the document + m = open(args.outPath, "w+") + m.write("---\r\n") + m.write("region: [The region where Customization for Control Tower is deployed]\r\n") + m.write("version: 2021-03-15\r\n") + m.write("resources:\r\n") + m.write(" - name: [The name for this deployment]\r\n") + m.write(" description: " + cfn['Description'] + "\r\n") + m.write(" resource_file: [The s3 path where the template is located.]\r\n") + m.write(" parameters:\r\n") + + parameters.sort(key=lambda x: x.name) + for p in parameters: + if args.verboseManifest: + if hasattr(p,"Description"): + m.write(" # Description: " + p.Description + "\r\n") + if hasattr(p,"AllowedPattern"): + m.write(" # AllowedPattern: " + p.AllowedPattern + "\r\n") + if hasattr(p,"AllowedValues"): + m.write(" # AllowedValues: " + ' '.join(p.AllowedValues) + "\r\n") + if hasattr(p,"ConstraintDescription"): + m.write(" # ConstraintDescription: " + p.ConstraintDescription + "\r\n") + if hasattr(p,"MaxLength"): + m.write(" # MaxLength: " + p.MaxLength + "\r\n") + if hasattr(p,"MaxValue"): + m.write(" # MaxValue: " + p.MaxValue + "\r\n") + if hasattr(p,"MinLength"): + m.write(" # MinLength: " + p.MinLength + "\r\n") + if hasattr(p,"MinValue"): + m.write(" # MinValue: " + p.MinValue + "\r\n") + if hasattr(p,"NoEcho"): + m.write(" # NoEcho: " + p.NoEcho + "\r\n") + if hasattr(p,"Type"): + m.write(" # Type: " + p.Type + "\r\n") + m.write(" - parameter_key: " + p.name + "\r\n") + if hasattr(p,"Default"): + m.write(" parameter_value: " + p.Default + "\r\n") + else: + m.write(" parameter_value: \r\n") + m.write(" deploy_method: stack_set\r\n") + m.write(" deployment_targets: stack_set\r\n") + m.write(" organizational_units:\r\n") + m.write(" - [Enter your Organizational Unit]\r\n") + m.write(" regions:\r\n") + m.write(" - [The region where you wish to deploy this workload]\r\n") + + +except YAMLError as exc: + print(exc)