cEPRSDeployAzureWebsite.psm1

enum Ensure
{
    Absent
    Present
}
[DscResource()]
Class cEPRSCreateAzureWebsite
{
    [DscProperty(Key)][String]$SubscriptionName

    [DscProperty()][String]$WebsiteName

    [DscProperty()][String][ValidateSet("East US","West US","Central US","South Central US","East US 2","North Europe","West Europe","Southeast Asia","East Asia")]
    $Location

    [DscProperty()][String]$HostName

    [DscProperty()][String]$PublishingUsername

    [DscProperty()][String][ValidateSet('Staging','Production')]$Slot

    [DscProperty()][Ensure]$Ensure

    [DscProperty()][String]$PublishSettingsFile

    [cEPRSCreateAzureWebsite] Get()
    {
        Import-AzurePublishSettingsFile "$($this.PublishSettingsFile)"

        Select-AzureSubscription -SubscriptionName "$($this.SubscriptionName)"
        Set-AzureSubscription -SubscriptionName "$($this.SubscriptionName)"

        $GetWebsite = Get-AzureWebsite -Name ($this.WebsiteName) -Slot $($this.Slot) -ErrorAction SilentlyContinue

        $this.WebsiteName = $GetWebsite.Name
        
        return $this
    }
    [bool] Test()
    {
        Import-AzurePublishSettingsFile "$($this.PublishSettingsFile)"

        Select-AzureSubscription -SubscriptionName "$($this.SubscriptionName)"
        Set-AzureSubscription -SubscriptionName "$($this.SubscriptionName)"

        $TestWebsite = Get-AzureWebsite -Name ($this.WebsiteName) -Slot $($this.Slot) -ErrorAction SilentlyContinue

        if($this.Ensure -eq [Ensure]::Present)
        {
            if($TestWebsite -eq $null)
            {
                return $false
            }
            else
            {
                return $true
            }
        }
        else
        {
            if($TestWebsite -eq $null)
            {
                return $true
            }
            else
            {
                return $false
            }
        }
    }
    Set()
    {
        Import-AzurePublishSettingsFile "$($this.PublishSettingsFile)"

        Select-AzureSubscription -SubscriptionName "$($this.SubscriptionName)"
        Set-AzureSubscription -SubscriptionName "$($this.SubscriptionName)"

        if($this.Ensure -eq [Ensure]::Present)
        {
            Write-Verbose "Creating New Azure Website $($this.WebsiteName) in slot $($this.Slot) with host name $($this.HostName)"
            New-AzureWebsite -Location $($this.Location) -Hostname $($this.HostName) -PublishingUsername $($this.PublishingUsername) -Name $($this.WebsiteName) -Slot $this.Slot
        }
        else
        {
            Write-Verbose "Ensure Set to Absent... Removing the website $($this.WebsiteName)"
            Remove-AzureWebsite -Force -Name $($this.WebsiteName) -Slot $this.Slot
        }
    }
}

[DscResource()]
Class cEPRSDeployAzureWebsite
{
    [DscProperty()][String]
    $PackageFile

    [DscProperty()][String]
    $PublishSettingsFile

    [DscProperty()][String]
    $WebsiteName

    [DscProperty(Key)][String]
    $SubscriptionName

    [DscProperty()][String]
    $Slot

    [cEPRSDeployAzureWebsite] Get()
    {
        Import-AzurePublishSettingsFile "$($this.PublishSettingsFile)"

        Select-AzureSubscription -SubscriptionName "$($this.SubscriptionName)"
        Set-AzureSubscription -SubscriptionName "$($this.SubscriptionName)"

        $GetDeployment = Get-AzureWebsiteDeployment -Name $this.WebsiteName -Slot $this.Slot -ErrorAction SilentlyContinue

        return $this
    }
    [bool] Test()
    {
        Import-AzurePublishSettingsFile "$($this.PublishSettingsFile)"

        Select-AzureSubscription -SubscriptionName "$($this.SubscriptionName)"
        Set-AzureSubscription -SubscriptionName "$($this.SubscriptionName)"

        $GetDeployment = Get-AzureWebsiteDeployment -Name $this.WebsiteName -Slot $this.Slot -ErrorAction SilentlyContinue

        return $false
    }
    Set()
    {
        Import-AzurePublishSettingsFile "$($this.PublishSettingsFile)"

        Select-AzureSubscription -SubscriptionName "$($this.SubscriptionName)"
        Set-AzureSubscription -SubscriptionName "$($this.SubscriptionName)"

        Get-AzureWebsite $this.WebsiteName;
        Publish-AzureWebsiteProject -Name wwtkwebint -Package "$($this.PackageFile)"
    }
}