Public/Terraform/New-TerraformResourceBlock.ps1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
function New-TerraformResourceBlock { [CmdletBinding()] param ( # The resource type to create [Parameter(Mandatory = $true, Position = 0, ValueFromPipelineByPropertyName = $true)] [string] $ResourceType, # The name of the resource [Parameter(Mandatory = $true, Position = 1, ValueFromPipelineByPropertyName = $true)] [string] $ResourceName, # The arguments for the resource [Parameter(Mandatory = $true, Position = 2, ValueFromPipelineByPropertyName = $true)] [pscustomobject] $ResourceArgs ) begin { } process { try { $ResourceBlock = "resource `"$ResourceType`" `"$ResourceName`" {`n" $ResourceArgs.PSObject.Properties | ForEach-Object { if ($_.Value -is [hashtable]) { $ResourceBlock += "`t$($_.Name) $(ConvertTo-TerraformObject -Object $_.Value)`n" } else { $ResourceBlock += "`t$($_.Name) = $(ConvertTo-TerraformObject -Object $_.Value)`n" } } $ResourceBlock += "}`n" } catch { throw $_.Exception.Message } } end { if ($ResourceBlock) { Return $ResourceBlock } else { Return $null } } } |