Skip to content

Commit 9720bdd

Browse files
New stable release
Add *Node* parameter on **Get-CouchDBNode** cmdlet Add all parameters on **Get-CouchDBDatabaseChanges** cmdlet Add *tests* folder for Pester testing
2 parents b575ea2 + eab3720 commit 9720bdd

22 files changed

Lines changed: 866 additions & 81 deletions

CHANGES.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
# Release notes
22

3+
## 2.5.0
4+
Jun 09, 2022
5+
6+
- Fix *GetHeader* method on **PSCouchDBRequest** class.
7+
- Fix **ConvertTo-CouchDBPassword** cmdlet.
8+
- Fix position of **PSCouchDBRequestException** class.
9+
- Fix check permission on **Revoke-CouchDBDatabasePermission** cmdlet.
10+
- Fix **Copy-CouchDBDatabase** cmdlet.
11+
- Fix check document info in all cmdlet.
12+
- Add *Node* parameter on **Get-CouchDBNode** cmdlet.
13+
- Add all parameters on **Get-CouchDBDatabaseChanges** cmdlet.
14+
- Add *tests* folder for [**Pester**](https://pester-docs.netlify.app/) testing.
15+
316
## 2.4.0
417
May 28, 2021
518

PSCouchDB/About_PSCouchDB.help.txt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ TOPIC
33
SHORT DESCRIPTION
44
Advanced CLI for CouchDB server
55
LONG DESCRIPTION
6-
This module is a coplete CLI for CouchDB server database.
6+
This module is a complete CLI for CouchDB server database.
77
With this you can:
88
- Configure server
9-
- Manage database (create, modify, delete)
10-
- Manage documents (create, modify, delete)
11-
- Manage design documents (create, modify, delete)
9+
- Manage database (create, read, update, delete)
10+
- Manage documents (create, read, update, delete)
11+
- Manage design documents (create, read, update, delete)
12+
- Manage permissions and authentication
1213
- Find documents in a database
1314
- Configure and manage replica
1415
- Configure and manage node of cluster

PSCouchDB/PSCouchDB.psd1

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
)
2929

3030
# Version number of this module.
31-
ModuleVersion = '2.4.2'
31+
ModuleVersion = '2.5.0'
3232

3333
# Supported PSEditions
3434
# CompatiblePSEditions = @()
@@ -119,6 +119,7 @@
119119
"Get-CouchDBRevisionDifference",
120120
"Get-CouchDBRevisionLimit",
121121
"Get-CouchDBSession",
122+
"Submit-CouchDBConfiguration",
122123
"Sync-CouchDBDatabaseShards",
123124
"Get-CouchDBReshards",
124125
"Set-CouchDBReshards",
@@ -211,6 +212,7 @@
211212
"gcconf",
212213
"gcnode",
213214
"scdbrs",
215+
"subconf",
214216
"gcrpl",
215217
"gcrpls",
216218
"gcrd",

PSCouchDB/PSCouchDB.psm1

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,36 @@
22
[bool] $Global:CouchDBCachePreference = $false
33
[bool] $Global:CouchDBSaveCredentialPreference = $true
44

