Ejemplo n.º 1
0
        public void LogProfiles_ListTest()
        {
            var logProfile = CreateLogProfile();

            var expectedResponse = new LogProfileListResponse
            {
                LogProfileCollection = new LogProfileCollection
                {
                    Value = new List <LogProfileResource>
                    {
                        new LogProfileResource()
                        {
                            Id         = ResourceId,
                            Name       = DefaultName,
                            Properties = logProfile
                        }
                    }
                }
            };

            var response = new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent(expectedResponse.LogProfileCollection.ToJson()),
            };

            var handler = new RecordedDelegatingHandler(response);
            InsightsManagementClient customClient = this.GetInsightsManagementClient(handler);

            LogProfileListResponse actualResponse = customClient.LogProfilesOperations.List();

            Assert.Equal(expectedResponse.LogProfileCollection.Value.Count, actualResponse.LogProfileCollection.Value.Count);
            AreEqual(
                expectedResponse.LogProfileCollection.Value[0].Properties,
                actualResponse.LogProfileCollection.Value[0].Properties);
        }
Ejemplo n.º 2
0
        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);
        }
        /// <summary>
        /// List the log profiles.
        /// </summary>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        /// <returns>
        /// A standard service response including an HTTP status code and
        /// request ID.
        /// </returns>
        public async Task <LogProfileListResponse> ListAsync(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>();
                TracingAdapter.Enter(invocationId, this, "ListAsync", 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";
            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
                    LogProfileListResponse result = null;
                    // Deserialize Response
                    if (statusCode == HttpStatusCode.OK)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);

                        result = new LogProfileListResponse();
                        JToken responseDoc = null;
                        if (string.IsNullOrEmpty(responseContent) == false)
                        {
                            responseDoc = JToken.Parse(responseContent);
                        }

                        if (responseDoc != null && responseDoc.Type != JTokenType.Null)
                        {
                            LogProfileCollection logProfileCollectionInstance = new LogProfileCollection();
                            result.LogProfileCollection = logProfileCollectionInstance;

                            JToken valueArray = responseDoc["value"];
                            if (valueArray != null && valueArray.Type != JTokenType.Null)
                            {
                                foreach (JToken valueValue in ((JArray)valueArray))
                                {
                                    LogProfileResource logProfileResourceInstance = new LogProfileResource();
                                    logProfileCollectionInstance.Value.Add(logProfileResourceInstance);

                                    JToken idValue = valueValue["id"];
                                    if (idValue != null && idValue.Type != JTokenType.Null)
                                    {
                                        string idInstance = ((string)idValue);
                                        logProfileResourceInstance.Id = idInstance;
                                    }

                                    JToken nameValue = valueValue["name"];
                                    if (nameValue != null && nameValue.Type != JTokenType.Null)
                                    {
                                        string nameInstance = ((string)nameValue);
                                        logProfileResourceInstance.Name = nameInstance;
                                    }

                                    JToken propertiesValue = valueValue["properties"];
                                    if (propertiesValue != null && propertiesValue.Type != JTokenType.Null)
                                    {
                                        LogProfile propertiesInstance = new LogProfile();
                                        logProfileResourceInstance.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();
                }
            }
        }