DSCClassResources/QlikLicense.psm1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
enum Ensure {
    Absent
    Present
}

enum RefreshPolicy {
    Always
    Never
    ExpiredOrInvalid
}

[DscResource()]
class QlikLicense{

    [DscProperty(Key)]
    [ValidateLength(16, 16)]
    [string]
    $Serial

    [DscProperty()]
    [ValidateLength(5, 5)]
    [string]
    $Control

    [DscProperty(Mandatory)]
    [ValidateNotNullOrEmpty()]
    [string]
    $Name

    [DscProperty(Mandatory)]
    [ValidateNotNullOrEmpty()]
    [string]
    $Organization

    [DscProperty()]
    [string]
    $Lef

    [DscProperty()]
    [string]
    $Key

    [DscProperty()]
    [ValidateNotNullOrEmpty()]
    [RefreshPolicy]
    $RefreshLef = 'ExpiredOrInvalid'

    [DscProperty(Mandatory)]
    [Ensure]
    $Ensure

    [void] Set() {
        $SetLicenseParams = @{
            Name = $this.Name
            Organization = $this.Organization
        }

        if ($this.Ensure -eq [Ensure]::Present) {
            if ($this.Control) {
                $SetLicenseParams.Control = $this.Control
                $SetLicenseParams.Serial = $this.Serial
            }

            if ($this.Lef) {
                $SetLicenseParams.Lef = $this.Lef
            }

            if ($this.Key) {
                $SetLicenseParams.Key = $this.Key
            }

            Set-QlikLicense @SetLicenseParams
        }
        else {
            $license = Get-QlikLicense
            Write-Verbose -Message "Deleting license $($license.Serial)"
            Invoke-QlikDelete "/qrs/license/$($license.id)"
        }
    }

    [bool] Test() {
        $License = Get-QlikLicense
        Write-Verbose "Serial: $($License.Serial)"
        if($this.Ensure -eq [Ensure]::Present) {
            if ((! $License) -or $License -eq 'null') {
                Write-Verbose 'No license found but should be Present'
                return $false
            }

            if ($License.Serial.Length -eq 0 -or $License.Serial.Replace(' ', '') -ne $this.Serial.Replace(' ', '')) {
                Write-Verbose "Serial number does not match. Desired: $($this.Serial), Actual: $($License.serial)"
                return $false
            }

            if ($License.Name -ne $this.Name) {
                Write-Verbose "User name does not match. Desired: $($this.Name), Actual: $($License.name)"
                return $false
            }

            if ($License.Organization -ne $this.Organization) {
                Write-Verbose "Organization does not match. Desired: $($this.Organization), Actual: $($License.Organization)"
                return $false
            }

            if ($this.Key) {
                if ($License.Key -ne $this.Key) {
                    Write-Verbose "Signed license key does not match. Desired: $($this.Key), Actual: $($License.key)"
                    return $false
                }
            }
            elseif (! $this.Control) {
                throw [System.Management.Automation.ValidationMetadataException] 'One of Key or Control must be provided.'
            }

            if ($this.Lef) {
                if ($this.Lef -ne $License.lef) {
                    Write-Verbose "LEF does not match. Desired: $($this.Lef), Actual: $($License.lef)"
                    return $false
                }
            }
            elseif ($this.RefreshLef -eq [RefreshPolicy]::Always -or 
                ($this.RefreshLef -eq [RefreshPolicy]::ExpiredOrInvalid -and ($License.isExpired -or $License.isInvalid))) {

                Write-Verbose 'Checking for updated LEF'
                $query = 'serial={0}&control={1}&user={2}&org={3}' -f
                    $this.Serial,
                    $this.Control,
                    $this.Name,
                    $this.Organization
                $LatestLef = Invoke-QlikGet "/qrs/license/download?$query"

                if ($LatestLef -ne $License.lef) {
                    Write-Verbose "Newer LEF is available. Current: $($License.Lef), Latest: $LatestLef"
                    return $false
                }
            }
        }
        elseif ($License.id) {
            Write-Verbose "License is Present but should be Absent"
            return $false
        }

        Write-Verbose 'License is in desired state'
        return $true
    }

    [QlikLicense] Get() {
        $license = Get-QlikLicense
        if ($license.serial) {
            $this.Serial = $license.serial.Replace(' ', '')
            $this.Ensure = [Ensure]::Present
        }
        else {
            $this.Ensure = [Ensure]::Absent
        }
        $this.Name = $license.name
        $this.Organization = $license.organization
        $this.Lef = $license.lef
        $this.Key = $license.key

        return $this
    }
}