5+
# CouchDB exception class
6+
class PSCouchDBRequestException : System.Exception {
7+
[string] $CouchDBMessage
8+
[int] $CouchDBStausCode
9+
10+
PSCouchDBRequestException([int]$statusCode, [string]$response) {
11+
$this.CouchDBStausCode = $statusCode
12+
switch ($this.CouchDBStausCode) {
13+
304 { $this.CouchDBMessage = "[$($this.CouchDBStausCode)] Not Modified: The additional content requested has not been modified." }
14+
400 { $this.CouchDBMessage = "[$($this.CouchDBStausCode)] Bad Request: Bad request structure. The error can indicate an error with the request URL, path or headers." }
15+
401 { $this.CouchDBMessage = "[$($this.CouchDBStausCode)] Unauthorized: The item requested was not available using the supplied authorization, or authorization was not supplied." }
16+
403 { $this.CouchDBMessage = "[$($this.CouchDBStausCode)] Forbidden: The requested item or operation is forbidden." }
17+
404 { $this.CouchDBMessage = "[$($this.CouchDBStausCode)] Not Found: The requested content could not be found." }
18+
405 { $this.CouchDBMessage = "[$($this.CouchDBStausCode)] Method Not Allowed: A request was made using an invalid HTTP request type for the URL requested." }
19+
406 { $this.CouchDBMessage = "[$($this.CouchDBStausCode)] Not Acceptable: The requested content type is not supported by the server." }
20+
409 { $this.CouchDBMessage = "[$($this.CouchDBStausCode)] Conflict: Request resulted in an update conflict." }
21+
412 { $this.CouchDBMessage = "[$($this.CouchDBStausCode)] Precondition Failed: The request headers from the client and the capabilities of the server do not match." }
22+
413 { $this.CouchDBMessage = "[$($this.CouchDBStausCode)] Request Entity Too Large: A document exceeds the configured couchdb/max_document_size value or the entire request exceeds the httpd/max_http_request_size value." }
23+
415 { $this.CouchDBMessage = "[$($this.CouchDBStausCode)] Unsupported Media Type: The content types supported, and the content type of the information being requested or submitted indicate that the content type is not supported." }
24+
416 { $this.CouchDBMessage = "[$($this.CouchDBStausCode)] Requested Range Not Satisfiable: The range specified in the request header cannot be satisfied by the server." }
25+
417 { $this.CouchDBMessage = "[$($this.CouchDBStausCode)] Expectation Failed: The bulk load operation failed." }
26+
{ $this.CouchDBStausCode -ge 500 } { $this.CouchDBMessage = "[$($this.CouchDBStausCode)] Internal Server Error: The request was invalid, either because the supplied JSON was invalid, or invalid information was supplied as part of the request." }
27+
Default { $this.CouchDBMessage = "[$($this.CouchDBStausCode)] Unknown Error: something wrong." }
28+
}
29+
if ($response) {
30+
$this.CouchDBMessage += "`nCouchDB Response -> $response"
31+
}
32+
}
33+
}
34+
535
# Native Powershell CouchDB class
636
class PSCouchDBDocument {
737
<#
@@ -1418,15 +1448,19 @@ class PSCouchDBRequest {
14181448
$this.client.Headers.Add("Authorization", ("Basic {0}" -f $base64AuthInfo))
14191449
}
14201450
try {
1421-
[System.Net.WebResponse] $resp = $this.client.GetResponse()
1451+
[System.Net.HttpWebResponse] $resp = $this.client.GetResponse()
14221452
$resp.Close()
14231453
$this.uri.LastStatusCode = $resp.StatusCode
14241454
} catch [System.Net.WebException] {
1455+
$resp = $false
14251456
[System.Net.HttpWebResponse] $errcode = $_.Exception.Response
14261457
$this.uri.LastStatusCode = $errcode.StatusCode
1427-
throw ([PSCouchDBRequestException]::New($errcode.StatusCode, $errcode.StatusDescription)).CouchDBMessage
14281458
}
1429-
return $resp.Headers.ToString()
1459+
if (Get-Member 'Headers' -InputObject $resp) {
1460+
return $resp.Headers
1461+
} else {
1462+
return $resp
1463+
}
14301464
}
14311465

14321466
SetProxy ([string]$uri) {
@@ -1587,37 +1621,6 @@ Uri: {4}
15871621
}
15881622
}
15891623

1590-
# CouchDB exception class
1591-
1592-
class PSCouchDBRequestException : System.Exception {
1593-
[string] $CouchDBMessage
1594-
[int] $CouchDBStausCode
1595-
1596-
PSCouchDBRequestException([int]$statusCode, [string]$response) {
1597-
$this.CouchDBStausCode = $statusCode
1598-
switch ($this.CouchDBStausCode) {
1599-
304 { $this.CouchDBMessage = "[$($this.CouchDBStausCode)] Not Modified: The additional content requested has not been modified." }
1600-
400 { $this.CouchDBMessage = "[$($this.CouchDBStausCode)] Bad Request: Bad request structure. The error can indicate an error with the request URL, path or headers." }
1601-
401 { $this.CouchDBMessage = "[$($this.CouchDBStausCode)] Unauthorized: The item requested was not available using the supplied authorization, or authorization was not supplied." }
1602-
403 { $this.CouchDBMessage = "[$($this.CouchDBStausCode)] Forbidden: The requested item or operation is forbidden." }
1603-
404 { $this.CouchDBMessage = "[$($this.CouchDBStausCode)] Not Found: The requested content could not be found." }
1604-
405 { $this.CouchDBMessage = "[$($this.CouchDBStausCode)] Method Not Allowed: A request was made using an invalid HTTP request type for the URL requested." }
1605-
406 { $this.CouchDBMessage = "[$($this.CouchDBStausCode)] Not Acceptable: The requested content type is not supported by the server." }
1606-
409 { $this.CouchDBMessage = "[$($this.CouchDBStausCode)] Conflict: Request resulted in an update conflict." }
1607-
412 { $this.CouchDBMessage = "[$($this.CouchDBStausCode)] Precondition Failed: The request headers from the client and the capabilities of the server do not match." }
1608-
413 { $this.CouchDBMessage = "[$($this.CouchDBStausCode)] Request Entity Too Large: A document exceeds the configured couchdb/max_document_size value or the entire request exceeds the httpd/max_http_request_size value." }
1609-
415 { $this.CouchDBMessage = "[$($this.CouchDBStausCode)] Unsupported Media Type: The content types supported, and the content type of the information being requested or submitted indicate that the content type is not supported." }
1610-
416 { $this.CouchDBMessage = "[$($this.CouchDBStausCode)] Requested Range Not Satisfiable: The range specified in the request header cannot be satisfied by the server." }
1611-
417 { $this.CouchDBMessage = "[$($this.CouchDBStausCode)] Expectation Failed: The bulk load operation failed." }
1612-
{ $this.CouchDBStausCode -ge 500 } { $this.CouchDBMessage = "[$($this.CouchDBStausCode)] Internal Server Error: The request was invalid, either because the supplied JSON was invalid, or invalid information was supplied as part of the request." }
1613-
Default { $this.CouchDBMessage = "[$($this.CouchDBStausCode)] Unknown Error: something wrong." }
1614-
}
1615-
if ($response) {
1616-
$this.CouchDBMessage += "`nCouchDB Response -> $response"
1617-
}
1618-
}
1619-
}
1620-
16211624
# Functions of CouchDB module
16221625

