DSCResources/Cartwheel_xAzureBlobItem/Cartwheel_xAzureBlobItem.psm1

#
# Cartwheel_xAzureBlobItem.psm1
# xAzureBlobItem is a simplified boilerplate template for new modules
#



function Test-TargetResource 
{
    param 
    (    
    
        [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,
    
        [Parameter(Mandatory)]   
        [ValidateSet("Present", "Absent")]
        [string]$Ensure = "Present"
     
    )


 
$testTargetResourceResult = Get-Item -path $ItemDestinationPath  -ErrorVariable ItemErr -ErrorAction SilentlyContinue ;
$exists = $false
if ($testTargetResourceResult){$exists = $true}
$test = Test-Target $Ensure $exists "" $ItemDestinationPath #never allow erro on this since Get-Item will always err if item not found
return $test.ReturnValue
}






# The Set-TargetResource function is used to create, delete or configure on the target machine.
function Set-TargetResource 
{
    [CmdletBinding(SupportsShouldProcess=$true)]
    param 
    (  
        [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,
    
        [Parameter(Mandatory)]   
        [ValidateSet("Present", "Absent")]
        [string]$Ensure = "Present"

    )

  
Import-Module Cartwheel.IO -DisableNameChecking

 # Dependency
if (-Not(Get-Module -ListAvailable AzureRM)) {Install-Module -Name AzureRM -Force -SkipPublisherCheck}

$modname = $MyInvocation.MyCommand.ModuleName
$testTargetResourceResult = Get-Item -path $ItemDestinationPath  -ErrorVariable ItemErr -ErrorAction SilentlyContinue ;
$exists = $false
if ($testTargetResourceResult){$exists = $true}
$test = Test-Target $Ensure $exists "" $ItemDestinationPath


    if ($test.Command -eq "Add") {
        Set-AzureShareItem  -Ensure $ensure -ItemSourcePath $ItemSourcePath -ItemDestinationPath $ItemDestinationPath -StorageAccountName $StorageAccountName -StorageAccountKey $StorageAccountKey -ShareName $ShareName 
        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.ReturnValue    
    
            
}













function Get-TargetResource 
{
    param 
    (    
    
        [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,
    
        [Parameter(Mandatory)]   
        [ValidateSet("Present", "Absent")]
        [string]$Ensure = "Present"
     
    )


    Write-Verbose  "$modname : Running Test for ($ItemDestinationPath)."
    Test-TargetResource $ItemSourcePath $ItemDestinationPath $StorageAccountName $StorageAccountKey $ShareName $Ensure



}


Export-ModuleMember -Function *-TargetResource