/// <summary> /// Finds a valid Storage Account in the subscription to create storage containers on. /// </summary> /// <param name="client">The <see cref="StorageManagementClient"/> that is performing the operation.</param> /// <param name="storageType">The type of storage that the account should have.</param> /// <returns>A suitable storage account name if one is found, null otherwise.</returns> public async Task<string> Choose(StorageManagementClient client, string storageType) { return (await client.StorageAccounts.ListAsync()) .FirstOrDefault(a => a.Name.Contains(FlexDataConfiguration.StorageAccountString) && a.Properties.AccountType == storageType)? .Name; }
public override OperationResult Deprovision(AddonDeprovisionRequest request) { var connectionData = request.ConnectionData; var deprovisionResult = new ProvisionAddOnResult(connectionData); var devOptions = DeveloperParameters.Parse(request.DeveloperParameters, request.Manifest.GetProperties()); // set up the credentials for azure SubscriptionCloudCredentials creds = CertificateAuthenticationHelper.GetCredentials(devOptions.AzureManagementSubscriptionId, devOptions.AzureAuthenticationKey); // set up the storage management client var client = new StorageManagementClient(creds); // AI-121 & AI-122 // we're going to have to implement some additional handling here, including parsing of the connection data // i strongly recommend we look at putting this in json // then if requested, delete the storage account name client.StorageAccounts.Delete(devOptions.StorageAccountName); do { var verificationResponse = client.StorageAccounts.Get(devOptions.StorageAccountName); if (verificationResponse.StorageAccount.Properties.Status.Equals(StorageAccountStatus.Deleting)) { deprovisionResult.IsSuccess = true; deprovisionResult.EndUserMessage = "Deprovision Request Complete, please allow a few minutes for resources to be fully deleted."; break; } Thread.Sleep(TimeSpan.FromSeconds(10d)); } while (true); return deprovisionResult; }
public static void DeletePackageFromBlob( StorageManagementClient storageClient, string storageName, Uri packageUri) { cloudBlobUtility.DeletePackageFromBlob(storageClient, storageName, packageUri); }
/// <summary> /// Initializes a new instance of the AzureAccount class. /// </summary> /// <param name="subscriptionId">Subscription ID of the Azure account.</param> /// <param name="cert">X509 certificate used for authentication.</param> public AzureAccount(string subscriptionId, X509Certificate2 cert) { this.credentials = new CertificateCloudCredentials(subscriptionId, cert); this.computeManager = CloudContext.Clients.CreateComputeManagementClient(this.credentials); this.storageManager = CloudContext.Clients.CreateStorageManagementClient(this.credentials); }
public static CloudStorageAccount GenerateCloudStorageAccount(StorageManagementClient storageClient, string accountName) { var storageServiceResponse = storageClient.StorageAccounts.Get(accountName); var storageKeysResponse = storageClient.StorageAccounts.GetKeys(accountName); Uri fileEndpoint = null; Uri blobEndpoint = null; Uri queueEndpoint = null; Uri tableEndpoint = null; if (storageServiceResponse.StorageAccount.Properties.Endpoints.Count >= 4) { fileEndpoint = GeneralUtilities.CreateHttpsEndpoint(storageServiceResponse.StorageAccount.Properties.Endpoints[3].ToString()); } if (storageServiceResponse.StorageAccount.Properties.Endpoints.Count >= 3) { tableEndpoint = GeneralUtilities.CreateHttpsEndpoint(storageServiceResponse.StorageAccount.Properties.Endpoints[2].ToString()); queueEndpoint = GeneralUtilities.CreateHttpsEndpoint(storageServiceResponse.StorageAccount.Properties.Endpoints[1].ToString()); } if (storageServiceResponse.StorageAccount.Properties.Endpoints.Count >= 1) { blobEndpoint = GeneralUtilities.CreateHttpsEndpoint(storageServiceResponse.StorageAccount.Properties.Endpoints[0].ToString()); } return new CloudStorageAccount( new StorageCredentials(storageServiceResponse.StorageAccount.Name, storageKeysResponse.PrimaryKey), blobEndpoint, queueEndpoint, tableEndpoint, fileEndpoint); }
public static PSStorageService Create(StorageManagementClient client, StorageServicePropertiesOperationContext account) { return new PSStorageService(account, new LazyAzureStorageContext((s) => { return StorageUtilities.GenerateCloudStorageAccount(client, account.StorageAccountName); }, account.StorageAccountName) as AzureStorageContext); }
public TestClientProvider(ManagementClient mgmtClient, ComputeManagementClient computeClient, StorageManagementClient storageClient, NetworkManagementClient networkClient) { this.managementClient = mgmtClient; this.computeManagementClient = computeClient; this.storageManagementClient = storageClient; this.networkManagementClient = networkClient; }
public async Task<HttpResponseMessage> DeleteAccount(DeleteAccountRequest request) { using (var storageClient = new StorageManagementClient(new CertificateCloudCredentials(_subscriptionId, new X509Certificate2( Convert.FromBase64String(_certificateBase64))))) { var response = await storageClient.StorageAccounts.DeleteAsync(request.AccountName); return new HttpResponseMessage(response.StatusCode); } }
public async Task<IEnumerable<StorageAccount>> ListAccounts() { using (var storageClient = new StorageManagementClient(new CertificateCloudCredentials(_subscriptionId, new X509Certificate2( Convert.FromBase64String(_certificateBase64))))) { var result = await storageClient.StorageAccounts.ListAsync(); return result.StorageAccounts; } }
public ManagementController(ManagementControllerParameters parameters) { _parameters = parameters; // To authenticate against the Microsoft Azure service management API we require management certificate // load this from a publish settings file and later use it with the Service Management Libraries var credential = GetSubscriptionCloudCredentials(parameters.PublishSettingsFilePath); _storageManagementClient = CloudContext.Clients.CreateStorageManagementClient(credential); }
internal ManagementController(ManagementControllerParamters parameters) { _parameters = parameters; var credential = CertificateAuthenticationHelper.GetCredentials( parameters.SubscriptionId, parameters.Base64EncodedCertificate); _storageManagementClient = CloudContext.Clients.CreateStorageManagementClient(credential); _computeManagementClient = CloudContext.Clients.CreateComputeManagementClient(credential); }
protected override void Clone(ServiceClient <StorageManagementClient> client) { base.Clone(client); StorageManagementClient management = client as StorageManagementClient; if (management != null) { management._credentials = Credentials; management._baseUri = BaseUri; management.Credentials.InitializeServiceClient(management); } }
public EarzyProvisioningCore() { //Get credentials from subscription ID & Certificate var credential = CertificateAuthenticationHelper.GetCredentials( ConfigurationManager.AppSettings["SubscriptionId"], ConfigurationManager.AppSettings["Certificate"]); //Initialize clients _storageManagementClient = CloudContext.Clients.CreateStorageManagementClient(credential); _computeManagementClient = CloudContext.Clients.CreateComputeManagementClient(credential); _webSitesManagementClient = CloudContext.Clients.CreateWebSiteManagementClient(credential); }
public static Uri UploadPackageToBlob( StorageManagementClient storageClient, string storageName, string packagePath, BlobRequestOptions blobRequestOptions) { return cloudBlobUtility.UploadPackageToBlob( storageClient, storageName, packagePath, blobRequestOptions); }
public async Task<HttpResponseMessage> GenerateNewKey(GenerateNewKeyRequest request) { using (var storageClient = new StorageManagementClient(new CertificateCloudCredentials(_subscriptionId, new X509Certificate2( Convert.FromBase64String(_certificateBase64))))) { var response = await storageClient.StorageAccounts.RegenerateKeysAsync( new StorageAccountRegenerateKeysParameters() { KeyType = request.AccountKeyType.Equals("Primary", StringComparison.OrdinalIgnoreCase) ? StorageKeyType.Primary : StorageKeyType.Secondary, Name = request.AccountName }); return new HttpResponseMessage(response.StatusCode); } }
public static void CreateStorageAccount(string location, StorageManagementClient storageClient, string storageAccountName, out bool storageAccountCreated) { AzureOperationResponse storageCreate = storageClient.StorageAccounts.Create( new StorageAccountCreateParameters { Location = location, Label = storageAccountName, Name = storageAccountName, AccountType = "Standard_LRS" }); Assert.True(storageCreate.StatusCode == HttpStatusCode.OK); storageAccountCreated = true; }
public virtual Uri UploadPackageToBlob( StorageManagementClient storageClient, string storageName, string packagePath, BlobRequestOptions blobRequestOptions) { StorageAccountGetKeysResponse keys = storageClient.StorageAccounts.GetKeys(storageName); string storageKey = keys.PrimaryKey; var storageService = storageClient.StorageAccounts.Get(storageName); Uri blobEndpointUri = storageService.StorageAccount.Properties.Endpoints[0]; return UploadFile(storageName, GeneralUtilities.CreateHttpsEndpoint(blobEndpointUri.ToString()), storageKey, packagePath, blobRequestOptions); }
public virtual void DeletePackageFromBlob( StorageManagementClient storageClient, string storageName, Uri packageUri) { StorageAccountGetKeysResponse keys = storageClient.StorageAccounts.GetKeys(storageName); string storageKey = keys.PrimaryKey; var storageService = storageClient.StorageAccounts.Get(storageName); var blobStorageEndpoint = storageService.StorageAccount.Properties.Endpoints[0]; var credentials = new StorageCredentials(storageName, storageKey); var client = new CloudBlobClient(blobStorageEndpoint, credentials); ICloudBlob blob = client.GetBlobReferenceFromServer(packageUri); blob.DeleteIfExists(); }
public VMManagement(string subscriptionId, byte[] managementCertificate, string certificateKey, string blobStorageName, string blobStorageKey, ILogger logger) { _managementCertificate = new X509Certificate2(managementCertificate, certificateKey, X509KeyStorageFlags.MachineKeySet); _subscriptionId = subscriptionId; CertificateCloudCredentials cloudCredentials = new CertificateCloudCredentials(_subscriptionId, _managementCertificate); _datacenter = new ManagementClient(cloudCredentials);// CloudContext.Clients.CreateManagementClient(cloudCredentials); //var roleSizes = _datacenter.RoleSizes.List(); _compute = new ComputeManagementClient(cloudCredentials);// CloudContext.Clients.CreateComputeManagementClient(cloudCredentials); _storage = new StorageManagementClient(cloudCredentials);// CloudContext.Clients.CreateStorageManagementClient(cloudCredentials); }
public async Task<HttpResponseMessage> CreateAccount(CreateAccountRequest request) { // Create storage client using (var storageClient = new StorageManagementClient(new CertificateCloudCredentials(_subscriptionId, new X509Certificate2( Convert.FromBase64String(_certificateBase64))))) { var response = await storageClient.StorageAccounts.CreateAsync( new StorageAccountCreateParameters { AccountType = request.AccountType, Location = request.Location, Name = request.AccountName }); return new HttpResponseMessage(response.HttpStatusCode); } }
public override ProvisionAddOnResult Provision(AddonProvisionRequest request) { var provisionResult = new ProvisionAddOnResult(""); try { var devParameters = DeveloperParameters.Parse(request.DeveloperParameters, request.Manifest.GetProperties()); SubscriptionCloudCredentials creds = CertificateAuthenticationHelper.GetCredentials(devParameters.AzureManagementSubscriptionId, devParameters.AzureAuthenticationKey); // ok so if we need a storage account, we need to use the storage management client. if (devParameters.NewStorageAccountFlag) { var client = new StorageManagementClient(creds); var parameters = CreateStorageAccountParameters(devParameters); client.StorageAccounts.Create(parameters); do { var verificationResponse = client.StorageAccounts.Get(parameters.Name); if (verificationResponse.StorageAccount.Properties.Status.Equals(StorageAccountStatus.Created)) { var azureconnectioninfo = client.StorageAccounts.Get(devParameters.StorageAccountName); var keysForStorageUnit = client.StorageAccounts.GetKeys(devParameters.StorageAccountName); var connectionInfo = new ConnectionInfo { PrimaryKey = keysForStorageUnit.PrimaryKey, SecondaryKey = keysForStorageUnit.SecondaryKey, StorageAccountName = azureconnectioninfo.StorageAccount.Name, Uri = keysForStorageUnit.Uri.ToString() }; provisionResult.ConnectionData = connectionInfo.ToString(); provisionResult.IsSuccess = true; break; } Thread.Sleep(TimeSpan.FromSeconds(10d)); } while (true); } } catch (Exception e) { provisionResult.IsSuccess = false; provisionResult.EndUserMessage = e.Message + "\n We're in an error\n"; } return provisionResult; }
public override OperationResult Deprovision(AddonDeprovisionRequest request) { var connectionData = request.ConnectionData; // changing to overloaded constructor - 5/22/14 var deprovisionResult = new ProvisionAddOnResult(connectionData); var manifest = request.Manifest; var inputDevParameters = request.DeveloperParameters; // parse required options here, use developer options class to do so. var manifestProperties = manifest.GetProperties(); // Developer Options will be instantiated first time here. var devParams = DeveloperParameters.Parse(inputDevParameters, manifestProperties); // set up the credentials for azure var creds = CertificateAuthenticationHelper.GetCredentials(devParams.AzureManagementSubscriptionId, devParams.AzureAuthenticationKey); // set up the storage management client var client = new StorageManagementClient(); var mResponse = client.StorageAccounts.Delete(devParams.StorageAccountName); if (mResponse.StatusCode.Equals(HttpStatusCode.OK)) { do { var verificationResponse = client.StorageAccounts.Get(devParams.StorageAccountName); if (verificationResponse.StorageAccount.Properties.Status.Equals(StorageAccountStatus.Deleting)) { deprovisionResult.IsSuccess = true; deprovisionResult.EndUserMessage = "Deprovision Request Complete, please allow a few minutes for resources to be fully deleted."; break; } Thread.Sleep(TimeSpan.FromSeconds(10d)); } while (true); return deprovisionResult; } else { return new OperationResult() { EndUserMessage = "Azure Query failed. Please check your syntax and credetials.", IsSuccess = false }; } }
private async static Task<OperationResponse> DeployCloudService(SubscriptionCloudCredentials credentials, string storageAccountName, string serviceName) { Console.WriteLine("Deploying to Cloud Service {0}", serviceName); OperationResponse response = null; string storageAccountKey = null; using (var client = new StorageManagementClient(credentials)) { var keys = await client.StorageAccounts.GetKeysAsync(storageAccountName); storageAccountKey = keys.PrimaryKey; } string storageConnectionString = string.Format("DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1}", storageAccountName, storageAccountKey); CloudStorageAccount account = CloudStorageAccount.Parse(storageConnectionString); CloudBlobClient blobClient = account.CreateCloudBlobClient(); CloudBlobContainer deploymentContainer = blobClient.GetContainerReference("maml-deployment"); await deploymentContainer.CreateIfNotExistsAsync(); CloudBlockBlob deploymentBlob = deploymentContainer.GetBlockBlobReference("AzureCloudService1.cspkg"); await deploymentBlob.UploadFromFileAsync(@"C:\Projects\Demos\AzureAutomationDemos\AzureAutomation\AzureCloudService1\bin\Release\app.publish\AzureCloudService1.cspkg", FileMode.Open); using (var client = new ComputeManagementClient(credentials)) { response = await client.Deployments.CreateAsync(serviceName, DeploymentSlot.Production, new DeploymentCreateParameters { Label = serviceName, Name = serviceName + "Prod", PackageUri = deploymentBlob.Uri, Configuration = File.ReadAllText(@"C:\Projects\Demos\AzureAutomationDemos\AzureAutomation\AzureCloudService1\bin\Release\app.publish\ServiceConfiguration.Cloud.cscfg"), StartDeployment = true }); } return response; }
private async static Task<string> CreateStorageAccount(SubscriptionCloudCredentials credentials) { var storageAccountName = string.Format("{0}{1}", ResourceName.ToLower(), new Random().Next(1, 200)); Console.WriteLine("Creating new storage account '{0}' . . .", storageAccountName); using (var client = new StorageManagementClient(credentials)) { var result = await client.StorageAccounts.CreateAsync(new StorageAccountCreateParameters { Location = LocationNames.EastUS, Label = string.Format("{0} Demo Account", ResourceName), Name = storageAccountName, AccountType = StorageAccountTypes.StandardGRS }); } Console.WriteLine("Storage account '{0}' created.", storageAccountName); return storageAccountName; }
private async static Task<OperationResponse> RemoveStorageAccount(SubscriptionCloudCredentials credentials, string storageAccountName) { OperationResponse response = null; using (var client = new StorageManagementClient(credentials)) { response = await client.StorageAccounts.DeleteAsync(storageAccountName); } Console.WriteLine("Deleted storage account {0}", storageAccountName); return response; }
private bool CreateWeb( CloudWebDeployInputs inputs ) { Console.WriteLine( "Retrieving namespace metadata..." ); // Create Namespace ServiceBusManagementClient sbMgmt = new ServiceBusManagementClient( inputs.Credentials ); var nsDescription = sbMgmt.Namespaces.GetNamespaceDescription( inputs.SBNamespace ); string nsConnectionString = nsDescription.NamespaceDescriptions.First( ( d ) => String.Equals( d.AuthorizationType, "SharedAccessAuthorization" ) ).ConnectionString; NamespaceManager nsManager = NamespaceManager.CreateFromConnectionString( nsConnectionString ); StorageManagementClient stgMgmt = new StorageManagementClient( inputs.Credentials ); var keyResponse = stgMgmt.StorageAccounts.GetKeys( inputs.StorageAccountName.ToLowerInvariant( ) ); if( keyResponse.StatusCode != System.Net.HttpStatusCode.OK ) { Console.WriteLine( "Error retrieving access keys for storage account {0} in Location {1}: {2}", inputs.StorageAccountName, inputs.Location, keyResponse.StatusCode ); return false; } var storageKey = keyResponse.PrimaryKey; EventHubDescription ehDevices = nsManager.GetEventHub( inputs.EventHubNameDevices ); string ehDevicesWebSiteConnectionString = new ServiceBusConnectionStringBuilder( nsConnectionString ) { SharedAccessKeyName = "WebSite", SharedAccessKey = ( ehDevices.Authorization.First( ( d ) => String.Equals( d.KeyName, "WebSite", StringComparison.InvariantCultureIgnoreCase) ) as SharedAccessAuthorizationRule ).PrimaryKey, }.ToString( ); string ehAlertsWebSiteConnectionString = string.Empty; try { EventHubDescription ehAlerts = nsManager.GetEventHub( inputs.EventHubNameAlerts ); ehAlertsWebSiteConnectionString = new ServiceBusConnectionStringBuilder( nsConnectionString ) { SharedAccessKeyName = "WebSite", SharedAccessKey = ( ehAlerts.Authorization.First( ( d ) => String.Equals( d.KeyName, "WebSite", StringComparison.InvariantCultureIgnoreCase ) ) as SharedAccessAuthorizationRule ).PrimaryKey, }.ToString( ); } catch { } Console.WriteLine( "Started processing..." ); // Write a new web.config template file var doc = new XmlDocument { PreserveWhitespace = true }; //var inputFileName = ( inputs.Transform ? "\\web.PublishTemplate.config" : "\\web.config" ); string inputFileName = "web.PublishTemplate.config"; //var outputFileName = ( inputs.Transform ? String.Format("\\web.{0}.config", inputs.NamePrefix) : "\\web.config" ); string outputFileName = "web.config"; //doc.Load( inputs.WebSiteDirectory + inputFileName ); string inputFilePath = Environment.CurrentDirectory +@"\"; Console.WriteLine("Opening and updating " + inputFilePath + inputFileName); doc.Load( inputFilePath + inputFileName ); doc.SelectSingleNode( "/configuration/appSettings/add[@key='Microsoft.ServiceBus.EventHubDevices']/@value" ).Value = inputs.EventHubNameDevices; doc.SelectSingleNode( "/configuration/appSettings/add[@key='Microsoft.ServiceBus.EventHubAlerts']/@value" ) .Value = inputs.EventHubNameAlerts; doc.SelectSingleNode( "/configuration/appSettings/add[@key='Microsoft.ServiceBus.ConnectionString']/@value" ).Value = nsConnectionString; doc.SelectSingleNode( "/configuration/appSettings/add[@key='Microsoft.ServiceBus.ConnectionStringDevices']/@value" ).Value = ehDevicesWebSiteConnectionString; doc.SelectSingleNode( "/configuration/appSettings/add[@key='Microsoft.ServiceBus.ConnectionStringAlerts']/@value" ).Value = ehAlertsWebSiteConnectionString; doc.SelectSingleNode( "/configuration/appSettings/add[@key='Microsoft.Storage.ConnectionString']/@value" ) .Value = String.Format( "DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1}", inputs.StorageAccountName, storageKey ); //var outputFile = System.IO.Path.GetFullPath( inputs.WebSiteDirectory + outputFileName ); string outputFilePath = Environment.GetFolderPath( Environment.SpecialFolder.Desktop ); //Console.WriteLine(outputFilePath); var outputFile = outputFilePath + @"\" + outputFileName; Console.WriteLine( "Writing updates to " + outputFile ); doc.Save( outputFile ); Console.WriteLine( " " ); Console.WriteLine( "Web config saved to {0}", outputFile ); Console.WriteLine( " " ); return true; }
private string GetAzureVmSasUri(string vmImageName) { string mediaLinkUri = null; Uri uri = null; StorageManagementClient storageClient = null; string storageAccountName = null; StorageAccountGetKeysResponse getKeysResponse = null; ErrorRecord er = null; StorageCredentials credentials = null; SharedAccessBlobPolicy accessPolicy = null; CloudPageBlob pageBlob = null; string sas = null; mediaLinkUri = GetImageUri(vmImageName); uri = new Uri(mediaLinkUri); storageClient = new StorageManagementClient(this.Client.Credentials, this.Client.BaseUri); storageAccountName = uri.Authority.Split('.')[0]; getKeysResponse = storageClient.StorageAccounts.GetKeys(storageAccountName); if (getKeysResponse.StatusCode != System.Net.HttpStatusCode.OK) { er = RemoteAppCollectionErrorState.CreateErrorRecordFromString( String.Format(Commands_RemoteApp.GettingStorageAccountKeyErrorFormat, getKeysResponse.StatusCode.ToString()), String.Empty, Client.TemplateImages, ErrorCategory.ConnectionError ); ThrowTerminatingError(er); } credentials = new StorageCredentials(storageAccountName, getKeysResponse.SecondaryKey); accessPolicy = new SharedAccessBlobPolicy(); pageBlob = new CloudPageBlob(uri, credentials); accessPolicy.Permissions = SharedAccessBlobPermissions.Read; accessPolicy.SharedAccessStartTime = DateTime.Now; accessPolicy.SharedAccessExpiryTime = DateTime.Now.AddHours(12); sas = pageBlob.GetSharedAccessSignature(accessPolicy); if (sas == null) { er = RemoteAppCollectionErrorState.CreateErrorRecordFromString( Commands_RemoteApp.FailedToGetSasUriError, String.Empty, Client.TemplateImages, ErrorCategory.ConnectionError ); ThrowTerminatingError(er); } return mediaLinkUri + sas; }
/// <summary> /// Rollback all imported storage accounts. /// </summary> /// <param name="storageAccounts">Storage accounts to be deleted.</param> private void RollBackStorageAccounts(List<string> storageAccounts) { string methodName = System.Reflection.MethodBase.GetCurrentMethod().Name; Logger.Info(methodName, ProgressResources.ExecutionStarted, ResourceType.StorageAccount.ToString()); dcMigrationManager.ReportProgress(ProgressResources.RollbackStorageAccounts); Logger.Info(methodName, ProgressResources.RollbackStorageAccounts, ResourceType.StorageAccount.ToString()); Stopwatch swTotalstorages = new Stopwatch(); swTotalstorages.Start(); if (storageAccounts.Count > 0) { using (var client = new StorageManagementClient(importParameters.DestinationSubscriptionSettings.Credentials)) { Parallel.ForEach(storageAccounts, storageAccount => { Stopwatch swStorage = new Stopwatch(); swStorage.Start(); try { Retry.RetryOperation(() => client.StorageAccounts.Delete(storageAccount), (BaseParameters)importParameters, ResourceType.StorageAccount, storageAccount, ignoreResourceNotFoundEx: true); resourceImporter.UpdateMedatadaFile(ResourceType.StorageAccount, storageAccount, false); swStorage.Stop(); Logger.Info(methodName, string.Format(ProgressResources.RollbackStorageAccount, storageAccount,swStorage.Elapsed.Days, swStorage.Elapsed.Hours, swStorage.Elapsed.Minutes, swStorage.Elapsed.Seconds), ResourceType.StorageAccount.ToString(), storageAccount); } catch (AggregateException exAgg) { foreach (var ex in exAgg.InnerExceptions) { Logger.Error(methodName, exAgg, ResourceType.StorageAccount.ToString(), storageAccount); } throw; } }); Logger.Info(methodName, ProgressResources.RollbackStorageAccountsWaiting, ResourceType.StorageAccount.ToString()); dcMigrationManager.ReportProgress(ProgressResources.RollbackStorageAccountsWaiting); Task.Delay(Constants.DelayTimeInMilliseconds_Rollback).Wait(); } } swTotalstorages.Stop(); Logger.Info(methodName, string.Format(ProgressResources.ExecutionCompletedWithTime,swTotalstorages.Elapsed.Days, swTotalstorages.Elapsed.Hours, swTotalstorages.Elapsed.Minutes, swTotalstorages.Elapsed.Seconds), ResourceType.StorageAccount.ToString()); }
public int Run() { // Obtain management via .publishsettings file from https://manage.windowsazure.com/publishsettings/index?schemaversion=2.0 var creds = new CertificateCloudCredentials(SubscriptionId, ManagementCertificate); // Create Namespace var sbMgmt = new ServiceBusManagementClient(creds); ServiceBusNamespaceResponse nsResponse = null; Console.WriteLine("Creating Service Bus namespace {0} in location {1}", SBNamespace, Location); try { var resultSb = sbMgmt.Namespaces.Create(SBNamespace, Location); if (resultSb.StatusCode != System.Net.HttpStatusCode.OK) { Console.WriteLine("Error creating Service Bus namespace {0} in Location {1}: {2}", SBNamespace, Location, resultSb.StatusCode); return 1; } } catch (CloudException) { try { // There is (currently) no clean error code returned when the namespace already exists // Check if it does nsResponse = sbMgmt.Namespaces.Get(SBNamespace); Console.WriteLine("Service Bus namespace {0} already existed.", SBNamespace); } catch (Exception) { nsResponse = null; } if (nsResponse == null) { throw; } } // Wait until the namespace is active while (nsResponse == null || nsResponse.Namespace.Status != "Active") { nsResponse = sbMgmt.Namespaces.Get(SBNamespace); if (nsResponse.Namespace.Status == "Active") { break; } Console.WriteLine("Namespace {0} in state {1}. Waiting...", SBNamespace, nsResponse.Namespace.Status); System.Threading.Thread.Sleep(5000); } // Get the namespace connection string var nsDescription = sbMgmt.Namespaces.GetNamespaceDescription(SBNamespace); var nsConnectionString = nsDescription.NamespaceDescriptions.First( (d) => String.Equals(d.AuthorizationType, "SharedAccessAuthorization") ).ConnectionString; // Create EHs + device keys + consumer keys (WebSite*) var nsManager = NamespaceManager.CreateFromConnectionString(nsConnectionString); var ehDescriptionDevices = new EventHubDescription(EventHubNameDevices) { PartitionCount = 8, }; ehDescriptionDevices.Authorization.Add(new SharedAccessAuthorizationRule("D1", new List<AccessRights> { AccessRights.Send })); ehDescriptionDevices.Authorization.Add(new SharedAccessAuthorizationRule("D2", new List<AccessRights> { AccessRights.Send })); ehDescriptionDevices.Authorization.Add(new SharedAccessAuthorizationRule("D3", new List<AccessRights> { AccessRights.Send })); ehDescriptionDevices.Authorization.Add(new SharedAccessAuthorizationRule("D4", new List<AccessRights> { AccessRights.Send })); ehDescriptionDevices.Authorization.Add(new SharedAccessAuthorizationRule("WebSite", new List<AccessRights> { AccessRights.Manage, AccessRights.Listen, AccessRights.Send })); ehDescriptionDevices.Authorization.Add(new SharedAccessAuthorizationRule("StreamingAnalytics", new List<AccessRights> { AccessRights.Manage, AccessRights.Listen, AccessRights.Send })); Console.WriteLine("Creating Event Hub {0}", EventHubNameDevices); EventHubDescription ehDevices = null; do { try { ehDevices = nsManager.CreateEventHubIfNotExists(ehDescriptionDevices); } catch (System.UnauthorizedAccessException) { Console.WriteLine("Service Bus connection string not valid yet. Waiting..."); System.Threading.Thread.Sleep(5000); } } while (ehDevices == null); var ehDescriptionAlerts = new EventHubDescription(EventHubNameAlerts) { PartitionCount = 8, }; ehDescriptionAlerts.Authorization.Add(new SharedAccessAuthorizationRule("WebSite", new List<AccessRights> { AccessRights.Manage, AccessRights.Listen, AccessRights.Send })); ehDescriptionAlerts.Authorization.Add(new SharedAccessAuthorizationRule("StreamingAnalytics", new List<AccessRights> { AccessRights.Manage, AccessRights.Listen, AccessRights.Send })); Console.WriteLine("Creating Event Hub {0}", EventHubNameAlerts); var ehAlerts = nsManager.CreateEventHubIfNotExists(ehDescriptionAlerts); // Create Storage Account for Event Hub Processor var stgMgmt = new StorageManagementClient(creds); try { Console.WriteLine("Creating Storage Account {0} in location {1}", StorageAccountName, Location); var resultStg = stgMgmt.StorageAccounts.Create( new StorageAccountCreateParameters { Name = StorageAccountName.ToLowerInvariant(), Location = Location, AccountType = "Standard_LRS" }); if (resultStg.StatusCode != System.Net.HttpStatusCode.OK) { Console.WriteLine("Error creating storage account {0} in Location {1}: {2}", StorageAccountName, Location, resultStg.StatusCode); return 1; } } catch (CloudException ce) { if (String.Equals(ce.ErrorCode, "ConflictError", StringComparison.InvariantCultureIgnoreCase)) { Console.WriteLine("Storage account {0} already existed.", StorageAccountName); } else { throw; } } var keyResponse = stgMgmt.StorageAccounts.GetKeys(StorageAccountName.ToLowerInvariant()); if (keyResponse.StatusCode != System.Net.HttpStatusCode.OK) { Console.WriteLine("Error retrieving access keys for storage account {0} in Location {1}: {2}", StorageAccountName, Location, keyResponse.StatusCode); return 1; } var storageKey = keyResponse.PrimaryKey; string ehDevicesWebSiteConnectionString = new ServiceBusConnectionStringBuilder(nsConnectionString) { SharedAccessKeyName = "WebSite", SharedAccessKey = (ehDevices.Authorization.First((d) => String.Equals(d.KeyName, "WebSite", StringComparison.InvariantCultureIgnoreCase)) as SharedAccessAuthorizationRule).PrimaryKey, }.ToString(); string ehAlertsWebSiteConnectionString = new ServiceBusConnectionStringBuilder(nsConnectionString) { SharedAccessKeyName = "WebSite", SharedAccessKey = (ehAlerts.Authorization.First((d) => String.Equals(d.KeyName, "WebSite", StringComparison.InvariantCultureIgnoreCase)) as SharedAccessAuthorizationRule).PrimaryKey, }.ToString(); // Write a new web.config template file var doc = new XmlDocument(); doc.PreserveWhitespace = true; var inputFileName = (this.Transform ? "\\web.PublishTemplate.config" : "\\web.config"); var outputFileName = (this.Transform ? String.Format("\\web.{0}.config", NamePrefix) : "\\web.config"); doc.Load(WebSiteDirectory + inputFileName); doc.SelectSingleNode("/configuration/appSettings/add[@key='Microsoft.ServiceBus.EventHubDevices']/@value").Value = EventHubNameDevices; doc.SelectSingleNode("/configuration/appSettings/add[@key='Microsoft.ServiceBus.EventHubAlerts']/@value").Value = EventHubNameAlerts; doc.SelectSingleNode("/configuration/appSettings/add[@key='Microsoft.ServiceBus.ConnectionString']/@value").Value = nsConnectionString; doc.SelectSingleNode("/configuration/appSettings/add[@key='Microsoft.ServiceBus.ConnectionStringDevices']/@value").Value = ehDevicesWebSiteConnectionString; doc.SelectSingleNode("/configuration/appSettings/add[@key='Microsoft.ServiceBus.ConnectionStringAlerts']/@value").Value = ehAlertsWebSiteConnectionString; doc.SelectSingleNode("/configuration/appSettings/add[@key='Microsoft.Storage.ConnectionString']/@value").Value = String.Format("DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1}", StorageAccountName, storageKey); var outputFile = System.IO.Path.GetFullPath(WebSiteDirectory + outputFileName); doc.Save(outputFile); Console.WriteLine(); Console.WriteLine("Service Bus management connection string (i.e. for use in Service Bus Explorer):"); Console.WriteLine(nsConnectionString); Console.WriteLine(); Console.WriteLine("Device AMQP address strings (for Raspberry PI/devices):"); for (int i = 1; i <= 4; i++) { var deviceKeyName = String.Format("D{0}", i); var deviceKey = (ehDevices.Authorization.First((d) => String.Equals(d.KeyName, deviceKeyName, StringComparison.InvariantCultureIgnoreCase)) as SharedAccessAuthorizationRule).PrimaryKey; Console.WriteLine("amqps://{0}:{1}@{2}.servicebus.windows.net", deviceKeyName, Uri.EscapeDataString(deviceKey), SBNamespace); //Console.WriteLine(new ServiceBusConnectionStringBuilder(nsConnectionString) //{ // SharedAccessKeyName = deviceKeyName, // SharedAccessKey = deviceKey, //}.ToString()); } Console.WriteLine(); Console.WriteLine("Web.Config saved to {0}", outputFile); #if AZURESTREAMANALYTICS // Create StreamAnalyticsJobs + inputs + outputs + enter keys // Untested code. May require AAD authentication, no support for management cert? // Create Resource Group for the Stream Analytics jobs var groupCreateRequest = WebRequest.Create(String.Format("https://management.azure.com/subscriptions/{0}/resourcegroups/{1}?api-version=2014-04-01-preview", SubscriptionId, StreamAnalyticsGroup)) as HttpWebRequest; groupCreateRequest.ClientCertificates.Add(creds.ManagementCertificate); groupCreateRequest.ContentType = "application/json"; groupCreateRequest.Method = "PUT"; groupCreateRequest.KeepAlive = true; var bytesGroup = Encoding.UTF8.GetBytes("{\"location\":\"Central US\"}"); groupCreateRequest.ContentLength = bytesGroup.Length; groupCreateRequest.GetRequestStream().Write(bytesGroup, 0, bytesGroup.Length); var groupCreateResponse = groupCreateRequest.GetResponse(); //var streamMgmt = new ManagementClient(creds); //, new Uri("https://management.azure.com")); //HttpClient client = streamMgmt.HttpClient; var createJob = new StreamAnalyticsJob() { location = Location, inputs = new List<StreamAnalyticsEntity> { new StreamAnalyticsEntity { name = "devicesInput", properties = new Dictionary<string,object> { { "type" , "stream" }, { "serialization" , new Dictionary<string,object> { { "type", "JSON"}, { "properties", new Dictionary<string, object> { { "encoding", "UTF8"}, } } } }, { "datasource", new Dictionary<string,object> { { "type", "Microsoft.ServiceBus/EventHub" }, { "properties", new Dictionary<string,object> { { "eventHubNamespace", Namespace }, { "eventHubName", EventHubDevices }, { "sharedAccessPolicyName", "StreamingAnalytics" }, { "sharedAccessPolicyKey", (ehDevices.Authorization.First( (d) => String.Equals(d.KeyName, "StreamingAnalytics", StringComparison.InvariantCultureIgnoreCase)) as SharedAccessAuthorizationRule).PrimaryKey }, } } } } }, }, }, transformation = new StreamAnalyticsEntity() { name = "Aggregates", properties = new Dictionary<string,object> { { "streamingUnits", 1 }, { "query" , "select * from devicesInput" }, } }, outputs = new List<StreamAnalyticsEntity> { new StreamAnalyticsEntity { name = "output", properties = new Dictionary<string,object> { { "datasource", new Dictionary<string,object> { { "type", "Microsoft.ServiceBus/EventHub" }, { "properties", new Dictionary<string,object> { { "eventHubNamespace", Namespace }, { "eventHubName", EventHubAlerts }, { "sharedAccessPolicyName", "StreamingAnalytics" }, { "sharedAccessPolicyKey", (ehAlerts.Authorization.First( (d) => String.Equals(d.KeyName, "StreamingAnalytics", StringComparison.InvariantCultureIgnoreCase)) as SharedAccessAuthorizationRule).PrimaryKey }, } } } }, { "serialization" , new Dictionary<string,object> { { "type", "JSON"}, { "properties", new Dictionary<string, object> { { "encoding", "UTF8"}, } } } }, }, }, } }; var jobCreateRequest = WebRequest.Create(String.Format("https://management.azure.com/subscriptions/{0}/resourcegroups/{1}/Microsoft.StreamAnalytics/streamingjobs/{2}?api-version=2014-10-01", SubscriptionId, StreamAnalyticsGroup, JobAggregates)) as HttpWebRequest; jobCreateRequest.ClientCertificates.Add(creds.ManagementCertificate); jobCreateRequest.ContentType = "application/json"; jobCreateRequest.Method = "PUT"; jobCreateRequest.KeepAlive = true; var bytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(createJob)); jobCreateRequest.ContentLength = bytes.Length; jobCreateRequest.GetRequestStream().Write(bytes, 0, bytes.Length); var jobCreateResponse = jobCreateRequest.GetResponse(); //var jobCreateTask = streamMgmt.HttpClient.PutAsync( // String.Format("https://management.azure.com/subscriptions/{0}/resourcegroups/{1}/Microsoft.StreamAnalytics/streamingjobs/{2}?api-version=2014-10-01", // SubscriptionId, StreamAnalyticsGroup, JobAggregates), // new StringContent(JsonConvert.SerializeObject(createJob))); //jobCreateTask.Wait(); //var jobCreateResponse = jobCreateTask.Result; #endif return 0; }
public static PSStorageService Create(StorageManagementClient client, StorageServicePropertiesOperationContext account) { var cloudStorageAccount = StorageUtilities.GenerateCloudStorageAccount(client, account.StorageAccountName); return new PSStorageService(account, new AzureStorageContext(cloudStorageAccount)); }
internal CloudServiceClient( AzureSubscription subscription, ManagementClient managementClient, StorageManagementClient storageManagementClient, ComputeManagementClient computeManagementClient) : this((string)null, null, null, null) { Subscription = subscription; CurrentDirectory = null; DebugStream = null; VerboseStream = null; WarningStream = null; CloudBlobUtility = new CloudBlobUtility(); ManagementClient = managementClient; StorageClient = storageManagementClient; ComputeClient = computeManagementClient; }