Adding several new Content Library functions

This commit is contained in:
William Lam
2017-11-14 06:25:50 -08:00
parent 77adef309d
commit 9f2fc3ec06

View File

@@ -25,12 +25,29 @@
$results = @()
foreach($libraryID in $libaryIDs) {
$library = $contentLibaryService.get($libraryId)
$library = $contentLibaryService.get($libraryID)
# Use vCenter REST API to retrieve name of Datastore that is backing the Content Library
$datastoreService = Get-CisService com.vmware.vcenter.datastore
$datastore = $datastoreService.get($library.storage_backings.datastore_id)
if($library.publish_info.published) {
$published = $library.publish_info.published
$publishedURL = $library.publish_info.publish_url
$externalReplication = $library.publish_info.persist_json_enabled
} else {
$published = $library.publish_info.published
$publishedURL = "N/A"
$externalReplication = "N/A"
}
if($library.subscription_info) {
$subscribeURL = $library.subscription_info.subscription_url
$published = "N/A"
} else {
$subscribeURL = "N/A"
}
if(!$LibraryName) {
$libraryResult = [pscustomobject] @{
Id = $library.Id;
@@ -38,6 +55,10 @@
Type = $library.Type;
Description = $library.Description;
Datastore = $datastore.name;
Published = $published;
PublishedURL = $publishedURL;
JSONPersistence = $externalReplication;
SubscribedURL = $subscribeURL;
CreationTime = $library.Creation_Time;
}
$results+=$libraryResult
@@ -49,6 +70,10 @@
Type = $library.Type;
Description = $library.Description;
Datastore = $datastore.name;
Published = $published;
PublishedURL = $publishedURL;
JSONPersistence = $externalReplication;
SubscribedURL = $subscribeURL;
CreationTime = $library.Creation_Time;
}
$results+=$libraryResult
@@ -194,4 +219,363 @@ Function Get-ContentLibraryItemFiles {
}
}
$results
}
Function Set-ContentLibrary {
<#
.NOTES
===========================================================================
Created by: William Lam
Organization: VMware
Blog: www.virtuallyghetto.com
Twitter: @lamw
===========================================================================
.DESCRIPTION
This function updates the JSON Persistence property for a given Content Library
.PARAMETER LibraryName
The name of a vSphere Content Library
.EXAMPLE
Set-ContentLibraryItems -LibraryName Test -JSONPersistenceEnabled
.EXAMPLE
Set-ContentLibraryItems -LibraryName Test -JSONPersistenceDisabled
#>
param(
[Parameter(Mandatory=$true)][String]$LibraryName,
[Parameter(Mandatory=$false)][Switch]$JSONPersistenceEnabled,
[Parameter(Mandatory=$false)][Switch]$JSONPersistenceDisabled
)
$contentLibaryService = Get-CisService com.vmware.content.library
$libaryIDs = $contentLibaryService.list()
$found = $false
foreach($libraryID in $libaryIDs) {
$library = $contentLibaryService.get($libraryId)
if($library.name -eq $LibraryName) {
$found = $true
break
}
}
if($found) {
$localLibraryService = Get-CisService -Name "com.vmware.content.local_library"
if($JSONPersistenceEnabled) {
$jsonPersist = $true
} else {
$jsonPersist = $false
}
$updateSpec = $localLibraryService.Help.update.update_spec.Create()
$updateSpec.type = $library.type
$updateSpec.publish_info.authentication_method = $library.publish_info.authentication_method
$updateSpec.publish_info.persist_json_enabled = $jsonPersist
Write-Host "Updating JSON Persistence configuration setting for $LibraryName ..."
$localLibraryService.update($library.id,$updateSpec)
} else {
Write-Host "Unable to find Content Library $Libraryname"
}
}
Function New-ExtReplicatedContentLibrary {
<#
.NOTES
===========================================================================
Created by: William Lam
Organization: VMware
Blog: www.virtuallyghetto.com
Twitter: @lamw
===========================================================================
.DESCRIPTION
This function creates a new Subscriber Content Library from a JSON Persisted
Content Library that has been externally replicated
.PARAMETER LibraryName
The name of the new vSphere Content Library
.PARAMETER DatastoreName
The name of the vSphere Datastore which contains JSON Persisted configuration file
.PARAMETER SubscribeLibraryName
The name fo the root directroy of the externally replicated Content Library residing on vSphere Datastore
.PARAMETER AutoSync
Whether or not to Automatically sync content
.PARAMETER OnDemand
Only sync content when requested
.EXAMPLE
New-ExtReplicatedContentLibrary -LibraryName Bar -DatastoreName iSCSI-02 -SubscribeLibraryName myExtReplicatedLibrary
.EXAMPLE
New-ExtReplicatedContentLibrary -LibraryName Bar -DatastoreName iSCSI-02 -SubscribeLibraryName myExtReplicatedLibrary -AutoSync $false -OnDemand $true
#>
param(
[Parameter(Mandatory=$true)][String]$LibraryName,
[Parameter(Mandatory=$true)][String]$DatastoreName,
[Parameter(Mandatory=$true)][String]$SubscribeLibraryName,
[Parameter(Mandatory=$false)][Boolean]$AutoSync=$false,
[Parameter(Mandatory=$false)][Boolean]$OnDemand=$true
)
$datastore = Get-Datastore -Name $DatastoreName
if($datastore) {
$datastoreId = $datastore.ExtensionData.MoRef.Value
$datastoreUrl = $datastore.ExtensionData.Info.Url
$subscribeUrl = $datastoreUrl + $SubscribeLibraryName + "/lib.json"
$subscribeLibraryService = Get-CisService -Name "com.vmware.content.subscribed_library"
$StorageSpec = [pscustomobject] @{
datastore_id = $datastoreId;
type = "DATASTORE";
}
$UniqueChangeId = [guid]::NewGuid().tostring()
$createSpec = $subscribeLibraryService.Help.create.create_spec.Create()
$createSpec.name = $LibraryName
$addResults = $createSpec.storage_backings.Add($StorageSpec)
$createSpec.subscription_info.automatic_sync_enabled = $false
$createSpec.subscription_info.on_demand = $true
$createSpec.subscription_info.subscription_url = $subscribeUrl
$createSpec.subscription_info.authentication_method = "NONE"
$createSpec.type = "SUBSCRIBED"
Write-Host "Creating new Externally Replicated Content Library called $LibraryName ..."
$library = $subscribeLibraryService.create($UniqueChangeId,$createSpec)
}
}
Function Remove-SubscribedContentLibrary {
<#
.NOTES
===========================================================================
Created by: William Lam
Organization: VMware
Blog: www.virtuallyghetto.com
Twitter: @lamw
===========================================================================
.DESCRIPTION
This function deletes a Subscriber Content Library
.PARAMETER LibraryName
The name of the new vSphere Content Library to delete
.EXAMPLE
Remove-SubscribedContentLibrary -LibraryName Bar
#>
param(
[Parameter(Mandatory=$true)][String]$LibraryName
)
$contentLibaryService = Get-CisService com.vmware.content.library
$libaryIDs = $contentLibaryService.list()
$found = $false
foreach($libraryID in $libaryIDs) {
$library = $contentLibaryService.get($libraryId)
if($library.name -eq $LibraryName) {
$found = $true
break
}
}
if($found) {
$subscribeLibraryService = Get-CisService -Name "com.vmware.content.subscribed_library"
Write-Host "Deleting Subscribed Content Library $LibraryName ..."
$subscribeLibraryService.delete($library.id)
} else {
Write-Host "Unable to find Content Library $LibraryName"
}
}
Function New-LocalContentLibrary {
<#
.NOTES
===========================================================================
Created by: William Lam
Organization: VMware
Blog: www.virtuallyghetto.com
Twitter: @lamw
===========================================================================
.DESCRIPTION
This function creates a new Subscriber Content Library from a JSON Persisted
Content Library that has been externally replicated
.PARAMETER LibraryName
The name of the new vSphere Content Library
.PARAMETER DatastoreName
The name of the vSphere Datastore to store the Content Library
.PARAMETER Publish
Whther or not to publish the Content Library, this is required for JSON Peristence
.PARAMETER JSONPersistence
Whether or not to enable JSON Persistence which enables external replication of Content Library
.EXAMPLE
New-LocalContentLibrary -LibraryName Foo -DatastoreName iSCSI-01 -Publish $true
.EXAMPLE
New-LocalContentLibrary -LibraryName Foo -DatastoreName iSCSI-01 -Publish $true -JSONPersistence $true
#>
param(
[Parameter(Mandatory=$true)][String]$LibraryName,
[Parameter(Mandatory=$true)][String]$DatastoreName,
[Parameter(Mandatory=$false)][Boolean]$Publish=$true,
[Parameter(Mandatory=$false)][Boolean]$JSONPersistence=$false
)
$datastore = Get-Datastore -Name $DatastoreName
if($datastore) {
$datastoreId = $datastore.ExtensionData.MoRef.Value
$localLibraryService = Get-CisService -Name "com.vmware.content.local_library"
$StorageSpec = [pscustomobject] @{
datastore_id = $datastoreId;
type = "DATASTORE";
}
$UniqueChangeId = [guid]::NewGuid().tostring()
$createSpec = $localLibraryService.Help.create.create_spec.Create()
$createSpec.name = $LibraryName
$addResults = $createSpec.storage_backings.Add($StorageSpec)
$createSpec.publish_info.authentication_method = "NONE"
$createSpec.publish_info.persist_json_enabled = $JSONPersistence
$createSpec.publish_info.published = $Publish
$createSpec.type = "LOCAL"
Write-Host "Creating new Local Content Library called $LibraryName ..."
$library = $localLibraryService.create($UniqueChangeId,$createSpec)
}
}
Function Remove-LocalContentLibrary {
<#
.NOTES
===========================================================================
Created by: William Lam
Organization: VMware
Blog: www.virtuallyghetto.com
Twitter: @lamw
===========================================================================
.DESCRIPTION
This function deletes a Local Content Library
.PARAMETER LibraryName
The name of the new vSphere Content Library to delete
.EXAMPLE
Remove-LocalContentLibrary -LibraryName Bar
#>
param(
[Parameter(Mandatory=$true)][String]$LibraryName
)
$contentLibaryService = Get-CisService com.vmware.content.library
$libaryIDs = $contentLibaryService.list()
$found = $false
foreach($libraryID in $libaryIDs) {
$library = $contentLibaryService.get($libraryId)
if($library.name -eq $LibraryName) {
$found = $true
break
}
}
if($found) {
$localLibraryService = Get-CisService -Name "com.vmware.content.local_library"
Write-Host "Deleting Local Content Library $LibraryName ..."
$localLibraryService.delete($library.id)
} else {
Write-Host "Unable to find Content Library $LibraryName"
}
}
Function Copy-ContentLibrary {
<#
.NOTES
===========================================================================
Created by: William Lam
Organization: VMware
Blog: www.virtuallyghetto.com
Twitter: @lamw
===========================================================================
.DESCRIPTION
This function copies all library items from one Content Library to another
.PARAMETER SourceLibaryName
The name of the source Content Library to copy from
.PARAMETER DestinationLibaryName
The name of the desintation Content Library to copy to
.PARAMETER DeleteSourceFile
Whther or not to delete library item from the source Content Library after copy
.EXAMPLE
Copy-ContentLibrary -SourceLibaryName Foo -DestinationLibaryName Bar
.EXAMPLE
Copy-ContentLibrary -SourceLibaryName Foo -DestinationLibaryName Bar -DeleteSourceFile $true
#>
param(
[Parameter(Mandatory=$true)][String]$SourceLibaryName,
[Parameter(Mandatory=$true)][String]$DestinationLibaryName,
[Parameter(Mandatory=$false)][Boolean]$DeleteSourceFile=$false
)
$sourceLibraryId = (Get-ContentLibrary -LibraryName $SourceLibaryName).Id
if($sourceLibraryId -eq $null) {
Write-Host -ForegroundColor red "Unable to find Source Content Library named $SourceLibaryName"
exit
}
$destinationLibraryId = (Get-ContentLibrary -LibraryName $DestinationLibaryName).Id
if($destinationLibraryId -eq $null) {
Write-Host -ForegroundColor Red "Unable to find Destination Content Library named $DestinationLibaryName"
break
}
$sourceItemFiles = Get-ContentLibraryItems -LibraryName $SourceLibaryName
if($sourceItemFiles -eq $null) {
Write-Host -ForegroundColor red "Unable to retrieve Content Library Items from $SourceLibaryName"
break
}
$contentLibaryItemService = Get-CisService com.vmware.content.library.item
foreach ($sourceItemFile in $sourceItemFiles) {
# Check to see if file already exists in destination Content Library
$result = Get-ContentLibraryItems -LibraryName $DestinationLibaryName -LibraryItemName $sourceItemFile.Name
if($result -eq $null) {
# Create CopySpec
$copySpec = $contentLibaryItemService.Help.copy.destination_create_spec.Create()
$copySpec.library_id = $destinationLibraryId
$copySpec.name = $sourceItemFile.Name
$copySpec.description = $sourceItemFile.Description
# Create random Unique Copy Id
$UniqueChangeId = [guid]::NewGuid().tostring()
# Perform Copy
try {
Write-Host -ForegroundColor Cyan "Copying" $sourceItemFile.Name "..."
$copyResult = $contentLibaryItemService.copy($UniqueChangeId, $sourceItemFile.Id, $copySpec)
} catch {
Write-Host -ForegroundColor Red "Failed to copy" $sourceItemFile.Name
$Error[0]
break
}
# Delete source file if set to true
if($DeleteSourceFile) {
try {
Write-Host -ForegroundColor Magenta "Deleteing" $sourceItemFile.Name "..."
#$deleteResult = $contentLibaryItemService.delete($sourceItemFile.Id)
} catch {
Write-Host -ForegroundColor Red "Failed to delete" $sourceItemFile.Name
$Error[0]
break
}
}
} else {
Write-Host -ForegroundColor Yellow "Skipping" $sourceItemFile.Name "already exists"
# Delete source file if set to true
if($DeleteSourceFile) {
try {
Write-Host -ForegroundColor Magenta "Deleteing" $sourceItemFile.Name "..."
#$deleteResult = $contentLibaryItemService.delete($sourceItemFile.Id)
} catch {
Write-Host -ForegroundColor Red "Failed to delete" $sourceItemFile.Name
break
}
}
}
}
}