Skip to content

Commit

Permalink
Merge branch 'v2dev'
Browse files Browse the repository at this point in the history
Fix RequestAsJob method for Linux machine.
Add SetDocumentId method on PSCouchDBDocument class.
Add SetSelector override method on PSCouchDBReplication class.
Rewrite Import-CouchDBDatabase cmdlet.
Add Search-CouchDBAnalyze, Get-CouchDBReshards, Set-CouchDBReshards, Remove-CouchDBReshards cmdlet.
Add CouchDB response to PSCouchDBRequestException class.
  • Loading branch information
MatteoGuadrini committed Sep 19, 2020
2 parents 30d3070 + 1584e4b commit 41575f2
Show file tree
Hide file tree
Showing 19 changed files with 553 additions and 50 deletions.
14 changes: 11 additions & 3 deletions PSCouchDB/PSCouchDB.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
)

# Version number of this module.
ModuleVersion = '2.1.0'
ModuleVersion = '2.2.0'

# Supported PSEditions
# CompatiblePSEditions = @()
Expand Down Expand Up @@ -120,6 +120,9 @@
"Get-CouchDBRevisionLimit",
"Get-CouchDBSession",
"Sync-CouchDBDatabaseShards",
"Get-CouchDBReshards",
"Set-CouchDBReshards",
"Remove-CouchDBReshards",
"Copy-CouchDBDocument",
"Measure-CouchDBStatistics",
"Enable-CouchDBCluster",
Expand Down Expand Up @@ -173,7 +176,8 @@
"Export-CouchDBDatabase",
"Import-CouchDBDatabase",
"Read-CouchDBLog",
"Clear-CouchDBLog"
"Clear-CouchDBLog",
"Search-CouchDBAnalyze"
)

# Cmdlet s 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 cmdlets to export.
Expand Down Expand Up @@ -205,9 +209,11 @@
"gcadm",
"gcconf",
"gcnode",
"scdbrs",
"gcrpl",
"gcrpls",
"gcrd",
"gcdbrs",
"gcrpdoc",
"gctsk",
"gccs",
Expand Down Expand Up @@ -259,6 +265,7 @@
"rcatt",
"rcusr",
"rcadm",
"rcdbrs",
"rcnode",
"rcrpl",
"rcidx",
Expand All @@ -282,7 +289,8 @@
"rmdb",
"rmdoc",
"rmuser",
"rmadmin"
"rmadmin",
"cdsa"
)

# DSC resources to export from this module
Expand Down
31 changes: 22 additions & 9 deletions PSCouchDB/PSCouchDB.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ class PSCouchDBDocument {
return $this.doc
}

SetDocumentId ([string]$id) {
$this.doc['_id'] = $id
$this._id = $id
}

