protected override void Clone(ServiceClient <ServiceBusManagementClient> client) { base.Clone(client); ServiceBusManagementClient management = client as ServiceBusManagementClient; if (management != null) { management._credentials = Credentials; management._baseUri = BaseUri; management.Credentials.InitializeServiceClient <ServiceBusManagementClient>(management); } }
/// <summary> /// Provisions all the namespaces in the <see cref="IEnumerable{T}"/> of <see cref="AzureServiceBusNamespace"/>. /// </summary> /// <param name="namespaces">The list of <see cref="AzureServiceBusNamespace"/> to provision.</param> /// <param name="client">The <see cref="ServiceBusManagementClient"/> that is performing the operation.</param> /// <returns>The async <see cref="Task"/> wrapper.</returns> public static async Task ProvisionAllAsync(this IEnumerable<AzureServiceBusNamespace> namespaces, ServiceBusManagementClient client) { Contract.Requires(namespaces != null); Contract.Requires(client != null); var tasks = namespaces.Select( async n => { await client.CreateNamespaceIfNotExistsAsync(n); }); await Task.WhenAll(tasks); }
public static ServiceBusNamespace[] GetNamespaces( SubscriptionCloudCredentials creds ) { var sbMgmt = new ServiceBusManagementClient( creds ); var regionsResponse = sbMgmt.Namespaces.List( ); int currentNamespace = 0, namespaceCount = regionsResponse.Count( ); ServiceBusNamespace[] namespaces = new ServiceBusNamespace[ namespaceCount ]; foreach( var region in regionsResponse ) { namespaces[ currentNamespace++ ] = region; } return namespaces; }
public static string[] GetRegions( SubscriptionCloudCredentials creds ) { var sbMgmt = new ServiceBusManagementClient( creds ); var regionsResponse = sbMgmt.GetServiceBusRegionsAsync( ).Result; int currentRegion = 0, regionsCount = regionsResponse.Count( ); string[] regions = new string[ regionsCount ]; foreach( var region in regionsResponse ) { regions[ currentRegion++ ] = region.FullName; } return regions; }
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; }
/// <summary> /// Creates new instance from ServiceBusClientExtensions /// </summary> /// <param name="subscription"></param> public ServiceBusClientExtensions(AzureSMProfile profile) { if (profile.Context.Subscription == null) { throw new ArgumentException(Resources.InvalidDefaultSubscription); } subscriptionId = profile.Context.Subscription.Id.ToString(); Subscription = profile.Context.Subscription; ServiceBusClient = AzureSession.ClientFactory.CreateClient<ServiceBusManagementClient>(profile, profile.Context.Subscription, AzureEnvironment.Endpoint.ServiceManagement); }
/// <summary> /// Creates new instance from ServiceBusClientExtensions /// </summary> /// <param name="subscription"></param> /// <param name="logger">The logger action</param> public ServiceBusClientExtensions(WindowsAzureSubscription subscription) { subscriptionId = subscription.SubscriptionId; Subscription = subscription; ServiceBusClient = Subscription.CreateClient<ServiceBusManagementClient>(); }
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; }
/// <summary> /// Creates new instance from ServiceBusClientExtensions /// </summary> /// <param name="subscription"></param> public ServiceBusClientExtensions(AzureProfile profile, AzureSubscription subscription) { subscriptionId = subscription.Id.ToString(); Subscription = subscription; ServiceBusClient = AzureSession.ClientFactory.CreateClient<ServiceBusManagementClient>(profile, subscription, AzureEnvironment.Endpoint.ServiceManagement); }
bool Run( ) { CloudWebDeployInputs inputs = null; if( !GetInputs( out inputs ) ) { return false; } 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 ); EventHubDescription ehDevices = AzureConsoleHelper.SelectEventHub( nsManager, inputs.Credentials ); var serviceNamespace = inputs.SBNamespace; var hubName = ehDevices.Path; var sharedAccessAuthorizationRule = ehDevices.Authorization.FirstOrDefault( ( d ) => d.Rights.Contains(AccessRights.Listen)) as SharedAccessAuthorizationRule; if( sharedAccessAuthorizationRule == null ) { Console.WriteLine( "Cannot locate Authorization rule for WebSite key." ); return false; } var receiverKeyName = sharedAccessAuthorizationRule.KeyName; var receiverKey = sharedAccessAuthorizationRule.PrimaryKey; //Console.WriteLine("Starting temperature processor with {0} partitions.", partitionCount); CancellationTokenSource cts = new CancellationTokenSource( ); int closedReceivers = 0; AutoResetEvent receiversStopped = new AutoResetEvent( false ); MessagingFactory factory = MessagingFactory.Create(ServiceBusEnvironment.CreateServiceUri( "sb", serviceNamespace, "" ), new MessagingFactorySettings { TokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider( receiverKeyName, receiverKey ), TransportType = TransportType.Amqp } ); EventHubClient eventHubClient = factory.CreateEventHubClient( hubName ); EventHubConsumerGroup eventHubConsumerGroup = eventHubClient.GetDefaultConsumerGroup( ); int partitionCount = ehDevices.PartitionCount; for( int i = 0; i < partitionCount; i++ ) { Task.Factory.StartNew( ( state ) => { try { _ConsoleBuffer.Add( string.Format( "Starting worker to process partition: {0}", state ) ); var receiver = eventHubConsumerGroup.CreateReceiver( state.ToString( ), DateTime.UtcNow ); _ConsoleBuffer.Add( string.Format( "Waiting for start receiving messages: {0} ...", state ) ); while( true ) { // Receive could fail, I would need a retry policy etc... var messages = receiver.Receive( 10 ); foreach( var message in messages ) { //var eventBody = Newtonsoft.Json.JsonConvert.DeserializeObject<TemperatureEvent>(Encoding.Default.GetString(message.GetBytes())); //Console.WriteLine("{0} [{1}] Temperature: {2}", DateTime.Now, message.PartitionKey, eventBody.Temperature); _ConsoleBuffer.Add( message.PartitionKey + " sent message:" + Encoding.Default.GetString( message.GetBytes( ) ) ); } if( cts.IsCancellationRequested ) { Console.WriteLine( "Stopping: {0}", state ); receiver.Close( ); if( Interlocked.Increment( ref closedReceivers ) >= partitionCount ) { receiversStopped.Set(); } break; } } } catch( Exception ex ) { _ConsoleBuffer.Add( ex.Message ); } }, i ); } Console.ReadLine( ); cts.Cancel( ); //waiting for all receivers to stop receiversStopped.WaitOne( ); bool saveToFile; for( ;; ) { Console.WriteLine( "Do you want to save received data to file? (y/n)" ); string answer = Console.ReadLine( ); string request = "do not"; saveToFile = false; if( !string.IsNullOrEmpty( answer ) && answer.ToLower( ).StartsWith( "y" ) ) { saveToFile = true; request = ""; } if( ConsoleHelper.Confirm( "Are you sure you " + request + " want to save received data?" ) ) { break; } } if( saveToFile ) { string fileName = inputs.SBNamespace + DateTime.UtcNow.ToString( "_d_MMM_h_mm" ) + ".log"; string filePath = Environment.GetFolderPath( Environment.SpecialFolder.Desktop ); string fileFullName = filePath + @"\" + fileName; if( _ConsoleBuffer.FlushToFile( fileFullName ) ) { Console.WriteLine( "Output was saved to your desktop, at " + fileFullName + " file." ); } } Console.WriteLine( "Wait for all receivers to close and then press ENTER." ); Console.ReadLine( ); return true; }
private void DeleteResources( ClearResourcesInputs inputs ) { if( inputs.NamespaceExists ) { _ConsoleBuffer.Add( "Connecting to Service Bus..." ); ServiceBusManagementClient sbMgmt = new ServiceBusManagementClient( inputs.Credentials ); bool deleteNamespace = ConsoleHelper.AskAndPerformAction( "Do you want to delete whole namespace " + inputs.SBNamespace + " including all entities under it?", "Are you sure you want to delete namespace " + inputs.SBNamespace + "?", "Are you sure you do not want to delete namespace " + inputs.SBNamespace + "?", ( ) => { _ConsoleBuffer.Add( "Sending request to delete " + inputs.SBNamespace + " namespace..." ); AzureOperationResponse nsResponse = sbMgmt.Namespaces.Delete( inputs.SBNamespace ); if( nsResponse.StatusCode == HttpStatusCode.OK ) { _ConsoleBuffer.Add( inputs.SBNamespace + " namespace was deleted." ); } }, _ConsoleBuffer ); //if we did not delete whole Namespace, maybe we want to delete some of its Event Hubs? if( !deleteNamespace ) { _ConsoleBuffer.Add( "Reading list of Event Hubs from " + inputs.SBNamespace + " namespace..." ); var nsDescription = sbMgmt.Namespaces.GetNamespaceDescription( inputs.SBNamespace ); var nsConnectionString = nsDescription.NamespaceDescriptions.First( ( d ) => String.Equals( d.AuthorizationType, "SharedAccessAuthorization" ) ).ConnectionString; var nsManager = NamespaceManager.CreateFromConnectionString( nsConnectionString ); var eventHubs = nsManager.GetEventHubs( ); foreach( var eventHubDescription in eventHubs ) { EventHubDescription description = eventHubDescription; ConsoleHelper.AskAndPerformAction( "Do you want to delete Event Hub " + eventHubDescription.Path + " including all messages under it?", "Are you sure you want to delete Event Hub " + eventHubDescription.Path + "?", "Are you sure you do not want to delete Event Hub " + eventHubDescription.Path + "?", ( ) => { _ConsoleBuffer.Add( "Sending request to delete " + description.Path + " Event Hub..." ); nsManager.DeleteEventHub( description.Path ); _ConsoleBuffer.Add( "Request to delete " + description.Path + " Event Hub was accepted." ); }, _ConsoleBuffer ); } } } //Deleting Storage _ConsoleBuffer.Add( "Reading list of Storage Accounts..." ); StorageManagementClient stgMgmt = new StorageManagementClient( inputs.Credentials ); HashSet<string> storageAccounts = new HashSet<string>( ); foreach( var storageAccount in stgMgmt.StorageAccounts.List( ) ) { storageAccounts.Add( storageAccount.Name ); } int deletedCount = 0; if( storageAccounts.Contains( inputs.StorageAccountName ) ) { ConsoleHelper.AskAndPerformAction( "Do you want to delete " + inputs.StorageAccountName + " storage account?", "Are you sure you want to delete " + inputs.StorageAccountName + " storage account?", "Are you sure you do not want to delete " + inputs.StorageAccountName + " storage account?", ( ) => { _ConsoleBuffer.Add( "Sending request to delete " + inputs.StorageAccountName + " Storage account..." ); AzureOperationResponse resultStg = stgMgmt.StorageAccounts.Delete( inputs.StorageAccountName ); deletedCount += 1; if( resultStg.StatusCode == System.Net.HttpStatusCode.OK ) { _ConsoleBuffer.Add( "Storage account " + inputs.StorageAccountName + " was deleted." ); } }, _ConsoleBuffer ); } if( deletedCount == 0 ) { _ConsoleBuffer.Add( "No related Storage account was detected." ); } //Deleting Stream Analytics jobs _ConsoleBuffer.Add( "Reading list of Stream Analytics jobs..." ); StreamAnalyticsManagementClient saMgmt = new StreamAnalyticsManagementClient( inputs.Credentials ); JobListResponse jobListResponse = saMgmt.StreamingJobs.ListJobsInSubscription( new JobListParameters { PropertiesToExpand = string.Empty } ); deletedCount = 0; foreach( var job in jobListResponse.Value ) { if( job.Name.StartsWith( inputs.NamePrefix ) ) { Job jobToAsk = job; ConsoleHelper.AskAndPerformAction( "Do you want to delete Stream Analytics job " + job.Name + "?", "Are you sure you want to delete Stream Analytics job " + job.Name + "?", "Are you sure you do not want to delete namespace " + job.Name + "?", ( ) => { //we need to figure out wat resourceGroup this job belongs to //--// const string resourceGroupPath = "/resourceGroups/"; const string providersPath = "/providers/"; int resourceGroupPathIndex = jobToAsk.Id.IndexOf( resourceGroupPath, System.StringComparison.Ordinal ); int providersPathIndex = jobToAsk.Id.IndexOf( providersPath, System.StringComparison.Ordinal ); int resourceGroupIdStartIndex = resourceGroupPathIndex + resourceGroupPath.Length; string resourceGroup = jobToAsk.Id.Substring( resourceGroupIdStartIndex, providersPathIndex - resourceGroupIdStartIndex ); //--// deletedCount += 1; _ConsoleBuffer.Add( "Sending request to delete " + jobToAsk.Name + " Stream Analytics job..." ); LongRunningOperationResponse response = saMgmt.StreamingJobs.Delete( resourceGroup, jobToAsk.Name ); if( response.Status == OperationStatus.Succeeded ) { _ConsoleBuffer.Add( "Stream Analytics job " + jobToAsk.Name + " was deleted." ); } }, _ConsoleBuffer ); } } if( deletedCount == 0 ) { _ConsoleBuffer.Add( "No Stream Analytics job was deleted." ); } }
/// <summary> /// Provisions all the components in the <see cref="DevOpsFlexDbContext"/>. /// </summary> /// <param name="context">The database context that we want to provision components from.</param> /// <param name="subscriptionId">The subscription Id where we want to provision in.</param> /// <param name="settingsPath">The path to the settings file with the management certificate.</param> /// <returns>The async <see cref="Task"/> wrapper.</returns> public static async Task ProvisionAllAsync(this DevOpsFlexDbContext context, string subscriptionId, string settingsPath) { Contract.Requires(context != null); Contract.Requires(string.IsNullOrEmpty(subscriptionId)); Contract.Requires(string.IsNullOrEmpty(settingsPath)); var azureSubscription = new AzureSubscription(settingsPath, subscriptionId); var azureCert = new X509Certificate2(Convert.FromBase64String(azureSubscription.ManagementCertificate)); using (var computeClient = new ComputeManagementClient(new CertificateCloudCredentials(subscriptionId, azureCert))) using (var networkClient = new NetworkManagementClient(new CertificateCloudCredentials(subscriptionId, azureCert))) using (var sbClient = new ServiceBusManagementClient(new CertificateCloudCredentials(subscriptionId, azureCert))) using (var sqlClient = new SqlManagementClient(new CertificateCloudCredentials(subscriptionId, azureCert))) using (var storageClient = new StorageManagementClient(new CertificateCloudCredentials(subscriptionId, azureCert))) using (var webSiteClient = new WebSiteManagementClient(new CertificateCloudCredentials(subscriptionId, azureCert))) { var tasks = new[] { context.Components.OfType<AzureCloudService>().ToList().ProvisionAllAsync(computeClient), context.Components.OfType<AzureCloudService>().ToList().ReserveAllIpsAsync(networkClient), context.Components.OfType<AzureServiceBusNamespace>().ToList().ProvisionAllAsync(sbClient), context.Components.OfType<SqlAzureDb>().ToList().ProvisionAllAsync(sqlClient), context.Components.OfType<AzureStorageContainer>().ToList().ProvisionAllAsync(storageClient), context.Components.OfType<AzureWebSite>().ToList().ProvisionAllAsync(webSiteClient) }; await Task.WhenAll(tasks); } }
private AzurePrepOutputs CreateEventHub( AzurePrepInputs inputs ) { AzurePrepOutputs result = new AzurePrepOutputs { SBNamespace = inputs.SBNamespace }; // Create Namespace var sbMgmt = new ServiceBusManagementClient( inputs.Credentials ); ServiceBusNamespaceResponse nsResponse = null; _ConsoleBuffer.Add( string.Format( "Creating Service Bus namespace {0} in location {1}", inputs.SBNamespace, inputs.Location ) ); try { // There is (currently) no clean error code returned when the namespace already exists // Check if it does nsResponse = sbMgmt.Namespaces.Create( inputs.SBNamespace, inputs.Location ); _ConsoleBuffer.Add( string.Format( "Service Bus namespace {0} created.", inputs.SBNamespace ) ); } catch ( Exception ) { nsResponse = null; _ConsoleBuffer.Add( string.Format( "Service Bus namespace {0} already existed.", inputs.SBNamespace ) ); } int triesCount = 0; // Wait until the namespace is active while( nsResponse == null || nsResponse.Namespace.Status != "Active" ) { nsResponse = sbMgmt.Namespaces.Get( inputs.SBNamespace ); if( nsResponse.Namespace.Status == "Active" ) { break; } triesCount += 1; if( triesCount % 10 == 0 ) { _ConsoleBuffer.Add( "Please note that activation could last about an hour if namespace with the same name prefix was deleted recently..." ); } else { _ConsoleBuffer.Add( string.Format( "Namespace {0} in state {1}. Waiting...", inputs.SBNamespace, nsResponse.Namespace.Status ) ); } System.Threading.Thread.Sleep( 5000 ); } // Get the namespace connection string var nsDescription = sbMgmt.Namespaces.GetNamespaceDescription( inputs.SBNamespace ); result.nsConnectionString = nsDescription.NamespaceDescriptions.First( ( d ) => String.Equals( d.AuthorizationType, "SharedAccessAuthorization" ) ).ConnectionString; // Create EHs + device keys + consumer keys (WebSite*) var nsManager = NamespaceManager.CreateFromConnectionString( result.nsConnectionString ); var ehDescriptionDevices = new EventHubDescription( inputs.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 } ) ); _ConsoleBuffer.Add( string.Format( "Creating Event Hub {0}...", inputs.EventHubNameDevices ) ); result.ehDevices = null; do { try { result.ehDevices = nsManager.CreateEventHubIfNotExists( ehDescriptionDevices ); } catch ( UnauthorizedAccessException ) { _ConsoleBuffer.Add( "Service Bus connection string not valid yet. Waiting..." ); System.Threading.Thread.Sleep( 5000 ); } } while ( result.ehDevices == null ); ConsoleHelper.AskAndPerformAction( "Do you want to create " + inputs.EventHubNameAlerts + " Event Hub?", "Are you sure you want to create " + inputs.EventHubNameAlerts + " Event Hub?", "Are you sure you do not want to create " + inputs.EventHubNameAlerts + " Event Hub?", ( ) => { var ehDescriptionAlerts = new EventHubDescription( inputs.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 } ) ); _ConsoleBuffer.Add( string.Format( "Creating Event Hub {0}...", inputs.EventHubNameAlerts ) ); result.ehAlerts = null; do { try { result.ehAlerts = nsManager.CreateEventHubIfNotExists( ehDescriptionAlerts ); } catch ( UnauthorizedAccessException ) { _ConsoleBuffer.Add( "Service Bus connection string not valid yet. Waiting..." ); System.Threading.Thread.Sleep( 5000 ); } } while ( result.ehAlerts == null ); }, _ConsoleBuffer ); // Create Storage Account for Event Hub Processor var stgMgmt = new StorageManagementClient( inputs.Credentials ); try { _ConsoleBuffer.Add( string.Format( "Creating Storage Account {0} in location {1}...", inputs.StorageAccountName, inputs.Location ) ); var resultStg = stgMgmt.StorageAccounts.Create( new StorageAccountCreateParameters { Name = inputs.StorageAccountName.ToLowerInvariant(), Location = inputs.Location, AccountType = "Standard_LRS" } ); if( resultStg.StatusCode != System.Net.HttpStatusCode.OK ) { _ConsoleBuffer.Add( string.Format( "Error creating storage account {0} in Location {1}: {2}", inputs.StorageAccountName, inputs.Location, resultStg.StatusCode ) ); return null; } } catch( CloudException ce ) { if( String.Equals( ce.Error.Code, "ConflictError", StringComparison.InvariantCultureIgnoreCase ) ) { _ConsoleBuffer.Add( string.Format( "Storage account {0} already existed.", inputs.StorageAccountName ) ); } else { throw; } } return result; }