Skip to content

Commit b18c1ac

Browse files
committed
completed get service bus namespaces and ruleauthorizations from the resource provider api
1 parent b82d6eb commit b18c1ac

File tree

6 files changed

+179
-0
lines changed

6 files changed

+179
-0
lines changed

Elastacloud.AzureManagement.Fluent/Clients/Interfaces/IServiceBusClient.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
* Email: [email protected] *
88
************************************************************************************************************/
99

10+
using System.Collections;
11+
using System.Collections.Generic;
1012
using Elastacloud.AzureManagement.Fluent.Helpers;
1113

1214
namespace Elastacloud.AzureManagement.Fluent.Clients.Interfaces
@@ -31,6 +33,14 @@ public interface IServiceBusClient
3133
/// </summary>
3234
void DeleteNamespace(string name);
3335
/// <summary>
36+
/// Returns a list of service bus namespaces
37+
/// </summary>
38+
IEnumerable<string> GetServiceBusNamspaceList();
39+
/// <summary>
40+
/// Gets a service bus connection string given a namespace name
41+
/// </summary>
42+
string GetServiceBusConnectionString(string @namespace, string ruleName);
43+
/// <summary>
3444
/// The name of the service bus namespace
3545
/// </summary>
3646
string Namespace { get; set; }

Elastacloud.AzureManagement.Fluent/Clients/ServiceBusClient.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
* Email: [email protected] *
88
************************************************************************************************************/
99

10+
using System;
11+
using System.Collections.Generic;
12+
using System.Data;
1013
using System.Security.Cryptography.X509Certificates;
1114
using Elastacloud.AzureManagement.Fluent.Clients.Interfaces;
1215
using Elastacloud.AzureManagement.Fluent.Commands.Services;
@@ -80,6 +83,38 @@ public void DeleteNamespace(string name)
8083
command.Execute();
8184
}
8285

