Last active
April 17, 2020 11:44
-
-
Save Youenn-Bouglouan/7ba237e6a4663d9b7575124cbd8cb3ac to your computer and use it in GitHub Desktop.
Use the Azure Fluent .Net API in F# to create a service bus with a topic and a subscription
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Deploy service bus + topic & subscription using the Azure .NET Fluent API | |
// Some references in C# here: | |
// https://github.com/Azure-Samples/service-bus-dotnet-manage-publish-subscribe-with-advanced-features/blob/master/Program.cs | |
(* | |
Necessary references to be added to your fsproj | |
<ItemGroup> | |
<PackageReference Include="Microsoft.Azure.Management.Fluent" Version="1.33.0" /> | |
<PackageReference Include="Microsoft.Azure.Management.ServiceBus.Fluent" Version="1.33.0" /> | |
</ItemGroup> | |
*) | |
open System | |
open Microsoft.Azure.Management.Fluent | |
open Microsoft.Azure.Management.ServiceBus.Fluent | |
open Microsoft.Azure.Management.ResourceManager.Fluent | |
open Microsoft.Azure.Management.ResourceManager.Fluent.Authentication | |
open Microsoft.Azure.Management.ResourceManager.Fluent.Core | |
[<CLIMutable>] | |
type ServicePrincipalCredentials = { | |
SubscriptionId: Guid | |
ClientId: Guid | |
ClientSecret: string | |
TenantId: Guid } | |
// Fill it with your own Service Principal credentials... | |
let credentials = { | |
SubscriptionId = Guid.NewGuid() | |
ClientId = Guid.NewGuid() | |
ClientSecret = "my secret" | |
TenantId = Guid.NewGuid() } | |
try | |
// Connect to Azure via the SDK using a service principal | |
let servicePrincipal = | |
ServicePrincipalLoginInformation( | |
ClientId = string credentials.ClientId, | |
ClientSecret = credentials.ClientSecret) | |
let sdkCredentials = | |
AzureCredentials( | |
servicePrincipal, | |
string credentials.TenantId, | |
AzureEnvironment.AzureGlobalCloud) | |
let azureSdk = | |
Azure | |
.Configure() | |
.Authenticate(sdkCredentials) | |
.WithSubscription(string credentials.SubscriptionId) | |
// Define a new service bus with 1 topic and 1 subscription | |
let existingResourceGroupName = "existing-resource-group-name" | |
let serviceBusNamespace = "myuniqueservicebusname" | |
let topicName = "my-topic-name" | |
let subscriptionName = "my-subscription-name" | |
// From my own testing, every operation run below seems to be idempotent. | |
// You can run it as many times as you want. | |
let serviceBus = | |
azureSdk.ServiceBusNamespaces | |
.Define(serviceBusNamespace) | |
.WithRegion(Region.EuropeWest) | |
.WithExistingResourceGroup(existingResourceGroupName) | |
.WithSku(NamespaceSku.Standard) | |
.WithNewTopic(topicName, 1024) | |
.Create() | |
// Here you could be immediately update the bus after its creation and access more APIs... | |
// .Update() | |
// .WithoutTopic(topicName) | |
// .Apply() | |
printfn "Service bus %s was successfully deployed." serviceBusNamespace | |
printfn "Listing topics:" | |
serviceBus.Topics.List() | |
|> Seq.iter (fun t -> printfn "Topic: %s" t.Name) | |
let topic = | |
serviceBus | |
.Topics | |
.GetByName(topicName) | |
topic | |
.Update() // Every instruction below is now updating the topic | |
.WithNewSubscription(subscriptionName) | |
// .WithoutSubscription("sub1name") // this would remove any subscription called 'sub1name' | |
.Apply() | |
|> ignore | |
printfn "Listing subscriptions for '%s' topic:" topicName | |
topic.Subscriptions.List() | |
|> Seq.iter (fun s -> printfn "Subscription: %s" s.Name) | |
with | |
| ex -> | |
printfn "Error while creating the service bus: %A" ex | |
|> ignore |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment