Esempio n. 1
0
        /// <summary>
        /// Finish validation action and retrieve result
        /// </summary>
        /// <param name="context"></param>
        /// <param name="result"></param>
        /// <returns></returns>
        public static string EndValidationAzureAccount(ValidationAsyncContext context, out IDictionary<ValidateItem, bool> result, out IList<string> services, out IList<string> storages, out IList<AzureImage> vmImages)
        {
            result = null;
            services = null;
            storages = null;
            vmImages = null;
            ValidateAzureAccountDelegate worker =
              (ValidateAzureAccountDelegate)((AsyncResult)context.Result).AsyncDelegate;

            // If the delegate didn't finish its job, the EndValidationAzureAccount function will block and until it finished
            object[] ret = worker.EndInvoke(context.Result);
            if (ret == null)
                return null;

            result = (IDictionary<ValidateItem, bool>)ret[1];
            services = (IList<string>)ret[2];
            storages = (IList<string>)ret[3];
            vmImages = (IList<AzureImage>)ret[4];
            return (string)ret[0];
        }
Esempio n. 2
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 };
        }
Esempio n. 3
0
        /// <summary>
        /// Start valiation the azure connection
        /// </summary>
        /// <param name="subscriptionId"></param>
        /// <param name="thumbprint"></param>
        /// <param name="serviceName"></param>
        /// <param name="storageName"></param>
        /// <returns></returns>
        public static ValidationAsyncContext BeginValidateAzureAccount(Guid subscriptionId, string thumbprint)
        {
            ValidateAzureAccountDelegate worker = new ValidateAzureAccountDelegate(ValidateAzureAccount);

            ValidationAsyncContext context = new ValidationAsyncContext();
            IAsyncResult result = worker.BeginInvoke(context, subscriptionId, thumbprint, null, null);

            //Use the context to save the AsyncResult information
            context.Result = result;

            return context;
        }