Skip to content

Commit c22ffb9

Browse files
authored
Merge pull request #82 from SkillsFundingAgency/DASD-3396-ServiceBus_Authorization_Rule
Add script to create and return auth rule connection string
2 parents ca7ebf7 + b7305c1 commit c22ffb9

1 file changed

Lines changed: 87 additions & 0 deletions

File tree

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<#
2+
3+
.SYNOPSIS
4+
Get a connection string for a servicebus namespace
5+
6+
.DESCRIPTION
7+
Get a connection string for a servicebus namespace. If one does not exist with the given name, it will be created
8+
9+
.PARAMETER ResourceGroupName
10+
The name of the destination Resource Group for the resource
11+
12+
.PARAMETER NamespaceName
13+
The name of the Service Bus Namespace
14+
15+
.PARAMETER AuthorizationRuleName
16+
The name of the authorization rule to be created
17+
18+
.PARAMETER Rights
19+
A list of rights. This can be one or all of the following:
20+
Listen
21+
Send
22+
Manage
23+
24+
.PARAMETER ConnectionStringType
25+
The connection string to return. This can be either Primary or Secondary.
26+
27+
.EXAMPLE
28+
.\New-ServiceBusConnectionString.ps1 -NamespaceName test-namespace -AuthorizationRuleName auth1 -Rights Listen, Send, Manage -ConnectionStringType Secondary
29+
30+
#>
31+
32+
[CmdletBinding()]
33+
Param (
34+
[Parameter(Mandatory = $true)]
35+
[String]$NamespaceName,
36+
[Parameter(Mandatory = $true)]
37+
[String]$AuthorizationRuleName,
38+
[Parameter(Mandatory = $true)]
39+
[ValidateSet("Listen", "Send", "Manage")]
40+
[String[]]$Rights,
41+
[Parameter(Mandatory = $false)]
42+
[ValidateSet("Primary", "Secondary")]
43+
[String[]]$ConnectionStringType = "Primary"
44+
)
45+
46+
try {
47+
# --- Import Azure Helpers
48+
Import-Module (Resolve-Path -Path $PSScriptRoot\..\Modules\Helpers.psm1).Path
49+
50+
Write-Log -LogLevel Information -Message "Checking for existing Service Bus Namespace: $NamespaceName"
51+
$ServiceBusResource = Get-AzureRMResource -Name $NamespaceName
52+
if (!$ServiceBusResource) {
53+
throw "Could not find servicebus namespace $NamespaceName"
54+
}
55+
56+
$AuthorizationRule = Get-AzureRmServiceBusAuthorizationRule -ResourceGroupName $ServiceBusResource.ResourceGroupName -Namespace $NameSpaceName -Name $AuthorizationRuleName -ErrorAction SilentlyContinue
57+
if (!$AuthorizationRule) {
58+
Write-Log -LogLevel Information -Message "Creating authorization rule with name $AuthorizationRuleName"
59+
$AuthorizationRuleParameters = @{
60+
ResourceGroupName = $ServiceBusResource.ResourceGroupName
61+
Namespace = $NamespaceName
62+
Name = $AuthorizationRuleName
63+
Rights = $Rights
64+
}
65+
$null = New-AzureRmServiceBusAuthorizationRule @AuthorizationRuleParameters
66+
}
67+
68+
Write-Log -LogLevel Information -Message "Retrieving $ConnectionStringType connection string"
69+
$ServiceBusKey = Get-AzureRmServiceBusKey -ResourceGroupName $ServiceBusResource.ResourceGroupName -Namespace $NamespaceName -Name $AuthorizationRuleName
70+
71+
switch ($ConnectionStringType) {
72+
'Primary' {
73+
$ConnectionString = $ServiceBusKey.PrimaryConnectionString
74+
break
75+
}
76+
'Secondary' {
77+
$ConnectionString = $ServiceBusKey.SecondaryConnectionString
78+
break
79+
}
80+
}
81+
82+
Write-Output ("##vso[task.setvariable variable=ServiceBusConnectionString;]$($ConnectionString)")
83+
84+
}
85+
catch {
86+
throw $_
87+
}

0 commit comments

Comments
 (0)