protected override void ProcessRecordInternal() { var result = new PSLogProfileCollection(); if (string.IsNullOrWhiteSpace(this.Name)) { LogProfileListResponse resultList = this.InsightsManagementClient.LogProfilesOperations.ListAsync(CancellationToken.None).Result; result.AddRange(resultList.LogProfileCollection.Value.Select(x => new PSLogProfile(x.Id, x.Name, x.Properties))); } else { LogProfileGetResponse logProfiles = this.InsightsManagementClient.LogProfilesOperations.GetAsync(this.Name, CancellationToken.None).Result; var psResult = new PSLogProfile(logProfiles.Id, this.Name, logProfiles.Properties); result.Add(psResult); } WriteObject(result); }
public void LogProfiles_GetTest() { var logProfile = CreateLogProfile(); var expectedResponse = new LogProfileGetResponse() { Id = ResourceId, Name = DefaultName, Properties = logProfile, RequestId = "request id", StatusCode = HttpStatusCode.OK }; var response = new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(expectedResponse.ToJson()), }; var handler = new RecordedDelegatingHandler(response); InsightsManagementClient customClient = this.GetInsightsManagementClient(handler); LogProfileGetResponse actualResponse = customClient.LogProfilesOperations.Get(DefaultName); AreEqual(expectedResponse.Properties, actualResponse.Properties); }
/// <summary> /// Gets the log profile. /// </summary> /// <param name='name'> /// Required. The name of the log profile. /// </param> /// <param name='cancellationToken'> /// Cancellation token. /// </param> /// <returns> /// A standard service response including an HTTP status code and /// request ID. /// </returns> public async Task <LogProfileGetResponse> GetAsync(string name, CancellationToken cancellationToken) { // Validate if (name == null) { throw new ArgumentNullException("name"); } // Tracing bool shouldTrace = TracingAdapter.IsEnabled; string invocationId = null; if (shouldTrace) { invocationId = TracingAdapter.NextInvocationId.ToString(); Dictionary <string, object> tracingParameters = new Dictionary <string, object>(); tracingParameters.Add("name", name); 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.insights/logprofiles/"; url = url + Uri.EscapeDataString(name); List <string> queryParameters = new List <string>(); queryParameters.Add("api-version=2016-03-01"); 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 httpRequest.Headers.Add("Accept", "application/json"); // 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 LogProfileGetResponse result = null; // Deserialize Response if (statusCode == HttpStatusCode.OK) { cancellationToken.ThrowIfCancellationRequested(); string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); result = new LogProfileGetResponse(); JToken responseDoc = null; if (string.IsNullOrEmpty(responseContent) == false) { responseDoc = JToken.Parse(responseContent); } if (responseDoc != null && responseDoc.Type != JTokenType.Null) { JToken idValue = responseDoc["id"]; if (idValue != null && idValue.Type != JTokenType.Null) { string idInstance = ((string)idValue); result.Id = idInstance; } JToken nameValue = responseDoc["name"]; if (nameValue != null && nameValue.Type != JTokenType.Null) { string nameInstance = ((string)nameValue); result.Name = nameInstance; } JToken propertiesValue = responseDoc["properties"]; if (propertiesValue != null && propertiesValue.Type != JTokenType.Null) { LogProfile propertiesInstance = new LogProfile(); result.Properties = propertiesInstance; JToken storageAccountIdValue = propertiesValue["storageAccountId"]; if (storageAccountIdValue != null && storageAccountIdValue.Type != JTokenType.Null) { string storageAccountIdInstance = ((string)storageAccountIdValue); propertiesInstance.StorageAccountId = storageAccountIdInstance; } JToken serviceBusRuleIdValue = propertiesValue["serviceBusRuleId"]; if (serviceBusRuleIdValue != null && serviceBusRuleIdValue.Type != JTokenType.Null) { string serviceBusRuleIdInstance = ((string)serviceBusRuleIdValue); propertiesInstance.ServiceBusRuleId = serviceBusRuleIdInstance; } JToken locationsArray = propertiesValue["locations"]; if (locationsArray != null && locationsArray.Type != JTokenType.Null) { foreach (JToken locationsValue in ((JArray)locationsArray)) { propertiesInstance.Locations.Add(((string)locationsValue)); } } JToken categoriesArray = propertiesValue["categories"]; if (categoriesArray != null && categoriesArray.Type != JTokenType.Null) { foreach (JToken categoriesValue in ((JArray)categoriesArray)) { propertiesInstance.Categories.Add(((string)categoriesValue)); } } JToken retentionPolicyValue = propertiesValue["retentionPolicy"]; if (retentionPolicyValue != null && retentionPolicyValue.Type != JTokenType.Null) { RetentionPolicy retentionPolicyInstance = new RetentionPolicy(); propertiesInstance.RetentionPolicy = retentionPolicyInstance; JToken enabledValue = retentionPolicyValue["enabled"]; if (enabledValue != null && enabledValue.Type != JTokenType.Null) { bool enabledInstance = ((bool)enabledValue); retentionPolicyInstance.Enabled = enabledInstance; } JToken daysValue = retentionPolicyValue["days"]; if (daysValue != null && daysValue.Type != JTokenType.Null) { int daysInstance = ((int)daysValue); retentionPolicyInstance.Days = daysInstance; } } } } } result.StatusCode = statusCode; if (httpResponse.Headers.Contains("x-ms-request-id")) { result.RequestId = httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault(); } if (shouldTrace) { TracingAdapter.Exit(invocationId, result); } return(result); } finally { if (httpResponse != null) { httpResponse.Dispose(); } } } finally { if (httpRequest != null) { httpRequest.Dispose(); } } }