From b7a90374c20ed403cc4724af6617b0a79048801d Mon Sep 17 00:00:00 2001 From: Matteo Guadrini Date: Mon, 19 Mar 2018 12:43:03 +0100 Subject: [PATCH] Add node cmdlet Get-CouchDBNode,Add-CouchDBNode,Remove-CouchDBNode --- PSCouchDB/PSCouchDB.psd1 | 3 ++ PSCouchDB/PSCouchDB.psm1 | 85 +++++++++++++++++++++++++++++++++++++--- 2 files changed, 82 insertions(+), 6 deletions(-) diff --git a/PSCouchDB/PSCouchDB.psd1 b/PSCouchDB/PSCouchDB.psd1 index a8a2279..27739fe 100644 --- a/PSCouchDB/PSCouchDB.psd1 +++ b/PSCouchDB/PSCouchDB.psd1 @@ -75,6 +75,8 @@ FunctionsToExport = @("Get-CouchDBDatabase", "Get-CouchDBUser", "Get-CouchDBAdmin", "Get-CouchDBConfiguration", + "Get-CouchDBNode", + "Add-CouchDBNode", "Set-CouchDBDocument", "Set-CouchDBAttachment", "Set-CouchDBUser", @@ -92,6 +94,7 @@ FunctionsToExport = @("Get-CouchDBDatabase", "Remove-CouchDBAttachment", "Remove-CouchDBUser", "Remove-CouchDBAdmin", + "Remove-CouchDBNode", "Find-CouchDBDocuments" ) diff --git a/PSCouchDB/PSCouchDB.psm1 b/PSCouchDB/PSCouchDB.psm1 index 0f5c32b..655a3e2 100644 --- a/PSCouchDB/PSCouchDB.psm1 +++ b/PSCouchDB/PSCouchDB.psm1 @@ -140,11 +140,10 @@ function Get-CouchDBDocument () { [string] $Server, [int] $Port, [string] $Database = $(throw "Please specify the database name."), - [string] $Document = "_all_docs", - [string] $Data, + [string] $Document = "_all_docs", [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 } # TODO: Add outfile parameter on ivoke-restmethod for download file attachment @@ -235,6 +234,48 @@ function Get-CouchDBConfiguration () { Send-CouchDBRequest -Server $Server -Port $Port -Method "GET" -Database $Database -Document $Document -Data $Data -Authorization $Authorization } +function Get-CouchDBNode () { + <# + .SYNOPSIS + Get server nodes. + .DESCRIPTION + Get server nodes of CouchDB. + .EXAMPLE + Get-CouchDBNode -Authorization "admin:passw0rd" + #> + [CmdletBinding()] + param( + [string] $Server, + [int] $Port, + [string] $Database = "_membership", + [string] $Authorization + ) + Send-CouchDBRequest -Server $Server -Port $Port -Method "GET" -Database $Database -Document $Document -Data $Data -Authorization $Authorization +} + +function Add-CouchDBNode () { + <# + .SYNOPSIS + Add server nodes. + .DESCRIPTION + Add server nodes of CouchDB. + .EXAMPLE + Add-CouchDBNode -Name test -Authorization "admin:passw0rd" + #> + [CmdletBinding()] + param( + [string] $Server, + [int] $Port = 5986, + [string] $Database = "_nodes", + [string] $Name = $(throw "Please specify name of node!"), + [string] $Hostname = 'localhost', + [string] $Authorization + ) + $Document = "$Name@$Hostname" + $Data = '{}' + Send-CouchDBRequest -Server $Server -Port $Port -Method "PUT" -Database $Database -Document $Document -Data $Data -Authorization $Authorization +} + function Set-CouchDBDocument () { <# .SYNOPSIS @@ -717,17 +758,49 @@ function Remove-CouchDBAdmin () { .EXAMPLE Remove-CouchDBAdmin -Userid test_user -Authorization "admin:passw0rd" #> - [CmdletBinding()] + [CmdletBinding(SupportsShouldProcess = $true)] param( [string] $Server, [int] $Port, [string] $Database = "_node", [string] $Node = "couchdb@localhost", [string] $Userid = $(throw "Please specify the admin username."), - [string] $Authorization + [string] $Authorization, + [switch]$Force ) $Document = "$Node/_config/admins/$Userid" - Send-CouchDBRequest -Server $Server -Port $Port -Method "DELETE" -Database $Database -Document $Document -Data $Data -Authorization $Authorization + 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 + } +} + +function Remove-CouchDBNode () { + <# + .SYNOPSIS + Remove server nodes. + .DESCRIPTION + Remove server nodes of CouchDB. + .EXAMPLE + Remove-CouchDBNode -Node test -Authorization "admin:passw0rd" + #> + [CmdletBinding(SupportsShouldProcess = $true)] + param( + [string] $Server, + [int] $Port = 5986, + [string] $Database = "_nodes", + [string] $Node = $(throw "Please specify name of node!"), + [string] $Authorization, + [switch]$Force + ) + if (Get-CouchDBDocument -Port $Port -Database $Database -Document $Node -Authorization $Authorization -ErrorAction SilentlyContinue) { + $Revision = (Get-CouchDBDocument -Port $Port -Database $Database -Document $Node -Authorization $Authorization)._rev + } else { + throw "Node $Node not exist." + } + $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 + } } function Find-CouchDBDocuments () {