Public/Add-RabbitMQExchange.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<#
.Synopsis
   Adds Exchange to RabbitMQ server.

.DESCRIPTION
   The Add-RabbitMQExchange allows for creating new Exchanges in given RabbitMQ server.

   To add Exchange to remote server you need to provide -HostName

   You may pipe an object with names and parameters, including HostName, to create multiple Exchanges. For more information how to do that see Examples.

   The cmdlet is using REST Api provided by RabbitMQ Management Plugin. For more information go to: https://www.rabbitmq.com/management.html

   To support requests using default virtual host (/), the cmdlet will temporarily disable UnEscapeDotsAndSlashes flag on UriParser. For more information check get-help about_UnEsapingDotsAndSlashes.

.EXAMPLE
   Add-RabbitMQExchange -Type direct TestExchange

   Creates direct exchange named TestExchange in the local RabbitMQ server.

.EXAMPLE
   Add-RabbitMQExchange -Type direct TestExchange -Durable -AutoDelete -Internal -AlternateExchange e2

   Creates direct exchange named TestExchange in the local RabbitMQ server and sets its properties to be Durable, AutoDelete, Internal and to use alternate exchange called e2.

.EXAMPLE
   Add-RabbitMQExchange -Type fanout TestExchange, ProdExchange

      Creates in the local RabbitMQ server two fanout exchanges named TestExchange and ProdExchange.

.EXAMPLE
   Add-RabbitMQExchange -Type direct TestExchange -HostName myrabbitmq.servers.com

   Creates direct exchange named TestExchange in the myrabbitmq.servers.com server.

.EXAMPLE
   @("e1", "e2") | Add-RabbitMQExchange -Type direct

   This command pipes list of exchanges to add to the RabbitMQ server. In the above example two new Exchanges named "e1" and "e2" will be created in local RabbitMQ server.

.EXAMPLE
    $a = $(
        New-Object -TypeName psobject -Prop @{"HostName" = "localhost"; "Name" = "e1", "Type"="direct"}
        New-Object -TypeName psobject -Prop @{"HostName" = "localhost"; "Name" = "e2", "Type"="fanout"}
        New-Object -TypeName psobject -Prop @{"HostName" = "127.0.0.1"; "Name" = "e3", "Type"="topic", Durable=$true, $Internal=$true}
    )

   $a | Add-RabbitMQExchange

   Above example shows how to pipe parameters for creating new exchanges.
   
   In the above example three new exchanges will be created with different parameters.

.INPUTS
   You can pipe Name, Type, Durable, AutoDelete, Internal, AlternateExchange, VirtualHost and HostName to this cmdlet.

.LINK
    https://www.rabbitmq.com/management.html - information about RabbitMQ management plugin.
#>

function Add-RabbitMQExchange
{
    [CmdletBinding(SupportsShouldProcess=$true, ConfirmImpact="Medium")]
    Param
    (
        # Name of RabbitMQ Exchange.
        [parameter(Mandatory=$true, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true, Position=0)]
        [Alias("Exchange", "ExchangeName")]
        [string[]]$Name,

        # Type of the Exchange to create.
        [parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true)]
        [ValidateSet("topic", "fanout", "direct", "headers", "x-jms-topic")]
        [string]$Type,

        # Determines whether the exchange should be Durable.
        [parameter(ValueFromPipelineByPropertyName=$true)]
        [switch]$Durable,
        
        # Determines whether the exchange will be deleted once all queues have finished using it.
        [parameter(ValueFromPipelineByPropertyName=$true)]
        [switch]$AutoDelete,
        
        # Determines whether the exchange should be Internal.
        [parameter(ValueFromPipelineByPropertyName=$true)]
        [switch]$Internal,

        # Allows to set alternate exchange to which all messages which cannot be routed will be send.
        [parameter(ValueFromPipelineByPropertyName=$true)]
        [Alias("alt")]
        [string]$AlternateExchange,

        # Name of RabbitMQ Virtual Host.
        [parameter(ValueFromPipelineByPropertyName=$true)]
        [Alias("vh", "vhost")]
        [string]$VirtualHost = $defaultVirtualhost,

        # Name of the computer hosting RabbitMQ server. Defalut value is localhost.
        [parameter(ValueFromPipelineByPropertyName=$true)]
        [Alias("HostName", "hn", "cn")]
        [string]$BaseUri = $defaultComputerName,

        # Credentials to use when logging to RabbitMQ server.
        [Parameter(Mandatory=$false)]
        [PSCredential]$Credentials = $defaultCredentials
    )

    Begin
    {
    }
    Process
    {
        if ($pscmdlet.ShouldProcess("server: $BaseUri, vhost: $VirtualHost", "Add exchange(s): $(NamesToString $Name '(all)')")) {
            
            $body = @{
                type = "$Type"
            }

            if ($Durable) { $body.Add("durable", $true) }
            if ($AutoDelete) { $body.Add("auto_delete", $true) }
            if ($Internal) { $body.Add("internal", $true) }
            if ($AlternateExchange) { $body.Add("arguments", @{ "alternate-exchange"=$AlternateExchange }) }

            $bodyJson = $body | ConvertTo-Json

            foreach($n in $Name)
            {
                $url = Join-Parts $BaseUri "/api/exchanges/$([System.Web.HttpUtility]::UrlEncode($VirtualHost))/$([System.Web.HttpUtility]::UrlEncode($n))"
                Write-Verbose "Invoking REST API: $url"
        
                $result = Invoke-RestMethod $url -Credential $Credentials -AllowEscapedDotsAndSlashes -DisableKeepAlive:$InvokeRestMethodKeepAlive -ErrorAction Continue -Method Put -ContentType "application/json" -Body $bodyJson

                Write-Verbose "Created Exchange $n on server $BaseUri, Virtual Host $VirtualHost"
                $cnt++
            }
        }
    }
    End
    {
        if ($cnt -gt 1) { Write-Verbose "Created $cnt Exchange(s)." }
    }
}