public async Task NamespaceCreateGetUpdateDelete()
        {
            var location      = GetLocation();
            var resourceGroup = Recording.GenerateAssetName(Helper.ResourceGroupPrefix);
            await Helper.TryRegisterResourceGroupAsync(ResourceGroupsOperations, location.Result, resourceGroup);

            var namespaceName           = Recording.GenerateAssetName(Helper.NamespacePrefix);
            var operationsResponse      = Operations.ListAsync();
            var checkNameAvailable      = NamespacesOperations.CheckNameAvailabilityAsync(new CheckNameAvailabilityParameter(namespaceName));
            var createNamespaceResponse = await NamespacesOperations.StartCreateOrUpdateAsync(resourceGroup, namespaceName,
                                                                                              new EHNamespace()
            {
                Location = location.Result
            }
                                                                                              );

            var np = (await WaitForCompletionAsync(createNamespaceResponse)).Value;

            Assert.NotNull(createNamespaceResponse);
            Assert.AreEqual(np.Name, namespaceName);
            DelayInTest(60);
            //get the created namespace
            var getNamespaceResponse = await NamespacesOperations.GetAsync(resourceGroup, namespaceName);

            if (string.Compare(getNamespaceResponse.Value.ProvisioningState, "Succeeded", true) != 0)
            {
                DelayInTest(10);
            }
            getNamespaceResponse = await NamespacesOperations.GetAsync(resourceGroup, namespaceName);

            Assert.NotNull(getNamespaceResponse);
            Assert.AreEqual("Succeeded", getNamespaceResponse.Value.ProvisioningState, StringComparer.CurrentCultureIgnoreCase.ToString());
            Assert.AreEqual(location.Result, getNamespaceResponse.Value.Location);
            // Get all namespaces created within a resourceGroup
            var getAllNamespacesResponse = NamespacesOperations.ListByResourceGroupAsync(resourceGroup);

            Assert.NotNull(getAllNamespacesResponse);
            //Assert.True(getAllNamespacesResponse.AsPages.c >= 1);
            bool isContainnamespaceName = false;
            bool isContainresourceGroup = false;
            var  list = await getAllNamespacesResponse.ToEnumerableAsync();

            foreach (var name in list)
            {
                if (name.Name == namespaceName)
                {
                    isContainnamespaceName = true;
                }
            }
            foreach (var name in list)
            {
                if (name.Id.Contains(resourceGroup))
                {
                    isContainresourceGroup = true;
                    break;
                }
            }
            Assert.True(isContainnamespaceName);
            Assert.True(isContainresourceGroup);
            // Get all namespaces created within the subscription irrespective of the resourceGroup
            var getAllNpResponse = NamespacesOperations.ListAsync();

            Assert.NotNull(getAllNamespacesResponse);
            // Update namespace tags and make the namespace critical
            var updateNamespaceParameter = new EHNamespace()
            {
                Tags = new Dictionary <string, string>()
                {
                    { "tag3", "value3" },
                    { "tag4", "value4" }
                }
            };
            // Will uncomment the assertions once the service is deployed
            var updateNamespaceResponse = NamespacesOperations.UpdateAsync(resourceGroup, namespaceName, updateNamespaceParameter);

            Assert.NotNull(updateNamespaceResponse);
            // Get the updated namespace and also verify the Tags.
            getNamespaceResponse = await NamespacesOperations.GetAsync(resourceGroup, namespaceName);

            DelayInTest(15);
            Assert.NotNull(getNamespaceResponse);
            Assert.AreEqual(location.Result, getNamespaceResponse.Value.Location);
            Assert.AreEqual(namespaceName, getNamespaceResponse.Value.Name);
            Assert.AreEqual(2, getNamespaceResponse.Value.Tags.Count);
            bool IsContainKey   = false;
            bool IsContainValue = false;

            foreach (var tag in updateNamespaceParameter.Tags)
            {
                foreach (var t in getNamespaceResponse.Value.Tags)
                {
                    if (t.Key == tag.Key)
                    {
                        IsContainKey = true;
                        break;
                    }
                }
                foreach (var t in getNamespaceResponse.Value.Tags)
                {
                    if (t.Value == tag.Value)
                    {
                        IsContainValue = true;
                        break;
                    }
                }
            }
            Assert.True(IsContainKey);
            Assert.True(IsContainValue);
            //delete namespace
            await WaitForCompletionAsync(await NamespacesOperations.StartDeleteAsync(resourceGroup, namespaceName));
        }
        public async Task EventhubCreateGetUpdateDeleteAuthorizationRules_Length()
        {
            var location = await GetLocation();

            var          resourceGroupName = Recording.GenerateAssetName(Helper.ResourceGroupPrefix);
            Subscription sub = await ArmClient.GetDefaultSubscriptionAsync();

            await sub.GetResourceGroups().CreateOrUpdateAsync(resourceGroupName, new ResourceGroupData(location));;
            var namespaceName           = Recording.GenerateAssetName(Helper.NamespacePrefix);
            var createNamespaceResponse = await NamespacesOperations.StartCreateOrUpdateAsync(resourceGroupName, namespaceName,
                                                                                              new EHNamespace()
            {
                Location = location,
                //Sku = new Sku("as")
                Tags =
                {
                    { "tag1", "value1" },
                    { "tag2", "value2" }
                }
            }
                                                                                              );

            var np = (await WaitForCompletionAsync(createNamespaceResponse)).Value;

            Assert.NotNull(createNamespaceResponse);
            Assert.AreEqual(np.Name, namespaceName);
            DelayInTest(5);
            //get the created namespace
            var getNamespaceResponse = await NamespacesOperations.GetAsync(resourceGroupName, namespaceName);

            if (string.Compare(getNamespaceResponse.Value.ProvisioningState, "Succeeded", true) != 0)
            {
                DelayInTest(5);
            }
            getNamespaceResponse = await NamespacesOperations.GetAsync(resourceGroupName, namespaceName);

            Assert.NotNull(getNamespaceResponse);
            Assert.AreEqual("Succeeded", getNamespaceResponse.Value.ProvisioningState, StringComparer.CurrentCultureIgnoreCase.ToString());
            Assert.AreEqual(location, getNamespaceResponse.Value.Location);
            // Create Eventhub
            var eventHubName           = Helper.EventHubPrefix + "thisisthenamewithmorethan53charschecktoverifytheremovlaof50charsnamelengthlimit";
            var createEventHubResponse = await EventHubsOperations.CreateOrUpdateAsync(resourceGroupName, namespaceName, eventHubName, new Eventhub()
            {
                MessageRetentionInDays = 5
            });

            Assert.NotNull(createEventHubResponse);
            Assert.AreEqual(createEventHubResponse.Value.Name, eventHubName);
            //Get the created EventHub
            var getEventHubResponse = await EventHubsOperations.GetAsync(resourceGroupName, namespaceName, eventHubName);

            Assert.NotNull(getEventHubResponse.Value);
            Assert.AreEqual(EntityStatus.Active, getEventHubResponse.Value.Status);
            Assert.AreEqual(getEventHubResponse.Value.Name, eventHubName);
            // Create a EventHub AuthorizationRule
            var    authorizationRuleName           = Helper.AuthorizationRulesPrefix + "thisisthenamewithmorethan53charschecktoverifytheremovlaof50charsnamelengthlimit";
            string createPrimaryKey                = Recording.GetVariable("CreatePrimaryKey", Helper.GenerateRandomKey());
            var    createAutorizationRuleParameter = new AuthorizationRule()
            {
                Rights = { AccessRights.Listen, AccessRights.Send }
            };
            var createEventhubAuthorizationRuleResponse = await EventHubsOperations.CreateOrUpdateAuthorizationRuleAsync(resourceGroupName, namespaceName, eventHubName,
                                                                                                                         authorizationRuleName, createAutorizationRuleParameter);

            Assert.NotNull(createEventhubAuthorizationRuleResponse);
            Assert.True(createEventhubAuthorizationRuleResponse.Value.Rights.Count == createAutorizationRuleParameter.Rights.Count);

            Assert.True(isContains(createAutorizationRuleParameter.Rights, createEventhubAuthorizationRuleResponse.Value.Rights));
            // Get created Eventhub AuthorizationRules
            var getEventhubAuthorizationRulesResponse = await EventHubsOperations.GetAuthorizationRuleAsync(resourceGroupName, namespaceName, eventHubName, authorizationRuleName);

            Assert.NotNull(getEventhubAuthorizationRulesResponse);
            Assert.True(getEventhubAuthorizationRulesResponse.Value.Rights.Count == createAutorizationRuleParameter.Rights.Count);
            Assert.True(isContains(createAutorizationRuleParameter.Rights, getEventhubAuthorizationRulesResponse.Value.Rights));
            // Get all Eventhub AuthorizationRules
            var getAllNamespaceAuthorizationRulesResponse = EventHubsOperations.ListAuthorizationRulesAsync(resourceGroupName, namespaceName, eventHubName);

            Assert.NotNull(getAllNamespaceAuthorizationRulesResponse);
            var getAllNSPAuthRulesRespList = await getAllNamespaceAuthorizationRulesResponse.ToEnumerableAsync();

            Assert.True(getAllNSPAuthRulesRespList.Count() == 1);
            bool isContainauthorizationRuleName = false;

            foreach (var detail in getAllNSPAuthRulesRespList)
            {
                if (detail.Name == authorizationRuleName)
                {
                    isContainauthorizationRuleName = true;
                    break;
                }
            }
            Assert.True(isContainauthorizationRuleName);
            // Update Eventhub authorizationRule
            string            updatePrimaryKey = Recording.GetVariable("UpdatePrimaryKey", Helper.GenerateRandomKey());
            AuthorizationRule updateEventhubAuthorizationRuleParameter = new AuthorizationRule();

            updateEventhubAuthorizationRuleParameter.Rights.Add(AccessRights.Listen);
            var updateEventhubAuthorizationRuleResponse = await EventHubsOperations.CreateOrUpdateAuthorizationRuleAsync(resourceGroupName,
                                                                                                                         namespaceName, eventHubName, authorizationRuleName, updateEventhubAuthorizationRuleParameter);

            Assert.NotNull(updateEventhubAuthorizationRuleResponse);
            Assert.AreEqual(authorizationRuleName, updateEventhubAuthorizationRuleResponse.Value.Name);
            Assert.True(updateEventhubAuthorizationRuleResponse.Value.Rights.Count == updateEventhubAuthorizationRuleParameter.Rights.Count);
            Assert.True(isContains(updateEventhubAuthorizationRuleParameter.Rights, updateEventhubAuthorizationRuleResponse.Value.Rights));
            // Get the updated Eventhub AuthorizationRule
            var getEventhubAuthorizationRuleResponse = await EventHubsOperations.GetAuthorizationRuleAsync(resourceGroupName, namespaceName, eventHubName,
                                                                                                           authorizationRuleName);

            Assert.NotNull(getEventhubAuthorizationRuleResponse);
            Assert.AreEqual(authorizationRuleName, getEventhubAuthorizationRuleResponse.Value.Name);
            Assert.True(getEventhubAuthorizationRuleResponse.Value.Rights.Count == updateEventhubAuthorizationRuleParameter.Rights.Count);
            Assert.True(isContains(updateEventhubAuthorizationRuleParameter.Rights, getEventhubAuthorizationRuleResponse.Value.Rights));
            // Get the connectionString to the Eventhub for a Authorization rule created
            var listKeysResponse = await EventHubsOperations.ListKeysAsync(resourceGroupName, namespaceName, eventHubName, authorizationRuleName);

            Assert.NotNull(listKeysResponse);
            Assert.NotNull(listKeysResponse.Value.PrimaryConnectionString);
            Assert.NotNull(listKeysResponse.Value.SecondaryConnectionString);
            //New connection string
            var regenerateConnection_primary = await EventHubsOperations.RegenerateKeysAsync(resourceGroupName, namespaceName, eventHubName, authorizationRuleName, new RegenerateAccessKeyParameters(KeyType.PrimaryKey));

            Assert.NotNull(regenerateConnection_primary);
            // Assert.AreNotEqual(listKeysResponse.Value.PrimaryConnectionString, regenerateConnection_primary.Value.PrimaryConnectionString);
            Assert.AreEqual(listKeysResponse.Value.SecondaryConnectionString, regenerateConnection_primary.Value.SecondaryConnectionString);
            var regenerateConnection_Secondary = await EventHubsOperations.RegenerateKeysAsync(resourceGroupName, namespaceName, eventHubName, authorizationRuleName, new RegenerateAccessKeyParameters(KeyType.SecondaryKey));

            Assert.NotNull(regenerateConnection_Secondary);
            // Assert.AreNotEqual(listKeysResponse.Value.SecondaryConnectionString, regenerateConnection_Secondary.Value.SecondaryConnectionString);
            Assert.AreEqual(regenerateConnection_primary.Value.PrimaryConnectionString, regenerateConnection_Secondary.Value.PrimaryConnectionString);
            // Delete Eventhub authorizationRule
            await EventHubsOperations.DeleteAuthorizationRuleAsync(resourceGroupName, namespaceName, eventHubName, authorizationRuleName);

            DelayInTest(5);
            // Delete Eventhub and check for the NotFound exception
            await EventHubsOperations.DeleteAsync(resourceGroupName, namespaceName, eventHubName);

            // Delete namespace and check for the NotFound exception
            await WaitForCompletionAsync(await NamespacesOperations.StartDeleteAsync(resourceGroupName, namespaceName));
        }
