public override void ExecuteCmdlet()
        {
            base.ExecuteCmdlet();

            var parameters = new VirtualMachineExtensionImageGetParameters
            {
                Location         = Location.Canonicalize(),
                PublisherName    = PublisherName,
                Type             = Type,
                FilterExpression = FilterExpression,
                Version          = Version
            };

            VirtualMachineExtensionImageGetResponse result = this.VirtualMachineExtensionImageClient.Get(parameters);

            var image = new PSVirtualMachineExtensionImageDetails
            {
                RequestId                  = result.RequestId,
                StatusCode                 = result.StatusCode,
                Id                         = result.VirtualMachineExtensionImage.Id,
                Location                   = result.VirtualMachineExtensionImage.Location,
                Name                       = result.VirtualMachineExtensionImage.Name,
                HandlerSchema              = result.VirtualMachineExtensionImage.HandlerSchema,
                OperatingSystem            = result.VirtualMachineExtensionImage.OperatingSystem,
                ComputeRole                = result.VirtualMachineExtensionImage.ComputeRole,
                SupportsMultipleExtensions = result.VirtualMachineExtensionImage.SupportsMultipleExtensions,
                VMScaleSetEnabled          = result.VirtualMachineExtensionImage.VMScaleSetEnabled,
                PublisherName              = this.PublisherName,
                Type                       = this.Type,
                Version                    = this.Version
            };

            WriteObject(image);
        }
        public override void ExecuteCmdlet()
        {
            base.ExecuteCmdlet();

            ExecuteClientAction(() =>
            {
                if (string.IsNullOrEmpty(this.Version))
                {
                    var parameters = new VirtualMachineExtensionImageListVersionsParameters
                    {
                        Location         = Location.Canonicalize(),
                        PublisherName    = PublisherName,
                        Type             = Type,
                        FilterExpression = FilterExpression
                    };

                    VirtualMachineImageResourceList result = this.VirtualMachineExtensionImageClient.ListVersions(parameters);

                    var images = from r in result.Resources
                                 select new PSVirtualMachineExtensionImage
                    {
                        RequestId        = result.RequestId,
                        StatusCode       = result.StatusCode,
                        Id               = r.Id,
                        Location         = r.Location,
                        Version          = r.Name,
                        PublisherName    = this.PublisherName,
                        Type             = this.Type,
                        FilterExpression = this.FilterExpression
                    };

                    WriteObject(images, true);
                }
                else
                {
                    var parameters = new VirtualMachineExtensionImageGetParameters
                    {
                        Location         = Location.Canonicalize(),
                        PublisherName    = PublisherName,
                        Type             = Type,
                        FilterExpression = FilterExpression,
                        Version          = Version
                    };

                    VirtualMachineExtensionImageGetResponse result = this.VirtualMachineExtensionImageClient.Get(parameters);

                    var image = new PSVirtualMachineExtensionImageDetails
                    {
                        RequestId                  = result.RequestId,
                        StatusCode                 = result.StatusCode,
                        Id                         = result.VirtualMachineExtensionImage.Id,
                        Location                   = result.VirtualMachineExtensionImage.Location,
                        HandlerSchema              = result.VirtualMachineExtensionImage.HandlerSchema,
                        OperatingSystem            = result.VirtualMachineExtensionImage.OperatingSystem,
                        ComputeRole                = result.VirtualMachineExtensionImage.ComputeRole,
                        SupportsMultipleExtensions = result.VirtualMachineExtensionImage.SupportsMultipleExtensions,
                        VMScaleSetEnabled          = result.VirtualMachineExtensionImage.VMScaleSetEnabled,
                        Version                    = result.VirtualMachineExtensionImage.Name,
                        PublisherName              = this.PublisherName,
                        Type                       = this.Type,
                        FilterExpression           = this.FilterExpression
                    };

                    WriteObject(image);
                }
            });
        }
Beispiel #3
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();
                }
            }
        }