SetElement ([string]$key) {
# Check key isn't _id
if (-not($key -eq "_id")) {
Expand Down Expand Up @@ -1179,6 +1184,11 @@ class PSCouchDBReplication {
$this.replicator.Add('selector', $this.selector)
}

SetSelector ([PSCouchDBQuery]$selector) {
$this.selector = $selector.GetNativeQuery()
$this.replicator.Add('selector', $this.selector)
}

SetSinceSequence ([string]$sequence) {
$this.since_seq = $sequence
$this.replicator.Add('since_seq', $this.since_seq)
Expand Down Expand Up @@ -1296,7 +1306,7 @@ class PSCouchDBRequest {
} catch [System.Net.WebException] {
[System.Net.HttpWebResponse] $errcode = $_.Exception.Response
$this.uri.LastStatusCode = $errcode.StatusCode
throw ([PSCouchDBRequestException]::new($errcode.StatusCode)).CouchDBMessage
throw ([PSCouchDBRequestException]::New($errcode.StatusCode, $errcode.StatusDescription)).CouchDBMessage
}
$rs = $resp.GetResponseStream()
[System.IO.StreamReader] $sr = New-Object System.IO.StreamReader -ArgumentList $rs
Expand All @@ -1316,7 +1326,7 @@ class PSCouchDBRequest {

RequestAsJob ([string]$name) {
$job = Start-Job -Name $name {
param($uri, $method, $authorization, $data, $attachment)
param($uri, $method, $user, $pass, $data, $attachment)
# Create web client
$Request = [System.Net.WebRequest]::Create($uri)
$Request.ContentType = "application/json; charset=utf-8";
Expand All @@ -1327,8 +1337,8 @@ class PSCouchDBRequest {
$this.client.Proxy = $this.proxy
}
# Check authorization
if ($authorization) {
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("$($authorization.UserName):$($authorization.GetNetworkCredential().Password)")))
if ($user -and $pass) {
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("${user}:${pass}")))
$Request.Headers.Add("Authorization", ("Basic {0}" -f $base64AuthInfo))
}
# Check data
Expand All @@ -1350,18 +1360,18 @@ class PSCouchDBRequest {
[System.Net.WebResponse] $resp = $Request.GetResponse()
} catch [System.Net.WebException] {
[System.Net.HttpWebResponse] $errcode = $_.Exception.Response
throw "[$($errcode.StatusCode)] Error."
throw "[$($errcode.StatusCode)] Error.`nCouchDB Response -> $($errcode.StatusDescription)"
}
$rs = $resp.GetResponseStream()
[System.IO.StreamReader] $sr = New-Object System.IO.StreamReader -ArgumentList $rs
[string] $results = $sr.ReadToEnd()
$resp.Close()
if ($results -match "^{.*}$") {
if ($results -match "^({.*)|(\[.*)$") {
return $results | ConvertFrom-Json
} else {
return [PSCustomObject]@{ results = $results }
}
} -ArgumentList $this.uri.Uri, $this.method, $this.authorization, $this.data, $this.attachment
} -ArgumentList $this.uri.Uri, $this.method, $(if ($this.authorization) {$this.authorization.UserName} else {$null}), $(if ($this.authorization) {$this.authorization.GetNetworkCredential().Password} else {$null}), $this.data, $this.attachment
Register-TemporaryEvent $job "StateChanged" -Action {
Write-Host -ForegroundColor Green "#$($sender.Id) ($($sender.Name)) complete."
}
Expand All @@ -1388,7 +1398,7 @@ class PSCouchDBRequest {
} catch [System.Net.WebException] {
[System.Net.HttpWebResponse] $errcode = $_.Exception.Response
$this.uri.LastStatusCode = $errcode.StatusCode
throw ([PSCouchDBRequestException]::new($errcode.StatusCode)).CouchDBMessage
throw ([PSCouchDBRequestException]::New($errcode.StatusCode, $errcode.StatusDescription)).CouchDBMessage
}
return $resp.Headers.ToString()
}
Expand Down Expand Up @@ -1543,7 +1553,7 @@ class PSCouchDBRequestException : System.Exception {
[string] $CouchDBMessage
[int] $CouchDBStausCode

PSCouchDBRequestException([int]$statusCode) {
PSCouchDBRequestException([int]$statusCode, [string]$response) {
$this.CouchDBStausCode = $statusCode
switch ($this.CouchDBStausCode) {
304 { $this.CouchDBMessage = "[$($this.CouchDBStausCode)] Not Modified: The additional content requested has not been modified." }
Expand All @@ -1562,6 +1572,9 @@ class PSCouchDBRequestException : System.Exception {
{ $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." }
Default { $this.CouchDBMessage = "[$($this.CouchDBStausCode)] Unknown Error: something wrong." }
}
if ($response) {
$this.CouchDBMessage += "`nCouchDB Response -> $response"
}
}
}

Expand Down
4 changes: 4 additions & 0 deletions PSCouchDB/alias/CouchDBalias.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ New-Alias -Name "gcrd" -Value Get-CouchDBRevisionDifference -Option ReadOnly
New-Alias -Name "gcrl" -Value Get-CouchDBRevisionLimit -Option ReadOnly
New-Alias -Name "gcss" -Value Get-CouchDBSession -Option ReadOnly
New-Alias -Name "scds" -Value Sync-CouchDBDatabaseShards -Option ReadOnly
New-Alias -Name "gcdbrs" -Value Get-CouchDBReshards -Option ReadOnly
New-Alias -Name "scdbrs" -Value Set-CouchDBReshards -Option ReadOnly
New-Alias -Name "rcdbrs" -Value Remove-CouchDBReshards -Option ReadOnly
New-Alias -Name "cpdoc" -Value Copy-CouchDBDocument -Option ReadOnly
New-Alias -Name "mcsts" -Value Measure-CouchDBStatistics -Option ReadOnly
New-Alias -Name "eccl" -Value Enable-CouchDBCluster -Option ReadOnly
Expand Down Expand Up @@ -87,6 +90,7 @@ New-Alias -Name "ecdb" -Value Export-CouchDBDatabase -Option ReadOnly
New-Alias -Name "icdb" -Value Import-CouchDBDatabase -Option ReadOnly
New-Alias -Name "rdblog" -Value Read-CouchDBLog -Option ReadOnly
New-Alias -Name "cdblog" -Value Clear-CouchDBLog -Option ReadOnly
New-Alias -Name "cdsa" -Value Search-CouchDBAnalyze -Option ReadOnly

# Intuitive alias
New-Alias -Name "mkdb" -Value New-CouchDBDatabase -Option ReadOnly
Expand Down
46 changes: 15 additions & 31 deletions PSCouchDB/functions/CouchDBdatabase.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,7 @@ function Get-CouchDBDatabaseShards () {
[pscredential] $ProxyCredential
)
$Database = $Database + '/_shards'
Send-CouchDBRequest -Server $Server -Port $Port -Method "GET" -Database $Database -Document $Document -Authorization $Authorization -Ssl:$Ssl-ProxyServer $ProxyServer -ProxyCredential $ProxyCredential
Send-CouchDBRequest -Server $Server -Port $Port -Method "GET" -Database $Database -Document $Document -Authorization $Authorization -Ssl:$Ssl -ProxyServer $ProxyServer -ProxyCredential $ProxyCredential
}

function Sync-CouchDBDatabaseShards () {
Expand Down Expand Up @@ -1348,45 +1348,29 @@ function Import-CouchDBDatabase () {
)
# Check RemoveRevision parameter
if ($RemoveRevision.IsPresent) {
$_docs = $(Get-Content -Path $Path)
$docs = @()
foreach ($doc in $_docs) {
$doc = $doc -replace '"_rev":.*,', ""
$docs += $doc
}
$docs = $(Get-Content -Path $Path) -replace '"_rev":.*,', "" | Out-String | ConvertFrom-Json
} else {
# Create container "docs"
$docs = $(Get-Content -Path $Path)
$docs = $(Get-Content -Path $Path) | Out-String | ConvertFrom-Json
}
# Check if database exists
if (-not(Test-CouchDBDatabase -Server $Server -Port $Port -Database $Database -Authorization $Authorization -Ssl:$Ssl -ProxyServer $ProxyServer -ProxyCredential $ProxyCredential -ErrorAction SilentlyContinue)) {
New-CouchDBDatabase -Server $Server -Port $Port -Database $Database -Authorization $Authorization -Ssl:$Ssl -ProxyServer $ProxyServer -ProxyCredential $ProxyCredential | Out-Null
}
if ($AsJob.IsPresent) {
$job = Start-Job -Name "Import-Database" {
param($Server, $Port, $Method, $Database, $Document, $Data, $Authorization, $Ssl, $docs, $ProxyServer, [pscredential]$ProxyCredential)
[string] $Document = "_bulk_docs"
if ($docs) {
$Data = "{ `"docs`" : $(($docs | ConvertFrom-Json) | ConvertTo-Json -Depth 99)}"
} else {
Write-Warning -Message "File is empty!"
return $null
}
Send-CouchDBRequest -Server $Server -Port $Port -Method "POST" -Database $Database -Document $Document -Data $Data -Authorization $Authorization -Ssl:$Ssl -ProxyServer $ProxyServer -ProxyCredential $ProxyCredential
} -ArgumentList $Server, $Port, $Method, $Database, $Document, $Data, $Authorization, $Ssl, $docs, $ProxyServer, $ProxyCredential
Register-TemporaryEvent $job "StateChanged" -Action {
Write-Host -ForegroundColor Green "Import database #$($sender.Id) ($($sender.Name)) complete."
# Import data in bulk
if ($docs) {
$Data = New-Object PSCouchDBBulkDocument
foreach ($doc in $docs) {
$Data.AddDocument(($doc | ConvertTo-Json -Depth 99))
}
} else {
# Import data in bulk
[string] $Document = "_bulk_docs"
if ($docs) {
$Data = "{ `"docs`" : $(($docs | ConvertFrom-Json) | ConvertTo-Json -Depth 99)}"
} else {
Write-Warning -Message "File is empty!"
return $null
}
Send-CouchDBRequest -Server $Server -Port $Port -Method "POST" -Database $Database -Document $Document -Data $Data -Authorization $Authorization -Ssl:$Ssl -ProxyServer $ProxyServer -ProxyCredential $ProxyCredential
Write-Warning -Message "File is empty!"
return $null
}
if ($AsJob.IsPresent) {
New-CouchDBBulkDocument -Server $Server -Port $Port -Database $Database -Data $Data -Authorization $Authorization -Ssl:$Ssl -ProxyServer $ProxyServer -ProxyCredential $ProxyCredential -AsJob
} else {
New-CouchDBBulkDocument -Server $Server -Port $Port -Database $Database -Data $Data -Authorization $Authorization -Ssl:$Ssl -ProxyServer $ProxyServer -ProxyCredential $ProxyCredential
}
}

Expand Down

0 comments on commit 41575f2

Please sign in to comment.