/// <summary> /// Specifies an existing event hub namespace in which event hub needs to be created. /// </summary> /// <param name="namespace">Event hub namespace.</param> /// <return>Next stage of the event hub definition.</return> EventHub.Definition.IWithCaptureProviderOrCreate EventHub.Definition.IWithNamespace.WithExistingNamespace(IEventHubNamespace ehNamespace) { return(this.WithExistingNamespace(ehNamespace)); }
///GENMHASH:C0BC49DC1543321A2DF43F4658EB9912:EDA80D678FC3FF2592A13E3E96233046 public EventHubImpl WithExistingNamespace(IEventHubNamespace nhNamespace) { this.ancestor = new OneAncestor(SelfId(nhNamespace.Id)); return(this); }
public static void RunSample(IAzure azure) { Region region = Region.USEast; string rgName = SdkContext.RandomResourceName("rgeh", 15); string namespaceName1 = SdkContext.RandomResourceName("ns", 15); string namespaceName2 = SdkContext.RandomResourceName("ns", 15); string storageAccountName = SdkContext.RandomResourceName("stg", 14); string eventHubName1 = SdkContext.RandomResourceName("eh", 14); string eventHubName2 = SdkContext.RandomResourceName("eh", 14); try { //============================================================ // Create an event hub namespace // Utilities.Log("Creating a namespace"); IEventHubNamespace namespace1 = azure.EventHubNamespaces .Define(namespaceName1) .WithRegion(Region.USEast2) .WithNewResourceGroup(rgName) .Create(); Utilities.Print(namespace1); Utilities.Log("Created a namespace"); //============================================================ // Create an event hub in the namespace with data capture enabled, with consumer group and auth rule // ICreatable <IStorageAccount> storageAccountCreatable = azure.StorageAccounts .Define(storageAccountName) .WithRegion(Region.USEast2) .WithExistingResourceGroup(rgName) .WithSku(StorageAccountSkuType.Standard_LRS); Utilities.Log("Creating an event hub with data capture enabled with a consumer group and rule in it"); IEventHub eventHub1 = azure.EventHubs .Define(eventHubName1) .WithExistingNamespace(namespace1) // Optional - configure data capture .WithNewStorageAccountForCapturedData(storageAccountCreatable, "datacpt") .WithDataCaptureEnabled() // Optional - create one consumer group in event hub .WithNewConsumerGroup("cg1", "sometadata") // Optional - create an authorization rule for event hub .WithNewListenRule("listenrule1") .Create(); Utilities.Log("Created an event hub with data capture enabled with a consumer group and rule in it"); Utilities.Print(eventHub1); //============================================================ // Retrieve consumer groups in the event hub // Utilities.Log("Retrieving consumer groups"); var consumerGroups = eventHub1.ListConsumerGroups(); Utilities.Log("Retrieved consumer groups"); foreach (IEventHubConsumerGroup group in consumerGroups) { Utilities.Print(group); } //============================================================ // Create another event hub in the namespace using event hub accessor in namespace accessor // Utilities.Log("Creating another event hub in the namespace"); IEventHub eventHub2 = azure.EventHubNamespaces .EventHubs .Define(eventHubName2) .WithExistingNamespace(namespace1) .Create(); Utilities.Log("Created second event hub"); Utilities.Print(eventHub2); //============================================================ // Create a consumer group in the event hub using consumer group accessor in event hub accessor // Utilities.Log("Creating a consumer group in the second event hub"); IEventHubConsumerGroup consumerGroup2 = azure.EventHubNamespaces .EventHubs .ConsumerGroups .Define("cg2") .WithExistingEventHub(eventHub2) // Optional .WithUserMetadata("sometadata") .Create(); Utilities.Log("Created a consumer group in the second event hub"); Utilities.Print(consumerGroup2); //============================================================ // Retrieve consumer groups in the event hub // Utilities.Log("Retrieving consumer groups in the second event hub"); consumerGroups = eventHub2.ListConsumerGroups(); Utilities.Log("Retrieved consumer groups in the seoond event hub"); foreach (IEventHubConsumerGroup group in consumerGroups) { Utilities.Print(group); } //============================================================ // Create an event hub namespace with event hub // Utilities.Log("Creating an event hub namespace along with event hub"); IEventHubNamespace namespace2 = azure.EventHubNamespaces .Define(namespaceName2) .WithRegion(Region.USEast2) .WithExistingResourceGroup(rgName) .WithNewEventHub(eventHubName2) .Create(); Utilities.Log("Created an event hub namespace along with event hub"); Utilities.Print(namespace2); foreach (IEventHub eh in namespace2.ListEventHubs()) { Utilities.Print(eh); } } finally { try { azure.ResourceGroups.DeleteByName(rgName); } catch (NullReferenceException) { Utilities.Log("Did not create any resources in Azure. No clean up is necessary"); } catch (Exception ex) { Utilities.Log(ex); } } }
/** * Azure Event Hub sample for managing geo disaster recovery pairing - * - Create two event hub namespaces * - Create a pairing between two namespaces * - Create an event hub in the primary namespace and retrieve it from the secondary namespace * - Retrieve the pairing connection string * - Fail over so that secondary namespace become primary. */ public static void RunSample(IAzure azure) { Region region = Region.USEast; string rgName = SdkContext.RandomResourceName("rgeh", 15); string primaryNamespaceName = SdkContext.RandomResourceName("ns", 15); string secondaryNamespaceName = SdkContext.RandomResourceName("ns", 15); string geoDRName = SdkContext.RandomResourceName("geodr", 14); string eventHubName = SdkContext.RandomResourceName("eh", 14); bool isFailOverSucceeded = false; IEventHubDisasterRecoveryPairing pairing = null; try { //============================================================ // Create resource group for the namespaces and recovery pairings // IResourceGroup resourceGroup = azure.ResourceGroups.Define(rgName) .WithRegion(Region.USSouthCentral) .Create(); Utilities.Log($"Creating primary event hub namespace {primaryNamespaceName}"); IEventHubNamespace primaryNamespace = azure.EventHubNamespaces .Define(primaryNamespaceName) .WithRegion(Region.USSouthCentral) .WithExistingResourceGroup(resourceGroup) .Create(); Utilities.Log("Primary event hub namespace created"); Utilities.Print(primaryNamespace); Utilities.Log($"Creating secondary event hub namespace {primaryNamespaceName}"); IEventHubNamespace secondaryNamespace = azure.EventHubNamespaces .Define(secondaryNamespaceName) .WithRegion(Region.USNorthCentral) .WithExistingResourceGroup(resourceGroup) .Create(); Utilities.Log("Secondary event hub namespace created"); Utilities.Print(secondaryNamespace); //============================================================ // Create primary and secondary namespaces and recovery pairing // Utilities.Log($"Creating geo-disaster recovery pairing {geoDRName}"); pairing = azure.EventHubDisasterRecoveryPairings .Define(geoDRName) .WithExistingPrimaryNamespace(primaryNamespace) .WithExistingSecondaryNamespace(secondaryNamespace) .Create(); while (pairing.ProvisioningState != ProvisioningStateDR.Succeeded) { pairing = pairing.Refresh(); SdkContext.DelayProvider.Delay(15 * 1000); if (pairing.ProvisioningState == ProvisioningStateDR.Failed) { throw new Exception("Provisioning state of the pairing is FAILED"); } } Utilities.Log($"Created geo-disaster recovery pairing {geoDRName}"); Utilities.Print(pairing); //============================================================ // Create an event hub and consumer group in primary namespace // Utilities.Log("Creating an event hub and consumer group in primary namespace"); IEventHub eventHubInPrimaryNamespace = azure.EventHubs .Define(eventHubName) .WithExistingNamespace(primaryNamespace) .WithNewConsumerGroup("consumerGrp1") .Create(); Utilities.Log("Created event hub and consumer group in primary namespace"); Utilities.Print(eventHubInPrimaryNamespace); Utilities.Log("Waiting for 60 seconds to allow metadata to sync across primary and secondary"); SdkContext.DelayProvider.Delay(60 * 1000); // Wait for syncing to finish Utilities.Log("Retrieving the event hubs in secondary namespace"); IEventHub eventHubInSecondaryNamespace = azure.EventHubs.GetByName(rgName, secondaryNamespaceName, eventHubName); Utilities.Log("Retrieved the event hubs in secondary namespace"); Utilities.Print(eventHubInSecondaryNamespace); //============================================================ // Retrieving the connection string // var rules = pairing.ListAuthorizationRules(); foreach (IDisasterRecoveryPairingAuthorizationRule rule in rules) { IDisasterRecoveryPairingAuthorizationKey key = rule.GetKeys(); Utilities.Print(key); } Utilities.Log("Initiating fail over"); pairing.FailOver(); isFailOverSucceeded = true; Utilities.Log("Fail over initiated"); } finally { try { try { // It is necessary to break pairing before deleting resource group // if (pairing != null && !isFailOverSucceeded) { pairing.BreakPairing(); } } catch (Exception ex) { Utilities.Log("Pairing breaking failed:" + ex.Message); } azure.ResourceGroups.BeginDeleteByName(rgName); } catch (NullReferenceException) { Utilities.Log("Did not create any resources in Azure. No clean up is necessary"); } catch (Exception ex) { Utilities.Log(ex); } } }
///GENMHASH:C0D4E0E0BD08B22D12CF95D0B0756897:DA0B7CAC98D64910412CB27D6CFA6B12 public EventHubDisasterRecoveryPairingImpl WithExistingSecondaryNamespace(IEventHubNamespace nhNamespace) { this.Inner.PartnerNamespace = nhNamespace.Id ?? throw new ArgumentNullException("nhNamespace.Id"); return(this); }
///GENMHASH:E44B2C1A872FCC1923371FF81AF28CA0:EDA80D678FC3FF2592A13E3E96233046 public EventHubDisasterRecoveryPairingImpl WithExistingPrimaryNamespace(IEventHubNamespace nhNamespace) { this.ancestor = new OneAncestor(SelfId(nhNamespace.Id)); return(this); }
public void CanManageBasicSettings() { using (var context = FluentMockContext.Start(GetType().FullName)) { var region = Region.USEast; var groupName = TestUtilities.GenerateName("rgns"); var namespaceName1 = TestUtilities.GenerateName("ns111"); var namespaceName2 = TestUtilities.GenerateName("ns222"); var namespaceName3 = TestUtilities.GenerateName("ns333"); var azure = TestHelper.CreateRollupClient(); try { IEventHubNamespace namespace1 = azure.EventHubNamespaces .Define(namespaceName1) .WithRegion(region) .WithNewResourceGroup(groupName) // SDK should use Sku as 'Standard' and set capacity.capacity in it as 1 .WithAutoScaling() .Create(); Assert.NotNull(namespace1); Assert.NotNull(namespace1.Inner); Assert.NotNull(namespace1.Sku); Assert.True(namespace1.Sku.Equals(EventHubNamespaceSkuType.Standard)); Assert.True(namespace1.IsAutoScaleEnabled); Assert.NotNull(namespace1.Inner.MaximumThroughputUnits); Assert.NotNull(namespace1.Inner.Sku.Capacity); IEventHubNamespace namespace2 = azure.EventHubNamespaces .Define(namespaceName2) .WithRegion(region) .WithExistingResourceGroup(groupName) // SDK should use Sku as 'Standard' and set capacity.capacity in it as 11 .WithCurrentThroughputUnits(11) .Create(); Assert.NotNull(namespace2); Assert.NotNull(namespace2.Inner); Assert.NotNull(namespace2.Sku); Assert.True(namespace2.Sku.Equals(EventHubNamespaceSkuType.Standard)); Assert.NotNull(namespace2.Inner.MaximumThroughputUnits); Assert.NotNull(namespace2.Inner.Sku.Capacity); Assert.Equal(11, namespace2.CurrentThroughputUnits); IEventHubNamespace namespace3 = azure.EventHubNamespaces .Define(namespaceName3) .WithRegion(region) .WithExistingResourceGroup(groupName) .WithSku(EventHubNamespaceSkuType.Basic) .Create(); Assert.NotNull(namespace3); Assert.NotNull(namespace3.Inner); Assert.NotNull(namespace3.Sku); Assert.True(namespace3.Sku.Equals(EventHubNamespaceSkuType.Basic)); namespace3.Update() .WithSku(EventHubNamespaceSkuType.Standard) .WithTag("aa", "bb") .Apply(); Assert.NotNull(namespace3.Sku); Assert.True(namespace3.Sku.Equals(EventHubNamespaceSkuType.Standard)); Assert.NotNull(namespace3.Tags); Assert.True(namespace3.Tags.Count > 0); } finally { try { azure.ResourceGroups.DeleteByName(groupName); } catch { } } } }
public void CanManage() { using (var context = FluentMockContext.Start(GetType().FullName)) { var region = Region.USEast; var groupName = TestUtilities.GenerateName("rgns"); var namespaceName = TestUtilities.GenerateName("ns111"); var eventHubName1 = TestUtilities.GenerateName("eh"); var eventHubName2 = TestUtilities.GenerateName("eh"); var eventHubName3 = TestUtilities.GenerateName("eh"); var azure = TestHelper.CreateRollupClient(); try { IEventHubNamespace ehNamespace = azure.EventHubNamespaces .Define(namespaceName) .WithRegion(region) .WithNewResourceGroup(groupName) .WithNewEventHub(eventHubName1) .WithNewEventHub(eventHubName2) .Create(); Assert.NotNull(ehNamespace); Assert.NotNull(ehNamespace.Inner); var hubs = ehNamespace.ListEventHubs(); HashSet <string> set = new HashSet <string>(); foreach (IEventHub hub in hubs) { set.Add(hub.Name); } Assert.Contains(eventHubName1, set); Assert.Contains(eventHubName2, set); hubs = azure.EventHubNamespaces .EventHubs .ListByNamespace(ehNamespace.ResourceGroupName, ehNamespace.Name); set.Clear(); foreach (IEventHub hub in hubs) { set.Add(hub.Name); } Assert.Contains(eventHubName1, set); Assert.Contains(eventHubName2, set); azure.EventHubNamespaces .EventHubs .Define(eventHubName3) .WithExistingNamespaceId(ehNamespace.Id) .WithPartitionCount(5) .WithRetentionPeriodInDays(6) .Create(); hubs = ehNamespace.ListEventHubs(); set.Clear(); foreach (IEventHub hub in hubs) { set.Add(hub.Name); } Assert.Contains(eventHubName1, set); Assert.Contains(eventHubName2, set); Assert.Contains(eventHubName3, set); } finally { try { azure.ResourceGroups.DeleteByName(groupName); } catch { } } } }
public void CanManageGeoDisasterRecoveryPairing() { using (var context = FluentMockContext.Start(GetType().FullName)) { var region = Region.USEast; var groupName = TestUtilities.GenerateName("rgns"); var geodrName = TestUtilities.GenerateName("geodr"); var namespaceName1 = TestUtilities.GenerateName("ns111"); var namespaceName2 = TestUtilities.GenerateName("ns222"); var azure = TestHelper.CreateRollupClient(); try { IEventHubNamespace primaryNamespace = azure.EventHubNamespaces .Define(namespaceName1) .WithRegion(Region.USSouthCentral) .WithNewResourceGroup(groupName) .Create(); IEventHubNamespace secondaryNamespace = azure.EventHubNamespaces .Define(namespaceName2) .WithRegion(Region.USNorthCentral) .WithExistingResourceGroup(groupName) .Create(); Exception exception = null; Exception breakingFailed = null; IEventHubDisasterRecoveryPairing pairing = null; try { pairing = azure.EventHubDisasterRecoveryPairings .Define(geodrName) .WithExistingPrimaryNamespace(primaryNamespace) .WithExistingSecondaryNamespace(secondaryNamespace) .Create(); while (pairing.ProvisioningState != ProvisioningStateDR.Succeeded) { pairing = pairing.Refresh(); SdkContext.DelayProvider.Delay(15 * 1000); if (pairing.ProvisioningState == ProvisioningStateDR.Failed) { Assert.True(false, "Provisioning state of the pairing is FAILED"); } } Assert.Equal(pairing.Name, geodrName, ignoreCase: true); Assert.Equal(pairing.PrimaryNamespaceResourceGroupName, groupName, ignoreCase: true); Assert.Equal(pairing.PrimaryNamespaceName, primaryNamespace.Name, ignoreCase: true); Assert.Equal(pairing.SecondaryNamespaceId, secondaryNamespace.Id, ignoreCase: true); var rules = pairing.ListAuthorizationRules(); Assert.True(rules.Count() > 0); foreach (IDisasterRecoveryPairingAuthorizationRule rule in rules) { IDisasterRecoveryPairingAuthorizationKey keys = rule.GetKeys(); Assert.NotNull(keys.AliasPrimaryConnectionString); Assert.NotNull(keys.AliasPrimaryConnectionString); Assert.NotNull(keys.PrimaryKey); Assert.NotNull(keys.SecondaryKey); } IEventHubDisasterRecoveryPairings pairingsCol = azure.EventHubDisasterRecoveryPairings; var pairings = pairingsCol .ListByNamespace(primaryNamespace.ResourceGroupName, primaryNamespace.Name); Assert.True(pairings.Count() > 0); bool found = false; foreach (IEventHubDisasterRecoveryPairing pairing1 in pairings) { if (pairing1.Name.Equals(pairing.Name, StringComparison.OrdinalIgnoreCase)) { found = true; Assert.Equal(pairing1.PrimaryNamespaceResourceGroupName, groupName); Assert.Equal(pairing1.PrimaryNamespaceName, primaryNamespace.Name); Assert.Equal(pairing1.SecondaryNamespaceId, secondaryNamespace.Id); } } Assert.True(found); } catch (Exception ex) { exception = ex; } finally { if (exception != null && pairing != null) { // Resource group cannot be deleted if the pairing-replication is // (backgroud work by RP) progress so pairing must forcefully break. try { pairing.BreakPairing(); } catch (Exception ex) { breakingFailed = ex; } } } if (exception != null && breakingFailed != null) { AggregateException cex = new AggregateException(exception, breakingFailed); throw cex; } if (exception != null) { throw exception; } if (breakingFailed != null) { throw breakingFailed; } pairing.Refresh(); pairing.FailOver(); } finally { try { azure.ResourceGroups.BeginDeleteByName(groupName); } catch { } } } }
public void CanManageAuthorizationRules() { using (var context = FluentMockContext.Start(GetType().FullName)) { var region = Region.USEast; var groupName = TestUtilities.GenerateName("rgns"); var namespaceName = TestUtilities.GenerateName("ns111"); var namespaceName2 = TestUtilities.GenerateName("ns222"); var namespaceName3 = TestUtilities.GenerateName("ns333"); var azure = TestHelper.CreateRollupClient(); try { IEventHubNamespace ehNamespace = azure.EventHubNamespaces .Define(namespaceName) .WithRegion(region) .WithNewResourceGroup(groupName) .WithNewManageRule("mngRule1") .WithNewSendRule("sndRule1") .Create(); Assert.NotNull(ehNamespace); Assert.NotNull(ehNamespace.Inner); var rules = ehNamespace.ListAuthorizationRules(); HashSet <string> set = new HashSet <string>(); foreach (IEventHubNamespaceAuthorizationRule rule in rules) { set.Add(rule.Name); } Assert.Contains("mngRule1", set); Assert.Contains("sndRule1", set); rules = azure.EventHubNamespaces .AuthorizationRules .ListByNamespace(ehNamespace.ResourceGroupName, ehNamespace.Name); set.Clear(); foreach (IEventHubNamespaceAuthorizationRule rule in rules) { set.Add(rule.Name); } Assert.Contains("mngRule1", set); Assert.Contains("sndRule1", set); azure.EventHubNamespaces .AuthorizationRules .Define("sndRule2") .WithExistingNamespaceId(ehNamespace.Id) .WithSendAccess() .Create(); rules = ehNamespace.ListAuthorizationRules(); set.Clear(); foreach (IEventHubNamespaceAuthorizationRule rule in rules) { set.Add(rule.Name); } Assert.Contains("mngRule1", set); Assert.Contains("sndRule1", set); Assert.Contains("sndRule2", set); } finally { try { azure.ResourceGroups.DeleteByName(groupName); } catch { } } } }
/// <summary> /// Specifies that authorization rule needs to be created for the given event hub namespace. /// </summary> /// <param name="namespace">The namespace.</param> /// <return>The next stage of the definition.</return> EventHubNamespaceAuthorizationRule.Definition.IWithAccessPolicy EventHubNamespaceAuthorizationRule.Definition.IWithNamespace.WithExistingNamespace(IEventHubNamespace ehNamespace) { return(this.WithExistingNamespace(ehNamespace) as EventHubNamespaceAuthorizationRule.Definition.IWithAccessPolicy); }
/// <summary> /// Specifies that the given namespace should be used as secondary namespace in disaster recovery pairing. /// </summary> /// <param name="namespace">The secondary event hub namespace.</param> /// <return>Next stage of the disaster recovery pairing update.</return> EventHubDisasterRecoveryPairing.Update.IUpdate EventHubDisasterRecoveryPairing.Update.IWithSecondaryNamespace.WithExistingSecondaryNamespace(IEventHubNamespace ehNamespace) { return(this.WithExistingSecondaryNamespace(ehNamespace)); }
/// <summary> /// Specifies that the given namespace should be used as secondary namespace in disaster recovery pairing. /// </summary> /// <param name="namespace">The secondary namespace.</param> /// <return>Next stage of the disaster recovery pairing definition.</return> EventHubDisasterRecoveryPairing.Definition.IWithCreate EventHubDisasterRecoveryPairing.Definition.IWithSecondaryNamespace.WithExistingSecondaryNamespace(IEventHubNamespace ehNamespace) { return(this.WithExistingSecondaryNamespace(ehNamespace)); }