Skip to content

Commit

Permalink
Merge pull request #175 from ClaudioESSilva/NewFeaturesWrite-Rs_Catal…
Browse files Browse the repository at this point in the history
…ogItem

New features to Write-Rs(Rest)CatalogItem
  • Loading branch information
jtarquino authored Jun 5, 2018
2 parents 1524fda + 498942e commit 52b0cd7
Show file tree
Hide file tree
Showing 8 changed files with 178 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ function Write-RsRestCatalogItem
.PARAMETER RsFolder
Folder on reportserver to upload the item to.
.PARAMETER Description
Specify the description to be added to the report.
.PARAMETER Overwrite
Overwrite the old entry, if an existing catalog item with same name exists at the specified destination.
Expand Down Expand Up @@ -73,6 +76,9 @@ function Write-RsRestCatalogItem
[string]
$RsFolder,

[string]
$Description,

[Alias('Override')]
[switch]
$Overwrite,
Expand Down Expand Up @@ -226,6 +232,7 @@ function Write-RsRestCatalogItem
"Content" = [System.Convert]::ToBase64String($bytes);
"ContentType"="";
"Name" = $itemName;
"Description" = $Description
"Path" = $itemPath;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@ function Write-RsCatalogItem
.DESCRIPTION
Uploads an item from disk to a report server.
Currently, we are only supporting Report, DataSource and DataSet for uploads
Currently, we are only supporting Report, DataSource, DataSet and jpg/png for uploads
.PARAMETER Path
Path to item to upload on disk.
.PARAMETER RsFolder
Folder on reportserver to upload the item to.
.PARAMETER Description
Specify the description to be added to the report.
.PARAMETER Overwrite
Overwrite the old entry, if an existing catalog item with same name exists at the specified destination.
Expand Down Expand Up @@ -52,6 +55,9 @@ function Write-RsCatalogItem
[string]
$RsFolder,

[string]
$Description,

[Alias('Override')]
[switch]
$Overwrite,
Expand All @@ -69,6 +75,8 @@ function Write-RsCatalogItem
Begin
{
$Proxy = New-RsWebServiceProxyHelper -BoundParameters $PSBoundParameters
$namespace = $proxy.GetType().Namespace
$propertyDataType = "$namespace.Property"
}

Process
Expand All @@ -86,11 +94,20 @@ function Write-RsCatalogItem
$itemType = Get-ItemType $item.Extension
$itemName = $item.BaseName

if ($itemType -ne "Report" -and
$itemType -ne "DataSource" -and
$itemType -ne "DataSet")
if (
(
$itemType -ne "Report" -and
$itemType -ne "DataSource" -and
$itemType -ne "DataSet" -and
$itemType -ne "Resource"
) -or
(
$itemType -eq "Resource" -and
$item.Extension -notin ('.png', '.jpg', '.jpeg')
)
)
{
throw "Invalid item specified! You can only upload Report, DataSource and DataSet using this command!"
throw "Invalid item specified! You can only upload Report, DataSource, DataSet and jpg/png files using this command!"
}

