resources/rule.ps1

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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
function Get-QlikAuditRule {
    [CmdletBinding()]
    param (
        [parameter(Mandatory = $true)]
        [string]$resourceType,

        [string]$resourceFilter,
        [string]$userFilter,
        [string[]]$environmentAttributes,
        [int]$userSkip,
        [int]$userTake,
        [int]$resourceSkip,
        [int]$resourceTake,
        [switch]$includeNonGrantingRules
    )

    process {
        $audit = @{
            resourceType = $resourceType
            resourceFilter = $resourceFilter
            userFilter = $userFilter
            environmentAttributes = $environmentAttributes
            userSkip = $userSkip
            userTake = $userTake
            resourceSkip = $resourceSkip
            resourceTake = $resourceTake
            includeNonGrantingRules = $includeNonGrantingRules.IsPresent
        }

        $path = '/qrs/systemrule/security/audit'
        $json = $audit | ConvertTo-Json -Compress -Depth 10

        return Invoke-QlikPost $path $json
    }
}

function Get-QlikRule {
    [CmdletBinding()]
    param (
        [parameter(Position = 0)]
        [string]$id,
        [string]$filter,
        [switch]$full,
        [switch]$raw
    )

    PROCESS {
        $path = "/qrs/systemrule"
        If ( $id ) { $path += "/$id" }
        If ( $full ) { $path += "/full" }
        If ( $raw ) { $rawOutput = $true }
        return Invoke-QlikGet $path $filter
    }
}

function New-QlikRule {
    [CmdletBinding()]
    param (
        [parameter(ValueFromPipeline = $true)]
        [PSObject]$object,

        [string]$name,

        [ValidateSet("License", "Security", "Sync")]
        [string]$category,

        [string]$rule,

        [alias("filter")]
        [string]$resourceFilter,

        [ValidateSet("hub", "qmc", "both", "BothQlikSenseAndQMC", "QlikSenseOnly", "QMCOnly")]
        [alias("context")]
        [string]$rulecontext = "both",

        [int64]$actions,
        [string]$comment,
        [switch]$disabled,
        [string[]]$tags
    )

    PROCESS {
        If ( $object ) {
            $json = $object | ConvertTo-Json -Compress -Depth 10
        }
        else {
            $systemrule = @{
                type = "Custom";
                rule = $rule;
                name = $name;
                resourceFilter = $resourceFilter;
                actions = $actions;
                comment = $comment;
                disabled = $disabled.IsPresent;
                ruleContext = $context;
                schemaPath = "SystemRule"
            }

            if ($PSBoundParameters.ContainsKey("tags")) { $systemrule.tags = @(GetTags $tags) }
            # category is case-sensitive so convert to Title Case
            $systemrule.category = (Get-Culture).TextInfo.ToTitleCase($category.ToLower())

            $systemrule.ruleContext = switch ($rulecontext) {
                both { 0 }
                hub { 1 }
                qmc { 2 }
                default { $rulecontext }
            }

            $json = $systemrule | ConvertTo-Json -Compress -Depth 10
        }

        return Invoke-QlikPost "/qrs/systemrule" $json
    }
}

function Remove-QlikRule {
    [CmdletBinding()]
    param (
        [parameter(Position = 0, ValueFromPipelinebyPropertyName = $true)]
        [string]$id
    )

    PROCESS {
        return Invoke-QlikDelete "/qrs/systemrule/$id"
    }
}

function Update-QlikRule {
    [CmdletBinding()]
    param (
        [parameter(Mandatory = $true, ValueFromPipeline = $True, ValueFromPipelinebyPropertyName = $True, Position = 0)]
        [string]$id,
        [string]$name,

        [ValidateSet("License", "Security", "Sync")]
        [string]$category,

        [string]$rule,

        [alias("filter")]
        [string]$resourceFilter,

        [ValidateSet("hub", "qmc", "both")]
        [alias("context")]
        [string]$rulecontext,

        [int64]$actions,
        [string]$comment,
        [switch]$disabled,

        [string[]]$tags
    )

    PROCESS {
        switch ($rulecontext) {
            both { $context = 0 }
            hub { $context = 1 }
            qmc { $context = 2 }
        }

        $systemrule = Get-QlikRule $id -raw
        If ( $name ) { $systemrule.name = $name }
        If ( $rule ) { $systemrule.rule = $rule }
        If ( $resourceFilter ) { $systemrule.resourceFilter = $resourceFilter }
        If ( $category ) { $systemrule.category = $category }
        If ( $rulecontext ) { $systemrule.rulecontext = $context }
        If ( $actions ) { $systemrule.actions = $actions }
        If ( $comment ) { $systemrule.comment = $comment }
        If ( $psBoundParameters.ContainsKey("disabled") ) { $systemrule.disabled = $disabled.IsPresent }
        if ($PSBoundParameters.ContainsKey("tags")) { $systemrule.tags = @(GetTags $tags $systemrule.tags) }

        $json = $systemrule | ConvertTo-Json -Compress -Depth 10
        return Invoke-QlikPut "/qrs/systemrule/$id" $json
    }
}

function New-QlikLicenseRule {
    [CmdletBinding()]
    param
    (
        [string]$Name,
        [Parameter(Mandatory = $true)]
        [ValidateSet('Analyzer', 'Professional')]
        [string]$Type,
        [string]$Rule,
        [string]$Comment,
        [switch]$Disabled,
        [string[]]$Tags
    )
    PROCESS    {
        $systemrule = @{
            type = "Custom";
            rule = $Rule;
            name = $Name;
            resourceFilter = "";
            actions = 1;
            comment = $Comment;
            disabled = $Disabled.IsPresent;
            ruleContext = 1;
            schemaPath = "SystemRule"
            category = "License"
        }
        if ($PSBoundParameters.ContainsKey("tags")) { $systemrule.tags = @(GetTags $tags) }
        $AccessGroup = @{
            name = $Name
        }
        $QSAccessGroup = Invoke-QlikPost -path "/qrs/license/$($type)accessgroup" -body $($AccessGroup | ConvertTo-Json)
        $systemrule.resourceFilter = "License.$($Type)AccessGroup_$($QSAccessGroup.id)"
        $json = $systemrule | ConvertTo-Json -Compress -Depth 10
        return Invoke-QlikPost -path "/qrs/systemrule" -body $json
    }
}