/// <summary>
        /// Waits for the public IP address to become active.
        /// </summary>
        /// <param name="publicIPId">The public IP address identifier.</param>
        /// <param name="refreshDelay">The amount of time to wait between requests.</param>
        /// <param name="timeout">The amount of time to wait before throwing a <see cref="TimeoutException"/>.</param>
        /// <param name="progress">The progress callback.</param>
        /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
        /// <exception cref="TimeoutException">If the <paramref name="timeout"/> value is reached.</exception>
        /// <exception cref="FlurlHttpException">If the API call returns a bad <see cref="HttpStatusCode"/>.</exception>
        public async Task <PublicIP> WaitUntilPublicIPIsActiveAsync(Identifier publicIPId, TimeSpan?refreshDelay = null, TimeSpan?timeout = null, IProgress <bool> progress = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (string.IsNullOrEmpty(publicIPId))
            {
                throw new ArgumentNullException("publicIPId");
            }

            refreshDelay = refreshDelay ?? TimeSpan.FromSeconds(5);
            timeout      = timeout ?? TimeSpan.FromMinutes(5);

            using (var timeoutSource = new CancellationTokenSource(timeout.Value))
                using (var rootCancellationToken = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, timeoutSource.Token))
                {
                    while (true)
                    {
                        PublicIP ip = await GetPublicIPAsync(publicIPId, cancellationToken).ConfigureAwait(false);

                        if (ip.Status == PublicIPStatus.CreateFailed || ip.Status == PublicIPStatus.UpdateFailed)
                        {
                            throw new ServiceOperationFailedException(ip.StatusDetails);
                        }

                        bool complete = ip.Status == PublicIPStatus.Active;

                        progress?.Report(complete);

                        if (complete)
                        {
                            return(ip);
                        }

                        try
                        {
                            await Task.Delay(refreshDelay.Value, rootCancellationToken.Token).ConfigureAwait(false);
                        }
                        catch (OperationCanceledException ex)
                        {
                            if (timeoutSource.IsCancellationRequested)
                            {
                                throw new TimeoutException($"The requested timeout of {timeout.Value.TotalSeconds} seconds has been reached while waiting for the public IP ({publicIPId}) to become active.", ex);
                            }

                            throw;
                        }
                    }
                }
        }
示例#2
0
        /// <summary>
        /// Waits for the public IP address to be removed from the current account.
        /// </summary>
        /// <param name="publicIPId">The public IP address identifier.</param>
        /// <param name="refreshDelay">The amount of time to wait between requests.</param>
        /// <param name="timeout">The amount of time to wait before throwing a <see cref="TimeoutException"/>.</param>
        /// <param name="progress">The progress callback.</param>
        /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
        /// <exception cref="TimeoutException">If the <paramref name="timeout"/> value is reached.</exception>
        /// <exception cref="FlurlHttpException">If the API call returns a bad <see cref="HttpStatusCode"/>.</exception>
        public async Task WaitUntilPublicIPIsDeletedAsync(Identifier publicIPId, TimeSpan?refreshDelay = null, TimeSpan?timeout = null, IProgress <bool> progress = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (string.IsNullOrEmpty(publicIPId))
            {
                throw new ArgumentNullException("publicIPId");
            }

            refreshDelay = refreshDelay ?? TimeSpan.FromSeconds(5);
            timeout      = timeout ?? TimeSpan.FromMinutes(5);

            using (var timeoutSource = new CancellationTokenSource(timeout.Value))
                using (var rootCancellationToken = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, timeoutSource.Token))
                {
                    while (true)
                    {
                        bool complete;
                        try
                        {
                            PublicIP ip = await GetPublicIPAsync(publicIPId, cancellationToken).ConfigureAwait(false);

                            if (ip.Status == PublicIPStatus.DeleteFailed)
                            {
                                throw new ServiceOperationFailedException(ip.StatusDetails);
                            }

                            complete = ip.Status == PublicIPStatus.Deleted;
                        }
                        catch (FlurlHttpException httpError)
                        {
                            if (httpError.Call.HttpResponseMessage.StatusCode == HttpStatusCode.NotFound)
                            {
                                complete = true;
                            }
                            else
                            {
                                throw;
                            }
                        }

                        progress?.Report(complete);

                        if (complete)
                        {
                            return;
                        }

                        try
                        {
                            await Task.Delay(refreshDelay.Value, rootCancellationToken.Token).ConfigureAwait(false);
                        }
                        catch (OperationCanceledException ex)
                        {
                            if (timeoutSource.IsCancellationRequested)
                            {
                                throw new TimeoutException($"The requested timeout of {timeout.Value.TotalSeconds} seconds has been reached while waiting for the public IP ({publicIPId}) to be removed.", ex);
                            }

                            throw;
                        }
                    }
                }
        }
示例#3
0
        public void WaitUntilDeleted_ThrowsException_WhenCalledOnManuallyCreatedInstance()
        {
            var ip = new PublicIP();

            Assert.Throws <InvalidOperationException>(() => ip.WaitUntilDeleted());
        }