Ejemplo n.º 1
0
 internal VirtualMachineImageModel(VirtualMachineVMImageListResponse.VirtualMachineVMImage image)
 {
     Name = image.Name;
     FriendlyName = image.Label;
     Description = image.DeploymentName;
     IsUserImage = image.Category == "User";
 }
        private string GetVmImageUri(string imageName)
        {
            ComputeManagementClient           computeClient = new ComputeManagementClient(this.Client.Credentials, this.Client.BaseUri);
            VirtualMachineVMImageListResponse vmList        = null;
            ErrorRecord er       = null;
            string      imageUri = null;

            try
            {
                vmList = computeClient.VirtualMachineVMImages.List();
            }
            catch (Exception ex)
            {
                er = RemoteAppCollectionErrorState.CreateErrorRecordFromString(
                    ex.Message,
                    String.Empty,
                    Client.TemplateImages,
                    ErrorCategory.InvalidArgument
                    );

                ThrowTerminatingError(er);
            }

            foreach (VirtualMachineVMImageListResponse.VirtualMachineVMImage image in vmList.VMImages)
            {
                if (string.Compare(image.Name, imageName, true) == 0)
                {
                    if (image.OSDiskConfiguration != null)
                    {
                        ValidateImageOsType(image.OSDiskConfiguration.OperatingSystem);
                        ValidateImageMediaLink(image.OSDiskConfiguration.MediaLink);

                        imageUri = image.OSDiskConfiguration.MediaLink.AbsoluteUri;
                        break;
                    }
                    else
                    {
                        er = RemoteAppCollectionErrorState.CreateErrorRecordFromString(
                            string.Format(Commands_RemoteApp.NoOsDiskFoundErrorFormat, imageName),
                            String.Empty,
                            Client.TemplateImages,
                            ErrorCategory.InvalidArgument
                            );

                        ThrowTerminatingError(er);
                    }
                }
            }

            if (imageUri == null)
            {
                er = RemoteAppCollectionErrorState.CreateErrorRecordFromString(
                    string.Format(Commands_RemoteApp.NoVmImageFoundErrorFormat, imageName),
                    String.Empty,
                    Client.TemplateImages,
                    ErrorCategory.InvalidArgument
                    );

                ThrowTerminatingError(er);
            }

            return(imageUri);
        }
        /// <summary>
        /// The List Virtual Machine Images operation retrieves a list of the
        /// virtual machine images.
        /// </summary>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        /// <returns>
        /// The List VM Images operation response.
        /// </returns>
        public async System.Threading.Tasks.Task <Microsoft.WindowsAzure.Management.Compute.Models.VirtualMachineVMImageListResponse> ListAsync(CancellationToken cancellationToken)
        {
            // Validate

            // Tracing
            bool   shouldTrace  = CloudContext.Configuration.Tracing.IsEnabled;
            string invocationId = null;

            if (shouldTrace)
            {
                invocationId = Tracing.NextInvocationId.ToString();
                Dictionary <string, object> tracingParameters = new Dictionary <string, object>();
                Tracing.Enter(invocationId, this, "ListAsync", tracingParameters);
            }

            // Construct URL
            string baseUrl = this.Client.BaseUri.AbsoluteUri;
            string url     = "/" + this.Client.Credentials.SubscriptionId.Trim() + "/services/vmimages";

            // 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;

            // 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-version", "2014-04-01");

                // Set Credentials
                cancellationToken.ThrowIfCancellationRequested();
                await this.Client.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false);

                // Send Request
                HttpResponseMessage httpResponse = null;
                try
                {
                    if (shouldTrace)
                    {
                        Tracing.SendRequest(invocationId, httpRequest);
                    }
                    cancellationToken.ThrowIfCancellationRequested();
                    httpResponse = await this.Client.HttpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false);

                    if (shouldTrace)
                    {
                        Tracing.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), CloudExceptionType.Xml);
                        if (shouldTrace)
                        {
                            Tracing.Error(invocationId, ex);
                        }
                        throw ex;
                    }

                    // Create Result
                    VirtualMachineVMImageListResponse result = null;
                    // Deserialize Response
                    cancellationToken.ThrowIfCancellationRequested();
                    string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);

                    result = new VirtualMachineVMImageListResponse();
                    XDocument responseDoc = XDocument.Parse(responseContent);

                    XElement vMImagesSequenceElement = responseDoc.Element(XName.Get("VMImages", "http://schemas.microsoft.com/windowsazure"));
                    if (vMImagesSequenceElement != null && vMImagesSequenceElement.IsEmpty == false)
                    {
                        foreach (XElement vMImagesElement in vMImagesSequenceElement.Elements(XName.Get("VMImage", "http://schemas.microsoft.com/windowsazure")))
                        {
                            VirtualMachineVMImageListResponse.VirtualMachineVMImage vMImageInstance = new VirtualMachineVMImageListResponse.VirtualMachineVMImage();
                            result.VMImages.Add(vMImageInstance);

                            XElement nameElement = vMImagesElement.Element(XName.Get("Name", "http://schemas.microsoft.com/windowsazure"));
                            if (nameElement != null && nameElement.IsEmpty == false)
                            {
                                string nameInstance = nameElement.Value;
                                vMImageInstance.Name = nameInstance;
                            }

                            XElement labelElement = vMImagesElement.Element(XName.Get("Label", "http://schemas.microsoft.com/windowsazure"));
                            if (labelElement != null && labelElement.IsEmpty == false)
                            {
                                string labelInstance = labelElement.Value;
                                vMImageInstance.Label = labelInstance;
                            }

                            XElement categoryElement = vMImagesElement.Element(XName.Get("Category", "http://schemas.microsoft.com/windowsazure"));
                            if (categoryElement != null && categoryElement.IsEmpty == false)
                            {
                                string categoryInstance = categoryElement.Value;
                                vMImageInstance.Category = categoryInstance;
                            }

                            XElement oSDiskConfigurationElement = vMImagesElement.Element(XName.Get("OSDiskConfiguration", "http://schemas.microsoft.com/windowsazure"));
                            if (oSDiskConfigurationElement != null && oSDiskConfigurationElement.IsEmpty == false)
                            {
                                VirtualMachineVMImageListResponse.OSDiskConfiguration oSDiskConfigurationInstance = new VirtualMachineVMImageListResponse.OSDiskConfiguration();
                                vMImageInstance.OSDiskConfiguration = oSDiskConfigurationInstance;

                                XElement nameElement2 = oSDiskConfigurationElement.Element(XName.Get("Name", "http://schemas.microsoft.com/windowsazure"));
                                if (nameElement2 != null && nameElement2.IsEmpty == false)
                                {
                                    string nameInstance2 = nameElement2.Value;
                                    oSDiskConfigurationInstance.Name = nameInstance2;
                                }

                                XElement hostCachingElement = oSDiskConfigurationElement.Element(XName.Get("HostCaching", "http://schemas.microsoft.com/windowsazure"));
                                if (hostCachingElement != null && hostCachingElement.IsEmpty == false && string.IsNullOrEmpty(hostCachingElement.Value) == false)
                                {
                                    VirtualHardDiskHostCaching hostCachingInstance = ((VirtualHardDiskHostCaching)Enum.Parse(typeof(VirtualHardDiskHostCaching), hostCachingElement.Value, true));
                                    oSDiskConfigurationInstance.HostCaching = hostCachingInstance;
                                }

                                XElement oSStateElement = oSDiskConfigurationElement.Element(XName.Get("OSState", "http://schemas.microsoft.com/windowsazure"));
                                if (oSStateElement != null && oSStateElement.IsEmpty == false)
                                {
                                    string oSStateInstance = oSStateElement.Value;
                                    oSDiskConfigurationInstance.OSState = oSStateInstance;
                                }

                                XElement osElement = oSDiskConfigurationElement.Element(XName.Get("OS", "http://schemas.microsoft.com/windowsazure"));
                                if (osElement != null && osElement.IsEmpty == false)
                                {
                                    string osInstance = osElement.Value;
                                    oSDiskConfigurationInstance.OperatingSystem = osInstance;
                                }

                                XElement mediaLinkElement = oSDiskConfigurationElement.Element(XName.Get("MediaLink", "http://schemas.microsoft.com/windowsazure"));
                                if (mediaLinkElement != null && mediaLinkElement.IsEmpty == false)
                                {
                                    Uri mediaLinkInstance = TypeConversion.TryParseUri(mediaLinkElement.Value);
                                    oSDiskConfigurationInstance.MediaLink = mediaLinkInstance;
                                }

                                XElement logicalDiskSizeInGBElement = oSDiskConfigurationElement.Element(XName.Get("LogicalDiskSizeInGB", "http://schemas.microsoft.com/windowsazure"));
                                if (logicalDiskSizeInGBElement != null && logicalDiskSizeInGBElement.IsEmpty == false)
                                {
                                    int logicalDiskSizeInGBInstance = int.Parse(logicalDiskSizeInGBElement.Value, CultureInfo.InvariantCulture);
                                    oSDiskConfigurationInstance.LogicalDiskSizeInGB = logicalDiskSizeInGBInstance;
                                }
                            }

                            XElement dataDiskConfigurationsSequenceElement = vMImagesElement.Element(XName.Get("DataDiskConfigurations", "http://schemas.microsoft.com/windowsazure"));
                            if (dataDiskConfigurationsSequenceElement != null && dataDiskConfigurationsSequenceElement.IsEmpty == false)
                            {
                                foreach (XElement dataDiskConfigurationsElement in dataDiskConfigurationsSequenceElement.Elements(XName.Get("DataDiskConfiguration", "http://schemas.microsoft.com/windowsazure")))
                                {
                                    VirtualMachineVMImageListResponse.DataDiskConfiguration dataDiskConfigurationInstance = new VirtualMachineVMImageListResponse.DataDiskConfiguration();
                                    vMImageInstance.DataDiskConfigurations.Add(dataDiskConfigurationInstance);

                                    XElement nameElement3 = dataDiskConfigurationsElement.Element(XName.Get("Name", "http://schemas.microsoft.com/windowsazure"));
                                    if (nameElement3 != null && nameElement3.IsEmpty == false)
                                    {
                                        string nameInstance3 = nameElement3.Value;
                                        dataDiskConfigurationInstance.Name = nameInstance3;
                                    }

                                    XElement hostCachingElement2 = dataDiskConfigurationsElement.Element(XName.Get("HostCaching", "http://schemas.microsoft.com/windowsazure"));
                                    if (hostCachingElement2 != null && hostCachingElement2.IsEmpty == false && string.IsNullOrEmpty(hostCachingElement2.Value) == false)
                                    {
                                        VirtualHardDiskHostCaching hostCachingInstance2 = ((VirtualHardDiskHostCaching)Enum.Parse(typeof(VirtualHardDiskHostCaching), hostCachingElement2.Value, true));
                                        dataDiskConfigurationInstance.HostCaching = hostCachingInstance2;
                                    }

                                    XElement lunElement = dataDiskConfigurationsElement.Element(XName.Get("Lun", "http://schemas.microsoft.com/windowsazure"));
                                    if (lunElement != null && lunElement.IsEmpty == false && string.IsNullOrEmpty(lunElement.Value) == false)
                                    {
                                        int lunInstance = int.Parse(lunElement.Value, CultureInfo.InvariantCulture);
                                        dataDiskConfigurationInstance.LogicalUnitNumber = lunInstance;
                                    }

                                    XElement mediaLinkElement2 = dataDiskConfigurationsElement.Element(XName.Get("MediaLink", "http://schemas.microsoft.com/windowsazure"));
                                    if (mediaLinkElement2 != null && mediaLinkElement2.IsEmpty == false)
                                    {
                                        Uri mediaLinkInstance2 = TypeConversion.TryParseUri(mediaLinkElement2.Value);
                                        dataDiskConfigurationInstance.MediaLink = mediaLinkInstance2;
                                    }

                                    XElement logicalDiskSizeInGBElement2 = dataDiskConfigurationsElement.Element(XName.Get("LogicalDiskSizeInGB", "http://schemas.microsoft.com/windowsazure"));
                                    if (logicalDiskSizeInGBElement2 != null && logicalDiskSizeInGBElement2.IsEmpty == false)
                                    {
                                        int logicalDiskSizeInGBInstance2 = int.Parse(logicalDiskSizeInGBElement2.Value, CultureInfo.InvariantCulture);
                                        dataDiskConfigurationInstance.LogicalDiskSizeInGB = logicalDiskSizeInGBInstance2;
                                    }
                                }
                            }

                            XElement serviceNameElement = vMImagesElement.Element(XName.Get("ServiceName", "http://schemas.microsoft.com/windowsazure"));
                            if (serviceNameElement != null && serviceNameElement.IsEmpty == false)
                            {
                                string serviceNameInstance = serviceNameElement.Value;
                                vMImageInstance.ServiceName = serviceNameInstance;
                            }

                            XElement deploymentNameElement = vMImagesElement.Element(XName.Get("DeploymentName", "http://schemas.microsoft.com/windowsazure"));
                            if (deploymentNameElement != null && deploymentNameElement.IsEmpty == false)
                            {
                                string deploymentNameInstance = deploymentNameElement.Value;
                                vMImageInstance.DeploymentName = deploymentNameInstance;
                            }

                            XElement roleNameElement = vMImagesElement.Element(XName.Get("RoleName", "http://schemas.microsoft.com/windowsazure"));
                            if (roleNameElement != null && roleNameElement.IsEmpty == false)
                            {
                                string roleNameInstance = roleNameElement.Value;
                                vMImageInstance.RoleName = roleNameInstance;
                            }

                            XElement affinityGroupElement = vMImagesElement.Element(XName.Get("AffinityGroup", "http://schemas.microsoft.com/windowsazure"));
                            if (affinityGroupElement != null && affinityGroupElement.IsEmpty == false)
                            {
                                string affinityGroupInstance = affinityGroupElement.Value;
                                vMImageInstance.AffinityGroup = affinityGroupInstance;
                            }

                            XElement createdTimeElement = vMImagesElement.Element(XName.Get("CreatedTime", "http://schemas.microsoft.com/windowsazure"));
                            if (createdTimeElement != null && createdTimeElement.IsEmpty == false && string.IsNullOrEmpty(createdTimeElement.Value) == false)
                            {
                                DateTime createdTimeInstance = DateTime.Parse(createdTimeElement.Value, CultureInfo.InvariantCulture);
                                vMImageInstance.CreatedTime = createdTimeInstance;
                            }
                        }
                    }

                    result.StatusCode = statusCode;
                    if (httpResponse.Headers.Contains("x-ms-request-id"))
                    {
                        result.RequestId = httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault();
                    }

                    if (shouldTrace)
                    {
                        Tracing.Exit(invocationId, result);
                    }
                    return(result);
                }
                finally
                {
                    if (httpResponse != null)
                    {
                        httpResponse.Dispose();
                    }
                }
            }
            finally
            {
                if (httpRequest != null)
                {
                    httpRequest.Dispose();
                }
            }
        }