Skip to content

Commit

Permalink
Add cmdlet Get-CouchDBDatabaseChanges,Get-CouchDBReplication,Get-Couc…
Browse files Browse the repository at this point in the history
…hDBReplicationScheduler,Set-CouchDBReplication,New-CouchDBReplication,Remove-CouchDBReplication. Fix other cmdlet. Update README.
  • Loading branch information
Matteo Guadrini committed Apr 16, 2018
1 parent b7a9037 commit c3ad4aa
Show file tree
Hide file tree
Showing 3 changed files with 202 additions and 14 deletions.
14 changes: 10 additions & 4 deletions PSCouchDB/PSCouchDB.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
RootModule = 'PSCouchDB.psm1'

# Version number of this module.
ModuleVersion = '1.2.0'
ModuleVersion = '1.3.0'

# Supported PSEditions
# CompatiblePSEditions = @()
Expand All @@ -30,7 +30,7 @@ Author = 'Matteo Guadrini <m.guadrini@gmail.com>'
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 = ''
Expand Down Expand Up @@ -70,31 +70,37 @@ 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",
"New-CouchDBDocument",
"New-CouchDBAttachment",
"New-CouchDBUser",
"New-CouchDBAdmin",
"New-CouchDBReplication",
"Remove-CouchDBDatabase",
"Remove-CouchDBDocument",
"Remove-CouchDBAttachment",
"Remove-CouchDBUser",
"Remove-CouchDBAdmin",
"Remove-CouchDBNode",
"Remove-CouchDBReplication",
"Find-CouchDBDocuments"
)

Expand Down Expand Up @@ -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 = ''
Expand Down
192 changes: 184 additions & 8 deletions PSCouchDB/PSCouchDB.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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") }
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 () {
Expand All @@ -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 () {
Expand All @@ -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 () {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
}

Expand Down Expand Up @@ -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
}
}

Expand All @@ -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,
Expand Down
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)
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)

0 comments on commit c3ad4aa

Please sign in to comment.