PowerShell/PSArm

Decomplile ARM template/bicep template to PSArm template

Opened this issue · 6 comments

One area that I think would be a great area of investment for all users would be to generate a PSArm definition from either an ARM template or longer term a bicep template

This would allow for users across all three template types to work together without enforcing them to learn 1 specific type.

Take a look at ConvertTo-PSArm. It's not perfect, but it goes a long way. See https://github.com/PowerShell/PSArm#conversion-cmdlets

PSArm's underlying object model means we could also plausibly transform to Bicep, although I'm not planning on writing that myself in the immediate

The way to transform from Bicep is probably to compile the Bicep template to ARM and then convert from that to PSArm. Otherwise we'd need to embed a Bicep parser in PSArm, which would be going pretty far

It was not too complicated to achieve. This is what I used to decompile a bicep file to PSArm object.

<#
    Decompiling a bicep file to PSArm

    Inspired on issue posted on PSArm Github repo.
    https://github.com/PowerShell/PSArm/issues/154

#>

# Decompile ARM template to Bicep file
bicep decompile C:\Users\stefstr\Documents\Github\PSArm\examples\simple-test\template.json 

# Convert Bicep file to ARM Template JSON string
[string]$bicep2armjson = (bicep build C:\Users\stefstr\Documents\Github\PSArm\examples\simple-test\template.bicep --stdout)

# Convert Bicep -> ARM Template to PSARM
ConvertFrom-ArmTemplate -Input $bicep2armjson | ConvertTo-PSArm -PassThru

# Store PSArm output in file.
ConvertFrom-ArmTemplate -Input $bicep2armjson | ConvertTo-PSArm -OutFile C:\Users\stefstr\Documents\Github\PSArm\examples\simple-test\template.psarm.ps1 -Force

# Publish ARM Template file. Does not work yet.
$parameters = @{
    'StorageAccountName' = 'demo'
    'Location' = 'WestEurope'
}
Publish-PSArmTemplate -Path C:\Users\stefstr\Documents\Github\PSArm\examples\simple-test\template.psarm.ps1 -Parameters $Parameters -OutFile template-final.json

The way to transform from Bicep is probably to compile the Bicep template to ARM and then convert from that to PSArm.

Can we update the readme to suggest this is the preferred approach as I personally don't see bicep -> psarm as a priority need due to complexity vs doing bicep -> arm - PSArm

Take a look at ConvertTo-PSArm. It's not perfect, but it goes a long way. See https://github.com/PowerShell/PSArm#conversion-cmdlets

I may have also completely missed this in my reading of the readme, my bad