16231626
function Send-CouchDBRequest {

PSCouchDB/alias/CouchDBalias.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ New-Alias -Name "gcmr" -Value Get-CouchDBMissingRevision -Option ReadOnly
3131
New-Alias -Name "gcrd" -Value Get-CouchDBRevisionDifference -Option ReadOnly
3232
New-Alias -Name "gcrl" -Value Get-CouchDBRevisionLimit -Option ReadOnly
3333
New-Alias -Name "gcss" -Value Get-CouchDBSession -Option ReadOnly
34+
New-Alias -Name "subconf" -Value Submit-CouchDBConfiguration -Option ReadOnly
3435
New-Alias -Name "scds" -Value Sync-CouchDBDatabaseShards -Option ReadOnly
3536
New-Alias -Name "gcdbrs" -Value Get-CouchDBReshards -Option ReadOnly
3637
New-Alias -Name "scdbrs" -Value Set-CouchDBReshards -Option ReadOnly

PSCouchDB/functions/CouchDBconfiguration.ps1

Lines changed: 82 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,20 @@ function Get-CouchDBNode () {
134134
CouchDB API:
135135
GET /_membership
136136
GET /_node/{node-name}
137+
GET /_node/{node-name}/_versions
138+
GET /_node/{node-name}/_prometheus
137139
.PARAMETER Server
138140
The CouchDB server name. Default is localhost.
139141
.PARAMETER Port
140142
The CouchDB server port. Default is 5984.
143+
.PARAMETER Node
144+
Erlang node name of the server that processes the request. Default is _local.
145+
.PARAMETER Membership
146+
Displays the nodes that are part of the cluster as cluster_nodes.
147+
.PARAMETER Versions
148+
ICU driver and collator algorithm versions are returned.
149+
.PARAMETER Prometheus
150+
The _prometheus resource returns a text/plain response that consolidates our /_node/{node-name}/_stats, and /_node/{node-name}/_system endpoints. The format is determined by Prometheus.
141151
.PARAMETER Authorization
142152
The CouchDB authorization form; user and password.
143153
Authorization format like this: user:password
@@ -161,13 +171,25 @@ function Get-CouchDBNode () {
161171
[Parameter(ValueFromPipeline = $true)]
162172
[string] $Server,
163173
[int] $Port,
174+
[string] $Node = '_local',
175+
[switch] $Membership,
176+
[switch] $Versions,
177+
[switch] $Prometheus,
164178
$Authorization,
165179
[switch] $Ssl,
166180
[string] $ProxyServer,
167181
[pscredential] $ProxyCredential
168182
)
169-
$Database = "_node/_local"
170-
Send-CouchDBRequest -Server $Server -Port $Port -Method "GET" -Database $Database -Document $Document -Authorization $Authorization -Ssl:$Ssl -ProxyServer $ProxyServer -ProxyCredential $ProxyCredential
183+
if ($Membership.IsPresent) {
184+
$Database = "_membership"
185+
} elseif ($Versions.IsPresent) {
186+
$Database = "_node/$Node/_versions"
187+
} elseif ($Prometheus.IsPresent) {
188+
$Database = "_node/$Node/_prometheus"
189+
} else {
190+
$Database = "_node/$Node"
191+
}
192+
Send-CouchDBRequest -Server $Server -Port $Port -Method "GET" -Database $Database -Authorization $Authorization -Ssl:$Ssl -ProxyServer $ProxyServer -ProxyCredential $ProxyCredential
171193
}
172194

173195
function Add-CouchDBNode () {
@@ -239,7 +261,7 @@ function Remove-CouchDBNode () {
239261
Remove server nodes on CouchDB server.
240262
.NOTES
241263
CouchDB API:
242-
DELETE /_node/{node-name}
264+
DELETE /_node/_local/_nodes/{node-name}
243265
.PARAMETER Server
244266
The CouchDB server name. Default is localhost.
245267
.PARAMETER Port
@@ -272,27 +294,22 @@ function Remove-CouchDBNode () {
272294
[int] $Port,
273295
[Parameter(mandatory = $true, ValueFromPipeline = $true)]
274296
[string] $Node,
297+
[string] $Revision,
275298
$Authorization,
276299
[switch]$Force,
277300
[switch] $Ssl,
278301
[string] $ProxyServer,
279302
[pscredential] $ProxyCredential
280303
)
281-
$Database = "_nodes/_local"
304+
$Database = "_node/_local/_nodes"
282305
# Check node
283-
if ((Get-CouchDBClusterSetup -Authorization $Authorization -ProxyServer $ProxyServer -ProxyCredential $ProxyCredential).state -eq 'single_node_enabled') {
284-
$clu_property = 'name'
285-
} else {
286-
$clu_property = 'all_nodes'
306+
if ((Get-CouchDBNode -Authorization "admin:password" -Membership -ProxyServer $ProxyServer -ProxyCredential $ProxyCredential -ErrorAction SilentlyContinue).cluster_nodes -contains $Node) {
307+
if (-not($Revision)) {
308+
$Revision = (Get-CouchDBDocument -Server $Server -Port $Port -Database $Database -Document $Node -Authorization $Authorization -Ssl:$Ssl -ProxyServer $ProxyServer -ProxyCredential $ProxyCredential)._rev
309+
}
287310
}
288-
if ((Get-CouchDBNode -Authorization "admin:password" -ProxyServer $ProxyServer -ProxyCredential $ProxyCredential -ErrorAction SilentlyContinue).$clu_property -contains $Node) {
289-
$Revision = (Get-CouchDBDocument -Server $Server -Port $Port -Database $Database -Document $Node -Authorization $Authorization -Ssl:$Ssl -ProxyServer $ProxyServer -ProxyCredential $ProxyCredential)._rev
290-
} else {
291-
throw "Node $Node not exist."
292-
}
293-
$Document = $Node
294311
if ($Force -or $PSCmdlet.ShouldContinue("Do you wish remove node $Node ?", "Remove $Node")) {
295-
Send-CouchDBRequest -Server $Server -Port $Port -Method "DELETE" -Database $Database -Document $Document -Revision $Revision -Authorization $Authorization -Ssl:$Ssl -ProxyServer $ProxyServer -ProxyCredential $ProxyCredential
312+
Send-CouchDBRequest -Server $Server -Port $Port -Method "DELETE" -Database $Database -Document $Node -Revision $Revision -Authorization $Authorization -Ssl:$Ssl -ProxyServer $ProxyServer -ProxyCredential $ProxyCredential
296313
}
297314
}
298315

@@ -428,4 +445,54 @@ function Set-CouchDBConfiguration () {
428445
}
429446
$Data = $Value | ConvertTo-Json
430447
Send-CouchDBRequest -Server $Server -Port $Port -Method "PUT" -Database $Database -Document $Document -Data $Data -Authorization $Authorization -Ssl:$Ssl -ProxyServer $ProxyServer -ProxyCredential $ProxyCredential
448+
}
449+
450+
function Submit-CouchDBConfiguration () {
451+
<#
452+
.SYNOPSIS
453+
Reloads the configuration from disk.
454+
.DESCRIPTION
455+
This has a side effect of flushing any in-memory configuration changes that have not been committed to disk.
456+
.NOTES
457+
CouchDB API:
458+
POST /_node/{node-name}/_config/_reload
459+
.PARAMETER Server
460+
The CouchDB server name. Default is localhost.
461+
.PARAMETER Port
462+
The CouchDB server port. Default is 5984.
463+
.PARAMETER Node
464+
The CouchDB node of cluster. Default is couchdb@localhost.
465+
.PARAMETER Authorization
466+
The CouchDB authorization form; user and password.
467+
Authorization format like this: user:password
468+
ATTENTION: if the password is not specified, it will be prompted.
469+
.PARAMETER Ssl
470+
Set ssl connection on CouchDB server.
471+
This modify protocol to https and port to 6984.
472+
.PARAMETER ProxyServer
473+
Proxy server through which all non-local calls pass.
474+
Ex. ... -ProxyServer 'http://myproxy.local:8080' ...
475+
.PARAMETER ProxyCredential
476+
Proxy server credential. It must be specified with a PSCredential object.
477+
.EXAMPLE
478+
Submit-CouchDBConfiguration -Authorization "admin:password"
479+
Reloads the configuration from disk.
480+
.LINK
481+
https://pscouchdb.readthedocs.io/en/latest/config.html#reload-configuration
482+
#>
483+
[CmdletBinding()]
484+
param(
485+
[Parameter(ValueFromPipeline = $true)]
486+
[string] $Server,
487+
[int] $Port,
488+
[Parameter(Position = 0)]
489+
[string] $Node = $(if ((Get-CouchDBNode -Server $Server -Port $Port -Authorization $Authorization -Ssl:$Ssl -ProxyServer $ProxyServer -ProxyCredential $ProxyCredential).name -contains "couchdb@localhost") { "couchdb@localhost" } else { "couchdb@127.0.0.1" }),
490+
[string] $Value,
491+
$Authorization,
492+
[switch] $Ssl,
493+
[string] $ProxyServer,
494+
[pscredential] $ProxyCredential
495+
)
496+
$Database = "/_node/$Node/_config/_reload"
497+
Send-CouchDBRequest -Server $Server -Port $Port -Method "POST" -Database $Database -Authorization $Authorization -Ssl:$Ssl -ProxyServer $ProxyServer -ProxyCredential $ProxyCredential
431498
}

PSCouchDB/functions/CouchDBdatabase.ps1

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ function Copy-CouchDBDatabase () {
141141
} else {
142142
$DestinationAuthorization = $Authorization
143143
}
144-
if (-not(Test-CouchDBDatabase -Server $DestinationServer -Port $DestinationPort -Database $Destination -Authorization $DestinationAuthorization -Ssl:$Ssl -ProxyServer $ProxyServer -ProxyCredential $ProxyCredential -ErrorAction SilentlyContinue)) {
144+
if ($null -ne (Test-CouchDBDatabase -Server $DestinationServer -Port $DestinationPort -Database $Destination -Authorization $DestinationAuthorization -Ssl:$Ssl -ProxyServer $ProxyServer -ProxyCredential $ProxyCredential)) {
145145
New-CouchDBDatabase -Server $DestinationServer -Port $DestinationPort -Database $Destination -Authorization $DestinationAuthorization -Ssl:$Ssl -ProxyServer $ProxyServer -ProxyCredential $ProxyCredential | Out-Null
146146
} else {
147147
throw "Database $Destination exists! Choose another name."
@@ -559,7 +559,7 @@ function Remove-CouchDBIndex () {
559559
Proxy server credential. It must be specified with a PSCredential object.
560560
.EXAMPLE
561561
$ddoc = Get-CouchDBIndex -Database test -Authorization "admin:password"
562-
Remove-CouchDBIndex -Database test -DesignDoc $ddoc.indexes.ddoc[1] -Name $ddoc.indexes.name[1] -Authorization "admin:password"
562+
Remove-CouchDBIndex -Database test -DesignDoc $ddoc.indexes[1].ddoc -Name $ddoc.indexes[1].name -Authorization "admin:password"
563563
The example removes an index document on database "test".
564564
.LINK
565565
https://pscouchdb.readthedocs.io/en/latest/databases.html#remove-a-index
@@ -1111,7 +1111,7 @@ function Get-CouchDBRevisionDifference () {
11111111
function Get-CouchDBRevisionLimit () {
11121112
<#
11131113
.SYNOPSIS
1114-
Get revision's limit.
1114+
Get revisions limit.
11151115
.DESCRIPTION
11161116
Gets the current revs_limit (revision limit) setting.
11171117
.NOTES
@@ -1185,7 +1185,7 @@ function Set-CouchDBRevisionLimit () {
11851185
.PARAMETER ProxyCredential
11861186
Proxy server credential. It must be specified with a PSCredential object.
11871187
.EXAMPLE
1188-
Set-CouchDBRevisionLimit -Database test -Limit 100 -Authorization "admin:password"
1188+
Set-CouchDBRevisionLimit -Database test -Limit 1000 -Authorization "admin:password"
11891189
This example set revision limit number to 1000 on database "test".
11901190
.LINK
11911191
https://pscouchdb.readthedocs.io/en/latest/databases.html#set-revision-limit

0 commit comments

Comments
 (0)