/// <summary>
        /// Updates the public IP address.
        /// </summary>
        /// <param name="publicIPId">The public IP address identifier.</param>
        /// <param name="definition">The updated 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>
        public async Task <PublicIP> UpdatePublicIPAsync(Identifier publicIPId, PublicIPUpdateDefinition 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 endpoint
                         .AppendPathSegments("public_ips", publicIPId)
                         .Authenticate(_authenticationProvider)
                         .PatchJsonAsync(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());
            }
        }
Beispiel #2
0
        /// <summary>
        /// Removes the Public IP from the server to which it is currently assigned.
        /// </summary>
        /// <exception cref="InvalidOperationException">When the <see cref="PublicIP"/> instance was not constructed by the <see cref="RackConnectService"/>, as it is missing the appropriate internal state to execute service calls.</exception>
        public async Task UnassignAsync(CancellationToken cancellationToken = default(CancellationToken))
        {
            var owner      = GetOwner();
            var definition = new PublicIPUpdateDefinition {
                ServerId = null
            };
            var result = await owner.UpdatePublicIPAsync(Id, definition, cancellationToken).ConfigureAwait(false);

            result.CopyProperties(this);
        }
 /// <inheritdoc cref="RackConnectService.UpdatePublicIPAsync"/>
 public static PublicIP UpdatePublicIP(this RackConnectService rackConnectService, Identifier publicIPId, PublicIPUpdateDefinition definition)
 {
     return rackConnectService.UpdatePublicIPAsync(publicIPId, definition).ForceSynchronous();
 }
        /// <summary>
        /// Updates the public IP address.
        /// </summary>
        /// <param name="publicIPId">The public IP address identifier.</param>
        /// <param name="definition">The updated 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>
        public async Task<PublicIP> UpdatePublicIPAsync(Identifier publicIPId, PublicIPUpdateDefinition 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 endpoint
                 .AppendPathSegments("public_ips", publicIPId)
                 .Authenticate(_authenticationProvider)
                 .PatchJsonAsync(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();
            }
        }