Ejemplo n.º 1
0
        private string[] LoadAccountTenants(AzureAccount account, AzureEnvironment environment, SecureString password, ShowDialog promptBehavior)
        {
            var commonTenantToken = AzureSession.AuthenticationFactory.Authenticate(account, environment,
                                                                                    AuthenticationFactory.CommonAdTenant, password, promptBehavior);

            if (environment.IsEndpointSet(AzureEnvironment.Endpoint.ResourceManager))
            {
                using (CSMSubscriptionClient csmSubscriptionClient = AzureSession.ClientFactory
                                                                     .CreateCustomClient <CSMSubscriptionClient>(
                           new TokenCloudCredentials(commonTenantToken.AccessToken),
                           environment.GetEndpointAsUri(AzureEnvironment.Endpoint.ResourceManager)))
                {
                    return(csmSubscriptionClient.Tenants.List().TenantIds.Select(ti => ti.TenantId).ToArray());
                }
            }
            else
            {
                using (RDFESubscriptionClient rdfeSubscriptionClient = AzureSession.ClientFactory
                                                                       .CreateCustomClient <RDFESubscriptionClient>(
                           new TokenCloudCredentials(commonTenantToken.AccessToken),
                           environment.GetEndpointAsUri(AzureEnvironment.Endpoint.ServiceManagement)))
                {
                    var subscriptionListResult = rdfeSubscriptionClient.Subscriptions.List();
                    return(subscriptionListResult.Subscriptions.Select(s => s.ActiveDirectoryTenantId).Distinct().ToArray());
                }
            }
        }
            GetSubscriptionList( SubscriptionCloudCredentials credentials )
        {
            IList<SubscriptionListOperationResponse.Subscription> ret = null;

            using( var subscriptionClient = new SubscriptionClient( credentials ) )
            {
                var listSubscriptionResults =
                    await subscriptionClient.Subscriptions.ListAsync( );

                var subscriptions = listSubscriptionResults.Subscriptions;

                ret = subscriptions;
            }
            return ret;
        }
Ejemplo n.º 3
0
        private static async Task<SubscriptionListOperationResponse.Subscription> GetSubscription(
            CloudCredentials credentials, string filter)
        {
            IEnumerable<SubscriptionListOperationResponse.Subscription> subscriptionList = null;

            using (var client = new SubscriptionClient(credentials))
            {
                var results = await client.Subscriptions.ListAsync();
                subscriptionList = results.Subscriptions;
            }

            var selectedSubscription = subscriptionList.First(s => s.SubscriptionName.Contains(filter));

            return selectedSubscription;
        }
        public static void GetStorageAccountInfo(IAppBuilder map)
        {

            map.Run(async ctx =>
            {
                if (!ctx.Authentication.User.Identity.IsAuthenticated)
                {
                    ctx.Response.Redirect("/login");
                    return;
                }

                var Authorization = ctx.Request.Headers.Get("Authorization");
                var parts = Authorization.Split(' ');
                var token = parts.Last();
                string account = ctx.Request.Query["account"];

                ctx.Response.ContentType = "text/json";

                using (var azure = new SubscriptionClient(new TokenCloudCredentials(token)))
                {
                    var subscriptions = await azure.Subscriptions.ListAsync();

                    var tasks = await Task.WhenAll(subscriptions.Select(id => new TokenCloudCredentials(id.SubscriptionId, token))
                        .Select(async cred =>
                        {
                            using (var storage = CloudContext.Clients.CreateStorageManagementClient(cred))
                            {
                                var storages = await storage.StorageAccounts.ListAsync(new CancellationToken());
                                if (storages.Any(s => s.ServiceName.Equals(account)))
                                {

                                    return await storage.StorageAccounts.GetKeysAsync(account, new CancellationToken());

                                }
                                return null;
                            }
                        }));
                    var key = tasks.First(s => s != null).PrimaryKey;

                    var blobs = new CloudStorageAccount(new StorageCredentials(account, key), true);
                    var blobClient = blobs.CreateCloudBlobClient();
                    try
                    {
                        var allblobs = blobClient.ListContainers().SelectMany(c => c.ListBlobs(null, true, BlobListingDetails.Metadata).OfType<CloudBlockBlob>());

                        var bytesize = allblobs.Aggregate(0L, (value, b) => value + b.Properties.Length);
                        await ctx.Response.WriteAsync(JsonConvert.SerializeObject(new { Bytes = bytesize }));

                    }
                    catch (Exception ex)
                    {
                        Trace.TraceError(ex.ToString());
                        var a = ex;
                    }


                }

            });
        }
        public static void GetStorageAccounts(IAppBuilder map)
        {
              map.Run(async ctx =>
                {
                    if (!ctx.Authentication.User.Identity.IsAuthenticated)
                    {
                        ctx.Response.Redirect("/login");
                        return;
                    }

                    var Authorization = ctx.Request.Headers.Get("Authorization");
                    var parts = Authorization.Split(' ');
                    var token = parts.Last();
                    

                    ctx.Response.ContentType = "text/json";



                    using (var azure = new SubscriptionClient(new TokenCloudCredentials(token)))
                    {
                        var subscriptions = await azure.Subscriptions.ListAsync();

                        var tasks = await Task.WhenAll(subscriptions.Select(id => new TokenCloudCredentials(id.SubscriptionId, token))
                            .Select(async cred =>
                         {
                             using (var storage = CloudContext.Clients.CreateStorageManagementClient(cred))
                             {
                               
                                 //0.9.4-preview Returns no storage accounts for all subscriptions.
                                 //var storages = await storage.StorageAccounts.ListAsync();
                                 //storages.Count is 0
                                 //return storages.Select(s => s.Name);

                                 //0.9.3-preview  Works
                                 var storages = await storage.StorageAccounts.ListAsync(new CancellationToken());
                                 return storages.Select(s => s.ServiceName);
                             }
                         }));
                        await ctx.Response.WriteAsync(JsonConvert.SerializeObject(tasks.SelectMany(s => s).ToArray()));

                    }
                  

                });

      
        }
