From c3ad4aa42805f6b9e8875af2a1408e27043c59fd Mon Sep 17 00:00:00 2001 From: Matteo Guadrini Date: Mon, 16 Apr 2018 10:00:24 +0200 Subject: [PATCH] Add cmdlet Get-CouchDBDatabaseChanges,Get-CouchDBReplication,Get-CouchDBReplicationScheduler,Set-CouchDBReplication,New-CouchDBReplication,Remove-CouchDBReplication. Fix other cmdlet. Update README. --- PSCouchDB/PSCouchDB.psd1 | 14 ++- PSCouchDB/PSCouchDB.psm1 | 192 +++++++++++++++++++++++++++++++++++++-- README.md | 10 +- 3 files changed, 202 insertions(+), 14 deletions(-) diff --git a/PSCouchDB/PSCouchDB.psd1 b/PSCouchDB/PSCouchDB.psd1 index 27739fe..6256bc6 100644 --- a/PSCouchDB/PSCouchDB.psd1 +++ b/PSCouchDB/PSCouchDB.psd1 @@ -12,7 +12,7 @@ RootModule = 'PSCouchDB.psm1' # Version number of this module. -ModuleVersion = '1.2.0' +ModuleVersion = '1.3.0' # Supported PSEditions # CompatiblePSEditions = @() @@ -30,7 +30,7 @@ Author = 'Matteo Guadrini ' Copyright = '(c) Matteo Guadrini. All rights reserved.' # Description of the functionality provided by this module -# Description = '' +Description = 'Powershell module for CouchDB v2.0' # Minimum version of the PowerShell engine required by this module # PowerShellVersion = '' @@ -70,18 +70,22 @@ Copyright = '(c) Matteo Guadrini. All rights reserved.' # Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export. FunctionsToExport = @("Get-CouchDBDatabase", + "Get-CouchDBDatabaseChanges", "Get-CouchDBDocument", "Get-CouchDBAttachment", "Get-CouchDBUser", "Get-CouchDBAdmin", "Get-CouchDBConfiguration", "Get-CouchDBNode", + "Get-CouchDBReplication", + "Get-CouchDBReplicationScheduler", "Add-CouchDBNode", "Set-CouchDBDocument", "Set-CouchDBAttachment", "Set-CouchDBUser", "Set-CouchDBAdmin", "Set-CouchDBConfiguration", + "Set-CouchDBReplication", "Grant-CouchDBDatabasePermission", "Revoke-CouchDBDatabasePermission", "New-CouchDBDatabase", @@ -89,12 +93,14 @@ FunctionsToExport = @("Get-CouchDBDatabase", "New-CouchDBAttachment", "New-CouchDBUser", "New-CouchDBAdmin", + "New-CouchDBReplication", "Remove-CouchDBDatabase", "Remove-CouchDBDocument", "Remove-CouchDBAttachment", "Remove-CouchDBUser", "Remove-CouchDBAdmin", "Remove-CouchDBNode", + "Remove-CouchDBReplication", "Find-CouchDBDocuments" ) @@ -122,13 +128,13 @@ PrivateData = @{ PSData = @{ # Tags applied to this module. These help with module discovery in online galleries. - # Tags = @() + Tags = @("CouchDB","Apache","curl") # A URL to the license for this module. # LicenseUri = '' # A URL to the main website for this project. - # ProjectUri = '' + ProjectUri = 'https://matteoguadrini.github.io/PSCouchDB' # A URL to an icon representing this module. # IconUri = '' diff --git a/PSCouchDB/PSCouchDB.psm1 b/PSCouchDB/PSCouchDB.psm1 index 655a3e2..5ea24e7 100644 --- a/PSCouchDB/PSCouchDB.psm1 +++ b/PSCouchDB/PSCouchDB.psm1 @@ -9,7 +9,7 @@ function Send-CouchDBRequest { #> [CmdletBinding()] param ( - [string] $Method, + [ValidateSet("HEAD","GET","PUT","DELETE","POST")] [string] $Method, [string] $Server, [int] $Port, [string] $Database, @@ -38,6 +38,7 @@ function Send-CouchDBRequest { Write-Verbose -Message "Check http method, default is GET" Write-Debug -Message "`$Method is $Method" switch ($Method) { + "HEAD" { $options.Add("Method","HEAD") } "GET" { $options.Add("Method","GET") } "PUT" { $options.Add("Method","PUT") } "DELETE" { $options.Add("Method","DELETE") } @@ -96,7 +97,7 @@ function Send-CouchDBRequest { } # Build the json data Write-Verbose -Message "Check json data" - if (($Data) -and ($Document) -and ($Database)) { + if (($Data) -and ($Database)) { $options.Add("ContentType","application/json") $options.Add("Body",([System.Text.Encoding]::UTF8.GetBytes($Data))) Write-Debug -Message "`$Data is $Data" @@ -126,6 +127,29 @@ function Get-CouchDBDatabase () { Send-CouchDBRequest -Server $Server -Port $Port -Method "GET" -Database $Database -Authorization $Authorization } +function Get-CouchDBDatabaseChanges () { + <# + .SYNOPSIS + Get database changelogs. + .DESCRIPTION + Get database changelogs of CouchDB . + .EXAMPLE + Get-CouchDBDatabaseChanges -Database test -Authorization "admin:passw0rd" + #> + [CmdletBinding()] + param( + [string] $Server, + [int] $Port, + [string] $Database = $(throw "Please specify the database name."), + [string] $Authorization + ) + if (-not(Get-CouchDBDatabase -Database $Database)) { + throw "Database replicator $Database is not exists." + } + $Document = '_changes' + Send-CouchDBRequest -Server $Server -Port $Port -Method "GET" -Database $Database -Document $Document -Authorization $Authorization +} + function Get-CouchDBDocument () { <# .SYNOPSIS @@ -210,7 +234,7 @@ function Get-CouchDBAdmin () { [string] $Authorization ) $Document = "$Node/_config/admins" - Send-CouchDBRequest -Server $Server -Port $Port -Method "GET" -Database $Database -Document $Document -Data $Data -Authorization $Authorization + Send-CouchDBRequest -Server $Server -Port $Port -Method "GET" -Database $Database -Document $Document -Authorization $Authorization } function Get-CouchDBConfiguration () { @@ -231,7 +255,7 @@ function Get-CouchDBConfiguration () { [string] $Authorization ) $Document = "$Node/_config" - Send-CouchDBRequest -Server $Server -Port $Port -Method "GET" -Database $Database -Document $Document -Data $Data -Authorization $Authorization + Send-CouchDBRequest -Server $Server -Port $Port -Method "GET" -Database $Database -Document $Document -Authorization $Authorization } function Get-CouchDBNode () { @@ -250,7 +274,50 @@ function Get-CouchDBNode () { [string] $Database = "_membership", [string] $Authorization ) - Send-CouchDBRequest -Server $Server -Port $Port -Method "GET" -Database $Database -Document $Document -Data $Data -Authorization $Authorization + Send-CouchDBRequest -Server $Server -Port $Port -Method "GET" -Database $Database -Document $Document -Authorization $Authorization +} + +function Get-CouchDBReplication () { + <# + .SYNOPSIS + Get database replication documents. + .DESCRIPTION + Get database replication documents of CouchDB . + .EXAMPLE + Get-CouchDBReplication -Authorization "admin:passw0rd" + #> + [CmdletBinding()] + param( + [string] $Server, + [int] $Port, + [string] $Database = "_replicator", + [string] $Document = '_all_docs', + [string] $Authorization + ) + if (-not(Get-CouchDBDatabase -Database $Database)) { + throw "Database replicator $Database is not exists." + } + Send-CouchDBRequest -Server $Server -Port $Port -Method "GET" -Database $Database -Document $Document -Authorization $Authorization +} + +function Get-CouchDBReplicationScheduler () { + <# + .SYNOPSIS + Get database replication documents. + .DESCRIPTION + Get database replication documents of CouchDB . + .EXAMPLE + Get-CouchDBReplicationScheduler -Authorization "admin:passw0rd" + #> + [CmdletBinding()] + param( + [string] $Server, + [int] $Port, + [string] $Authorization + ) + $Database = "_scheduler" + $Document = 'jobs' + Send-CouchDBRequest -Server $Server -Port $Port -Method "GET" -Database $Database -Document $Document -Authorization $Authorization } function Add-CouchDBNode () { @@ -413,6 +480,39 @@ function Set-CouchDBConfiguration () { Send-CouchDBRequest -Server $Server -Port $Port -Method "PUT" -Database $Database -Document $Document -Data $Data -Authorization $Authorization } +function Set-CouchDBReplication () { + <# + .SYNOPSIS + Set replication documents. + .DESCRIPTION + Set replication documents of CouchDB . + .EXAMPLE + Set-CouchDBReplication -Document replica_id1 -Revision "2-4705a219cdcca7c72aac4f623f5c46a8" -Continuous -Authorization "admin:passw0rd" + #> + [CmdletBinding()] + param( + [string] $Server, + [int] $Port, + [string] $Database = "_replicator", + [string] $Document = $(throw "Please specify the document id."), + [string] $Revision = $(throw "Please specify the revision id."), + [switch] $Continuous, + [string] $Authorization + ) + if (-not(Get-CouchDBDatabase -Database $Database)) { + throw "Database replicator $Database is not exists." + } + if ($Continuous.IsPresent) { + $Continuous_value = $true + } else { + $Continuous_value = $false + } + $Data = Get-CouchDBReplication -Document $Document -Authorization $Authorization + $Data.continuous = $Continuous_value + $Data = $Data | ConvertTo-Json + Send-CouchDBRequest -Server $Server -Port $Port -Method "PUT" -Database $Database -Document $Document -Revision $Revision -Data $Data -Authorization $Authorization +} + function Grant-CouchDBDatabasePermission () { <# .SYNOPSIS @@ -654,6 +754,55 @@ function New-CouchDBAdmin () { Send-CouchDBRequest -Server $Server -Port $Port -Method "PUT" -Database $Database -Document $Document -Data $Data -Authorization $Authorization } +function New-CouchDBReplication () { + <# + .SYNOPSIS + Create a new replication job. + .DESCRIPTION + Create a new replication job for a specidfic database. + .EXAMPLE + New-CouchDBReplication -Name "Test replication" -SourceServer localhost -TargetServer server1 -SourceDatabase test -TargetDatabase test_replica -Continuous -Authorization "admin:passw0rd" + #> + [CmdletBinding()] + param( + [string] $SourceServer = 'localhost', + [string] $TargetServer = 'localhost', + [int] $SourcePort = 5984, + [int] $TargetPort = 5984, + [string] $Database = "_replicator", + [string] $SourceDatabase, + [string] $TargetDatabase, + [switch] $Continuous, + [string] $Authorization + ) + $Server = $SourceServer + $Port = $SourcePort + # Check if replicator database exists + if (-not(Get-CouchDBDatabase -Server $Server -Port $Port -Database $Database -Authorization $Authorization -ErrorAction SilentlyContinue)) { + New-CouchDBDatabase -Server $Server -Port $Port -Database $Database -Authorization $Authorization | Out-Null + } + # Check if target database exists + if (-not(Get-CouchDBDatabase -Server $Server -Port $Port -Database $TargetDatabase -Authorization $Authorization -ErrorAction SilentlyContinue)) { + New-CouchDBDatabase -Server $Server -Port $Port -Database $TargetDatabase -Authorization $Authorization | Out-Null + } + # Create Source and Target URL + $Source = "http://$SourceServer`:$SourcePort/$SourceDatabase" + $Target = "http://$TargetServer`:$TargetPort/$TargetDatabase" + if ($Continuous.IsPresent) { + $Continuous_value = "true" + } else { + $Continuous_value = "false" + } + # Create data + $Data = "{ + `"_id`":`"$SourceServer`-$SourceDatabase`_$TargetServer`-$TargetDatabase`", + `"source`":`"$Source`", + `"target`":`"$Target`", + `"continuous`":$Continuous_value + }" + Send-CouchDBRequest -Server $Server -Port $Port -Method "POST" -Database $Database -Document $Document -Data $Data -Authorization $Authorization +} + function Remove-CouchDBDatabase () { <# .SYNOPSIS @@ -770,7 +919,7 @@ function Remove-CouchDBAdmin () { ) $Document = "$Node/_config/admins/$Userid" if ($Force -or $PSCmdlet.ShouldContinue("Do you wish remove admin user $Userid ?","Remove $Userid on node $Node")) { - Send-CouchDBRequest -Server $Server -Port $Port -Method "DELETE" -Database $Database -Document $Document -Data $Data -Authorization $Authorization + Send-CouchDBRequest -Server $Server -Port $Port -Method "DELETE" -Database $Database -Document $Document -Authorization $Authorization } } @@ -799,7 +948,34 @@ function Remove-CouchDBNode () { } $Document = $Node if ($Force -or $PSCmdlet.ShouldContinue("Do you wish remove node $Node ?","Remove $Node")) { - Send-CouchDBRequest -Server $Server -Port $Port -Method "DELETE" -Database $Database -Document $Document -Revision $Revision -Data $Data -Authorization $Authorization + Send-CouchDBRequest -Server $Server -Port $Port -Method "DELETE" -Database $Database -Document $Document -Revision $Revision -Authorization $Authorization + } +} + +function Remove-CouchDBReplication () { + <# + .SYNOPSIS + Remove replication documents. + .DESCRIPTION + Remove replication documents of CouchDB . + .EXAMPLE + Remove-CouchDBReplication -Document replica_id1 -Revision "2-4705a219cdcca7c72aac4f623f5c46a8" -Authorization "admin:passw0rd" + #> + [CmdletBinding(SupportsShouldProcess = $true)] + param( + [string] $Server, + [int] $Port, + [string] $Database = "_replicator", + [string] $Document = $(throw "Please specify the document id."), + [string] $Revision = $(throw "Please specify the revision id."), + [string] $Authorization, + [switch] $Force + ) + if (-not(Get-CouchDBDatabase -Database $Database)) { + throw "Database replicator $Database is not exists." + } + if ($Force -or $PSCmdlet.ShouldContinue("Do you wish remove replication $Document ?","Remove $Document")) { + Send-CouchDBRequest -Server $Server -Port $Port -Method "DELETE" -Database $Database -Document $Document -Revision $Revision -Authorization $Authorization } } @@ -819,7 +995,7 @@ function Find-CouchDBDocuments () { [string] $Database = $(throw "Please specify the database name."), [string] $Selector, [string] $Value, - [array]$Fields, + [array] $Fields, [string] $Sort, [ValidateSet('lt','lte','eq','ne','gte','gt','exists','type','in','nin','size','regex')] [string] $Operator, diff --git a/README.md b/README.md index a9a0277..74f167b 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ New-CouchDBAdmin -Userid adminuser -Password "password" ``` > ATTENTION: Authentication for read and write no required by default, but required if you create custom user, like session "Grant permission" on this document. For more information see permission on [wiki permission page](https://github.com/MatteoGuadrini/PSCouchDB/wiki/Permission) -3. Download and install latest PSCouchDB module by copying it under `%Windir%\System32\WindowsPowerShell\v1.0\Modules` for all users or under `%UserProfile%\Documents\WindowsPowerShell\Modules` for the current user. +3. Download and install latest PSCouchDB module by copying it under `%Windir%\System32\WindowsPowerShell\v1.0\Modules` for all users or under `%UserProfile%\Documents\WindowsPowerShell\Modules` for the current user or install through [PowershellGallery](https://www.powershellgallery.com/packages/PSCouchDB). > ATTENTION: This module is not signed. Before import or execute cmdlet on this module, see [about_signing](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_signing) session. To skip this part and continue, run ```Set-ExecutionPolicy -ExecutionPolicy Unrestricted``` 4. Now, configure database mode, in single node or cluster, clicking on "Setup" on left of the menu; follow the wizard and complete this procedure. Before configure database mode, create this system document whit this cmdlet: @@ -125,9 +125,15 @@ For now, I've used the native powershell logical operators. (-not((Find-CouchDBDocuments -Database test -Selector "color" -Value "red" -Fields _id,color -Operator eq).docs -or (Find-CouchDBDocuments -Database test -Selector "color" -Value "blue" -Fields _id,color -Operator eq).docs)) ``` +### Other operation +For other operation see the [wiki](https://github.com/MatteoGuadrini/PSCouchDB/wiki). + ### Cmdlet example To get examples of all the cmdlets of this module, use this command: ```powershell Get-Command -Module *PSCouchDB* | foreach {Get-Help $_.Name -Example} ``` -or see [wiki page](https://github.com/MatteoGuadrini/PSCouchDB/wiki) \ No newline at end of file +or see [wiki page](https://github.com/MatteoGuadrini/PSCouchDB/wiki) + + +[![paypal](https://www.paypalobjects.com/en_US/i/btn/btn_donateCC_LG.gif)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=CYB2W93Z5JY8C) \ No newline at end of file