Example #1
0
        public void AddSecurityGroupRule()
        {
            using (var httpTest = new HttpTest())
            {
                Identifier securityGroupId     = Guid.NewGuid();
                Identifier securityGroupRuleId = Guid.NewGuid();
                httpTest.RespondWithJson(new SecurityGroup {
                    Id = securityGroupId
                });
                httpTest.RespondWithJson(new SecurityGroupRule {
                    Id = securityGroupRuleId
                });

                var request       = new SecurityGroupRuleDefinition(IPProtocol.TCP, 0, 0, "{cidr}");
                var securityGroup = _compute.GetSecurityGroup(securityGroupId);
                var result        = securityGroup.AddRule(request);

                httpTest.ShouldHaveCalled("*/os-security-group-rules");
                Assert.Contains(securityGroupId, httpTest.CallLog.Last().RequestBody); // Make sure that we automatically set the parent group id

                Assert.NotNull(result);
                Assert.Contains(result, securityGroup.Rules);
                Assert.Equal(securityGroupRuleId, result.Id);
                Assert.IsType <ComputeApi>(((IServiceResource)result).Owner);
            }
        }
Example #2
0
        /// <inheritdoc cref="ComputeApi.CreateSecurityGroupRuleAsync{T}" />
        /// <exception cref="InvalidOperationException">When the <see cref="Server"/> instance was not constructed by the <see cref="ComputeService"/>, as it is missing the appropriate internal state to execute service calls.</exception>
        public async Task <SecurityGroupRule> AddRuleAsync(SecurityGroupRuleDefinition rule, CancellationToken cancellationToken = default(CancellationToken))
        {
            var compute = this.GetOwnerOrThrow <ComputeApi>();

            rule.GroupId = Id;

            var result = await compute.CreateSecurityGroupRuleAsync <SecurityGroupRule>(rule, cancellationToken).ConfigureAwait(false);

            Rules.Add(result);
            return(result);
        }
Example #3
0
        public async Task SecurityGroupRuleTest()
        {
            Trace.WriteLine("Creating security group...");
            var securityGroup = await _testData.CreateSecurityGroup();

            Trace.WriteLine("Adding a rule...");
            var ruleDefinition = new SecurityGroupRuleDefinition(IPProtocol.TCP, 22, 22, "0.0.0.0/24");
            var rule           = await securityGroup.AddRuleAsync(ruleDefinition);

            Trace.WriteLine("Verifying rule matches requested definition...");
            Assert.NotNull(rule);
            Assert.Equal(ruleDefinition.Protocol, rule.Protocol);
            Assert.Equal(ruleDefinition.ToPort, rule.ToPort);
            Assert.Equal(ruleDefinition.FromPort, rule.FromPort);
            Assert.Equal(ruleDefinition.CIDR, rule.CIDR);
            Assert.Equal(securityGroup.Id, rule.GroupId);

            Trace.WriteLine("Deleting the rule...");
            await rule.DeleteAsync();

            securityGroup = await _compute.GetSecurityGroupAsync(securityGroup.Id);

            Assert.DoesNotContain(securityGroup.Rules, x => x.Id == rule.Id);
        }