Skip to content

Commit

Permalink
Add node cmdlet Get-CouchDBNode,Add-CouchDBNode,Remove-CouchDBNode
Browse files Browse the repository at this point in the history
  • Loading branch information
Matteo Guadrini committed Mar 19, 2018
1 parent 8796e5b commit b7a9037
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 6 deletions.
3 changes: 3 additions & 0 deletions PSCouchDB/PSCouchDB.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ FunctionsToExport = @("Get-CouchDBDatabase",
"Get-CouchDBUser",
"Get-CouchDBAdmin",
"Get-CouchDBConfiguration",
"Get-CouchDBNode",
"Add-CouchDBNode",
"Set-CouchDBDocument",
"Set-CouchDBAttachment",
"Set-CouchDBUser",
Expand All @@ -92,6 +94,7 @@ FunctionsToExport = @("Get-CouchDBDatabase",
"Remove-CouchDBAttachment",
"Remove-CouchDBUser",
"Remove-CouchDBAdmin",
"Remove-CouchDBNode",
"Find-CouchDBDocuments"
)

Expand Down
85 changes: 79 additions & 6 deletions PSCouchDB/PSCouchDB.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 () {
Expand Down

0 comments on commit b7a9037

Please sign in to comment.