Ejemplo n.º 6
0
 private static async Task<ServiceInformation> GetAzureServiceInformation(string bearerToken)
 {
     string deploymentId = RoleEnvironment.DeploymentId;
     var accumlatedExceptions = new List<Exception>();
     using (var subscriptionClient = new SubscriptionClient(new TokenCloudCredentials(bearerToken)))
     {
         foreach (var subscription in (await subscriptionClient.Subscriptions.ListAsync())
                                             .Where(sub => sub.SubscriptionStatus == Microsoft.WindowsAzure.Subscriptions.Models.SubscriptionStatus.Active))
         {
             using (var cloudServiceClient = new ComputeManagementClient(new TokenCloudCredentials(subscription.SubscriptionId, bearerToken)))
             {
                 try
                 {
                     foreach (var cloudService in (await cloudServiceClient.HostedServices.ListAsync()))
                     {
                         try
                         {
                             var deployment = await cloudServiceClient.Deployments.GetBySlotAsync(cloudService.ServiceName, Microsoft.WindowsAzure.Management.Compute.Models.DeploymentSlot.Production);
                             if (deployment.PrivateId == deploymentId)
                             {
                                 return new ServiceInformation
                                 {
                                     SubscriptionId = subscription.SubscriptionId,
                                     ServiceName = cloudService.ServiceName,
                                 };
                             }
                         }
                         catch (CloudException ex)
                         {
                             // Accumulate the exception & keep enumerating
                             accumlatedExceptions.Add(ex);
                         }
                     }
                 }
                 catch (CloudException ex)
                 {
                     // Accumulate the exception & keep enumerating
                     accumlatedExceptions.Add(ex);
                 }
             }
         }
     }
     DashTrace.TraceWarning("Unable to identify running service. Possible unauthorized user.");
     if (accumlatedExceptions.Any())
     {
         DashTrace.TraceWarning("Accumulated exceptions when enumerating services: {0}", String.Join("\n", accumlatedExceptions
             .Select(ex => ex.ToString())));
     }
     return null;
 }