resources/dataconnection.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
function Get-QlikDataConnection {
    [CmdletBinding()]
    param (
        [parameter(Position = 0)]
        [string]$id,
        [string]$filter,
        [switch]$full,
        [switch]$raw
    )

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

function New-QlikDataConnection {
    [CmdletBinding()]
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingPlainTextForPassword", "password", Justification = "Deprecation warning")]
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingUserNameAndPassWordParams", "", Justification = "Deprecation warning")]
    param
    (
        [Parameter(ValueFromPipelineByPropertyName = $true,
            Position = 0)]
        [string]$name,
        [Parameter(ValueFromPipelineByPropertyName = $true,
            Position = 1)]
        [string]$connectionstring,
        [Parameter(ValueFromPipelineByPropertyName = $true,
            Position = 2)]
        [string]$type,
        [Parameter(ValueFromPipelineByPropertyName = $true)]
        [string[]]$customProperties,
        [Parameter(ValueFromPipelineByPropertyName = $true)]
        [string[]]$tags,
        [Parameter(ValueFromPipelineByPropertyName = $true)]
        [string]$username,
        [Parameter(ValueFromPipelineByPropertyName = $true)]
        [string]$password,
        [Parameter(ValueFromPipelineByPropertyName = $true)]
        [string]$architecture,
        [Parameter(ValueFromPipelineByPropertyName = $true)]
        [string]$logOn,
        [Parameter(ValueFromPipelineByPropertyName = $true)]
        [PSCredential]$Credential,
        [Parameter(ValueFromPipelineByPropertyName = $true)]
        [object]$owner
    )

    PROCESS {
        if ($username -Or $password) {
            Write-Warning "Use of username/password parameters is deprecated, please use Credential instead."
        }
        if ($Credential) {
            $username = $Credential.Username
            $password = $Credential.GetNetworkCredential().Password
        }
        if ($password.Trim().Length -gt 0) {
            if ($password.IndexOf("%2") -eq 10) {
                $password = $password.Substring(12, $password.Length - 14)
            }
        }
        else {
            $Pass = ""
        }
        if ($username.Trim().Length -eq 0) {
            $username = ""
        }

        switch ($architecture) {
            ( { ($_ -eq "0") -or ($_ -eq "Undefined") } ) { $architecture = 0 }
            ( { ($_ -eq "1") -or ($_ -eq "x86") } ) { $architecture = 1 }
            ( { ($_ -eq "2") -or ($_ -eq "x64") } ) { $architecture = 2 }
            default { $architecture = 0 }
        }

        switch ($logOn) {
            ( { ($_ -eq "0") -or ($_ -eq "LOG_ON_SERVICE_USER") } ) { $logOn = 0 }
            ( { ($_ -eq "1") -or ($_ -eq "LOG_ON_CURRENT_USER") } ) { $logOn = 1 }
            default { $logOn = 0 }
        }

        $qdc = @{
            name = $name;
            connectionstring = $connectionstring;
            type = $type
            LogOn = $logOn
            Architecture = $architecture
            customProperties = @();
            tags = @();
            engineObjectId = [Guid]::NewGuid();
            username = $username;
            password = $password;
        }

        if ($PSBoundParameters.ContainsKey("customProperties")) { $qdc.customProperties = @(GetCustomProperties $customProperties) }
        if ($PSBoundParameters.ContainsKey("tags")) { $qdc.tags = @(GetTags $tags) }
        if ($PSBoundParameters.ContainsKey("owner")) { $qdc.owner = GetUser $owner }

        $json = $qdc | ConvertTo-Json -Compress -Depth 10

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

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

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

function Update-QlikDataConnection {
    [CmdletBinding()]
    param (
        [parameter(Mandatory = $true, ValueFromPipeline = $True, ValueFromPipelinebyPropertyName = $True, Position = 0)]
        [string]$id,
        [string]$ConnectionString,
        [PSCredential]$Credential,
        [object]$owner,
        [object[]]$customProperties,
        [object[]]$tags
    )

    PROCESS {
        $qdc = Get-QlikDataConnection -raw $id
        If ($PSBoundParameters.ContainsKey("ConnectionString")) {
            $qdc.connectionstring = $ConnectionString
        }
        if ( $Credential ) {
            $qdc.username = $Credential.Username
            if ($qdc.psobject.Properties.name -contains "password") {
                $qdc.password = $Credential.GetNetworkCredential().Password
            }
            else {
                $qdc | Add-Member -MemberType NoteProperty -Name "password" -Value $($Credential.GetNetworkCredential().Password)
            }
        }
        if ($PSBoundParameters.ContainsKey("customProperties")) { $qdc.customProperties = @(GetCustomProperties $customProperties $qdc.customProperties) }
        if ($PSBoundParameters.ContainsKey("tags")) { $qdc.tags = @(GetTags $tags $qdc.tags) }
        if ($PSBoundParameters.ContainsKey("owner")) { $qdc.owner = GetUser $owner }

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