コード例 #1
0
        /// <summary>
        /// Allocates a public IP address to the current account.
        /// </summary>
        /// <param name="definition">The public IP definition.</param>
        /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
        /// <returns>The identifer of the public IP address while it is being provisioned. Use <see cref="WaitUntilPublicIPIsActiveAsync"/> to wait for the IP address to be fully active.</returns>
        public async Task <PublicIP> CreatePublicIPAsync(PublicIPCreateDefinition definition, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (definition == null)
            {
                throw new ArgumentNullException("definition");
            }

            string endpoint = await _urlBuilder.GetEndpoint(cancellationToken).ConfigureAwait(false);

            Func <Task <PublicIP> > executeRequest = async() =>
            {
                var ip = await new Url(endpoint)
                         .AppendPathSegment("public_ips")
                         .Authenticate(_authenticationProvider)
                         .PostJsonAsync(definition, cancellationToken)
                         .ReceiveJson <PublicIP>()
                         .ConfigureAwait(false);

                SetOwner(ip);

                return(ip);
            };

            try
            {
                return(await executeRequest());
            }
            catch (FlurlHttpException ex) when(AssignIPFailedDueToServerCreationRaceCondition(ex))
            {
                await Task.Delay(TimeSpan.FromSeconds(1), cancellationToken);

                return(await executeRequest());
            }
        }
コード例 #2
0
        public async Task CreatePublicIPTest()
        {
            Trace.WriteLine("Looking up the RackConnect network...");
            var network = (await _rackConnectService.ListNetworksAsync()).First();

            Trace.Write("Creating a test cloud server...");
            var server = _testData.CreateServer(network.Id);

            Trace.WriteLine(server.Id);

            Trace.Write("Assigning a public ip address to the test cloud server... ");
            var ipRequest = new PublicIPCreateDefinition {
                ServerId = server.Id, ShouldRetain = true
            };
            var ip = await _testData.CreatePublicIP(ipRequest);

            await ip.WaitUntilActiveAsync();

            Trace.WriteLine(ip.PublicIPv4Address);

            Assert.NotNull(ip);
            Assert.Equal(server.Id, ip.Server.ServerId);
            Assert.NotNull(ip.PublicIPv4Address);
            Assert.Equal(PublicIPStatus.Active, ip.Status);

            Trace.WriteLine("Retrieving public IPs assigned to the test cloud server...");
            var filterByServer = new ListPublicIPsFilter {
                ServerId = server.Id
            };
            var ips = await _rackConnectService.ListPublicIPsAsync(filterByServer);

            Assert.NotNull(ips);
            Assert.True(ips.Any(x => x.Id == ip.Id));

            Trace.WriteLine("Update the IP address to not be retained...");
            ip = await _rackConnectService.UpdatePublicIPAsync(ip.Id, new PublicIPUpdateDefinition { ShouldRetain = false });

            await ip.WaitUntilActiveAsync();

            Assert.NotNull(ip);
            Assert.False(ip.ShouldRetain);

            Trace.WriteLine("Removing public IP from test cloud server...");
            await ip.DeleteAsync();

            await ip.WaitUntilDeletedAsync();

            Trace.WriteLine($"Verifying that {ip.PublicIPv4Address} is no longer assigned...");
            ips = await _rackConnectService.ListPublicIPsAsync(filterByServer);

            Assert.NotNull(ips);
            Assert.False(ips.Any(x => x.Id == ip.Id));
        }
コード例 #3
0
        public void CreatePublicIP()
        {
            using (var httpTest = new HttpTest())
            {
                string serverId = Guid.NewGuid().ToString();
                Identifier id = Guid.NewGuid();
                httpTest.RespondWithJson(new PublicIP { Id = id });

                var ipRequest = new PublicIPCreateDefinition {ServerId = serverId};
                var result = _rackConnectService.CreatePublicIP(ipRequest);

                httpTest.ShouldHaveCalled($"*/public_ips");
                Assert.NotNull(result);
                Assert.Equal(id, result.Id);
                Assert.NotNull(((IServiceResource<RackConnectService>)result).Owner);
            }
        }
コード例 #4
0
        public async Task CreatePublicIPTest()
        {
            Trace.WriteLine("Looking up the RackConnect network...");
            var network = (await _rackConnectService.ListNetworksAsync()).First();

            Trace.Write("Creating a test cloud server...");
            var server = _testData.CreateServer(network.Id);
            Trace.WriteLine(server.Id);

            Trace.Write("Assigning a public ip address to the test cloud server... ");
            var ipRequest = new PublicIPCreateDefinition {ServerId = server.Id, ShouldRetain = true};
            var ip = await _testData.CreatePublicIP(ipRequest);
            await ip.WaitUntilActiveAsync();
            Trace.WriteLine(ip.PublicIPv4Address);

            Assert.NotNull(ip);
            Assert.Equal(server.Id, ip.Server.ServerId);
            Assert.NotNull(ip.PublicIPv4Address);
            Assert.Equal(PublicIPStatus.Active, ip.Status);

            Trace.WriteLine("Retrieving public IPs assigned to the test cloud server...");
            var filterByServer = new ListPublicIPsFilter {ServerId = server.Id};
            var ips = await _rackConnectService.ListPublicIPsAsync(filterByServer);
            Assert.NotNull(ips);
            Assert.True(ips.Any(x => x.Id == ip.Id));

            Trace.WriteLine("Update the IP address to not be retained...");
            ip = await _rackConnectService.UpdatePublicIPAsync(ip.Id, new PublicIPUpdateDefinition { ShouldRetain = false });
            await ip.WaitUntilActiveAsync();
            Assert.NotNull(ip);
            Assert.False(ip.ShouldRetain);

            Trace.WriteLine("Removing public IP from test cloud server...");
            await ip.DeleteAsync();
            await ip.WaitUntilDeletedAsync();

            Trace.WriteLine($"Verifying that {ip.PublicIPv4Address} is no longer assigned...");
            ips = await _rackConnectService.ListPublicIPsAsync(filterByServer);
            Assert.NotNull(ips);
            Assert.False(ips.Any(x => x.Id == ip.Id));
        }