if ($RsFolder -eq "/")
Expand Down Expand Up @@ -187,22 +204,49 @@ function Write-RsCatalogItem
#region Upload other stuff
else
{
$additionalProperties = New-Object System.Collections.Generic.List[$propertyDataType]
$property = New-Object $propertyDataType

if ($itemType -eq 'Resource')
{
#If it is a resource we need to save the extension so the file can be recognized
$itemName = $item.Name
$property.Name = 'MimeType'
if ($item.Extension -eq ".png")
{
$property.Value = 'image/png'
}
else
{
$property.Value = 'image/jpeg'
}
$erroMessageItemType = 'resource'
}
else
{
$property.Name = 'Description'
$property.Value = $Description
$erroMessageItemType = 'catalog'
}

$additionalProperties.Add($property)

$bytes = [System.IO.File]::ReadAllBytes($EntirePath)
$warnings = $null
try
{
$Proxy.CreateCatalogItem($itemType, $itemName, $RsFolder, $Overwrite, $bytes, $null, [ref]$warnings) | Out-Null
$Proxy.CreateCatalogItem($itemType, $itemName, $RsFolder, $Overwrite, $bytes, $additionalProperties, [ref]$warnings) | Out-Null
if ($warnings)
{
foreach ($warn in $warnings)
{
Write-Warning $warn.Message
}
foreach ($warn in $warnings)
{
Write-Warning $warn.Message
}
}
}
catch
{
throw (New-Object System.Exception("Failed to create catalog item: $($_.Exception.Message)", $_.Exception))
throw (New-Object System.Exception("Failed to create $erroMessageItemType item $($item.FullName) : $($_.Exception.Message)", $_.Exception))
}
}
#endregion Upload other stuff
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,39 @@ function Write-RsFolderContent
<#
.SYNOPSIS
Uploads all items in a folder on disk to a report server.
.DESCRIPTION
Uploads all items in a folder on disk to a report server.
Currently, we are only supporting Report, DataSource and DataSet for uploads
Currently, we are only supporting Report, DataSource, DataSet and jpg/png for uploads
.PARAMETER Recurse
A description of the Recurse parameter.
.PARAMETER Path
Path to folder which contains items to upload on disk.
.PARAMETER RsFolder
Folder on reportserver to upload the item to.
.PARAMETER Overwrite
Overwrite the old entry, if an existing catalog item with same name exists at the specified destination.
.PARAMETER ReportServerUri
Specify the Report Server URL to your SQL Server Reporting Services Instance.
Use the "Connect-RsReportServer" function to set/update a default value.
.PARAMETER Credential
Specify the credentials to use when connecting to the Report Server.
Use the "Connect-RsReportServer" function to set/update a default value.
.PARAMETER Proxy
Report server proxy to use.
Use "New-RsWebServiceProxy" to generate a proxy object for reuse.
Useful when repeatedly having to connect to multiple different Report Server.
.EXAMPLE
Write-RsFolderContent -ReportServerUri 'http://localhost/reportserver_sql2012' -Path c:\monthlyreports -RsFolder /monthlyReports
Description
-----------
Uploads all reports under c:\monthlyreports to folder /monthlyReports.
Expand Down Expand Up @@ -127,12 +127,15 @@ function Write-RsFolderContent
if ($item.Extension -eq ".rdl" -or
$item.Extension -eq ".rsds" -or
$item.Extension -eq ".rsd" -or
$item.Extension -eq ".rds")
$item.Extension -eq ".rds" -or
$item.Extension -eq ".jpg" -or
$item.Extension -eq ".jpeg" -or
$item.Extension -eq ".png" )
{
$relativePath = Clear-Substring -string $item.FullName -substring $sourceFolder.FullName.TrimEnd("\") -position front
$relativePath = Clear-Substring -string $relativePath -substring ("\" + $item.Name) -position back
$relativePath = $relativePath.replace("\", "/")

if ($RsFolder -eq "/" -and $relativePath -ne "")
{
$parentFolder = $relativePath
Expand Down
24 changes: 24 additions & 0 deletions Tests/CatalogItems/Rest/Write-RsRestCatalogItem.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,18 @@ Describe "Write-RsRestCatalogItem" {
Write-RsRestCatalogItem -ReportPortalUri $reportPortalUri -Path $itemPath -RsFolder $rsFolderPath
{ Write-RsRestCatalogItem -ReportPortalUri $reportPortalUri -Path $itemPath -RsFolder $rsFolderPath -Verbose } | Should Throw
}

It "Should upload a local JPG file" {
$itemPath = $localPath + '\imagesResources\PowerShellHero.jpg'
Write-RsRestCatalogItem -ReportPortalUri $reportPortalUri -Path $itemPath -RsFolder $rsFolderPath -Verbose
VerifyCatalogItemExists -itemName 'PowerShellHero.jpg' -itemType 'Resource' -folderPath $rsFolderPath -reportServerUri $reportServerUri
}

It "Should upload a local PNG file" {
$itemPath = $localPath + '\imagesResources\SSRS.png'
Write-RsRestCatalogItem -ReportPortalUri $reportPortalUri -Path $itemPath -RsFolder $rsFolderPath -Verbose
VerifyCatalogItemExists -itemName 'SSRS.png' -itemType 'Resource' -folderPath $rsFolderPath -reportServerUri $reportServerUri
}
}

Context "WebSession parameter" {
Expand Down Expand Up @@ -167,6 +179,18 @@ Describe "Write-RsRestCatalogItem" {
VerifyCatalogItemExists -itemName 'NewKPI' -itemType 'Kpi' -folderPath $rsFolderPath -reportServerUri $reportServerUri
}

It "Should upload a local JPG file" {
$itemPath = $localPath + '\imagesResources\PowerShellHero.jpg'
Write-RsRestCatalogItem -WebSession $webSession -Path $itemPath -RsFolder $rsFolderPath -Verbose
VerifyCatalogItemExists -itemName 'PowerShellHero.jpg' -itemType 'Resource' -folderPath $rsFolderPath -reportServerUri $reportServerUri
}

It "Should upload a local PNG file" {
$itemPath = $localPath + '\imagesResources\SSRS.png'
Write-RsRestCatalogItem -WebSession $webSession -Path $itemPath -RsFolder $rsFolderPath -Verbose
VerifyCatalogItemExists -itemName 'SSRS.png' -itemType 'Resource' -folderPath $rsFolderPath -reportServerUri $reportServerUri
}

It "Should overwrite a file when -Overwrite is specified" {
$itemPath = Join-Path -Path $localPath -ChildPath emptyReport.rdl
Write-RsRestCatalogItem -WebSession $webSession -Path $itemPath -RsFolder $rsFolderPath
Expand Down
Loading

0 comments on commit 52b0cd7

Please sign in to comment.