winget-cli

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

xSimpleTestResource.psm1 (5634B)


      1 # Simple module with resources.
      2 
      3 enum Ensure
      4 {
      5     Absent
      6     Present
      7 }
      8 
      9 # This resource just checks if a file is there or not with and if its with the specified content.
     10 [DscResource()]
     11 class SimpleFileResource
     12 {
     13     [DscProperty(Key)]
     14     [string] $Path
     15 
     16     [DscProperty()]
     17     [Ensure] $Ensure = [Ensure]::Present
     18 
     19     [DscProperty()]
     20     [string] $Content = $null
     21 
     22     [SimpleFileResource] Get()
     23     {
     24         if ([string]::IsNullOrEmpty($this.Path))
     25         {
     26             throw
     27         }
     28 
     29         $fileContent = $null
     30         if (Test-Path -Path $this.Path -PathType Leaf)
     31         {
     32             $fileContent = Get-Content $this.Path -Raw
     33         }
     34 
     35         $result = @{
     36             Path = $this.Path
     37             Content = $fileContent
     38         }
     39 
     40         return $result
     41     }
     42 
     43     [bool] Test()
     44     {
     45         $get = $this.Get()
     46 
     47         if (Test-Path -Path $this.Path -PathType Leaf)
     48         {
     49             if ($this.Ensure -eq [Ensure]::Present)
     50             {
     51                 return $this.Content -eq $get.Content
     52             }
     53         }
     54         elseif ($this.Ensure -eq [Ensure]::Absent)
     55         {
     56             return $true
     57         }
     58 
     59         return $false
     60     }
     61 
     62     [void] Set()
     63     {
     64         if (-not $this.Test())
     65         {
     66             if (Test-Path -Path $this.Path -PathType Leaf)
     67             {
     68                 if ($this.Ensure -eq [Ensure]::Present)
     69                 {
     70                     Set-Content $this.Path $this.Content -NoNewline
     71                 }
     72                 else
     73                 {
     74                     Remove-Item $this.Path
     75                 }
     76             }
     77             else
     78             {
     79                 if ($this.Ensure -eq [Ensure]::Present)
     80                 {
     81                     Set-Content $this.Path $this.Content -NoNewline
     82                 }
     83             }
     84         }
     85     }
     86 }
     87 
     88 [DscResource()]
     89 class SimpleTestResource
     90 {
     91     [DscProperty(Key)]
     92     [string] $key
     93 
     94     [DscProperty(Mandatory)]
     95     [string] $secretCode
     96 
     97     [SimpleTestResource] Get()
     98     {
     99         $result = @{
    100             key = "SimpleTestResourceKey"
    101         }
    102         return $result
    103     }
    104 
    105     [bool] Test()
    106     {
    107         return $this.secretCode -eq "4815162342"
    108     }
    109 
    110     [void] Set()
    111     {
    112         if (-not $this.Test())
    113         {
    114             $global:DSCMachineStatus = 1
    115         }
    116     }
    117 }
    118 
    119 [DscResource()]
    120 class SimpleTestResourceThrows
    121 {
    122     [DscProperty(Key)]
    123     [string] $key
    124 
    125     [SimpleTestResourceThrows] Get()
    126     {
    127         $result = @{
    128             key = "SimpleTestResourceThrowsKey"
    129         }
    130         throw "throws in Get"
    131         return $result
    132     }
    133 
    134     [bool] Test()
    135     {
    136         throw "throws in Test"
    137         return $false
    138     }
    139 
    140     [void] Set()
    141     {
    142         throw "throws in Set"
    143     }
    144 }
    145 
    146 [DscResource()]
    147 class SimpleTestResourceError
    148 {
    149     [DscProperty(Key)]
    150     [string] $key
    151 
    152     [SimpleTestResourceError] Get()
    153     {
    154         $result = @{
    155             key = "SimpleTestResourceErrorKey"
    156         }
    157         Write-Error "Error in Get"
    158         return $result
    159     }
    160 
    161     [bool] Test()
    162     {
    163         Write-Error "Error in Test"
    164         return $true
    165     }
    166 
    167     [void] Set()
    168     {
    169         Write-Error "Error in Set"
    170     }
    171 }
    172 
    173 [DscResource()]
    174 class SimpleTestResourceTypes
    175 {
    176     [DscProperty(Key)]
    177     [string] $key
    178 
    179     [DscProperty()]
    180     [boolean] $boolProperty
    181 
    182     [DscProperty()]
    183     [int] $intProperty;
    184 
    185     [DscProperty()]
    186     [double] $doubleProperty;
    187 
    188     [DscProperty()]
    189     [char] $charProperty;
    190 
    191     [DscProperty()]
    192     [Hashtable] $hashtableProperty;
    193 
    194     [SimpleTestResourceTypes] Get()
    195     {
    196         $result = @{
    197             key = "SimpleTestResourceTypesKey"
    198             boolProperty = $false
    199             intProperty = 0
    200             doubleProperty = 0.0
    201             charProperty = 'z'
    202             hashtableProperty = @{}
    203         }
    204         return $result
    205     }
    206 
    207     [bool] Test()
    208     {
    209         # Because we can't get the error stream from a class based resource, I throw so is easier to know if
    210         # there's something wrong.
    211         if ($this.boolProperty -ne $true)
    212         {
    213             throw "Failed boolProperty"
    214         }
    215 
    216         if ($this.intProperty -ne 3)
    217         {
    218             throw "Failed intProperty. Got $($this.intProperty)"
    219         }
    220 
    221         if ($this.doubleProperty -ne -9.876)
    222         {
    223             throw "Failed doubleProperty Got $($this.doubleProperty)"
    224         }
    225 
    226         if ($this.charProperty -ne 'f')
    227         {
    228             throw "Failed charProperty Got $($this.charProperty)"
    229         }
    230 
    231         if ($this.hashtableProperty.ContainsKey("secretStringKey"))
    232         {
    233             if ($this.hashtableProperty["secretStringKey"] -ne "secretCode")
    234             {
    235                 throw "Failed comparing value of `$hashtableProperty.secretStringKey Got $($this.hashtableProperty["secretStringKey"])"
    236             }
    237         }
    238         else
    239         {
    240             throw "Failed finding secretStringKey in hashtableProperty"
    241         }
    242 
    243         if ($this.hashtableProperty.ContainsKey("secretIntKey"))
    244         {
    245             if ($this.hashtableProperty["secretIntKey"] -ne 123456)
    246             {
    247                 throw "Failed comparing value of `$hashtableProperty.secretIntKey Got $($this.hashtableProperty["secretIntKey"])"
    248             }
    249         }
    250         else
    251         {
    252             throw "Failed finding secretIntKey in hashtableProperty"
    253         }
    254 
    255         return $true
    256     }
    257 
    258     [void] Set()
    259     {
    260         # no-op
    261     }
    262 }