Beispiel #1
0
        private DataSourceGetResponse GenerateGetResponse(string content, MonikerIdentifier id, ulong version, PointInTime timestamp, int i, int total)
        {
            var response = new DataSourceGetResponse
            {
                Success = new StoredValue
                {
                    Id = new MonikerVersionPartId
                    {
                        Id         = id,
                        Version    = version,
                        Timestamp  = timestamp,
                        PartIndex  = (uint)i,
                        PartsCount = (uint)total,
                        Checksum   = HashString(content)
                    },
                    StringValue = content
                }
            };

            return(response);
        }
        public virtual PSDataSource UpdatePSDataSource(UpdatePSDataSourceParameters parameters)
        {
            // Get the existing data source
            DataSourceGetResponse response   = OperationalInsightsManagementClient.DataSources.Get(parameters.ResourceGroupName, parameters.WorkspaceName, parameters.Name);
            DataSource            dataSource = response.DataSource;

            if (parameters.Properties.Kind != dataSource.Kind)
            {
                throw new ArgumentException(Resources.DataSourceUpdateCannotModifyKind);
            }
            if (parameters.Name != dataSource.Name)
            {
                throw new ArgumentException(Resources.DataSourceUpdateCannotModifyName);
            }

            // Execute the update
            DataSource updatedDataSource = CreateOrUpdateDataSource(
                parameters.ResourceGroupName,
                parameters.WorkspaceName,
                dataSource.Name,
                parameters.Properties);

            return(new PSDataSource(updatedDataSource, parameters.ResourceGroupName, parameters.WorkspaceName));
        }
        /// <summary>
        /// Gets a data source instance.
        /// </summary>
        /// <param name='resourceGroupName'>
        /// Required. The resource group name of the data source.
        /// </param>
        /// <param name='workspaceName'>
        /// Required. The name of the workspace that contains the data source.
        /// </param>
        /// <param name='dataSourceName'>
        /// Required. Name of the data source
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        /// <returns>
        /// The get data source operation response.
        /// </returns>
        public async Task <DataSourceGetResponse> GetAsync(string resourceGroupName, string workspaceName, string dataSourceName, CancellationToken cancellationToken)
        {
            // Validate
            if (resourceGroupName == null)
            {
                throw new ArgumentNullException("resourceGroupName");
            }
            if (workspaceName == null)
            {
                throw new ArgumentNullException("workspaceName");
            }
            if (dataSourceName == null)
            {
                throw new ArgumentNullException("dataSourceName");
            }

            // Tracing
            bool   shouldTrace  = TracingAdapter.IsEnabled;
            string invocationId = null;

            if (shouldTrace)
            {
                invocationId = TracingAdapter.NextInvocationId.ToString();
                Dictionary <string, object> tracingParameters = new Dictionary <string, object>();
                tracingParameters.Add("resourceGroupName", resourceGroupName);
                tracingParameters.Add("workspaceName", workspaceName);
                tracingParameters.Add("dataSourceName", dataSourceName);
                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 + "/resourcegroups/";
            url = url + Uri.EscapeDataString(resourceGroupName);
            url = url + "/providers/Microsoft.OperationalInsights/workspaces/";
            url = url + Uri.EscapeDataString(workspaceName);
            url = url + "/dataSources/";
            url = url + Uri.EscapeDataString(dataSourceName);
            List <string> queryParameters = new List <string>();

            queryParameters.Add("api-version=2015-11-01-preview");
            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("x-ms-client-request-id", Guid.NewGuid().ToString());

                // 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
                    DataSourceGetResponse result = null;
                    // Deserialize Response
                    if (statusCode == HttpStatusCode.OK)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);

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

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

                            JToken propertiesValue = responseDoc["properties"];
                            if (propertiesValue != null && propertiesValue.Type != JTokenType.Null)
                            {
                                string propertiesInstance = propertiesValue.ToString(Newtonsoft.Json.Formatting.Indented);
                                dataSourceInstance.Properties = propertiesInstance;
                            }

                            JToken eTagValue = responseDoc["eTag"];
                            if (eTagValue != null && eTagValue.Type != JTokenType.Null)
                            {
                                string eTagInstance = ((string)eTagValue);
                                dataSourceInstance.ETag = eTagInstance;
                            }

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

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

                            JToken typeValue = responseDoc["type"];
                            if (typeValue != null && typeValue.Type != JTokenType.Null)
                            {
                                string typeInstance = ((string)typeValue);
                                dataSourceInstance.Type = typeInstance;
                            }

                            JToken kindValue = responseDoc["kind"];
                            if (kindValue != null && kindValue.Type != JTokenType.Null)
                            {
                                string kindInstance = ((string)kindValue);
                                dataSourceInstance.Kind = kindInstance;
                            }
                        }
                    }
                    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();
                }
            }
        }