Skip to content

Commit

Permalink
Added Credentials param to all cmdlets
Browse files Browse the repository at this point in the history
  • Loading branch information
mariuszwojcik committed Mar 22, 2014
1 parent ac4d433 commit e50d41e
Show file tree
Hide file tree
Showing 26 changed files with 340 additions and 220 deletions.
26 changes: 14 additions & 12 deletions AddExchange.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
#>
function Add-RabbitMQExchange
{
[CmdletBinding(SupportsShouldProcess=$true, ConfirmImpact="Medium")]
[CmdletBinding(DefaultParameterSetName='defaultLogin', SupportsShouldProcess=$true, ConfirmImpact="Medium")]
Param
(
# Name of RabbitMQ Exchange.
Expand Down Expand Up @@ -99,21 +99,23 @@ function Add-RabbitMQExchange
[Alias("HostName", "hn", "cn")]
[string]$ComputerName = $defaultComputerName,

# UserName to use when logging to RabbitMq server. Default value is guest.
[string]$UserName = $defaultUserName,

# UserName to use when logging to RabbitMq server.
[Parameter(Mandatory=$true, ParameterSetName='login')]
[string]$UserName,

# Password to use when logging to RabbitMq server.
[Parameter(Mandatory=$true, ParameterSetName='login')]
[string]$Password,

# Password to use when logging to RabbitMq server. Default value is guest.
[string]$Password = $defaultPassword
# Credentials to use when logging to RabbitMQ server.
[Parameter(Mandatory=$true, ParameterSetName='cred')]
[PSCredential]$Credentials
)

Begin
{
Add-Type -AssemblyName System.Web
Add-Type -AssemblyName System.Net

$cred = GetRabbitMqCredentials $UserName $Password
$cnt = 0

$Credentials = NormaliseCredentials
}
Process
{
Expand All @@ -135,7 +137,7 @@ function Add-RabbitMQExchange
$url = "http://$([System.Web.HttpUtility]::UrlEncode($ComputerName)):15672/api/exchanges/$([System.Web.HttpUtility]::UrlEncode($VirtualHost))/$([System.Web.HttpUtility]::UrlEncode($n))"
Write-Verbose "Invoking REST API: $url"

$result = Invoke-RestMethod $url -Credential $cred -AllowEscapedDotsAndSlashes -DisableKeepAlive -ErrorAction Continue -Method Put -ContentType "application/json" -Body $bodyJson
$result = Invoke-RestMethod $url -Credential $Credentials -AllowEscapedDotsAndSlashes -DisableKeepAlive -ErrorAction Continue -Method Put -ContentType "application/json" -Body $bodyJson

Write-Verbose "Created Exchange $n on server $ComputerName, Virtual Host $VirtualHost"
$cnt++
Expand Down
23 changes: 14 additions & 9 deletions AddMessage.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
#>
function Add-RabbitMQMessage
{
[CmdletBinding(SupportsShouldProcess=$true, ConfirmImpact='None')]
[CmdletBinding(DefaultParameterSetName='defaultLogin', SupportsShouldProcess=$true, ConfirmImpact='None')]
Param
(
# Name of the virtual host to filter channels by.
Expand Down Expand Up @@ -67,16 +67,23 @@ function Add-RabbitMQMessage
[Alias("HostName", "hn", "cn")]
[string]$ComputerName = $defaultComputerName,

# UserName to use when logging to RabbitMq server. Default value is guest.
[string]$UserName = $defaultUserName,

# UserName to use when logging to RabbitMq server.
[Parameter(Mandatory=$true, ParameterSetName='login')]
[string]$UserName,

# Password to use when logging to RabbitMq server.
[Parameter(Mandatory=$true, ParameterSetName='login')]
[string]$Password,

# Password to use when logging to RabbitMq server. Default value is guest.
[string]$Password = $defaultPassword
# Credentials to use when logging to RabbitMQ server.
[Parameter(Mandatory=$true, ParameterSetName='cred')]
[PSCredential]$Credentials
)

Begin
{
$cred = GetRabbitMqCredentials $UserName $Password
$Credentials = NormaliseCredentials

if ($Properties -eq $null) { $Properties = @{} }
}
Expand All @@ -101,7 +108,7 @@ function Add-RabbitMQMessage

while ($retryCounter -lt 3)
{
$result = Invoke-RestMethod $url -Credential $cred -AllowEscapedDotsAndSlashes -DisableKeepAlive -ErrorAction Continue -Method Post -ContentType "application/json" -Body $bodyJson
$result = Invoke-RestMethod $url -Credential $Credentials -AllowEscapedDotsAndSlashes -DisableKeepAlive -ErrorAction Continue -Method Post -ContentType "application/json" -Body $bodyJson

if ($result.routed -ne $true)
{
Expand All @@ -113,8 +120,6 @@ function Add-RabbitMQMessage
break
}
}


}
}
End
Expand Down
24 changes: 14 additions & 10 deletions AddQueue.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
#>
function Add-RabbitMQQueue
{
[CmdletBinding(SupportsShouldProcess=$true, ConfirmImpact="Low")]
[CmdletBinding(DefaultParameterSetName='defaultLogin', SupportsShouldProcess=$true, ConfirmImpact="Low")]
Param
(
# Name of RabbitMQ Queue.
Expand All @@ -79,19 +79,23 @@ function Add-RabbitMQQueue
[Alias("HostName", "hn", "cn")]
[string]$ComputerName = $defaultComputerName,

# UserName to use when logging to RabbitMq server. Default value is guest.
[string]$UserName = $defaultUserName,

# UserName to use when logging to RabbitMq server.
[Parameter(Mandatory=$true, ParameterSetName='login')]
[string]$UserName,

# Password to use when logging to RabbitMq server.
[Parameter(Mandatory=$true, ParameterSetName='login')]
[string]$Password,

# Password to use when logging to RabbitMq server. Default value is guest.
[string]$Password = $defaultPassword
# Credentials to use when logging to RabbitMQ server.
[Parameter(Mandatory=$true, ParameterSetName='cred')]
[PSCredential]$Credentials
)

Begin
{
Add-Type -AssemblyName System.Web
Add-Type -AssemblyName System.Net

$cred = GetRabbitMqCredentials $UserName $Password
$Credentials = NormaliseCredentials
$cnt = 0
}
Process
Expand All @@ -108,7 +112,7 @@ function Add-RabbitMQQueue
if ($AutoDelete) { $body.Add("auto_delete", $true) }

$bodyJson = $body | ConvertTo-Json
$result = Invoke-RestMethod $url -Credential $cred -AllowEscapedDotsAndSlashes -DisableKeepAlive -ErrorAction Continue -Method Put -ContentType "application/json" -Body $bodyJson
$result = Invoke-RestMethod $url -Credential $Credentials -AllowEscapedDotsAndSlashes -DisableKeepAlive -ErrorAction Continue -Method Put -ContentType "application/json" -Body $bodyJson

Write-Verbose "Created Queue $n on $ComputerName/$VirtualHost"
$cnt++
Expand Down
24 changes: 14 additions & 10 deletions AddQueueBinding.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#>
function Add-RabbitMQQueueBinding
{
[CmdletBinding(SupportsShouldProcess=$true, ConfirmImpact="Low")]
[CmdletBinding(DefaultParameterSetName='defaultLogin', SupportsShouldProcess=$true, ConfirmImpact="Low")]
Param
(
# Name of the virtual host.
Expand Down Expand Up @@ -56,19 +56,23 @@ function Add-RabbitMQQueueBinding
[Alias("HostName", "hn", "cn")]
[string]$ComputerName = $defaultComputerName,

# UserName to use when logging to RabbitMq server. Default value is guest.
[string]$UserName = $defaultUserName,

# UserName to use when logging to RabbitMq server.
[Parameter(Mandatory=$true, ParameterSetName='login')]
[string]$UserName,

# Password to use when logging to RabbitMq server.
[Parameter(Mandatory=$true, ParameterSetName='login')]
[string]$Password,

# Password to use when logging to RabbitMq server. Default value is guest.
[string]$Password = $defaultPassword
# Credentials to use when logging to RabbitMQ server.
[Parameter(Mandatory=$true, ParameterSetName='cred')]
[PSCredential]$Credentials
)

Begin
{
Add-Type -AssemblyName System.Web
Add-Type -AssemblyName System.Net

$cred = GetRabbitMqCredentials $UserName $Password
$Credentials = NormaliseCredentials
$cnt = 0
}
Process
Expand All @@ -85,7 +89,7 @@ function Add-RabbitMQQueueBinding
}

$bodyJson = $body | ConvertTo-Json
$result = Invoke-RestMethod $url -Credential $cred -AllowEscapedDotsAndSlashes -DisableKeepAlive -ErrorAction Continue -Method Post -ContentType "application/json" -Body $bodyJson
$result = Invoke-RestMethod $url -Credential $Credentials -AllowEscapedDotsAndSlashes -DisableKeepAlive -ErrorAction Continue -Method Post -ContentType "application/json" -Body $bodyJson

Write-Verbose "Bound exchange $ExchangeName to queue $Name $n on $ComputerName/$VirtualHost"
$cnt++
Expand Down
24 changes: 14 additions & 10 deletions AddVirtualHost.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
#>
function Add-RabbitMQVirtualHost
{
[CmdletBinding(SupportsShouldProcess=$true, ConfirmImpact="Low")]
[CmdletBinding(DefaultParameterSetName='defaultLogin', SupportsShouldProcess=$true, ConfirmImpact="Low")]
Param
(
# Name of RabbitMQ Virtual Host.
Expand All @@ -68,19 +68,23 @@ function Add-RabbitMQVirtualHost
[Alias("HostName", "hn", "cn")]
[string]$ComputerName = $defaultComputerName,

# UserName to use when logging to RabbitMq server. Default value is guest.
[string]$UserName = $defaultUserName,

# UserName to use when logging to RabbitMq server.
[Parameter(Mandatory=$true, ParameterSetName='login')]
[string]$UserName,

# Password to use when logging to RabbitMq server.
[Parameter(Mandatory=$true, ParameterSetName='login')]
[string]$Password,

# Password to use when logging to RabbitMq server. Default value is guest.
[string]$Password = $defaultPassword
# Credentials to use when logging to RabbitMQ server.
[Parameter(Mandatory=$true, ParameterSetName='cred')]
[PSCredential]$Credentials
)

Begin
{
Add-Type -AssemblyName System.Web
Add-Type -AssemblyName System.Net

$cred = GetRabbitMqCredentials $UserName $Password
$Credentials = NormaliseCredentials
$cnt = 0
}
Process
Expand All @@ -98,7 +102,7 @@ function Add-RabbitMQVirtualHost
foreach($n in $Name)
{
$url = "http://$([System.Web.HttpUtility]::UrlEncode($ComputerName)):15672/api/vhosts/$([System.Web.HttpUtility]::UrlEncode($n))"
$result = Invoke-RestMethod $url -Credential $cred -AllowEscapedDotsAndSlashes -DisableKeepAlive -ErrorAction Continue -Method Put -ContentType "application/json"
$result = Invoke-RestMethod $url -Credential $Credentials -AllowEscapedDotsAndSlashes -DisableKeepAlive -ErrorAction Continue -Method Put -ContentType "application/json"

Write-Verbose "Created Virtual Host $n on server $ComputerName"
$cnt++
Expand Down
21 changes: 14 additions & 7 deletions ClearQueue.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#>
function Clear-RabbitMQQueue
{
[CmdletBinding(SupportsShouldProcess=$true, ConfirmImpact="High")]
[CmdletBinding(DefaultParameterSetName='defaultLogin', SupportsShouldProcess=$true, ConfirmImpact="High")]
Param
(
# Name of RabbitMQ Virtual Host.
Expand All @@ -46,16 +46,23 @@ function Clear-RabbitMQQueue
[Alias("cn", "HostName")]
[string]$ComputerName = $defaultComputerName,

# UserName to use when logging to RabbitMq server. Default value is guest.
[string]$UserName = $defaultUserName,

# UserName to use when logging to RabbitMq server.
[Parameter(Mandatory=$true, ParameterSetName='login')]
[string]$UserName,

# Password to use when logging to RabbitMq server.
[Parameter(Mandatory=$true, ParameterSetName='login')]
[string]$Password,

# Password to use when logging to RabbitMq server. Default value is guest.
[string]$Password = $defaultPassword
# Credentials to use when logging to RabbitMQ server.
[Parameter(Mandatory=$true, ParameterSetName='cred')]
[PSCredential]$Credentials
)

Begin
{
$cred = GetRabbitMqCredentials $UserName $Password
$Credentials = NormaliseCredentials
}
Process
{
Expand All @@ -64,7 +71,7 @@ function Clear-RabbitMQQueue
$url = "http://$([System.Web.HttpUtility]::UrlEncode($ComputerName)):15672/api/queues/$([System.Web.HttpUtility]::UrlEncode($VirtualHost))/$([System.Web.HttpUtility]::UrlEncode($Name))/contents"
Write-Verbose "Invoking REST API: $url"

$result = Invoke-RestMethod $url -Credential $cred -AllowEscapedDotsAndSlashes -DisableKeepAlive -ErrorAction Continue -Method Delete
$result = Invoke-RestMethod $url -Credential $Credentials -AllowEscapedDotsAndSlashes -DisableKeepAlive -ErrorAction Continue -Method Delete
}
}
End
Expand Down
38 changes: 21 additions & 17 deletions CopyMessage.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
#>
function Copy-RabbitMQMessage
{
[CmdletBinding(SupportsShouldProcess=$true, ConfirmImpact='High')]
[CmdletBinding(DefaultParameterSetName='defaultLogin', SupportsShouldProcess=$true, ConfirmImpact='High')]
Param
(
# Name of the virtual host to filter channels by.
Expand All @@ -75,19 +75,23 @@ function Copy-RabbitMQMessage
[Alias("HostName", "hn", "cn")]
[string]$ComputerName = $defaultComputerName,

# UserName to use when logging to RabbitMq server. Default value is guest.
[string]$UserName = $defaultUserName,

# UserName to use when logging to RabbitMq server.
[Parameter(Mandatory=$true, ParameterSetName='login')]
[string]$UserName,

# Password to use when logging to RabbitMq server.
[Parameter(Mandatory=$true, ParameterSetName='login')]
[string]$Password,

# Password to use when logging to RabbitMq server. Default value is guest.
[string]$Password = $defaultPassword
# Credentials to use when logging to RabbitMQ server.
[Parameter(Mandatory=$true, ParameterSetName='cred')]
[PSCredential]$Credentials
)

Begin
{
Add-Type -AssemblyName System.Web
Add-Type -AssemblyName System.Net

$cred = GetRabbitMqCredentials $UserName $Password
$Credentials = NormaliseCredentials
$cnt = 0
$requiresMessageRemoval = $SourceQueueName -ne $DestinationQueueName
}
Expand All @@ -98,13 +102,13 @@ function Copy-RabbitMQMessage
{
$ename = "RabbitMQTools_copy"
$routingKey = "RabbitMQTools_copy"
Add-RabbitMQExchange -ComputerName $ComputerName -VirtualHost $VirtualHost -Type fanout -AutoDelete -Name $ename
Add-RabbitMQQueueBinding -ComputerName $ComputerName -VirtualHost $VirtualHost -ExchangeName $ename -Name $SourceQueueName -RoutingKey $routingKey
Add-RabbitMQQueueBinding -ComputerName $ComputerName -VirtualHost $VirtualHost -ExchangeName $ename -Name $DestinationQueueName -RoutingKey $routingKey
Add-RabbitMQExchange -ComputerName $ComputerName -VirtualHost $VirtualHost -Type fanout -AutoDelete -Name $ename -Credentials $Credentials
Add-RabbitMQQueueBinding -ComputerName $ComputerName -VirtualHost $VirtualHost -ExchangeName $ename -Name $SourceQueueName -RoutingKey $routingKey -Credentials $Credentials
Add-RabbitMQQueueBinding -ComputerName $ComputerName -VirtualHost $VirtualHost -ExchangeName $ename -Name $DestinationQueueName -RoutingKey $routingKey -Credentials $Credentials

try
{
$m = Get-RabbitMQMessage -ComputerName $ComputerName $VirtualHost $SourceQueueName
$m = Get-RabbitMQMessage -Credentials $Credentials -ComputerName $ComputerName -VirtualHost $VirtualHost -Name $SourceQueueName
$c = $m.message_count + 1

if ($Count -eq 0 -or $Count -gt $c ) { $Count = $c }
Expand All @@ -113,13 +117,13 @@ function Copy-RabbitMQMessage
for ($i = 1; $i -le $Count; $i++)
{
# get message to be copies, but do not remove it from the server yet.
$m = Get-RabbitMQMessage -ComputerName $ComputerName $VirtualHost $SourceQueueName
$m = Get-RabbitMQMessage -Credentials $Credentials -ComputerName $ComputerName -VirtualHost $VirtualHost -Name $SourceQueueName

# publish message to copying exchange, this will publish it onto dest queue as well as src queue.
Add-RabbitMQMessage -ComputerName $ComputerName $VirtualHost $ename $routingKey $m.payload $m.properties
Add-RabbitMQMessage -Credentials $Credentials -ComputerName $ComputerName -VirtualHost $VirtualHost -ExchangeName $ename -RoutingKey $routingKey -Payload $m.payload -Properties $m.properties

# remove message from src queue. It has been published step earlier.
if ($requiresMessageRemoval) { $m = Get-RabbitMQMessage -ComputerName $ComputerName $VirtualHost $SourceQueueName -Remove }
if ($requiresMessageRemoval) { $m = Get-RabbitMQMessage -Credentials $Credentials -ComputerName $ComputerName -VirtualHost $VirtualHost -Name $SourceQueueName -Remove }

[int]$p = ($i * 100) / $Count
if ($p -gt 100) { $p = 100 }
Expand All @@ -131,7 +135,7 @@ function Copy-RabbitMQMessage
}
finally
{
Remove-RabbitMQExchange -ComputerName $ComputerName -VirtualHost $VirtualHost -Name $ename -Confirm:$false
Remove-RabbitMQExchange -Credentials $Credentials -ComputerName $ComputerName -VirtualHost $VirtualHost -Name $ename -Confirm:$false
}
}
}
Expand Down
Loading

0 comments on commit e50d41e

Please sign in to comment.