protected override void ProcessRecordInternal() { var putParameters = new LogProfileCreatOrUpdateParameters(); if (this.Categories == null) { this.Categories = new List <string>(ValidCategories); } putParameters.Properties = new LogProfile { Categories = this.Categories, Locations = this.Locations, RetentionPolicy = new RetentionPolicy { Days = this.RetentionInDays.HasValue ? this.RetentionInDays.Value : 0, Enabled = this.RetentionInDays.HasValue }, ServiceBusRuleId = this.ServiceBusRuleId, StorageAccountId = this.StorageAccountId }; this.InsightsManagementClient.LogProfilesOperations.CreateOrUpdateAsync( this.Name, putParameters, CancellationToken.None).Wait(); PSLogProfile psResult = new PSLogProfile( "/subscriptions/{0}/providers/microsoft.insights/logprofiles/{1}" .FormatInvariant(DefaultContext.Subscription, this.Name), this.Name, putParameters.Properties); WriteObject(psResult); }
public void LogProfiles_CreateOrUpdateTest() { HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(string.Empty) }; var handler = new RecordedDelegatingHandler(response); InsightsManagementClient customClient = this.GetInsightsManagementClient(handler); var parameters = new LogProfileCreatOrUpdateParameters { Properties = CreateLogProfile() }; customClient.LogProfilesOperations.CreateOrUpdate(DefaultName, parameters); var actualRequest = JsonExtensions.FromJson <LogProfileCreatOrUpdateParameters>(handler.Request); AreEqual(parameters.Properties, actualRequest.Properties); }
/// <summary> /// Create or update the log profile. /// </summary> /// <param name='operations'> /// Reference to the /// Microsoft.Azure.Management.Insights.ILogProfilesOperations. /// </param> /// <param name='name'> /// Required. The name of the log profile. /// </param> /// <param name='parameters'> /// Required. Parameters supplied to the operation. /// </param> /// <returns> /// Generic empty response. We only pass it to ensure json error /// handling /// </returns> public static Task <EmptyResponse> CreateOrUpdateAsync(this ILogProfilesOperations operations, string name, LogProfileCreatOrUpdateParameters parameters) { return(operations.CreateOrUpdateAsync(name, parameters, CancellationToken.None)); }
/// <summary> /// Create or update the log profile. /// </summary> /// <param name='operations'> /// Reference to the /// Microsoft.Azure.Management.Insights.ILogProfilesOperations. /// </param> /// <param name='name'> /// Required. The name of the log profile. /// </param> /// <param name='parameters'> /// Required. Parameters supplied to the operation. /// </param> /// <returns> /// Generic empty response. We only pass it to ensure json error /// handling /// </returns> public static EmptyResponse CreateOrUpdate(this ILogProfilesOperations operations, string name, LogProfileCreatOrUpdateParameters parameters) { return(Task.Factory.StartNew((object s) => { return ((ILogProfilesOperations)s).CreateOrUpdateAsync(name, parameters); } , operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult()); }
/// <summary> /// Create or update the log profile. /// </summary> /// <param name='name'> /// Required. The name of the log profile. /// </param> /// <param name='parameters'> /// Required. Parameters supplied to the operation. /// </param> /// <param name='cancellationToken'> /// Cancellation token. /// </param> /// <returns> /// Generic empty response. We only pass it to ensure json error /// handling /// </returns> public async Task <EmptyResponse> CreateOrUpdateAsync(string name, LogProfileCreatOrUpdateParameters parameters, CancellationToken cancellationToken) { // Validate if (name == null) { throw new ArgumentNullException("name"); } if (parameters == null) { throw new ArgumentNullException("parameters"); } // 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); tracingParameters.Add("parameters", parameters); TracingAdapter.Enter(invocationId, this, "CreateOrUpdateAsync", 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.Put; 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); // Serialize Request string requestContent = null; JToken requestDoc = null; JObject logProfileCreatOrUpdateParametersValue = new JObject(); requestDoc = logProfileCreatOrUpdateParametersValue; if (parameters.Properties != null) { JObject propertiesValue = new JObject(); logProfileCreatOrUpdateParametersValue["properties"] = propertiesValue; if (parameters.Properties.StorageAccountId != null) { propertiesValue["storageAccountId"] = parameters.Properties.StorageAccountId; } if (parameters.Properties.ServiceBusRuleId != null) { propertiesValue["serviceBusRuleId"] = parameters.Properties.ServiceBusRuleId; } if (parameters.Properties.Locations != null) { if (parameters.Properties.Locations is ILazyCollection == false || ((ILazyCollection)parameters.Properties.Locations).IsInitialized) { JArray locationsArray = new JArray(); foreach (string locationsItem in parameters.Properties.Locations) { locationsArray.Add(locationsItem); } propertiesValue["locations"] = locationsArray; } } if (parameters.Properties.Categories != null) { if (parameters.Properties.Categories is ILazyCollection == false || ((ILazyCollection)parameters.Properties.Categories).IsInitialized) { JArray categoriesArray = new JArray(); foreach (string categoriesItem in parameters.Properties.Categories) { categoriesArray.Add(categoriesItem); } propertiesValue["categories"] = categoriesArray; } } if (parameters.Properties.RetentionPolicy != null) { JObject retentionPolicyValue = new JObject(); propertiesValue["retentionPolicy"] = retentionPolicyValue; retentionPolicyValue["enabled"] = parameters.Properties.RetentionPolicy.Enabled; retentionPolicyValue["days"] = parameters.Properties.RetentionPolicy.Days; } } requestContent = requestDoc.ToString(Newtonsoft.Json.Formatting.Indented); httpRequest.Content = new StringContent(requestContent, Encoding.UTF8); httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json"); // 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 && statusCode != HttpStatusCode.Created && statusCode != HttpStatusCode.Accepted) { cancellationToken.ThrowIfCancellationRequested(); CloudException ex = CloudException.Create(httpRequest, requestContent, httpResponse, await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false)); if (shouldTrace) { TracingAdapter.Error(invocationId, ex); } throw ex; } // Create Result EmptyResponse result = null; // Deserialize Response if (statusCode == HttpStatusCode.OK || statusCode == HttpStatusCode.Created || statusCode == HttpStatusCode.Accepted) { cancellationToken.ThrowIfCancellationRequested(); string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); result = new EmptyResponse(); JToken responseDoc = null; if (string.IsNullOrEmpty(responseContent) == false) { responseDoc = JToken.Parse(responseContent); } if (responseDoc != null && responseDoc.Type != JTokenType.Null) { } } 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(); } } }