Example #1
0
        public async Task <Response <VirtualMachineExtensionImage> > GetAsync(string location, string publisherName, string type, string version, CancellationToken cancellationToken = default)
        {
            if (location == null)
            {
                throw new ArgumentNullException(nameof(location));
            }
            if (publisherName == null)
            {
                throw new ArgumentNullException(nameof(publisherName));
            }
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }
            if (version == null)
            {
                throw new ArgumentNullException(nameof(version));
            }

            using var message = CreateGetRequest(location, publisherName, type, version);
            await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false);

            switch (message.Response.Status)
            {
            case 200:
            {
                VirtualMachineExtensionImage value = default;
                using var document = await JsonDocument.ParseAsync(message.Response.ContentStream, default, cancellationToken).ConfigureAwait(false);

                value = VirtualMachineExtensionImage.DeserializeVirtualMachineExtensionImage(document.RootElement);
                return(Response.FromValue(value, message.Response));
            }
Example #2
0
        /// <summary>
        /// Gets a virtual machine extension image.
        /// </summary>
        /// <param name='parameters'>
        /// Optional.
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        /// <returns>
        /// The get virtual machine extension image operation response.
        /// </returns>
        public async Task <VirtualMachineExtensionImageGetResponse> GetAsync(VirtualMachineExtensionImageGetParameters parameters, CancellationToken cancellationToken)
        {
            // Validate
            if (parameters != null)
            {
                if (parameters.Location == null)
                {
                    throw new ArgumentNullException("parameters.Location");
                }
                if (parameters.PublisherName == null)
                {
                    throw new ArgumentNullException("parameters.PublisherName");
                }
                if (parameters.Type == null)
                {
                    throw new ArgumentNullException("parameters.Type");
                }
                if (parameters.Version == null)
                {
                    throw new ArgumentNullException("parameters.Version");
                }
            }

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

            if (shouldTrace)
            {
                invocationId = TracingAdapter.NextInvocationId.ToString();
                Dictionary <string, object> tracingParameters = new Dictionary <string, object>();
                tracingParameters.Add("parameters", parameters);
                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/";
            url = url + "Microsoft.Compute";
            url = url + "/locations/";
            if (parameters != null)
            {
                url = url + Uri.EscapeDataString(parameters.Location);
            }
            url = url + "/publishers/";
            if (parameters != null)
            {
                url = url + Uri.EscapeDataString(parameters.PublisherName);
            }
            url = url + "/artifacttypes/vmextension/types/";
            if (parameters != null)
            {
                url = url + Uri.EscapeDataString(parameters.Type);
            }
            url = url + "/versions/";
            if (parameters != null)
            {
                url = url + Uri.EscapeDataString(parameters.Version);
            }
            List <string> queryParameters = new List <string>();

            queryParameters.Add("api-version=2015-06-15");
            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
                    VirtualMachineExtensionImageGetResponse result = null;
                    // Deserialize Response
                    if (statusCode == HttpStatusCode.OK)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);

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

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

                            JToken propertiesValue = responseDoc["properties"];
                            if (propertiesValue != null && propertiesValue.Type != JTokenType.Null)
                            {
                                JToken operatingSystemValue = propertiesValue["operatingSystem"];
                                if (operatingSystemValue != null && operatingSystemValue.Type != JTokenType.Null)
                                {
                                    string operatingSystemInstance = ((string)operatingSystemValue);
                                    virtualMachineExtensionImageInstance.OperatingSystem = operatingSystemInstance;
                                }

                                JToken computeRoleValue = propertiesValue["computeRole"];
                                if (computeRoleValue != null && computeRoleValue.Type != JTokenType.Null)
                                {
                                    string computeRoleInstance = ((string)computeRoleValue);
                                    virtualMachineExtensionImageInstance.ComputeRole = computeRoleInstance;
                                }

                                JToken handlerSchemaValue = propertiesValue["handlerSchema"];
                                if (handlerSchemaValue != null && handlerSchemaValue.Type != JTokenType.Null)
                                {
                                    string handlerSchemaInstance = ((string)handlerSchemaValue);
                                    virtualMachineExtensionImageInstance.HandlerSchema = handlerSchemaInstance;
                                }

                                JToken vmScaleSetEnabledValue = propertiesValue["vmScaleSetEnabled"];
                                if (vmScaleSetEnabledValue != null && vmScaleSetEnabledValue.Type != JTokenType.Null)
                                {
                                    bool vmScaleSetEnabledInstance = ((bool)vmScaleSetEnabledValue);
                                    virtualMachineExtensionImageInstance.VMScaleSetEnabled = vmScaleSetEnabledInstance;
                                }

                                JToken supportsMultipleExtensionsValue = propertiesValue["supportsMultipleExtensions"];
                                if (supportsMultipleExtensionsValue != null && supportsMultipleExtensionsValue.Type != JTokenType.Null)
                                {
                                    bool supportsMultipleExtensionsInstance = ((bool)supportsMultipleExtensionsValue);
                                    virtualMachineExtensionImageInstance.SupportsMultipleExtensions = supportsMultipleExtensionsInstance;
                                }
                            }

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

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

                            JToken locationValue = responseDoc["location"];
                            if (locationValue != null && locationValue.Type != JTokenType.Null)
                            {
                                string locationInstance = ((string)locationValue);
                                virtualMachineExtensionImageInstance.Location = locationInstance;
                            }
                        }
                    }
                    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();
                }
            }
        }