public void EqualsObj()
        {
            object             input    = TrackedResourceId;
            ResourceIdentifier resource = new ResourceIdentifier(TrackedResourceId);

            Assert.AreEqual(true, resource.Equals(input));
            Assert.IsFalse(resource.Equals(new object()));
        }
Exemplo n.º 2
0
        /// <summary>
        /// TODO: Make use of the entity tags on the resource - we may need to add to the generated management client
        /// TODO: Look for PATCH update methods in the swagger
        /// TODO: How to represent PATCH where the patch model has properties that are collections (replace/merge)?
        /// </summary>
        /// <param name="rules">The new set of network security rules</param>
        /// <returns>A network security group with the given set of rules merged with thsi one</returns>
        public AzureNetworkSecurityGroup UpdateRules(params SecurityRule[] rules)
        {
            foreach (var rule in rules)
            {
                // Note that this makes use of the
                var matchingRule = Model.SecurityRules.FirstOrDefault(r => ResourceIdentifier.Equals(r.Id, rule.Id));
                if (matchingRule != null)
                {
                    matchingRule.Access      = rule.Access;
                    matchingRule.Description = rule.Description;
                    matchingRule.DestinationAddressPrefix   = rule.DestinationAddressPrefix;
                    matchingRule.DestinationAddressPrefixes = rule.DestinationAddressPrefixes;
                    matchingRule.DestinationPortRange       = rule.DestinationPortRange;
                    matchingRule.DestinationPortRanges      = rule.DestinationPortRanges;
                    matchingRule.Direction             = rule.Direction;
                    matchingRule.Priority              = rule.Priority;
                    matchingRule.Protocol              = rule.Protocol;
                    matchingRule.SourceAddressPrefix   = rule.SourceAddressPrefix;
                    matchingRule.SourceAddressPrefixes = rule.SourceAddressPrefixes;
                    matchingRule.SourcePortRange       = rule.SourcePortRange;
                    matchingRule.SourcePortRanges      = rule.SourcePortRanges;
                }
                else
                {
                    Model.SecurityRules.Add(rule);
                }
            }

            this.Model = Client.NetworkSecurityGroups.StartCreateOrUpdate(Id.ResourceGroup, Name, Model).WaitForCompletionAsync().Result.Value;
            return(this);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Updates the network security group rules.
        /// </summary>
        /// <param name="cancellationToken"> A token to allow the caller to cancel the call to the service. The default value is <see cref="P:System.Threading.CancellationToken.None" />. </param>
        /// <param name="rules"> The rules to be updated. </param>
        /// <returns> An <see cref="ArmOperation{NetworkSecurityGroup}"/> that allows polling for completion of the operation. </returns>
        public ArmOperation <NetworkSecurityGroup> UpdateRules(CancellationToken cancellationToken = default, params SecurityRule[] rules)
        {
            var resource = GetResource();

            foreach (var rule in rules)
            {
                // Note that this makes use of the
                var matchingRule = resource.Data.SecurityRules.FirstOrDefault(r => ResourceIdentifier.Equals(r.Id, rule.Id));
                if (matchingRule != null)
                {
                    matchingRule.Access      = rule.Access;
                    matchingRule.Description = rule.Description;
                    matchingRule.DestinationAddressPrefix   = rule.DestinationAddressPrefix;
                    matchingRule.DestinationAddressPrefixes = rule.DestinationAddressPrefixes;
                    matchingRule.DestinationPortRange       = rule.DestinationPortRange;
                    matchingRule.DestinationPortRanges      = rule.DestinationPortRanges;
                    matchingRule.Direction             = rule.Direction;
                    matchingRule.Priority              = rule.Priority;
                    matchingRule.Protocol              = rule.Protocol;
                    matchingRule.SourceAddressPrefix   = rule.SourceAddressPrefix;
                    matchingRule.SourceAddressPrefixes = rule.SourceAddressPrefixes;
                    matchingRule.SourcePortRange       = rule.SourcePortRange;
                    matchingRule.SourcePortRanges      = rule.SourcePortRanges;
                }
                else
                {
                    resource.Data.SecurityRules.Add(rule);
                }
            }

            return(new PhArmOperation <NetworkSecurityGroup, Azure.ResourceManager.Network.Models.NetworkSecurityGroup>(
                       Operations.StartCreateOrUpdate(Id.ResourceGroup, Id.Name, resource.Data),
                       n => new NetworkSecurityGroup(this, new NetworkSecurityGroupData(n))));
        }
Exemplo n.º 4
0
        public void EqualsShouldBeEqual()
        {
            var identifier1 = new ResourceIdentifier("thing", "10");
            var identifier2 = new ResourceIdentifier("thing", "10");

            Assert.IsTrue(identifier1.Equals(identifier2));
        }
        public void WhenComparingTwoResourceIdentifiersCreatedWithDifferentResourceNamesThenTheyAreNotEqual()
        {
            var first  = new ResourceIdentifier(ResourceType.VirtualMachine, TestSubscriptionId, TestResourceGroup, TestResourceName);
            var second = new ResourceIdentifier(ResourceType.VirtualMachine, TestSubscriptionId, TestResourceGroup, "otherResource");

            Assert.IsFalse(first.Equals(second), "Expected both identifiers to be not equal");
            Assert.IsFalse(first == second, "Expected both identifiers to be not equal using equality comparison");
            Assert.IsTrue(first != second, "Expected both identifiers to be not equal using inequality comparison");
            Assert.AreNotEqual(first.GetHashCode(), second.GetHashCode(), "Expected both identifiers have not equal hash codes");
        }
Exemplo n.º 6
0
        public void EqualsToString(string resourceProviderID1, string resourceProviderID2, bool expected)
        {
            ResourceIdentifier a = resourceProviderID1 == null ? null : new ResourceIdentifier(resourceProviderID1);

            if (a != null)
            {
                Assert.AreEqual(expected, a.Equals(resourceProviderID2));
            }

            Assert.AreEqual(expected, ResourceIdentifier.Equals(a, resourceProviderID2));
        }
        public void EqualsToResourceIdentifier(string resourceProviderID1, string resourceProviderID2, bool expected)
        {
            ResourceIdentifier a = resourceProviderID1;
            ResourceIdentifier b = resourceProviderID2;

            if (a != null)
            {
                Assert.AreEqual(expected, a.Equals(b));
            }

            Assert.AreEqual(expected, ResourceIdentifier.Equals(a, b));
        }
Exemplo n.º 8
0
        public void TryGetPropertiesForSubscriptionProvider()
        {
            ResourceIdentifier id1 = new ResourceIdentifier("/subscriptions/db1ab6f0-4769-4b27-930e-01e2ef9c123c/providers/Microsoft.Compute");

            Assert.NotNull(id1.SubscriptionId);
            Assert.AreEqual("db1ab6f0-4769-4b27-930e-01e2ef9c123c", id1.SubscriptionId);
            Assert.Null(id1.Location);
            Assert.Null(id1.ResourceGroupName);
            ResourceIdentifier expectedId = new ResourceIdentifier("/subscriptions/db1ab6f0-4769-4b27-930e-01e2ef9c123c");

            Assert.NotNull(id1.Parent);
            Assert.IsTrue(expectedId.Equals(id1.Parent));
        }
Exemplo n.º 9
0
        public void TryGetPropertiesForSubscriptionResource()
        {
            ResourceIdentifier id1 = new ResourceIdentifier("/subscriptions/6b085460-5f21-477e-ba44-1035046e9101/providers/Contoso.Widgets/widgets/myWidget");

            Assert.NotNull(id1.SubscriptionId);
            Assert.AreEqual("6b085460-5f21-477e-ba44-1035046e9101", id1.SubscriptionId);
            Assert.Null(id1.Location);
            Assert.Null(id1.ResourceGroupName);
            ResourceIdentifier expectedId = new ResourceIdentifier("/subscriptions/6b085460-5f21-477e-ba44-1035046e9101");

            Assert.NotNull(id1.Parent);
            Assert.IsTrue(expectedId.Equals(id1.Parent));
        }
Exemplo n.º 10
0
        public void TryGetPropertiesForTenantResource()
        {
            ResourceIdentifier id1 = new ResourceIdentifier("/providers/Contoso.Widgets/widgets/myWidget");

            Assert.Null(id1.SubscriptionId);
            Assert.Null(id1.Location);
            Assert.Null(id1.ResourceGroupName);
            Assert.NotNull(id1.Parent);
            ResourceIdentifier id2 = new ResourceIdentifier("/providers/Contoso.Widgets/widgets/myWidget/flages/myFlange");

            Assert.NotNull(id2.Parent);
            Assert.AreEqual(true, id1.Equals(id2.Parent));
        }
        public void TryGetPropertiesForTenantResource()
        {
            ResourceIdentifier id1 = "/providers/Contoso.Widgets/widgets/myWidget";

            Assert.AreEqual(false, id1.TryGetSubscriptionId(out _));
            Assert.AreEqual(false, id1.TryGetLocation(out _));
            Assert.AreEqual(false, id1.TryGetResourceGroupName(out _));
            Assert.AreEqual(true, id1.TryGetParent(out _));
            ResourceIdentifier id2 = "/providers/Contoso.Widgets/widgets/myWidget/flages/myFlange";
            ResourceIdentifier parent;

            Assert.AreEqual(true, id2.TryGetParent(out parent));
            Assert.AreEqual(true, id1.Equals(parent));
        }
Exemplo n.º 12
0
        public void TryGetPropertiesForSubscriptionProvider()
        {
            ResourceIdentifier id1 = "/subscriptions/db1ab6f0-4769-4b27-930e-01e2ef9c123c/providers/Microsoft.Compute";
            string             subscription;

            Assert.AreEqual(true, id1.TryGetSubscriptionId(out subscription));
            Assert.AreEqual("db1ab6f0-4769-4b27-930e-01e2ef9c123c", subscription);
            Assert.AreEqual(false, id1.TryGetLocation(out _));
            Assert.AreEqual(false, id1.TryGetResourceGroupName(out _));
            ResourceIdentifier expectedId = "/subscriptions/db1ab6f0-4769-4b27-930e-01e2ef9c123c";
            ResourceIdentifier parentId;

            Assert.AreEqual(true, id1.TryGetParent(out parentId));
            Assert.IsTrue(expectedId.Equals(parentId));
        }
Exemplo n.º 13
0
        public void TryGetPropertiesForSubscriptionResource()
        {
            ResourceIdentifier id1 = "/subscriptions/6b085460-5f21-477e-ba44-1035046e9101/providers/Contoso.Widgets/widgets/myWidget";
            string             subscription;

            Assert.AreEqual(true, id1.TryGetSubscriptionId(out subscription));
            Assert.AreEqual("6b085460-5f21-477e-ba44-1035046e9101", subscription);
            Assert.AreEqual(false, id1.TryGetLocation(out _));
            Assert.AreEqual(false, id1.TryGetResourceGroupName(out _));
            ResourceIdentifier expectedId = "/subscriptions/6b085460-5f21-477e-ba44-1035046e9101";
            ResourceIdentifier parentId;

            Assert.AreEqual(true, id1.TryGetParent(out parentId));
            Assert.IsTrue(expectedId.Equals(parentId));
        }
Exemplo n.º 14
0
        public void TestResourceIdentifierEqualsMethod()
        {
            // Arrange
            var resourceIdentifier0 = default(ResourceIdentifier);
            var resourceIdentifier1 = new ResourceIdentifier();
            var resourceIdentifier2 = ApiSampleData.PersonResourceIdentifier1;
            var resourceIdentifier3 = ApiSampleData.PersonResourceIdentifier2;
            var resourceIdentifier4 = ApiSampleData.PersonResourceIdentifier1;
            var resourceIdentifier5 = ApiSampleData.CommentResourceIdentifier1;

            // Act

            // Assert

            // ReSharper disable ExpressionIsAlwaysNull
            // ReSharper disable EqualExpressionComparison
            // ReSharper disable HeuristicUnreachableCode
            Assert.False(resourceIdentifier1.Equals(resourceIdentifier0));
            Assert.True(resourceIdentifier1.Equals(resourceIdentifier1));
            Assert.False(resourceIdentifier1.Equals(resourceIdentifier2));
            Assert.False(resourceIdentifier1.Equals(resourceIdentifier3));
            Assert.False(resourceIdentifier1.Equals(resourceIdentifier4));
            Assert.False(resourceIdentifier1.Equals(resourceIdentifier5));

            Assert.False(resourceIdentifier2.Equals(resourceIdentifier0));
            Assert.False(resourceIdentifier2.Equals(resourceIdentifier1));
            Assert.True(resourceIdentifier2.Equals(resourceIdentifier2));
            Assert.False(resourceIdentifier2.Equals(resourceIdentifier3));
            Assert.True(resourceIdentifier2.Equals(resourceIdentifier4));
            Assert.False(resourceIdentifier2.Equals(resourceIdentifier5));

            Assert.False(resourceIdentifier3.Equals(resourceIdentifier0));
            Assert.False(resourceIdentifier3.Equals(resourceIdentifier1));
            Assert.False(resourceIdentifier3.Equals(resourceIdentifier2));
            Assert.True(resourceIdentifier3.Equals(resourceIdentifier3));
            Assert.False(resourceIdentifier3.Equals(resourceIdentifier4));
            Assert.False(resourceIdentifier3.Equals(resourceIdentifier5));

            Assert.False(resourceIdentifier4.Equals(resourceIdentifier0));
            Assert.False(resourceIdentifier4.Equals(resourceIdentifier1));
            Assert.True(resourceIdentifier4.Equals(resourceIdentifier2));
            Assert.False(resourceIdentifier4.Equals(resourceIdentifier3));
            Assert.True(resourceIdentifier4.Equals(resourceIdentifier4));
            Assert.False(resourceIdentifier4.Equals(resourceIdentifier5));

            Assert.False(resourceIdentifier5.Equals(resourceIdentifier0));
            Assert.False(resourceIdentifier5.Equals(resourceIdentifier1));
            Assert.False(resourceIdentifier5.Equals(resourceIdentifier2));
            Assert.False(resourceIdentifier5.Equals(resourceIdentifier3));
            Assert.False(resourceIdentifier5.Equals(resourceIdentifier4));
            Assert.True(resourceIdentifier5.Equals(resourceIdentifier5));
            // ReSharper restore HeuristicUnreachableCode
            // ReSharper restore EqualExpressionComparison
            // ReSharper restore ExpressionIsAlwaysNull
        }
        public void WhenComparingTwoResourceIdentifiersCreatedWithTheSameDataThenTheyAreEqual()
        {
            var first  = new ResourceIdentifier(ResourceType.VirtualMachine, TestSubscriptionId, TestResourceGroup, TestResourceName);
            var second = new ResourceIdentifier(ResourceType.VirtualMachine, TestSubscriptionId, TestResourceGroup, TestResourceName);

            Assert.IsTrue(first.Equals(second), "Expected both identifiers to be equal");
            Assert.IsTrue(first == second, "Expected both identifiers to be equal using equality comparison");
            Assert.IsFalse(first != second, "Expected both identifiers to be equal using inequality comparison");
            Assert.AreEqual(first.GetHashCode(), second.GetHashCode(), "Expected both identifiers have equal hash codes");

            var firstKubernetesService  = new ResourceIdentifier(ResourceType.KubernetesService, TestSubscriptionId, TestResourceGroup, TestResourceName);
            var secondKubernetesService = new ResourceIdentifier(ResourceType.KubernetesService, TestSubscriptionId, TestResourceGroup, TestResourceName);

            Assert.IsTrue(firstKubernetesService.Equals(secondKubernetesService), "Expected both identifiers to be equal");
            Assert.IsTrue(firstKubernetesService == secondKubernetesService, "Expected both identifiers to be equal using equality comparison");
            Assert.IsFalse(firstKubernetesService != secondKubernetesService, "Expected both identifiers to be equal using inequality comparison");
            Assert.AreEqual(firstKubernetesService.GetHashCode(), secondKubernetesService.GetHashCode(), "Expected both identifiers have equal hash codes");
        }
Exemplo n.º 16
0
 public bool Equals([AllowNull] SecurityRule x, [AllowNull] SecurityRule y)
 {
     return(ResourceIdentifier.Equals(x?.Id, y?.Id));
 }
Exemplo n.º 17
0
 public bool Equals(SecurityRule x, SecurityRule y)
 {
     return(ResourceIdentifier.Equals(x?.Id, y?.Id));
 }