예제 #1
0
        private void CreateStreamAnalyticsJobs(AzurePrepInputs azurePrepIn, AzurePrepOutputs azurePrepOut)
        {
            string resourceGroupName = SelectResourceGroup(azurePrepIn);

            string path = Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly( ).Location);

            path += "\\..\\..\\..\\..\\StreamAnalyticsQueries";
            foreach (string filename in Directory.GetFiles(path))
            {
                string extension = Path.GetExtension(filename);
                if (extension != null && extension.Contains("sql"))
                {
                    string nameWithoutExtension  = Path.GetFileNameWithoutExtension(filename);
                    EventHubDescription ehOutput = (filename.ToLower( ).Contains("aggregates") || azurePrepOut.ehAlerts == null)
                        ? azurePrepOut.ehDevices
                        : azurePrepOut.ehAlerts;

                    if (ehOutput == null)
                    {
                        _ConsoleBuffer.Add(string.Format(" Skip creating {0} Stream Analytics job because there is no output Event Hub...", nameWithoutExtension));
                        continue;
                    }

                    string queryFilename = filename;
                    ConsoleHelper.AskAndPerformAction(
                        "Do you want to create " + nameWithoutExtension + " job?",
                        "Are you sure you want to create " + nameWithoutExtension + " job?",
                        "Are you sure you do not want to create " + nameWithoutExtension + " job?",
                        () =>
                    {
                        string query = File.ReadAllText(queryFilename);

                        _ConsoleBuffer.Add(string.Format("Creating {0} Stream Analytics job...", nameWithoutExtension));

                        CreateStreamAnalyticsJob(nameWithoutExtension, query, resourceGroupName,
                                                 azurePrepIn, azurePrepOut.ehDevices, ehOutput);
                    },
                        _ConsoleBuffer);
                }
            }
        }
예제 #2
0
        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));
            }

            // 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;
                }
                _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);
        }
예제 #3
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.");
            }
        }