コード例 #5
0
        public void CreatePublicIP()
        {
            using (var httpTest = new HttpTest())
            {
                string     serverId = Guid.NewGuid().ToString();
                Identifier id       = Guid.NewGuid();
                httpTest.RespondWithJson(new PublicIP {
                    Id = id
                });

                var ipRequest = new PublicIPCreateDefinition {
                    ServerId = serverId
                };
                var result = _rackConnectService.CreatePublicIP(ipRequest);

                httpTest.ShouldHaveCalled($"*/public_ips");
                Assert.NotNull(result);
                Assert.Equal(id, result.Id);
                Assert.NotNull(((IServiceResource <RackConnectService>)result).Owner);
            }
        }
コード例 #6
0
        public void CreatePublicIP_RetriesWhenTheServerIsNotFound()
        {
            using (var httpTest = new HttpTest())
            {
                string     serverId = Guid.NewGuid().ToString();
                Identifier id       = Guid.NewGuid();
                httpTest.RespondWith((int)HttpStatusCode.Conflict, $"Cloud Server {serverId} does not exist");
                httpTest.RespondWithJson(new PublicIP {
                    Id = id
                });

                var ipRequest = new PublicIPCreateDefinition {
                    ServerId = serverId
                };
                var result = _rackConnectService.CreatePublicIP(ipRequest);

                httpTest.ShouldHaveCalled($"*/public_ips");
                Assert.NotNull(result);
                Assert.Equal(id, result.Id);
                Assert.NotNull(((IServiceResource <RackConnectService>)result).Owner);
            }
        }
コード例 #7
0
        public void CreatePublicIP_RetriesWhenTheServerIsNotFound()
        {
            using (var httpTest = new HttpTest())
            {
                string serverId = Guid.NewGuid().ToString();
                Identifier id = Guid.NewGuid();
                httpTest.RespondWith((int)HttpStatusCode.Conflict, $"Cloud Server {serverId} does not exist");
                httpTest.RespondWithJson(new PublicIP { Id = id });

                var ipRequest = new PublicIPCreateDefinition { ServerId = serverId };
                var result = _rackConnectService.CreatePublicIP(ipRequest);

                httpTest.ShouldHaveCalled($"*/public_ips");
                Assert.NotNull(result);
                Assert.Equal(id, result.Id);
                Assert.NotNull(((IServiceResource<RackConnectService>)result).Owner);
            }
        }
コード例 #8
0
 /// <inheritdoc cref="RackConnectService.CreatePublicIPAsync"/>
 public static PublicIP CreatePublicIP(this RackConnectService rackConnectService, PublicIPCreateDefinition definition)
 {
     return rackConnectService.CreatePublicIPAsync(definition).ForceSynchronous();
 }
コード例 #9
0
 public async Task<PublicIP> CreatePublicIP(PublicIPCreateDefinition definition)
 {
     var ip = await _rackConnectService.CreatePublicIPAsync(definition);
     Register(ip);
     return ip;
 }
コード例 #10
0
        /// <summary>
        /// Allocates a public IP address to the current account.
        /// </summary>
        /// <param name="definition">The public IP definition.</param>
        /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
        /// <returns>The identifer of the public IP address while it is being provisioned. Use <see cref="WaitUntilPublicIPIsActiveAsync"/> to wait for the IP address to be fully active.</returns>
        public async Task<PublicIP> CreatePublicIPAsync(PublicIPCreateDefinition definition, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (definition == null)
                throw new ArgumentNullException("definition");

            string endpoint = await _urlBuilder.GetEndpoint(cancellationToken).ConfigureAwait(false);

            Func<Task<PublicIP>> executeRequest = async () =>
            {
                var ip = await new Url(endpoint)
                    .AppendPathSegment("public_ips")
                    .Authenticate(_authenticationProvider)
                    .PostJsonAsync(definition, cancellationToken)
                    .ReceiveJson<PublicIP>()
                    .ConfigureAwait(false);

                SetOwner(ip);

                return ip;
            };

            try
            {
                return await executeRequest();
            }
            catch (FlurlHttpException ex) when (AssignIPFailedDueToServerCreationRaceCondition(ex))
            {
                await Task.Delay(TimeSpan.FromSeconds(1), cancellationToken);
                return await executeRequest();
            }
        }