Cartwheel.Item.psm1


function Set-AzureItemLocal {
    [OutputType([System.Collections.Hashtable])]
    [CmdletBinding()]
    param(
        [Parameter(Mandatory)]
        [ValidateSet("Present", "Absent")]
        [string]$Ensure,
       
        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [string]$ItemSourcePath,

        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [string]$ItemDestinationPath,

        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [string]$StorageAccountName,

        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [string]$StorageAccountKey,

        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [string]$ShareName
        )

        $modname = $MyInvocation.MyCommand.ModuleName
        $test = Test-TargetItem -ItemPath $ItemDestinationPath -Ensure $Ensure


    if ($test.Command -eq "Add") {

                Set-Directory -Ensure $Ensure -DirectoryPath (Split-Path $ItemDestinationPath) #ensure out directory exists

                $ctx = New-AzureStorageContext ` -StorageAccountName $StorageAccountName  `
                -StorageAccountKey $StorageAccountKey

                Get-AzureStorageFileContent `
                -Context $ctx `
                -ShareName $ShareName `
                -Path $ItemSourcePath `
                -Destination $ItemDestinationPath
                Write-Verbose  "$modname : Item ($ItemDestinationPath) in wrong state... added."
    }
    if ($test.Command -eq "Remove") {
        Remove-Item -Recurse -Force $ItemDestinationPath
        Write-Verbose  "$modname : Item ($ItemDestinationPath) in wrong state... removed."
    }
    if ($test.Command -eq "Skip") {
        Write-Verbose  "$modname : Item ($ItemDestinationPath) in correct state."
    }
    if ($test.Command -eq "Error") {
        Write-Verbose  "$modname : Item ($ItemDestinationPath) had errors."
    }

return $test

    }

function Get-DynamicVariableFile([string] $JsonFile)
{
    # Note a potential for a race condition with the Get-Variable since its global

    $json = Get-Content $JsonFile | Out-String  | ConvertFrom-Json
    $StringFormJson = $json | ConvertTo-Json -Depth 20 


    $SubsInString = ($StringFormJson | ForEach-Object { [Regex]::Matches($_, '@\((.*?)\)') } | ForEach-Object { $_.Value.Substring(2,$_.Value.length-3) }) | Get-Unique

    foreach($var in $SubsInString)
        {
            $SearchString = "@($var)"
            $ReplaceString = (Get-Variable -Name $var -ValueOnly -Scope Global)

            $StringFormJson = $StringFormJson.Replace($SearchString,$ReplaceString)
        }

    return ($StringFormJson | Out-String  | ConvertFrom-Json)


}

<#
function Set-AzureItemRemote {
    [OutputType([System.Collections.Hashtable])]
    [CmdletBinding()]
    param(
        [Parameter(Mandatory)]
        [ValidateSet("Present", "Absent")]
        [string]$Ensure,
        
        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [string]$ItemSourcePath,
 
        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [string]$ItemDestinationPath,
 
        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [string]$StorageAccountName,
 
        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [string]$StorageAccountKey,
 
        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [string]$ShareName
        )
 
        $modname = $MyInvocation.MyCommand.ModuleName
 
 
        $ctx = New-AzureStorageContext ` -StorageAccountName $StorageAccountName `
        -StorageAccountKey $StorageAccountKey
 
        $test = Get-AzureStorageFile `
                -Context $ctx `
                -ShareName $ShareName `
                -Path $ItemSourcePath
 
    if ($null -eq $test.Name ) {
 
                Set-AzureStorageFileContent `
                -Context $ctx `
                -ShareName $ShareName `
                -Path $ItemDestinationPath `
                -Source $ItemSourcePath
                Write-Verbose "$modname : Item ($ItemDestinationPath) in wrong state... added."
    }
    if ($test.Command -eq "Remove") {
        Write-Verbose "NOT IMPLEMENTED $modname : Item ($ItemDestinationPath) in wrong state... removed."
    }
    if ($test.Command -eq "Skip") {
        Write-Verbose "$modname : Item ($ItemDestinationPath) in correct state."
    }
    if ($test.Command -eq "Error") {
        Write-Verbose "$modname : Item ($ItemDestinationPath) had errors."
    }
 
return $test
 
    }
 
#>