Beispiel #3
0
        public async Task NetworkRuleSetCreateGetUpdateDelete()
        {
            var location = await GetLocation();

            var          resourceGroupName = Recording.GenerateAssetName(Helper.ResourceGroupPrefix);
            Subscription sub = await ArmClient.GetDefaultSubscriptionAsync();

            ResourceGroup resourceGroup = await sub.GetResourceGroups().CreateOrUpdate(resourceGroupName, new ResourceGroupData(location)).WaitForCompletionAsync();

            // Prepare VNet
            var vnetName   = Recording.GenerateAssetName("sdktestvnet");
            var parameters = new VirtualNetworkData
            {
                AddressSpace = new AddressSpace {
                    AddressPrefixes = { "10.0.0.0/16" }
                },
                Subnets =
                {
                    new SubnetData
                    {
                        Name             = "default1",
                        AddressPrefix    = "10.0.0.0/24",
                        ServiceEndpoints ={ new ServiceEndpointPropertiesFormat                        {
                          Service = "Microsoft.EventHub"
                      } }
                    },
                    new SubnetData
                    {
                        Name             = "default2",
                        AddressPrefix    = "10.0.1.0/24",
                        ServiceEndpoints ={ new ServiceEndpointPropertiesFormat                        {
                          Service = "Microsoft.EventHub"
                      } }
                    },
                    new SubnetData
                    {
                        Name             = "default3",
                        AddressPrefix    = "10.0.2.0/24",
                        ServiceEndpoints ={ new ServiceEndpointPropertiesFormat                        {
                          Service = "Microsoft.EventHub"
                      } }
                    }
                },
                Location = "eastus2"
            };

            await WaitForCompletionAsync(await resourceGroup.GetVirtualNetworks().CreateOrUpdateAsync(vnetName, parameters));

            // Create a namespace
            var namespaceName           = Recording.GenerateAssetName(Helper.NamespacePrefix);
            var createNamespaceResponse = await NamespacesOperations.StartCreateOrUpdateAsync(resourceGroupName, namespaceName,
                                                                                              new EHNamespace()
            {
                Location = location,
                Tags     =
                {
                    { "tag1", "value1" },
                    { "tag2", "value2" }
                }
            }
                                                                                              );

            var np = (await WaitForCompletionAsync(createNamespaceResponse)).Value;

            Assert.NotNull(createNamespaceResponse);
            Assert.AreEqual(np.Name, namespaceName);
            DelayInTest(5);
            //get the created namespace
            var getNamespaceResponse = await NamespacesOperations.GetAsync(resourceGroupName, namespaceName);

            if (string.Compare(getNamespaceResponse.Value.ProvisioningState, "Succeeded", true) != 0)
            {
                DelayInTest(5);
            }
            getNamespaceResponse = await NamespacesOperations.GetAsync(resourceGroupName, namespaceName);

            Assert.NotNull(getNamespaceResponse);
            Assert.AreEqual("Succeeded", getNamespaceResponse.Value.ProvisioningState, StringComparer.CurrentCultureIgnoreCase.ToString());
            Assert.AreEqual(location, getNamespaceResponse.Value.Location, StringComparer.CurrentCultureIgnoreCase.ToString());

            var netWorkRuleSet = await NamespacesOperations.CreateOrUpdateNetworkRuleSetAsync(resourceGroupName, namespaceName,
                                                                                              new NetworkRuleSet()
            {
                DefaultAction       = DefaultAction.Deny,
                VirtualNetworkRules =
                {
                    new NWRuleSetVirtualNetworkRules()
                    {
                        Subnet = new ResourceManager.EventHubs.Models.Subnet("/subscriptions/" + SubscriptionId + "/resourcegroups/" + resourceGroupName + "/providers/Microsoft.Network/virtualNetworks/" + vnetName + "/subnets/default1")
                    },
                    new NWRuleSetVirtualNetworkRules()
                    {
                        Subnet = new ResourceManager.EventHubs.Models.Subnet("/subscriptions/" + SubscriptionId + "/resourcegroups/" + resourceGroupName + "/providers/Microsoft.Network/virtualNetworks/" + vnetName + "/subnets/default2")
                    },
                    new NWRuleSetVirtualNetworkRules()
                    {
                        Subnet = new ResourceManager.EventHubs.Models.Subnet("/subscriptions/" + SubscriptionId + "/resourcegroups/" + resourceGroupName + "/providers/Microsoft.Network/virtualNetworks/" + vnetName + "/subnets/default3")
                    }
                },
                IpRules =
                {
                    new NWRuleSetIpRules()
                    {
                        IpMask = "1.1.1.1", Action = "Allow"
                    },
                    new NWRuleSetIpRules()
                    {
                        IpMask = "1.1.1.2", Action = "Allow"
                    },
                    new NWRuleSetIpRules()
                    {
                        IpMask = "1.1.1.3", Action = "Allow"
                    },
                    new NWRuleSetIpRules()
                    {
                        IpMask = "1.1.1.4", Action = "Allow"
                    },
                    new NWRuleSetIpRules()
                    {
                        IpMask = "1.1.1.5", Action = "Allow"
                    }
                }
            });

            var getNetworkRuleSet = await NamespacesOperations.GetNetworkRuleSetAsync(resourceGroupName, namespaceName);

            var netWorkRuleSet1 = await NamespacesOperations.CreateOrUpdateNetworkRuleSetAsync(resourceGroupName, namespaceName, new NetworkRuleSet()
            {
                DefaultAction = "Allow"
            });

            var getNetworkRuleSet1 = await NamespacesOperations.GetNetworkRuleSetAsync(resourceGroupName, namespaceName);

            DelayInTest(60);
            //Delete namespace
            await WaitForCompletionAsync(await NamespacesOperations.StartDeleteAsync(resourceGroupName, namespaceName));
        }
        public async Task NamespaceKafkaCreateGetUpdateDelete()
        {
            var location          = "West US";
            var resourceGroupName = Recording.GenerateAssetName(Helper.ResourceGroupPrefix);
            await ArmClient.DefaultSubscription.GetResourceGroups().CreateOrUpdateAsync(resourceGroupName, new ResourceGroupData(location));

            var namespaceName           = Recording.GenerateAssetName(Helper.NamespacePrefix);
            var createNamespaceResponse = await NamespacesOperations.StartCreateOrUpdateAsync(resourceGroupName, namespaceName,
                                                                                              new EHNamespace()
            {
                Location = location,
                Tags     =
                {
                    { "tag1", "value1" },
                    { "tag2", "value2" }
                },
                IsAutoInflateEnabled   = true,
                MaximumThroughputUnits = 10,
                KafkaEnabled           = true
            }
                                                                                              );

            var np = (await WaitForCompletionAsync(createNamespaceResponse)).Value;

            Assert.NotNull(createNamespaceResponse);
            Assert.AreEqual(np.Name, namespaceName);
            Assert.True(np.KafkaEnabled, "KafkaEnabled is false");
            DelayInTest(5);
            //get the created namespace
            var getNamespaceResponse = await NamespacesOperations.GetAsync(resourceGroupName, namespaceName);

            if (string.Compare(getNamespaceResponse.Value.ProvisioningState, "Succeeded", true) != 0)
            {
                DelayInTest(5);
            }
            getNamespaceResponse = await NamespacesOperations.GetAsync(resourceGroupName, namespaceName);

            Assert.NotNull(getNamespaceResponse);
            Assert.AreEqual("Succeeded", getNamespaceResponse.Value.ProvisioningState, StringComparer.CurrentCultureIgnoreCase.ToString());
            Assert.AreEqual(location, getNamespaceResponse.Value.Location);
            // Get all namespaces created within a resourceGroupName
            var getAllNamespacesResponse = NamespacesOperations.ListByResourceGroupAsync(resourceGroupName);

            Assert.NotNull(getAllNamespacesResponse);
            //Assert.True(getAllNamespacesResponse.AsPages.c >= 1);
            var getAllNamespRespList = await getAllNamespacesResponse.ToEnumerableAsync();

            bool isContainnamespaceName = false;
            bool isContainresourceGroup = false;

            foreach (var name in getAllNamespRespList)
            {
                if (name.Name == namespaceName)
                {
                    isContainnamespaceName = true;
                    break;
                }
            }
            foreach (var name in getAllNamespRespList)
            {
                if (name.Id.Contains(resourceGroupName))
                {
                    isContainresourceGroup = true;
                    break;
                }
            }
            Assert.True(isContainnamespaceName);
            Assert.True(isContainresourceGroup);
            // Get all namespaces created within the subscription irrespective of the resourceGroupName
            getAllNamespacesResponse = NamespacesOperations.ListAsync();
            Assert.NotNull(getAllNamespacesResponse);
            bool isContainNamespacename = false;

            getAllNamespRespList = await getAllNamespacesResponse.ToEnumerableAsync();

            foreach (var getNameSpResp in getAllNamespRespList)
            {
                if (getNameSpResp.Name == namespaceName)
                {
                    isContainNamespacename = true;
                    break;
                }
            }
            Assert.IsTrue(isContainNamespacename);
            // Update namespace tags and make the namespace critical
            var updateNamespaceParameter = new EHNamespace()
            {
                Tags =
                {
                    { "tag3", "value3" },
                    { "tag4", "value4" }
                }
            };
            // Will uncomment the assertions once the service is deployed
            var updateNamespaceResponse = await NamespacesOperations.UpdateAsync(resourceGroupName, namespaceName, updateNamespaceParameter);

            Assert.NotNull(updateNamespaceResponse);
            Assert.True(updateNamespaceResponse.Value.ProvisioningState.Equals("Active", StringComparison.CurrentCultureIgnoreCase) ||
                        updateNamespaceResponse.Value.ProvisioningState.Equals("Updating", StringComparison.CurrentCultureIgnoreCase));
            Assert.AreEqual(namespaceName, updateNamespaceResponse.Value.Name);
            // Get the updated namespace and also verify the Tags.
            getNamespaceResponse = await NamespacesOperations.GetAsync(resourceGroupName, namespaceName);

            Assert.NotNull(getNamespaceResponse);
            Assert.AreEqual(location, getNamespaceResponse.Value.Location);
            Assert.AreEqual(namespaceName, getNamespaceResponse.Value.Name);
            Assert.AreEqual(2, getNamespaceResponse.Value.Tags.Count);
            bool isContainKey   = false;
            bool isContainValue = false;

            foreach (var tag in updateNamespaceParameter.Tags)
            {
                foreach (var key in getNamespaceResponse.Value.Tags)
                {
                    if (key.Key == tag.Key)
                    {
                        isContainKey = true;
                        break;
                    }
                }
                foreach (var value in getNamespaceResponse.Value.Tags)
                {
                    if (value.Value == tag.Value)
                    {
                        isContainValue = true;
                        break;
                    }
                }
                //Assert.Contains(getNamespaceResponse.Value.Tags, t => t.Key.Equals(tag.Key));
                //Assert.Contains(getNamespaceResponse.Value.Tags, t => t.Value.Equals(tag.Value));
            }
            Assert.True(isContainKey);
            Assert.True(isContainValue);
            DelayInTest(5);
            // Delete namespace
            await WaitForCompletionAsync(await NamespacesOperations.StartDeleteAsync(resourceGroupName, namespaceName));
        }
        public async Task NamespaceCreateGetUpdateDeleteAuthorizationRules()
        {
            var location = await GetLocation();

            var resourceGroupName = Recording.GenerateAssetName(Helper.ResourceGroupPrefix);
            await ArmClient.DefaultSubscription.GetResourceGroups().CreateOrUpdateAsync(resourceGroupName, new ResourceGroupData(location));;
            var namespaceName           = Recording.GenerateAssetName(Helper.NamespacePrefix);
            var createNamespaceResponse = await NamespacesOperations.StartCreateOrUpdateAsync(resourceGroupName, namespaceName,
                                                                                              new EHNamespace()
            {
                Location = location,
                //Sku = new Sku("as")
                Tags =
                {
                    { "tag1", "value1" },
                    { "tag2", "value2" }
                }
            }
                                                                                              );

            var np = (await WaitForCompletionAsync(createNamespaceResponse)).Value;

            Assert.NotNull(createNamespaceResponse);
            Assert.AreEqual(np.Name, namespaceName);
            DelayInTest(5);
            //get the created namespace
            var getNamespaceResponse = await NamespacesOperations.GetAsync(resourceGroupName, namespaceName);

            if (string.Compare(getNamespaceResponse.Value.ProvisioningState, "Succeeded", true) != 0)
            {
                DelayInTest(5);
            }
            getNamespaceResponse = await NamespacesOperations.GetAsync(resourceGroupName, namespaceName);

            Assert.NotNull(getNamespaceResponse);
            Assert.AreEqual("Succeeded", getNamespaceResponse.Value.ProvisioningState, StringComparer.CurrentCultureIgnoreCase.ToString());
            Assert.AreEqual(location, getNamespaceResponse.Value.Location);
            // Create a namespace AuthorizationRule
            var    authorizationRuleName           = Recording.GenerateAssetName(Helper.AuthorizationRulesPrefix);
            string createPrimaryKey                = Recording.GetVariable("authorizaRuNa", Helper.GenerateRandomKey());
            var    createAutorizationRuleParameter = new AuthorizationRule()
            {
                Rights = { AccessRights.Listen, AccessRights.Send }
            };
            var createNamespaceAuthorizationRuleResponse = await NamespacesOperations.CreateOrUpdateAuthorizationRuleAsync(resourceGroupName, namespaceName,
                                                                                                                           authorizationRuleName, createAutorizationRuleParameter);

            Assert.NotNull(createNamespaceAuthorizationRuleResponse);
            Assert.True(createNamespaceAuthorizationRuleResponse.Value.Rights.Count == createAutorizationRuleParameter.Rights.Count);
            Assert.True(isContains(createAutorizationRuleParameter.Rights, createNamespaceAuthorizationRuleResponse.Value.Rights));
            // Get default namespace AuthorizationRules
            var getNamespaceAuthorizationRulesResponse = await NamespacesOperations.GetAuthorizationRuleAsync(resourceGroupName, namespaceName, Helper.DefaultNamespaceAuthorizationRule);

            Assert.NotNull(getNamespaceAuthorizationRulesResponse);
            Assert.AreEqual(getNamespaceAuthorizationRulesResponse.Value.Name, Helper.DefaultNamespaceAuthorizationRule);
            var accessRights = new List <AccessRights>()
            {
                AccessRights.Listen, AccessRights.Send, AccessRights.Manage
            };

            Assert.True(isContains(getNamespaceAuthorizationRulesResponse.Value.Rights, accessRights));
            // Get created namespace AuthorizationRules
            getNamespaceAuthorizationRulesResponse = await NamespacesOperations.GetAuthorizationRuleAsync(resourceGroupName, namespaceName, authorizationRuleName);

            Assert.NotNull(getNamespaceAuthorizationRulesResponse);
            Assert.True(getNamespaceAuthorizationRulesResponse.Value.Rights.Count == createAutorizationRuleParameter.Rights.Count);
            Assert.True(isContains(createAutorizationRuleParameter.Rights, getNamespaceAuthorizationRulesResponse.Value.Rights));
            // Get all namespaces AuthorizationRules
            var getAllNamespaceAuthorizationRulesResponse = NamespacesOperations.ListAuthorizationRulesAsync(resourceGroupName, namespaceName);

            Assert.NotNull(getAllNamespaceAuthorizationRulesResponse);
            var getAllNpsAuthResp = await getAllNamespaceAuthorizationRulesResponse.ToEnumerableAsync();

            Assert.True(getAllNpsAuthResp.Count() > 1);
            bool isContainAuthorizationRuleName    = false;
            bool isContainEventHubManagementHelper = false;

            foreach (var Authrule in getAllNpsAuthResp)
            {
                if (Authrule.Name == authorizationRuleName)
                {
                    isContainAuthorizationRuleName = true;
                    break;
                }
            }
            foreach (var Authrule in getAllNpsAuthResp)
            {
                if (Authrule.Name == Helper.DefaultNamespaceAuthorizationRule)
                {
                    isContainEventHubManagementHelper = true;
                    break;
                }
            }
            Assert.True(isContainAuthorizationRuleName);
            Assert.True(isContainEventHubManagementHelper);
            // Update namespace authorizationRule
            string            updatePrimaryKey = Recording.GetVariable("UpdatePrimaryKey", Helper.GenerateRandomKey());
            AuthorizationRule updateNamespaceAuthorizationRuleParameter = new AuthorizationRule();

            updateNamespaceAuthorizationRuleParameter.Rights.Add(AccessRights.Listen);
            var updateNamespaceAuthorizationRuleResponse = await NamespacesOperations.CreateOrUpdateAuthorizationRuleAsync(resourceGroupName,
                                                                                                                           namespaceName, authorizationRuleName, updateNamespaceAuthorizationRuleParameter);

            Assert.NotNull(updateNamespaceAuthorizationRuleResponse);
            Assert.AreEqual(authorizationRuleName, updateNamespaceAuthorizationRuleResponse.Value.Name);
            Assert.True(updateNamespaceAuthorizationRuleResponse.Value.Rights.Count == updateNamespaceAuthorizationRuleParameter.Rights.Count);
            Assert.True(isContains(updateNamespaceAuthorizationRuleParameter.Rights, updateNamespaceAuthorizationRuleResponse.Value.Rights));
            // Get the updated namespace AuthorizationRule
            var getNamespaceAuthorizationRuleResponse = await NamespacesOperations.GetAuthorizationRuleAsync(resourceGroupName, namespaceName,
                                                                                                             authorizationRuleName);

            Assert.NotNull(getNamespaceAuthorizationRuleResponse);
            Assert.AreEqual(authorizationRuleName, getNamespaceAuthorizationRuleResponse.Value.Name);
            Assert.True(getNamespaceAuthorizationRuleResponse.Value.Rights.Count == updateNamespaceAuthorizationRuleParameter.Rights.Count);
            Assert.True(isContains(updateNamespaceAuthorizationRuleParameter.Rights, getNamespaceAuthorizationRuleResponse.Value.Rights));
            // Get the connection string to the namespace for a Authorization rule created
            var listKeysResponse = await NamespacesOperations.ListKeysAsync(resourceGroupName, namespaceName, authorizationRuleName);

            Assert.NotNull(listKeysResponse);
            Assert.NotNull(listKeysResponse.Value.PrimaryConnectionString);
            Assert.NotNull(listKeysResponse.Value.SecondaryConnectionString);
            // Regenerate connection string to the namespace for a Authorization rule created
            var NewKeysResponse_primary = await NamespacesOperations.RegenerateKeysAsync(resourceGroupName, namespaceName, authorizationRuleName, new RegenerateAccessKeyParameters(KeyType.PrimaryKey));

            Assert.NotNull(NewKeysResponse_primary);
            // Assert.AreNotEqual(NewKeysResponse_primary.Value.PrimaryConnectionString, listKeysResponse.Value.PrimaryConnectionString);
            Assert.AreEqual(NewKeysResponse_primary.Value.SecondaryConnectionString, listKeysResponse.Value.SecondaryConnectionString);
            // Regenerate connection string to the namespace for a Authorization rule created
            var NewKeysResponse_secondary = await NamespacesOperations.RegenerateKeysAsync(resourceGroupName, namespaceName, authorizationRuleName, new RegenerateAccessKeyParameters(KeyType.SecondaryKey));

            Assert.NotNull(NewKeysResponse_secondary);
            // Assert.AreNotEqual(NewKeysResponse_secondary.Value.PrimaryConnectionString, listKeysResponse.Value.PrimaryConnectionString);
            // Assert.AreNotEqual(NewKeysResponse_secondary.Value.SecondaryConnectionString, listKeysResponse.Value.SecondaryConnectionString);
            // Delete namespace authorizationRule
            await NamespacesOperations.DeleteAuthorizationRuleAsync(resourceGroupName, namespaceName, authorizationRuleName);

            DelayInTest(5);
            // Delete namespace
            await WaitForCompletionAsync(await NamespacesOperations.StartDeleteAsync(resourceGroupName, namespaceName));
        }
        public async Task EventhubCreateGetUpdateDeleteAuthorizationRules()
        {
            var location      = GetLocation();
            var resourceGroup = Recording.GenerateAssetName(Helper.ResourceGroupPrefix);
            await Helper.TryRegisterResourceGroupAsync(ResourceGroupsOperations, location.Result, resourceGroup);

            var namespaceName = Recording.GenerateAssetName(Helper.NamespacePrefix);

            var createNamespaceResponse = await NamespacesOperations.StartCreateOrUpdateAsync(resourceGroup, namespaceName,
                                                                                              new EHNamespace()
            {
                Location = location.Result,
                Sku      = new Sku(SkuName.Standard)
                {
                    Tier = SkuTier.Standard
                },
                Tags = new Dictionary <string, string>()
                {
                    { "tag1", "value1" },
                    { "tag2", "value2" }
                }
            }
                                                                                              );

            var np = (await WaitForCompletionAsync(createNamespaceResponse)).Value;

            Assert.NotNull(createNamespaceResponse);
            Assert.AreEqual(np.Name, namespaceName);
            DelayInTest(5);
            //get the created namespace
            var getNamespaceResponse = await NamespacesOperations.GetAsync(resourceGroup, namespaceName);

            if (string.Compare(getNamespaceResponse.Value.ProvisioningState, "Succeeded", true) != 0)
            {
                DelayInTest(5);
            }
            getNamespaceResponse = await NamespacesOperations.GetAsync(resourceGroup, namespaceName);

            Assert.NotNull(getNamespaceResponse);
            Assert.AreEqual("Succeeded", getNamespaceResponse.Value.ProvisioningState, StringComparer.CurrentCultureIgnoreCase.ToString());
            Assert.AreEqual(location.Result, getNamespaceResponse.Value.Location);
            // Create Eventhub
            var eventHubName           = Recording.GenerateAssetName(Helper.EventHubPrefix);
            var createEventHubResponse = await EventHubsOperations.CreateOrUpdateAsync(resourceGroup, namespaceName, eventHubName, new Eventhub()
            {
                MessageRetentionInDays = 5
            });

            Assert.NotNull(createEventHubResponse);
            Assert.AreEqual(createEventHubResponse.Value.Name, eventHubName);
            //Get the created EventHub
            var getEventHubResponse = await EventHubsOperations.GetAsync(resourceGroup, namespaceName, eventHubName);

            Assert.NotNull(getEventHubResponse.Value);
            Assert.AreEqual(EntityStatus.Active, getEventHubResponse.Value.Status);
            Assert.AreEqual(getEventHubResponse.Value.Name, eventHubName);
            // Create a EventHub AuthorizationRule
            var    authorizationRuleName           = Recording.GenerateAssetName(Helper.AuthorizationRulesPrefix);
            string createPrimaryKey                = Recording.GetVariable("CreatePrimaryKey", Helper.GenerateRandomKey());
            var    createAutorizationRuleParameter = new AuthorizationRule()
            {
                Rights = new List <AccessRights>()
                {
                    AccessRights.Listen, AccessRights.Send
                }
            };
            var createEventhubAuthorizationRuleResponse = await EventHubsOperations.CreateOrUpdateAuthorizationRuleAsync(resourceGroup, namespaceName, eventHubName,
                                                                                                                         authorizationRuleName, createAutorizationRuleParameter);

            Assert.NotNull(createEventhubAuthorizationRuleResponse);
            Assert.True(createEventhubAuthorizationRuleResponse.Value.Rights.Count == createAutorizationRuleParameter.Rights.Count);
            Assert.True(isContains(createAutorizationRuleParameter.Rights, createEventhubAuthorizationRuleResponse.Value.Rights));
            // Get created Eventhub AuthorizationRules
            var getEventhubAuthorizationRulesResponse = await EventHubsOperations.GetAuthorizationRuleAsync(resourceGroup, namespaceName, eventHubName, authorizationRuleName);

            Assert.NotNull(getEventhubAuthorizationRulesResponse);
            Assert.True(getEventhubAuthorizationRulesResponse.Value.Rights.Count == createAutorizationRuleParameter.Rights.Count);
            Assert.True(isContains(createAutorizationRuleParameter.Rights, getEventhubAuthorizationRulesResponse.Value.Rights));
            // Get all Eventhub AuthorizationRules
            var getAllNamespaceAuthorizationRulesResponse = EventHubsOperations.ListAuthorizationRulesAsync(resourceGroup, namespaceName, eventHubName);

            Assert.NotNull(getAllNamespaceAuthorizationRulesResponse);
            var getAllNSPAuthRulesRespList = await getAllNamespaceAuthorizationRulesResponse.ToEnumerableAsync();

            Assert.True(getAllNSPAuthRulesRespList.Count() == 1);
            bool isContainauthorizationRuleName = false;

            foreach (var detail in getAllNSPAuthRulesRespList)
            {
                if (detail.Name == authorizationRuleName)
                {
                    isContainauthorizationRuleName = true;
                    break;
                }
            }
            Assert.True(isContainauthorizationRuleName);
            // Update Eventhub authorizationRule
            string            updatePrimaryKey = Recording.GetVariable("UpdatePrimaryKey", Helper.GenerateRandomKey());
            AuthorizationRule updateEventhubAuthorizationRuleParameter = new AuthorizationRule();

            updateEventhubAuthorizationRuleParameter.Rights = new List <AccessRights>()
            {
                AccessRights.Listen
            };
            var updateEventhubAuthorizationRuleResponse = await EventHubsOperations.CreateOrUpdateAuthorizationRuleAsync(resourceGroup,
                                                                                                                         namespaceName, eventHubName, authorizationRuleName, updateEventhubAuthorizationRuleParameter);

            Assert.NotNull(updateEventhubAuthorizationRuleResponse);
            Assert.AreEqual(authorizationRuleName, updateEventhubAuthorizationRuleResponse.Value.Name);
            Assert.True(updateEventhubAuthorizationRuleResponse.Value.Rights.Count == updateEventhubAuthorizationRuleParameter.Rights.Count);
            Assert.True(isContains(updateEventhubAuthorizationRuleParameter.Rights, updateEventhubAuthorizationRuleResponse.Value.Rights));
            // Get the updated Eventhub AuthorizationRule
            var getEventhubAuthorizationRuleResponse = await EventHubsOperations.GetAuthorizationRuleAsync(resourceGroup, namespaceName, eventHubName,
                                                                                                           authorizationRuleName);

            Assert.NotNull(getEventhubAuthorizationRuleResponse);
            Assert.AreEqual(authorizationRuleName, getEventhubAuthorizationRuleResponse.Value.Name);
            Assert.True(getEventhubAuthorizationRuleResponse.Value.Rights.Count == updateEventhubAuthorizationRuleParameter.Rights.Count);
            Assert.True(isContains(updateEventhubAuthorizationRuleParameter.Rights, getEventhubAuthorizationRuleResponse.Value.Rights));
            // Get the connectionString to the Eventhub for a Authorization rule created
            var listKeysResponse = await EventHubsOperations.ListKeysAsync(resourceGroup, namespaceName, eventHubName, authorizationRuleName);

            Assert.NotNull(listKeysResponse);
            Assert.NotNull(listKeysResponse.Value.PrimaryConnectionString);
            Assert.NotNull(listKeysResponse.Value.SecondaryConnectionString);
            //New connection string
            var regenerateConnection_primary = await EventHubsOperations.RegenerateKeysAsync(resourceGroup, namespaceName, eventHubName, authorizationRuleName, new RegenerateAccessKeyParameters(KeyType.PrimaryKey));

            Assert.NotNull(regenerateConnection_primary);
            Assert.AreNotEqual(listKeysResponse.Value.PrimaryConnectionString, regenerateConnection_primary.Value.PrimaryConnectionString);
            Assert.AreEqual(listKeysResponse.Value.SecondaryConnectionString, regenerateConnection_primary.Value.SecondaryConnectionString);
            var regenerateConnection_Secondary = await EventHubsOperations.RegenerateKeysAsync(resourceGroup, namespaceName, eventHubName, authorizationRuleName, new RegenerateAccessKeyParameters(KeyType.SecondaryKey));

            Assert.NotNull(regenerateConnection_Secondary);
            Assert.AreNotEqual(listKeysResponse.Value.SecondaryConnectionString, regenerateConnection_Secondary.Value.SecondaryConnectionString);
            Assert.AreEqual(regenerateConnection_primary.Value.PrimaryConnectionString, regenerateConnection_Secondary.Value.PrimaryConnectionString);
            // Delete Eventhub authorizationRule
            await EventHubsOperations.DeleteAuthorizationRuleAsync(resourceGroup, namespaceName, eventHubName, authorizationRuleName);

            DelayInTest(5);
            // Delete Eventhub and check for the NotFound exception
            await EventHubsOperations.DeleteAsync(resourceGroup, namespaceName, eventHubName);

            // Delete namespace and check for the NotFound exception
            await WaitForCompletionAsync(await NamespacesOperations.StartDeleteAsync(resourceGroup, namespaceName));
        }
        public async Task NetworkRuleSetCreateGetUpdateDelete()
        {
            var location      = GetLocation();
            var resourceGroup = Recording.GenerateAssetName(Helper.ResourceGroupPrefix);
            await Helper.TryRegisterResourceGroupAsync(ResourceGroupsOperations, location.Result, resourceGroup);

            //Create a namespace
            var namespaceName           = Recording.GenerateAssetName(Helper.NamespacePrefix);
            var createNamespaceResponse = await NamespacesOperations.StartCreateOrUpdateAsync(resourceGroup, namespaceName,
                                                                                              new EHNamespace()
            {
                Location = location.Result,
                Tags     =
                {
                    { "tag1", "value1" },
                    { "tag2", "value2" }
                }
            }
                                                                                              );

            var np = (await WaitForCompletionAsync(createNamespaceResponse)).Value;

            Assert.NotNull(createNamespaceResponse);
            Assert.AreEqual(np.Name, namespaceName);
            DelayInTest(5);
            //get the created namespace
            var getNamespaceResponse = await NamespacesOperations.GetAsync(resourceGroup, namespaceName);

            if (string.Compare(getNamespaceResponse.Value.ProvisioningState, "Succeeded", true) != 0)
            {
                DelayInTest(5);
            }
            getNamespaceResponse = await NamespacesOperations.GetAsync(resourceGroup, namespaceName);

            Assert.NotNull(getNamespaceResponse);
            Assert.AreEqual("Succeeded", getNamespaceResponse.Value.ProvisioningState, StringComparer.CurrentCultureIgnoreCase.ToString());
            Assert.AreEqual(location.Result, getNamespaceResponse.Value.Location, StringComparer.CurrentCultureIgnoreCase.ToString());

            var netWorkRuleSet = await NamespacesOperations.CreateOrUpdateNetworkRuleSetAsync(resourceGroup, namespaceName,
                                                                                              new NetworkRuleSet()
            {
                DefaultAction       = DefaultAction.Deny,
                VirtualNetworkRules =
                {
                    new NWRuleSetVirtualNetworkRules()
                    {
                        Subnet = new Subnet("/subscriptions/" + SubscriptionId + "/resourcegroups/" + resourceGroup + "/providers/Microsoft.Network/virtualNetworks/sbehvnettest1/subnets/default")
                    },
                    new NWRuleSetVirtualNetworkRules()
                    {
                        Subnet = new Subnet("/subscriptions/" + SubscriptionId + "/resourcegroups/" + resourceGroup + "/providers/Microsoft.Network/virtualNetworks/sbehvnettest1/subnets/sbdefault")
                    },
                    new NWRuleSetVirtualNetworkRules()
                    {
                        Subnet = new Subnet("/subscriptions/" + SubscriptionId + "/resourcegroups/" + resourceGroup + "/providers/Microsoft.Network/virtualNetworks/sbehvnettest1/subnets/sbdefault01")
                    }
                },
                IpRules =
                {
                    new NWRuleSetIpRules()
                    {
                        IpMask = "1.1.1.1", Action = "Allow"
                    },
                    new NWRuleSetIpRules()
                    {
                        IpMask = "1.1.1.2", Action = "Allow"
                    },
                    new NWRuleSetIpRules()
                    {
                        IpMask = "1.1.1.3", Action = "Allow"
                    },
                    new NWRuleSetIpRules()
                    {
                        IpMask = "1.1.1.4", Action = "Allow"
                    },
                    new NWRuleSetIpRules()
                    {
                        IpMask = "1.1.1.5", Action = "Allow"
                    }
                }
            });

            var getNetworkRuleSet = await NamespacesOperations.GetNetworkRuleSetAsync(resourceGroup, namespaceName);

            var netWorkRuleSet1 = await NamespacesOperations.CreateOrUpdateNetworkRuleSetAsync(resourceGroup, namespaceName, new NetworkRuleSet()
            {
                DefaultAction = "Allow"
            });

            var getNetworkRuleSet1 = await NamespacesOperations.GetNetworkRuleSetAsync(resourceGroup, namespaceName);

            DelayInTest(60);
            //Delete namespace
            await WaitForCompletionAsync(await NamespacesOperations.StartDeleteAsync(resourceGroup, namespaceName));
        }