Beispiel #1
0
        private static DateTime GetRGCreationDateTime(
            AzureCredentials credentials,
            Microsoft.Azure.Management.ResourceManager.ResourceManagementClient client,
            IResourceGroup rg)
        {
            var url     = $"{client.BaseUri.AbsoluteUri}subscriptions/{client.SubscriptionId}/resourceGroups/{rg.Name}?$expand=createdTime&api-version={client.ApiVersion}";
            var request = new HttpRequestMessage(HttpMethod.Get, url);

            credentials.ProcessHttpRequestAsync(request, CancellationToken.None)
            .ConfigureAwait(false)
            .GetAwaiter()
            .GetResult();

            var response = client.HttpClient.SendAsync(request)
                           .ConfigureAwait(false)
                           .GetAwaiter()
                           .GetResult();

            if (response.StatusCode != HttpStatusCode.OK)
            {
                throw new Exception($"Resource Group '{rg.Name}' did not return creation date. Skipping.");
            }

            string responseContent = response.Content.ReadAsStringAsync()
                                     .ConfigureAwait(false)
                                     .GetAwaiter()
                                     .GetResult();

            JObject body = null;

            if (!string.IsNullOrWhiteSpace(responseContent))
            {
                body = JObject.Parse(responseContent);
            }
            else
            {
                throw new Exception($"Resource Group '{rg.Name}' did not provide body content. Skipping.");
            }

            if (body == null ||
                body["createdTime"] == null)
            {
                throw new Exception($"Resource Group '{rg.Name}' did not return creation date in the body message. Skipping.");
            }

            var settings = new JsonSerializerSettings
            {
                Formatting            = Formatting.Indented,
                DateFormatHandling    = DateFormatHandling.IsoDateFormat,
                DateTimeZoneHandling  = DateTimeZoneHandling.Utc,
                NullValueHandling     = NullValueHandling.Ignore,
                ReferenceLoopHandling = ReferenceLoopHandling.Serialize,
                ContractResolver      = new ReadOnlyJsonContractResolver(),
                Converters            = new List <JsonConverter>
                {
                    new Iso8601TimeSpanConverter()
                }
            };

            string str = SafeJsonConvert.SerializeObject(body["createdTime"], settings).Trim(new[] { '\"' });

            return(DateTime.Parse(str));
        }