Ejemplo n.º 1
0
        protected virtual Operation WaitForOperation(string opdesc, bool silent)
        {
            string    operationId = RetrieveOperationId();
            Operation operation   = null;

            if (!string.IsNullOrEmpty(operationId))
            {
                try
                {
                    WindowsAzureSubscription currentSubscription = Profile.CurrentSubscription;

                    operation = RetryCall(s => GetOperationStatus(currentSubscription.SubscriptionId, operationId));

                    var activityId = new Random().Next(1, 999999);
                    var progress   = new ProgressRecord(activityId, opdesc, "Operation Status: " + operation.Status);

                    while (string.Compare(operation.Status, OperationState.Succeeded, StringComparison.OrdinalIgnoreCase) != 0 &&
                           string.Compare(operation.Status, OperationState.Failed, StringComparison.OrdinalIgnoreCase) != 0)
                    {
                        if (silent == false)
                        {
                            WriteProgress(progress);
                        }

                        Thread.Sleep(1 * 1000);
                        operation = RetryCall(s => GetOperationStatus(currentSubscription.SubscriptionId, operationId));
                    }

                    if (string.Compare(operation.Status, OperationState.Failed, StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        var errorMessage = string.Format(CultureInfo.InvariantCulture, "{0}: {1}", operation.Status, operation.Error.Message);
                        var exception    = new Exception(errorMessage);
                        WriteError(new ErrorRecord(exception, string.Empty, ErrorCategory.CloseError, null));
                    }

                    if (silent == false)
                    {
                        progress = new ProgressRecord(activityId, opdesc, "Operation Status: " + operation.Status);
                        WriteProgress(progress);
                    }
                }
                catch (CommunicationException ex)
                {
                    WriteErrorDetails(ex);
                }
            }
            else
            {
                operation = new Operation
                {
                    OperationTrackingId = string.Empty,
                    Status = OperationState.Failed
                };
            }

            return(operation);
        }
Ejemplo n.º 2
0
        public IEnumerable <WindowsAzureSubscription> AddAccount(ITokenProvider tokenProvider, PSCredential credential)
        {
            if (ActiveDirectoryEndpoint == null || ActiveDirectoryServiceEndpointResourceId == null)
            {
                throw new Exception(string.Format(Resources.EnvironmentDoesNotSupportActiveDirectory, Name));
            }

            IAccessToken mainToken;

            if (credential != null)
            {
                mainToken = tokenProvider.GetNewToken(this, credential.UserName, credential.Password);
            }
            else
            {
                mainToken = tokenProvider.GetNewToken(this);
            }
            var credentials = new TokenCloudCredentials(mainToken.AccessToken);

            using (var subscriptionClient = AddRestLogHandler(AddUserAgent(new SubscriptionClient(credentials, new Uri(ServiceEndpoint)))))
            {
                var result = subscriptionClient.Subscriptions.List();
                // Filter out subscriptions with no tenant, backfill's not done on them
                foreach (var subscription in result.Subscriptions.Where(s => !string.IsNullOrEmpty(s.ActiveDirectoryTenantId)))
                {
                    var azureSubscription = new WindowsAzureSubscription
                    {
                        ActiveDirectoryEndpoint = ActiveDirectoryEndpoint,
                        ActiveDirectoryTenantId = subscription.ActiveDirectoryTenantId,
                        ActiveDirectoryUserId   = mainToken.UserId,
                        ActiveDirectoryServiceEndpointResourceId = ActiveDirectoryServiceEndpointResourceId,
                        SubscriptionId          = subscription.SubscriptionId,
                        SubscriptionName        = subscription.SubscriptionName,
                        ServiceEndpoint         = !string.IsNullOrEmpty(ServiceEndpoint) ? new Uri(ServiceEndpoint) : null,
                        ResourceManagerEndpoint = !string.IsNullOrEmpty(ResourceManagerEndpoint) ? new Uri(ResourceManagerEndpoint) : null,
                        TokenProvider           = tokenProvider,
                        GalleryEndpoint         = !string.IsNullOrEmpty(GalleryEndpoint) ? new Uri(GalleryEndpoint) : null,
                        SqlDatabaseDnsSuffix    = SqlDatabaseDnsSuffix ?? WindowsAzureEnvironmentConstants.AzureSqlDatabaseDnsSuffix,
                    };

                    if (mainToken.LoginType == LoginType.LiveId)
                    {
                        azureSubscription.SetAccessToken(tokenProvider.GetNewToken(azureSubscription, mainToken.UserId));
                    }
                    else
                    {
                        azureSubscription.SetAccessToken(mainToken);
                    }
                    yield return(azureSubscription);
                }
            }
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Helper constructor to copy data from in memory to serialization format.
 /// </summary>
 /// <param name="inMemorySubscription">The subscription to serialize</param>
 public AzureSubscriptionData(WindowsAzureSubscription inMemorySubscription)
 {
     Name                        = inMemorySubscription.SubscriptionName;
     SubscriptionId              = inMemorySubscription.SubscriptionId;
     ManagementEndpoint          = inMemorySubscription.ServiceEndpoint.ToString();
     ActiveDirectoryEndpoint     = inMemorySubscription.ActiveDirectoryEndpoint;
     ActiveDirectoryTenantId     = inMemorySubscription.ActiveDirectoryTenantId;
     ActiveDirectoryUserId       = inMemorySubscription.ActiveDirectoryUserId;
     IsDefault                   = inMemorySubscription.IsDefault;
     ManagementCertificate       = inMemorySubscription.Certificate != null ? inMemorySubscription.Certificate.Thumbprint : null;
     CloudStorageAccount         = inMemorySubscription.CurrentStorageAccountName;
     RegisteredResourceProviders = inMemorySubscription.RegisteredResourceProviders;
 }
Ejemplo n.º 4
0
        public void UpdateSubscription(WindowsAzureSubscription s)
        {
            if (!subscriptions.Contains(s))
            {
                throw new ArgumentException(
                          string.Format(Resources.CannotUpdateUnknownSubscription,
                                        s.SubscriptionName, s.SubscriptionId));
            }

            if (s.IsDefault)
            {
                UpdateDefaultSubscription(s);
            }

            Save();
        }
Ejemplo n.º 5
0
        public void RemoveSubscription(WindowsAzureSubscription s)
        {
            if (s == currentSubscription)
            {
                currentSubscription = null;
            }

            subscriptions.Remove(s);

            if (s.IsDefault && subscriptions.Count > 0)
            {
                subscriptions[0].IsDefault = true;
            }

            Save();
        }
Ejemplo n.º 6
0
        public void AddSubscription(WindowsAzureSubscription s)
        {
            if (subscriptions.Contains(s) ||
                subscriptions.Any(es => string.Compare(es.SubscriptionName, s.SubscriptionName, StringComparison.OrdinalIgnoreCase) == 0))
            {
                throw new ArgumentException(
                          string.Format(Resources.SubscriptionAlreadyExists, s.SubscriptionName));
            }

            AddSubscriptionInternal(s);
            if (s.IsDefault)
            {
                UpdateDefaultSubscription(s);
            }

            Save();
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Helper constructor to copy data from in memory to serialization format.
        /// </summary>
        /// <param name="inMemorySubscription">The subscription to serialize</param>
        public AzureSubscriptionData(WindowsAzureSubscription inMemorySubscription)
        {
            Name               = inMemorySubscription.SubscriptionName;
            SubscriptionId     = inMemorySubscription.SubscriptionId;
            ManagementEndpoint = inMemorySubscription.ServiceEndpoint != null?inMemorySubscription.ServiceEndpoint.ToString() : null;

            ResourceManagerEndpoint = inMemorySubscription.ResourceManagerEndpoint != null?inMemorySubscription.ResourceManagerEndpoint.ToString() : null;

            ActiveDirectoryEndpoint = inMemorySubscription.ActiveDirectoryEndpoint;
            ActiveDirectoryTenantId = inMemorySubscription.ActiveDirectoryTenantId;
            ActiveDirectoryUserId   = inMemorySubscription.ActiveDirectoryUserId;
            ActiveDirectoryServiceEndpointResourceId = inMemorySubscription.ActiveDirectoryServiceEndpointResourceId;
            IsDefault                   = inMemorySubscription.IsDefault;
            ManagementCertificate       = inMemorySubscription.Certificate != null ? inMemorySubscription.Certificate.Thumbprint : null;
            CloudStorageAccount         = inMemorySubscription.CurrentStorageAccountName;
            RegisteredResourceProviders = inMemorySubscription.RegisteredResourceProviders;
            GalleryEndpoint             = inMemorySubscription.GalleryEndpoint != null?inMemorySubscription.GalleryEndpoint.ToString() : null;

            SqlDatabaseDnsSuffix = inMemorySubscription.SqlDatabaseDnsSuffix ?? WindowsAzureEnvironmentConstants.AzureSqlDatabaseDnsSuffix;
        }
Ejemplo n.º 8
0
        public void UpdateSubscription(WindowsAzureSubscription s)
        {
            if (!subscriptions.Contains(s))
            {
                throw new ArgumentException(
                          string.Format(Resources.CannotUpdateUnknownSubscription,
                                        s.SubscriptionName, s.SubscriptionId));
            }

            if (s.IsDefault)
            {
                UpdateDefaultSubscription(s);
            }

            if (string.IsNullOrEmpty(s.SqlDatabaseDnsSuffix))
            {
                s.SqlDatabaseDnsSuffix = WindowsAzureEnvironmentConstants.AzureSqlDatabaseDnsSuffix;
            }

            Save();
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Helper method to convert to an in memory subscription object.
        /// </summary>
        /// <returns>The in memory subscription</returns>
        public WindowsAzureSubscription ToAzureSubscription()
        {
            var result = new WindowsAzureSubscription
            {
                SubscriptionName        = this.Name,
                SubscriptionId          = this.SubscriptionId,
                ServiceEndpoint         = new Uri(ManagementEndpoint),
                ActiveDirectoryEndpoint = ActiveDirectoryEndpoint,
                ActiveDirectoryTenantId = ActiveDirectoryTenantId,
                ActiveDirectoryUserId   = ActiveDirectoryUserId,
                IsDefault   = this.IsDefault,
                Certificate = !string.IsNullOrEmpty(ManagementCertificate) ? WindowsAzureCertificate.FromThumbprint(ManagementCertificate) : null,
                CurrentStorageAccountName = CloudStorageAccount
            };

            RegisteredResourceProviders = RegisteredResourceProviders ?? new string[0];
            foreach (var resource in RegisteredResourceProviders)
            {
                result.RegisteredResourceProviders.Add(resource);
            }
            return(result);
        }
        public IEnumerable <WindowsAzureSubscription> AddAccount(ITokenProvider tokenProvider)
        {
            if (ActiveDirectoryEndpoint == null)
            {
                throw new Exception(string.Format(Resources.EnvironmentDoesNotSupportActiveDirectory, Name));
            }

            IAccessToken mainToken   = tokenProvider.GetNewToken(this);
            var          credentials = new TokenCloudCredentials(mainToken.AccessToken);

            using (var subscriptionClient = new SubscriptionClient(credentials, new Uri(ServiceEndpoint)))
            {
                var result = subscriptionClient.Subscriptions.List();
                // Filter out subscriptions with no tenant, backfill's not done on them
                foreach (var subscription in result.Subscriptions.Where(s => !string.IsNullOrEmpty(s.ActiveDirectoryTenantId)))
                {
                    var azureSubscription = new WindowsAzureSubscription
                    {
                        ActiveDirectoryEndpoint = ActiveDirectoryEndpoint,
                        ActiveDirectoryTenantId = subscription.ActiveDirectoryTenantId,
                        ActiveDirectoryUserId   = mainToken.UserId,
                        SubscriptionId          = subscription.SubscriptionId,
                        SubscriptionName        = subscription.SubscriptionName,
                        ServiceEndpoint         = new Uri(ServiceEndpoint),
                        TokenProvider           = tokenProvider
                    };

                    if (mainToken.LoginType == LoginType.LiveId)
                    {
                        azureSubscription.SetAccessToken(tokenProvider.GetNewToken(azureSubscription, mainToken.UserId));
                    }
                    else
                    {
                        azureSubscription.SetAccessToken(mainToken);
                    }
                    yield return(azureSubscription);
                }
            }
        }
        public static CloudStorageAccount GetCloudStorageAccount(this WindowsAzureSubscription subscription)
        {
            if (subscription == null || subscription.SubscriptionId == null)
            {
                return(null);
            }

            if (subscription.currentCloudStorageAccount != null)
            {
                return(subscription.currentCloudStorageAccount as CloudStorageAccount);
            }
            else
            {
                using (var storageClient = subscription.CreateClient <StorageManagementClient>())
                {
                    subscription.currentCloudStorageAccount = StorageUtilities.GenerateCloudStorageAccount(
                        storageClient,
                        subscription.currentStorageAccountName);

                    return(subscription.currentCloudStorageAccount as CloudStorageAccount);
                }
            }
        }
Ejemplo n.º 12
0
 private void AddSubscriptionInternal(WindowsAzureSubscription subscription)
 {
     subscription.TokenProvider = TokenProvider;
     subscription.Save          = Save;
     subscriptions.Add(subscription);
 }