|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Security.Cryptography.X509Certificates; |
| 5 | +using System.Text; |
| 6 | +using System.Threading.Tasks; |
| 7 | +using Elastacloud.AzureManagement.Fluent.Clients; |
| 8 | +using Elastacloud.AzureManagement.Fluent.Helpers; |
| 9 | +using Elastacloud.AzureManagement.Fluent.Linq; |
| 10 | +using Elastacloud.AzureManagement.Fluent.Types; |
| 11 | + |
| 12 | +namespace Elastacloud.FluentExamples |
| 13 | +{ |
| 14 | + public class LinqToStorage : IWorkflow |
| 15 | + { |
| 16 | + private readonly X509Certificate2 _managementCertificate; |
| 17 | + private readonly string _subscriptionId; |
| 18 | + private string _accountName; |
| 19 | + |
| 20 | + |
| 21 | + public LinqToStorage(string subscriptionId, X509Certificate2 managementCertificate) |
| 22 | + { |
| 23 | + _subscriptionId = subscriptionId; |
| 24 | + _managementCertificate = managementCertificate; |
| 25 | + } |
| 26 | + |
| 27 | + #region Implementation of IWorkflow |
| 28 | + |
| 29 | + public void PreActionSteps() |
| 30 | + { |
| 31 | + // get a random account name |
| 32 | + var namer = new RandomAccountName(); |
| 33 | + _accountName = namer.GetPureRandomValue(); |
| 34 | + |
| 35 | + var client = new StorageClient(_subscriptionId, _managementCertificate); |
| 36 | + client.CreateNewStorageAccount(_accountName); |
| 37 | + Console.WriteLine("Created new storage account with name {0}", _accountName); |
| 38 | + } |
| 39 | + |
| 40 | + public void DoWork() |
| 41 | + { |
| 42 | + var inputs = new LinqToAzureInputs() |
| 43 | + { |
| 44 | + ManagementCertificateThumbprint = _managementCertificate.Thumbprint, |
| 45 | + SubscriptionId = _subscriptionId |
| 46 | + }; |
| 47 | + var queryableStorage = new LinqToAzureOrderedQueryable<StorageAccount>(inputs); |
| 48 | + // build up a filtered query to check the new account |
| 49 | + var query = from account in queryableStorage |
| 50 | + where account.Name == _accountName |
| 51 | + select account; |
| 52 | + var myAccount = query.First(); |
| 53 | + |
| 54 | + // check that the account count is 1 |
| 55 | + Console.WriteLine("{0} record returned", query.Count()); |
| 56 | + Console.WriteLine("Name: {0}", myAccount.Name); |
| 57 | + Console.WriteLine("Url: {0}", myAccount.Url); |
| 58 | + Console.WriteLine("Primary Key: {0}", myAccount.PrimaryAccessKey); |
| 59 | + Console.WriteLine("Secondary Key: {0}", myAccount.SecondaryAccessKey); |
| 60 | + |
| 61 | + // do an unfiltered query and get the names of all of the storage accounts |
| 62 | + var unfilteredQuery = from account in queryableStorage |
| 63 | + select account; |
| 64 | + int storageCount = unfilteredQuery.Count(); |
| 65 | + var take = unfilteredQuery.Take(storageCount / 2); |
| 66 | + // bring back some info we can use |
| 67 | + Console.WriteLine("You have {0} storage accounts", storageCount); |
| 68 | + var storageAccounts = take.ToList(); |
| 69 | + foreach (var storageAccount in storageAccounts) |
| 70 | + Console.WriteLine("Account name: {0}", storageAccount.Name); |
| 71 | + } |
| 72 | + |
| 73 | + public void PostActionSteps() |
| 74 | + { |
| 75 | + var client = new StorageClient(_subscriptionId, _managementCertificate); |
| 76 | + client.DeleteStorageAccount(_accountName); |
| 77 | + |
| 78 | + Console.WriteLine("Deleted storage account with name {0}", _accountName); |
| 79 | + } |
| 80 | + |
| 81 | + |
| 82 | + |
| 83 | + #endregion |
| 84 | + } |
| 85 | +} |
0 commit comments