Ejemplo n.º 1
0
        /// <summary>
        /// Really worker function to validate the user input azure information
        /// </summary>
        /// <param name="subscriptionId"></param>
        /// <param name="thumbprint"></param>
        /// <param name="serviceName"></param>
        /// <param name="storageName"></param>
        private static object[] ValidateAzureAccount(ValidationAsyncContext context, Guid subscriptionId, string thumbprint)
        {
            IList<string> services = null;
            IList<string> storages = null;
            IList<AzureImage> vmImages = null;

            // Use to store the log for validation
            StringBuilder log = new StringBuilder();

            // This dictionary store the validation result
            // If the item through the validation, an item will be added to this dictionary, and true/false indicate the pass/fail.
            IDictionary<ValidateItem, bool> result = new Dictionary<ValidateItem, bool>();

            // This varaible is used for exception to set failed item
            ValidateItem currentItem = ValidateItem.Certificate;

            // If external user cancel this action, just exit this function
            if (context.IsCancelling)
                return null;

            try
            {
                log.AppendLine("Validating Thumbprint");

                // Here will validate the certificate
                AzureManagementClient client = new AzureManagementClient(thumbprint, subscriptionId);
                result.Add(ValidateItem.Certificate, true);

                // If external user cancel this action, just exit this function
                if (context.IsCancelling)
                    return null;

                log.AppendLine("Validating Subscription ID");
                currentItem = ValidateItem.SubscriptionId;

                // Here will validate the subscription
                AzureManagementResponse response = client.SubmitRequest(
                    RequestType.GET,
                    "2009-10-01",
                    "services/hostedservices"
                );
                result.Add(ValidateItem.SubscriptionId, true);

                log.AppendLine("Retrieving Service Name");
                currentItem = ValidateItem.ServiceName;

                services = new List<string>();
                // We will return all service name, so we don't need to validate in here
                XmlNodeList nodes = response.GetXmlNodes("HostedService");

                foreach (XmlNode node in nodes)
                {
                    services.Add(response.GetXmlValue(node, "ServiceName"));
                }

                // If external user cancel this action, just exit this function
                if (context.IsCancelling)
                    return null;

                log.AppendLine("Retrieving Storage Account Name");
                currentItem = ValidateItem.StorageAccountName;

                // We will return all storage name, so we don't need to validate in here
                response = client.SubmitRequest(
                    RequestType.GET,
                    "2009-10-01",
                    "services/storageservices"
                );

                storages = new List<string>();
                nodes = response.GetXmlNodes("StorageService");

                foreach (XmlNode node in nodes)
                {
                    storages.Add(response.GetXmlValue(node, "ServiceName"));
                }

                // If external user cancel this action, just exit this function
                if (context.IsCancelling)
                    return null;

                log.AppendLine("Retrieving Virtual Machine Image Name");
                currentItem = ValidateItem.VMImageName;

                // Query Azure for VM image list
                response = client.SubmitRequest(
                    RequestType.GET,
                    "2010-04-01",
                    "machineimages");

                vmImages = new List<AzureImage>();
                nodes = response.GetXmlNodes("MachineImage");

                foreach (XmlNode node in nodes)
                {
                    AzureImage image = new AzureImage();
                    image.Name = response.GetXmlValue(node, "Name");
                    image.Location = response.GetXmlValue(node, "Location");
                    image.Uuid = new Guid(response.GetXmlValue(node, "Uuid"));
                    image.LastModified = DateTime.Parse(response.GetXmlValue(node, "Timestamp")).ToUniversalTime();
                    image.ParentUuid = response.GetXmlValue(node, "ParentUuid");
                    image.Status = response.GetXmlValue(node, "Status");
                    vmImages.Add(image);
                }
            }
            catch (Exception e)
            {
                log.AppendLine(e.Message);
                result.Add(currentItem, false);
                return new object[] { log.ToString(), result, services, storages, vmImages };
            }

            // If there is no issue, we don't need return any information
            return new object[] { null, result, services, storages, vmImages };
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Get Azure VM image name/location list
        /// </summary>
        /// <param name="subscriptionId"></param>
        /// <param name="thumbprint"></param>
        /// <returns></returns>
        /// <summary>
        public static List<AzureImage> GetVMImagesInfo(Guid subscriptionId, string thumbprint)
        {
            // Query Azure for VM image list
            AzureManagementClient client = new AzureManagementClient(thumbprint, subscriptionId);
            AzureManagementResponse response = client.SubmitRequest(
                RequestType.GET,
                "2010-04-01",
                "machineimages");

            // Create collection to return
            List<AzureImage> images = new List<AzureImage>();

            // If there is no image in the result, return an empty collection
            XmlNodeList nodes = response.GetXmlNodes("MachineImage");
            if (nodes != null)
            {
                foreach (XmlNode node in nodes)
                {
                    AzureImage image = new AzureImage();
                    image.Name = response.GetXmlValue(node, "Name");
                    image.Location = response.GetXmlValue(node, "Location");
                    image.Uuid = new Guid(response.GetXmlValue(node, "Uuid"));
                    image.LastModified = DateTime.Parse(response.GetXmlValue(node, "Timestamp")).ToUniversalTime();
                    image.ParentUuid = response.GetXmlValue(node, "ParentUuid");
                    image.Status = response.GetXmlValue(node, "Status");
                    images.Add(image);
                }
            }

            return images;
        }