DSCClassResources/StorageSpacesDirect/StorageSpacesDirect.psm1

[DscResource()]
class StorageSpacesDirect {
    [DscProperty(Key)]
    [string] $ClusterName

    [DscProperty(Mandatory)]
    [ValidateSet("Enabled","Disabled")]
    [string] $State

    [DscProperty(Mandatory=$false)]
    [String]$NodeName = $env:COMPUTERNAME

    [DscProperty(NotConfigurable)]
    [String]$StoragePoolName

    [DscProperty(Mandatory=$false)]
    [bool]$SkipChecks

    [DscProperty(NotConfigurable)]
    [string]$CacheState
    
    # Gets the resource's current state.
    [StorageSpacesDirect] Get() {
        $clusterS2D = Get-ClusterS2D -Node $this.NodeName
        $this.State = $clusterS2D.State
        if ($clusterS2D.State -eq "Enabled") {
            Write-Verbose "Cluster Storage Spaces Direct are enabled on: $($this.ClusterName)"
            $this.CacheState = $clusterS2D.CacheState
            $this.StoragePoolName = (Get-StoragePool | Where-Object {$_.IsClustered -and $_.IsPrimordial -eq $false}).FriendlyName
        }
        else {
            Write-Verbose "Cluster Storage Spaces Direct are disabled on: $($this.ClusterName)"
            $this.CacheState = "NotApplicable"
        }
        return $this
    }
    
    # Sets the desired state of the resource.
    [void] Set() {
        switch ($this.State) {
            "Enabled" {
                Write-Verbose "Cluster Storage Spaces Direct will be enabled on : $($this.ClusterName)"
                $clusterS2DParam = @{
                    SkipEligibilityChecks = $this.SkipChecks
                    confirm = $false
                }
                Enable-ClusterS2D @clusterS2DParam
            }
            "Disabled" {
                Write-Verbose "Cluster Storage Spaces Direct will be disabled on : $($this.ClusterName)"
                Disable-ClusterS2D -Confirm:$false
            }
        }
    }
    
    # Tests if the resource is in the desired state.
    [bool] Test() {
        $clusterS2D = Get-ClusterS2D -Node $this.NodeName
        if ($clusterS2D.State -eq $this.State) {
            return $true
        }
        else {
            return $false
        }
    }
}