Private/Terraform/ConvertTo-TerraformObject.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
function ConvertTo-TerraformObject
{
    [CmdletBinding()]
    param
    (
        # The object to be converted
        # N.B - don't accept pipeline input here as it unfolds arrays!
        [Parameter(Mandatory = $true, Position = 0)]
        $Object
    )
    
    begin
    {
        
    }
    
    process
    {
        Write-Debug "Input determined to be $($Object.GetType().Name)"
        switch -Regex ($Object.GetType().Name)
        {
            'String'
            {
                # If the object is a string wrap it in quotes, unless it's a variable, local, data or a resource (rough regex match for this)
                if (($Object -match '^var\.') -or ($Object -match '^local\.') -or ($Object -match '^data\.') -or ($Object -match '(?:^(?:\w)*\.(?:\w|-)*\.(?:\w)*$)'))
                {
                    Write-Debug 'Converting to interpolated string'

                    $Return = $Object
                }
                else
                {
                    Write-Debug 'Converting to quoted string'
                    $Return = "`"$Object`""
                }
            }
            'Object'
            {
                try
                {
                    Write-Debug 'Converting to array'
                    # Got through the array and convert each item within it
                    $ConvertedObjects = @()
                    $ConvertedObjects += $Object | ForEach-Object { ConvertTo-TerraformObject $_ }
                    $Return = "[$($ConvertedObjects -join ', ')]"
                }
                catch
                {
                    Write-Error $_.Exception.Message
                }
            }
            'Hashtable'
            {
                Write-Debug 'Converting to hash'
                # Go through each value and convert it
                try
                {
                    $ConvertedHash = "{`n"
                    $Object.GetEnumerator() | ForEach-Object {
                        $Key = $_.Key
                        $Value = ConvertTo-TerraformObject $_.Value
                        if ($_.Value -is [Hashtable])
                        {
                            $ConvertedHash += "`t$Key $Value`n"
                        }
                        else
                        {
                            $ConvertedHash += "`t$Key = $Value`n"
                        }
                    }
                    $ConvertedHash += "`t}"
                    $Return = $ConvertedHash
                }
                catch
                {
                    Write-Error $_.Exception.Message
                }
            }
            'Boolean'
            {
                Write-Debug 'Converting to boolean'
                # Convert to terraform boolean
                $Return = $Object -eq $true ? 'true' : 'false'
            }
            'Int'
            {
                Write-Debug 'Converting to int'
                # Convert to terraform int (just return as is)
                $Return = $Object
            }
            Default
            {
                Write-Error "Unsupported type: $($Object.GetType().Name).`nValid types are string, array, hashtable."
            }
        }
    }
    
    end
    {
        if ($Return)
        {
            Return $Return
        }
        else
        {
            Return $null
        }
    }
}