86+
/// <summary>
87+
/// Returns a list of service bus namespaces
88+
/// </summary>
89+
public IEnumerable<string> GetServiceBusNamspaceList()
90+
{
91+
var command = new GetServiceBusNamespaceListCommand()
92+
{
93+
SubscriptionId = _subscriptionId,
94+
Certificate = _managementCertificate
95+
};
96+
command.Execute();
97+
return command.Namespaces;
98+
}
99+
100+
/// <summary>
101+
/// Gets a service bus connection string given a namespace name
102+
/// </summary>
103+
public string GetServiceBusConnectionString(string @namespace, string ruleName)
104+
{
105+
var command = new GetServiceBusPolicyConnectionStringCommand(@namespace, ruleName)
106+
{
107+
SubscriptionId = _subscriptionId,
108+
Certificate = _managementCertificate
109+
};
110+
command.Execute();
111+
if (command.ConnectionString == null)
112+
{
113+
throw new FluentManagementException("unable to get SAS policy with rulename: " + ruleName, "ServiceBusClient");
114+
}
115+
return command.ConnectionString;
116+
}
117+
83118
/// <summary>
84119
/// The name of the service bus namespace
85120
/// </summary>
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/************************************************************************************************************
2+
* This software is distributed under a GNU Lesser License by Elastacloud Limited and it is free to *
3+
* modify and distribute providing the terms of the license are followed. From the root of the source the *
4+
* license can be found in /Resources/license.txt *
5+
* *
6+
* Web at: www.elastacloud.com *
7+
* Email: [email protected] *
8+
************************************************************************************************************/
9+
10+
using System;
11+
using System.Collections;
12+
using System.Collections.Generic;
13+
using System.IO;
14+
using System.Linq;
15+
using System.Net;
16+
using System.Runtime.InteropServices;
17+
using System.Runtime.Remoting.Messaging;
18+
using System.Text;
19+
using System.Threading;
20+
using System.Xml;
21+
using System.Xml.Linq;
22+
using System.Xml.XPath;
23+
using Elastacloud.AzureManagement.Fluent.Helpers;
24+
using Elastacloud.AzureManagement.Fluent.Types.Exceptions;
25+
using FSharp.Data.Runtime;
26+
27+
namespace Elastacloud.AzureManagement.Fluent.Commands.Services
28+
{
29+
/// <summary>
30+
/// Used to create a hosted service within a given subscription
31+
/// </summary>
32+
internal class GetServiceBusPolicyConnectionStringCommand : ServiceCommand
33+
{
34+
internal GetServiceBusPolicyConnectionStringCommand(string @namespace, string ruleName)
35+
{
36+
HttpVerb = HttpVerbGet;
37+
ServiceType = "services";
38+
OperationId = "ServiceBus";
39+
HttpCommand = String.Format("Namespaces/{0}/AuthorizationRules", @namespace);
40+
ContentType = "application/xml;charset=utf-8";
41+
Namespace = @namespace;
42+
RuleName = ruleName;
43+
}
44+
45+
public string ConnectionString { get; private set; }
46+
public string Namespace { get; private set; }
47+
public string RuleName { get; private set; }
48+
49+
protected override void ResponseCallback(HttpWebResponse webResponse)
50+
{
51+
XNamespace netservices = "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect";
52+
XDocument document;
53+
54+
using (var reader = new StreamReader(webResponse.GetResponseStream()))
55+
{
56+
document = XDocument.Parse(reader.ReadToEnd());
57+
}
58+
var root = document.Descendants(netservices + "SharedAccessAuthorizationRule");
59+
var keyNode = root.FirstOrDefault(item => item.Element(netservices + "KeyName").Value == RuleName);
60+
61+
ConnectionString = keyNode == null ? null :
62+
String.Format("Endpoint=sb://{0}.servicebus.windows.net/;SharedAccessKeyName={1};SharedAccessKey={2}",
63+
Namespace, keyNode.Element(netservices + "KeyName").Value, keyNode.Element(netservices + "PrimaryKey").Value);
64+
SitAndWait.Set();
65+
}
66+
}
67+
68+
69+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/************************************************************************************************************
2+
* This software is distributed under a GNU Lesser License by Elastacloud Limited and it is free to *
3+
* modify and distribute providing the terms of the license are followed. From the root of the source the *
4+
* license can be found in /Resources/license.txt *
5+
* *
6+
* Web at: www.elastacloud.com *
7+
* Email: [email protected] *
8+
************************************************************************************************************/
9+
10+
using System;
11+
using System.Collections;
12+
using System.Collections.Generic;
13+
using System.IO;
14+
using System.Linq;
15+
using System.Net;
16+
using System.Runtime.InteropServices;
17+
using System.Text;
18+
using System.Threading;
19+
using System.Xml;
20+
using System.Xml.Linq;
21+
using System.Xml.XPath;
22+
using Elastacloud.AzureManagement.Fluent.Helpers;
23+
using Elastacloud.AzureManagement.Fluent.Types.Exceptions;
24+
using FSharp.Data.Runtime;
25+
26+
namespace Elastacloud.AzureManagement.Fluent.Commands.Services
27+
{
28+
/// <summary>
29+
/// Used to create a hosted service within a given subscription
30+
/// </summary>
31+
internal class GetServiceBusNamespaceListCommand : ServiceCommand
32+
{
33+
internal GetServiceBusNamespaceListCommand()
34+
{
35+
HttpVerb = HttpVerbGet;
36+
ServiceType = "services";
37+
OperationId = "ServiceBus";
38+
HttpCommand = "Namespaces";
39+
ContentType = "application/xml;charset=utf-8";
40+
}
41+
42+
public IEnumerable<string> Namespaces { get; private set; }
43+
44+
45+
protected override void ResponseCallback(HttpWebResponse webResponse)
46+
{
47+
XNamespace netservices = "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect";
48+
XDocument document;
49+
50+
using (var reader = new StreamReader(webResponse.GetResponseStream()))
51+
{
52+
document = XDocument.Parse(reader.ReadToEnd());
53+
}
54+
var root = document.Descendants(netservices + "Name");
55+
Namespaces = root.Select(item => item.Value);
56+
57+
SitAndWait.Set();
58+
}
59+
}
60+
}

Elastacloud.AzureManagement.Fluent/Elastacloud.AzureManagement.Fluent.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@
146146
<Compile Include="Commands\Parsers\WebsiteConfigParser.cs" />
147147
<Compile Include="Commands\Parsers\WebsiteParser.cs" />
148148
<Compile Include="Commands\Parsers\WebsiteListParser.cs" />
149+
<Compile Include="Commands\Service Bus\GetServiceBusPolicyConnectionStringCommand.cs" />
150+
<Compile Include="Commands\Service Bus\GetServiceBusNamespaceListCommand.cs" />
149151
<Compile Include="Commands\Service Bus\CreateServiceBusNamespaceCommand.cs" />
150152
<Compile Include="Commands\Service Bus\CheckServiceBusNamespaceAvailabilityCommand.cs" />
151153
<Compile Include="Commands\Service Bus\DeleteServiceBusNamspaceCommand.cs" />

Elastacloud.FluentManagement.FSTest/VirtualMachines.fsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,6 @@ let fred2 = sbClient.CreateNamespace("fred")
8383
let toolscheck = sbClient.CheckNamespaceExists("elastatools3")
8484
let elastacloud = sbClient.CreateNamespace("elastatools3")
8585
let clouddelete = sbClient.DeleteNamespace("elastatools3")
86+
let sblist = sbClient.GetServiceBusNamspaceList()
87+
let sbpolicy = sbClient.GetServiceBusConnectionString("briskuidev", "RootManageSharedAccessKey")
88+

0 commit comments

Comments
 (0)