Functions/CatalogItems/Rest/Set-RsRestItemDataSource.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# Copyright (c) 2017 Microsoft Corporation. All Rights Reserved.
# Licensed under the MIT License (MIT)

function Set-RsRestItemDataSource
{
    <#
        .SYNOPSIS
            This script updates data sources related to a catalog item from the Report Server.

        .DESCRIPTION
            This script updates data sources related to a catalog item from the Report Server.

        .PARAMETER RsItem
            Specify the location of the catalog item whose data sources will be updated.

        .PARAMETER RsItemType
            Specify the type of the $RsItem. Valid values are: Dataset, PowerBIReport or Report.

        .PARAMETER DataSources
            Specify the datasources which were initially fetched via Get-RsItemDataSource.

        .PARAMETER ReportPortalUri
            Specify the Report Portal URL to your SQL Server Reporting Services Instance.

        .PARAMETER RestApiVersion
            Specify the version of REST Endpoint to use. Valid values are: "v2.0".

        .PARAMETER Credential
            Specify the credentials to use when connecting to the Report Server.

        .PARAMETER WebSession
            Specify the session to be used when making calls to REST Endpoint.

        .EXAMPLE
            $dataSources = Get-RsRestItemDataSource -RsItem '/MyPowerBIReport'
            $dataSources[0].DataModelDataSource.AuthType = 'Windows'
            $dataSources[0].DataModelDataSource.Username = 'domain\\user'
            $dataSources[0].DataModelDataSource.Secret = 'UserPassword'
            Set-RsRestItemDataSource -RsItem '/MyPowerBIReport' -RsItemType PowerBIReport -DataSources $datasources

            Description
            -----------
            Updates data sources to the specified $dataSources object. This example is only applicable for Power BI Reports.

        .EXAMPLE
            $dataSources = Get-RsRestItemDataSource -RsItem '/MyPowerBIReport'
            $dataSources[0].DataModelDataSource.AuthType = 'UsernamePassword' # UsernamePassword should be used when specifying SQL or Basic Credentials
            $dataSources[0].DataModelDataSource.Username = 'sqlSa'
            $dataSources[0].DataModelDataSource.Secret = 'sqlSaPassword'
            Set-RsRestItemDataSource -RsItem '/MyPowerBIReport' -RsItemType PowerBIReport -DataSources $datasources

            Description
            -----------
            Updates data sources to the specified $dataSources object. This example is only applicable for Power BI Reports.

        .EXAMPLE
            $dataSources = Get-RsRestItemDataSource -RsItem '/MyPowerBIReport'
            $dataSources[0].DataModelDataSource.AuthType = 'Key'
            $dataSources[0].DataModelDataSource.Secret = 'aASDBsdas12?asd2+asdajkda='
            Set-RsRestItemDataSource -RsItem '/MyPowerBIReport' -RsItemType PowerBIReport -DataSources $datasources

            Description
            -----------
            Updates data sources to the specified $dataSources object. This example is only applicable for Power BI Reports.

        .EXAMPLE
            $dataSources = Get-RsRestItemDataSource -RsItem '/MyPaginatedReport'
            $dataSources[0].CredentialRetrieval = 'Integrated'
            Set-RsRestItemDataSource -RsItem '/MyPaginatedReport' -RsItemType Report -DataSources $datasources

            Description
            -----------
            Updates data sources to the specified $dataSources object. This example is only applicable to Paginated Reports.

        .EXAMPLE
            $dataSources = Get-RsRestItemDataSource -RsItem '/MyPaginatedReport'
            $dataSources[0].CredentialRetrieval = 'Store'
            $dataSources[0].CredentialsInServer = New-RsRestCredentialsInServerObject -Username "domain\\username" -Password "userPassword" -WindowsCredentials
            Set-RsRestItemDataSource -RsItem '/MyPaginatedReport' -RsItemType Report -DataSources $datasources

            Description
            -----------
            Updates data sources to the specified $dataSources object. This example is only applicable to Paginated Reports.

        .EXAMPLE
            $dataSources = Get-RsRestItemDataSource -RsItem '/MyPaginatedReport'
            $dataSources[0].CredentialRetrieval = 'Prompt'
            $dataSources[0].CredentialsByUser = New-RsRestCredentialsByUserObject -PromptMessage "Please enter your credentials" -WindowsCredentials
            Set-RsRestItemDataSource -RsItem '/MyPaginatedReport' -RsItemType Report -DataSources $datasources

            Description
            -----------
            Updates data sources to the specified $dataSources object. This example is only applicable to Paginated Reports.

        .EXAMPLE
            $dataSources = Get-RsRestItemDataSource -RsItem '/MyPaginatedReport'
            $dataSources[0].CredentialRetrieval = 'None'
            Set-RsRestItemDataSource -RsItem '/MyPaginatedReport' -RsItemType Report -DataSources $datasources

            Description
            -----------
            Updates data sources to the specified $dataSources object. This example is only applicable to Paginated Reports.
    #>


    [CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'Medium')]
    param(
        [Parameter(Mandatory = $True)]
        [Alias('ItemPath','Path')]
        [string]
        $RsItem,

        [Parameter(Mandatory = $True)]
        [ValidateSet("PowerBIReport", "Report")]
        [string]
        $RsItemType,

        [Parameter(Mandatory = $True)]
        $DataSources,

        [string]
        $ReportPortalUri,

        [Alias('ApiVersion')]
        [ValidateSet("v2.0")]
        [string]
        $RestApiVersion = "v2.0",

        [Alias('ReportServerCredentials')]
        [System.Management.Automation.PSCredential]
        $Credential,

        [Microsoft.PowerShell.Commands.WebRequestSession]
        $WebSession
    )
    Begin
    {
        $WebSession = New-RsRestSessionHelper -BoundParameters $PSBoundParameters
        $ReportPortalUri = Get-RsPortalUriHelper -WebSession $WebSession
        $dataSourcesUri = $ReportPortalUri + "api/$RestApiVersion/{0}(Path='{1}')/DataSources"
    }
    Process
    {
        try
        {
            # Basic DataSource Validation - Start
            foreach ($ds in $DataSources)
            {
                if ($ds.DataSourceSubType -eq 'DataModel')
                {
                    # DataModelDataSource.AuthType must be specified!
                    if ($ds.DataModelDataSource.AuthType -eq $null)
                    {
                        throw "DataModelDataSource.AuthType must be specified: $ds!"
                    }
                    elseif (($ds.DataModelDataSource.AuthType -LIKE 'Windows' -or
                            $ds.DataModelDataSource.AuthType -LIKE 'UsernamePassword' -or
                            $ds.DataModelDataSource.AuthType -LIKE 'Impersonate') -and
                            ($ds.DataModelDataSource.Username -eq $null -or
                            $ds.DataModelDataSource.Secret -eq $null))
                    {
                        # Username and Secret are required for Windows, UsernamePassword and Impersonate Auth Types
                        throw "Username and Secret must be specified for this AuthType: $ds!"
                    }
                    elseif ($ds.DataModelDataSource.AuthType -LIKE 'Key' -and
                            $ds.DataModelDataSource.Secret -eq $null)
                    {
                        # Secret is required for Key Auth Type
                        throw "Secret must be specified for this AuthType: $ds!"
                    }
                }
                elseif ($ds.DataSourceSubType -eq $null)
                {
                    # DataSourceType, ConnectionString and CredentialRetrieval must always be specified!
                    if ($ds.DataSourceType -eq $null -or
                        $ds.ConnectionString -eq $null -or
                        $ds.CredentialRetrieval -eq $null -or
                        !($ds.CredentialRetrieval -LIKE 'Integrated' -or
                        $ds.CredentialRetrieval -LIKE 'Store' -or
                        $ds.CredentialRetrieval -LIKE 'Prompt' -or
                        $ds.CredentialRetrieval -LIKE 'None'))
                    {
                        throw "Invalid data source specified: $ds!"
                    }
                    elseif ($ds.DataModelDataSource -ne $null)
                    {
                        # since this is an embedded data source for Paginated Report/Shared data set,
                        # you should not set any value to DataModelDataSource
                        throw "You cannot specify DataModelDataSource for this datasource: $ds!"
                    }

                    if ($ds.CredentialRetrieval -LIKE 'Store' -and $ds.CredentialsInServer -eq $null)
                    {
                        # CredentialsInServer must be specified for Store
                        throw "CredentialsInServer must be specified when CredentialRetrieval is set to Store: $ds!"
                    }
                    elseif ($ds.CredentialRetrieval -LIKE 'Prompt' -and $ds.CredentialsByUser -eq $null)
                    {
                        # CredentialsByUser must be specified for Prompt
                        throw "CredentialsByUser must be specified when CredentialRetrieval is set to Prompt: $ds!"
                    }
                }
                else
                {
                    throw "Unexpected data source subtype!"
                }
            }
            # Basic DataSource Validation - End

            $dataSourcesUri = [String]::Format($dataSourcesUri, $RsItemType + "s", $RsItem)

            # Converting $DataSources into array as PowerBIReport(...)/DataSources expects data sources
            # to be in an array in the request body. If $DataSources is already an array, this operation
            # combines $DataSources array with an empty array, so result is still an array.
            $dataSourcesArray = @($DataSources)

            # Depth needs to be specified otherwise whenever DataModelDataSource is present
            # Supported Auth Types will not be serialized correctly
            $payloadJson = ConvertTo-Json -InputObject $dataSourcesArray -Depth 3

            if ($RsItemType -eq "DataSet" -or $RsItemType -eq "Report")
            {
                $method = "PUT"
            }
            elseif ($RsItemType -eq "PowerBIReport")
            {
                $method = "PATCH"
            }
            else
            {
                throw "Invalid item type!"
            }

            if ($PSCmdlet.ShouldProcess($RsItem, "Update data sources"))
            {
                Write-Verbose "Updating data sources for $($RsItem)..."
                if ($Credential -ne $null)
                {
                    Invoke-WebRequest -Uri $dataSourcesUri -Method $method -Body ([System.Text.Encoding]::UTF8.GetBytes($payloadJson)) -ContentType "application/json" -WebSession $WebSession -Credential $Credential -Verbose:$false | Out-Null
                }
                else
                {
                    Invoke-WebRequest -Uri $dataSourcesUri -Method $method -Body ([System.Text.Encoding]::UTF8.GetBytes($payloadJson)) -ContentType "application/json" -WebSession $WebSession -UseDefaultCredentials -Verbose:$false | Out-Null
                }
                Write-Verbose "Data sources were updated successfully!"
            }
        }
        catch
        {
            throw (New-Object System.Exception("Failed to update data sources for '$RsItem': $($_.Exception.Message)", $_.Exception))
        }
    }
}