public void VerifyTheDateFieldsRemainAsUniversalTime() { var response = new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(HttpPayload.GetOneAggregates) }; var handler = new RecordedDelegatingHandler(response) { StatusCodeToReturn = HttpStatusCode.OK }; var client = GetUsageAggregationManagementClient(handler); UsageAggregationGetResponse result = client.UsageAggregates.Get(startDate, endDate, AggregationGranularity.Daily, false, null); // Validate headers Assert.Equal(HttpMethod.Get, handler.Method); UsageAggregation ua = result.UsageAggregations[0]; DateTime expectedStartTime = new DateTime(2015, 6, 1, 11, 0, 0); DateTime expectedEndTime = new DateTime(2015, 6, 1, 12, 0, 0); Assert.True(expectedStartTime == ua.Properties.UsageStartTime, "expectedStartTime == ua.Properties.UsageStartTime"); Assert.True(expectedEndTime == ua.Properties.UsageEndTime, "expectedEndTime == ua.Properties.UsageEndTime"); }
public void MeterResourceIdHandlesBothStringAndGuid() { var response = new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(HttpPayload.GetAggregateWithNonGuidResourceId) }; var handler = new RecordedDelegatingHandler(response) { StatusCodeToReturn = HttpStatusCode.OK }; var client = GetUsageAggregationManagementClient(handler); UsageAggregationGetResponse result = client.UsageAggregates.Get(startDate, endDate, AggregationGranularity.Daily, false, null); // Validate headers Assert.Equal(HttpMethod.Get, handler.Method); UsageAggregation ua = result.UsageAggregations[0]; Assert.True("AzureSql" == ua.Properties.MeterId, "AzureSql == ua.Properties.MeterId"); }
/// <summary> /// Query aggregated Azure subscription consumption data for a date /// range. (see /// https://msdn.microsoft.com/library/azure/1ea5b323-54bb-423d-916f-190de96c6a3c /// for more information) /// </summary> /// <param name='reportedStartTime'> /// Required. The start of the time range to retrieve data for. /// </param> /// <param name='reportedEndTime'> /// Required. The end of the time range to retrieve data for. /// </param> /// <param name='aggregationGranularity'> /// Required. Value is either daily (default) or hourly to tell the API /// how to return the results grouped by day or hour. /// </param> /// <param name='showDetails'> /// Required. When set to true (default), the aggregates are broken /// down into the instance metadata which is more granular. /// </param> /// <param name='continuationToken'> /// Optional. Retrieved from previous calls, this is the bookmark used /// for progress when the responses are paged. /// </param> /// <param name='cancellationToken'> /// Cancellation token. /// </param> /// <returns> /// The Get UsageAggregates operation response. /// </returns> public async Task <UsageAggregationGetResponse> GetAsync(DateTime reportedStartTime, DateTime reportedEndTime, AggregationGranularity aggregationGranularity, bool showDetails, string continuationToken, CancellationToken cancellationToken) { // Validate // Tracing bool shouldTrace = TracingAdapter.IsEnabled; string invocationId = null; if (shouldTrace) { invocationId = TracingAdapter.NextInvocationId.ToString(); Dictionary <string, object> tracingParameters = new Dictionary <string, object>(); tracingParameters.Add("reportedStartTime", reportedStartTime); tracingParameters.Add("reportedEndTime", reportedEndTime); tracingParameters.Add("aggregationGranularity", aggregationGranularity); tracingParameters.Add("showDetails", showDetails); tracingParameters.Add("continuationToken", continuationToken); TracingAdapter.Enter(invocationId, this, "GetAsync", tracingParameters); } // Construct URL string url = ""; url = url + "subscriptions/"; if (this.Client.Credentials.SubscriptionId != null) { url = url + Uri.EscapeDataString(this.Client.Credentials.SubscriptionId); } url = url + "/providers/Microsoft.Commerce/UsageAggregates"; List <string> queryParameters = new List <string>(); queryParameters.Add("api-version=2015-06-01-preview"); queryParameters.Add("reportedstartTime=" + Uri.EscapeDataString(reportedStartTime.ToString("O"))); queryParameters.Add("reportedEndTime=" + Uri.EscapeDataString(reportedEndTime.ToString("O"))); queryParameters.Add("showDetails=" + Uri.EscapeDataString(showDetails.ToString().ToLower())); queryParameters.Add("aggregationGranularity=" + Uri.EscapeDataString(UsageAggregationManagementClient.AggregationGranularityToString(aggregationGranularity))); if (continuationToken != null) { queryParameters.Add("continuationToken=" + Uri.EscapeDataString(continuationToken)); } if (queryParameters.Count > 0) { url = url + "?" + string.Join("&", queryParameters); } string baseUrl = this.Client.BaseUri.AbsoluteUri; // Trim '/' character from the end of baseUrl and beginning of url. if (baseUrl[baseUrl.Length - 1] == '/') { baseUrl = baseUrl.Substring(0, baseUrl.Length - 1); } if (url[0] == '/') { url = url.Substring(1); } url = baseUrl + "/" + url; url = url.Replace(" ", "%20"); // Create HTTP transport objects HttpRequestMessage httpRequest = null; try { httpRequest = new HttpRequestMessage(); httpRequest.Method = HttpMethod.Get; httpRequest.RequestUri = new Uri(url); // Set Headers // Set Credentials cancellationToken.ThrowIfCancellationRequested(); await this.Client.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false); // Send Request HttpResponseMessage httpResponse = null; try { if (shouldTrace) { TracingAdapter.SendRequest(invocationId, httpRequest); } cancellationToken.ThrowIfCancellationRequested(); httpResponse = await this.Client.HttpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false); if (shouldTrace) { TracingAdapter.ReceiveResponse(invocationId, httpResponse); } HttpStatusCode statusCode = httpResponse.StatusCode; if (statusCode != HttpStatusCode.OK) { cancellationToken.ThrowIfCancellationRequested(); CloudException ex = CloudException.Create(httpRequest, null, httpResponse, await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false)); if (shouldTrace) { TracingAdapter.Error(invocationId, ex); } throw ex; } // Create Result UsageAggregationGetResponse result = null; // Deserialize Response if (statusCode == HttpStatusCode.OK) { cancellationToken.ThrowIfCancellationRequested(); string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); result = new UsageAggregationGetResponse(); JToken responseDoc = null; if (string.IsNullOrEmpty(responseContent) == false) { responseDoc = JToken.Parse(responseContent); } if (responseDoc != null && responseDoc.Type != JTokenType.Null) { JToken valueArray = responseDoc["value"]; if (valueArray != null && valueArray.Type != JTokenType.Null) { foreach (JToken valueValue in ((JArray)valueArray)) { UsageAggregation usageAggregationInstance = new UsageAggregation(); result.UsageAggregations.Add(usageAggregationInstance); JToken idValue = valueValue["id"]; if (idValue != null && idValue.Type != JTokenType.Null) { string idInstance = ((string)idValue); usageAggregationInstance.Id = idInstance; } JToken nameValue = valueValue["name"]; if (nameValue != null && nameValue.Type != JTokenType.Null) { string nameInstance = ((string)nameValue); usageAggregationInstance.Name = nameInstance; } JToken typeValue = valueValue["type"]; if (typeValue != null && typeValue.Type != JTokenType.Null) { string typeInstance = ((string)typeValue); usageAggregationInstance.Type = typeInstance; } JToken propertiesValue = valueValue["properties"]; if (propertiesValue != null && propertiesValue.Type != JTokenType.Null) { UsageSample propertiesInstance = new UsageSample(); usageAggregationInstance.Properties = propertiesInstance; JToken meterIdValue = propertiesValue["meterId"]; if (meterIdValue != null && meterIdValue.Type != JTokenType.Null) { string meterIdInstance = (string)meterIdValue; propertiesInstance.MeterId = meterIdInstance; } JToken usageStartTimeValue = propertiesValue["usageStartTime"]; if (usageStartTimeValue != null && usageStartTimeValue.Type != JTokenType.Null) { DateTime usageStartTimeInstance = ((DateTime)usageStartTimeValue); propertiesInstance.UsageStartTime = usageStartTimeInstance.ToUniversalTime(); } JToken usageEndTimeValue = propertiesValue["usageEndTime"]; if (usageEndTimeValue != null && usageEndTimeValue.Type != JTokenType.Null) { DateTime usageEndTimeInstance = ((DateTime)usageEndTimeValue); propertiesInstance.UsageEndTime = usageEndTimeInstance.ToUniversalTime(); } JToken quantityValue = propertiesValue["quantity"]; if (quantityValue != null && quantityValue.Type != JTokenType.Null) { decimal quantityInstance = ((decimal)quantityValue); propertiesInstance.Quantity = quantityInstance; } JToken unitValue = propertiesValue["unit"]; if (unitValue != null && unitValue.Type != JTokenType.Null) { string unitInstance = ((string)unitValue); propertiesInstance.Unit = unitInstance; } JToken meterNameValue = propertiesValue["meterName"]; if (meterNameValue != null && meterNameValue.Type != JTokenType.Null) { string meterNameInstance = ((string)meterNameValue); propertiesInstance.MeterName = meterNameInstance; } JToken meterCategoryValue = propertiesValue["meterCategory"]; if (meterCategoryValue != null && meterCategoryValue.Type != JTokenType.Null) { string meterCategoryInstance = ((string)meterCategoryValue); propertiesInstance.MeterCategory = meterCategoryInstance; } JToken meterSubCategoryValue = propertiesValue["meterSubCategory"]; if (meterSubCategoryValue != null && meterSubCategoryValue.Type != JTokenType.Null) { string meterSubCategoryInstance = ((string)meterSubCategoryValue); propertiesInstance.MeterSubCategory = meterSubCategoryInstance; } JToken meterRegionValue = propertiesValue["meterRegion"]; if (meterRegionValue != null && meterRegionValue.Type != JTokenType.Null) { string meterRegionInstance = ((string)meterRegionValue); propertiesInstance.MeterRegion = meterRegionInstance; } JToken infoFieldsValue = propertiesValue["infoFields"]; if (infoFieldsValue != null && infoFieldsValue.Type != JTokenType.Null) { InfoField infoFieldsInstance = new InfoField(); propertiesInstance.InfoFields = infoFieldsInstance; JToken projectValue = infoFieldsValue["project"]; if (projectValue != null && projectValue.Type != JTokenType.Null) { string projectInstance = ((string)projectValue); infoFieldsInstance.Project = projectInstance; } } JToken instanceDataValue = propertiesValue["instanceData"]; if (instanceDataValue != null && instanceDataValue.Type != JTokenType.Null) { string instanceDataInstance = ((string)instanceDataValue); propertiesInstance.InstanceData = instanceDataInstance; } } } } JToken nextLinkValue = responseDoc["nextLink"]; if (nextLinkValue != null && nextLinkValue.Type != JTokenType.Null) { string nextLinkInstance = ((string)nextLinkValue); result.NextLink = nextLinkInstance; if (!string.IsNullOrWhiteSpace(nextLinkInstance)) { string key = "continuationToken="; int startLocation = nextLinkInstance.IndexOf(key, StringComparison.OrdinalIgnoreCase); if (startLocation >= 0) { startLocation = startLocation + key.Length; int length = nextLinkInstance.Length - startLocation; string token = nextLinkInstance.Substring(startLocation, length); result.ContinuationToken = Uri.UnescapeDataString(token); } } } } } result.StatusCode = statusCode; if (shouldTrace) { TracingAdapter.Exit(invocationId, result); } return(result); } finally { if (httpResponse != null) { httpResponse.Dispose(); } } } finally { if (httpRequest != null) { httpRequest.Dispose(); } } }