Example #1
0
        //--//

        public bool GetInputs( out ClearResourcesInputs result )
        {
            result = new ClearResourcesInputs( );

            result.Credentials = AzureConsoleHelper.GetUserSubscriptionCredentials( );
            if( result.Credentials == null )
            {
                result = null;
                return false;
            }

            ServiceBusNamespace selectedNamespace = AzureConsoleHelper.SelectNamespace( result.Credentials,
                "Please select namespace you want to clear resources for: ", "Enter manually (if Namespace was deleted but there are resources with the same name prefix)." );
            if( selectedNamespace == null )
            {
                result = null;
                Console.WriteLine( "Quiting..." );
                return false;
            }
            result.NamePrefix = selectedNamespace.Name;

            if( selectedNamespace.Region != null )
            {
                result.NamespaceExists = true;
                result.Location = selectedNamespace.Region;
            }

            result.SBNamespace = result.NamePrefix + "-ns";
            result.StorageAccountName = result.NamePrefix.ToLowerInvariant( ) + "storage";

            return true;
        }
Example #2
0
        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." );
            }
        }