예제 #1
0
        public async Task <StreamAnalyticsJobModel> AddAlertingAsync(string tenantId)
        {
            // Attempt to create the necessary database if it does not already exist
            await this.cosmosDb.CreateDatabaseIfNotExistsAsync(SaCosmosDbDatabaseName);

            StreamAnalyticsJobModel saJobModel = await this.GetAlertingAsync(tenantId);

            if (this.SaJobExists(saJobModel))
            {
                throw new Exception("The given tenant already has a deployed stream analytics job.");
            }

            TenantModel tenant = await this.GetTenantFromContainerAsync(tenantId);

            string saJobName = string.Format(SaNameFormat, tenantId.Substring(0, 8));

            await this.runbookHelper.CreateAlerting(tenantId, saJobName, tenant.IotHubName);

            return(new StreamAnalyticsJobModel
            {
                TenantId = tenant.TenantId,
                StreamAnalyticsJobName = saJobName,
                IsActive = false,
                JobState = "Creating",
            });
        }
        public async Task GetAlertingAsyncTest(bool createIfNotExists, bool expectCreateNew)
        {
            // Arrange
            StreamAnalyticsJobModel streamAnalyticsModel = new StreamAnalyticsJobModel()
            {
                TenantId = TenantId,
                StreamAnalyticsJobName = "StreamAnalyticsJob",
                JobState = "Job State",
                IsActive = true,
            };

            this.mockAlertingContainer.Setup(x => x.AddAlertingAsync(It.IsAny <string>()))
            .ReturnsAsync(streamAnalyticsModel);

            this.mockAlertingContainer.Setup(x => x.GetAlertingAsync(It.IsAny <string>()))
            .ReturnsAsync(streamAnalyticsModel);

            // Act
            var result = await this.controller.GetAlertingAsync(createIfNotExists);

            // Assert
            if (createIfNotExists)
            {
                Assert.True(expectCreateNew);
            }
            else
            {
                Assert.False(expectCreateNew);
            }

            Assert.Equal(result.TenantId, TenantId);
        }
예제 #3
0
        public async Task <StreamAnalyticsJobModel> StopAlertingAsync(string tenantId)
        {
            StreamAnalyticsJobModel saJobModel = await this.GetAlertingAsync(tenantId);

            if (!this.SaJobExists(saJobModel))
            {
                throw new Exception("There is no StreamAnalyticsJob is available to stop for this tenant.");
            }

            await this.streamAnalyticsHelper.StopAsync(saJobModel.StreamAnalyticsJobName);

            return(saJobModel);
        }
예제 #4
0
        public async Task <StreamAnalyticsJobModel> AddAlertingAsync(string tenantId)
        {
            string alarmsCollectionAppConfigKey = string.Format(AlarmsCollectionAppConfigKeyFormat, tenantId);
            string alarmsCollectionId;

            try
            {
                alarmsCollectionId = this.appConfig.GetValue(alarmsCollectionAppConfigKey);
            }
            catch (Exception)
            {
                alarmsCollectionId = string.Format(AlarmsCollectionNameFormat, tenantId);
            }

            try
            {
                await this.cosmosDb.CreateCollectionIfNotExistsAsync(
                    AlarmsDatabaseName,
                    alarmsCollectionId,
                    AlarmsCollectionPartitionKey);

                await this.appConfig.SetValueAsync(alarmsCollectionAppConfigKey, alarmsCollectionId);
            }
            catch (Exception e)
            {
                throw new Exception("Unable to ensure the necessary AppConfig configuration and CosmosDb collection exist before creating the stream analytics job.", e);
            }

            StreamAnalyticsJobModel saJobModel = await this.GetAlertingAsync(tenantId);

            if (this.SaJobExists(saJobModel))
            {
                throw new Exception("The given tenant already has a deployed stream analytics job.");
            }

            TenantModel tenant = await this.GetTenantFromContainerAsync(tenantId);

            string saJobName = string.Format(SaNameFormat, tenantId.Substring(0, 8));

            await this.tableStorageClient.InsertAsync(
                "tenantOperations",
                new TenantOperationModel(tenantId, TenantOperation.SaJobCreation, saJobName));

            return(new StreamAnalyticsJobModel
            {
                TenantId = tenant.TenantId,
                StreamAnalyticsJobName = saJobName,
                IsActive = false,
                JobState = "Creating",
            });
        }
예제 #5
0
        public async Task <StreamAnalyticsJobModel> GetAlertingAsync([FromQuery] bool createIfNotExists = false)
        {
            string tenantId = this.GetTenantId();
            StreamAnalyticsJobModel model = await this.alertingContainer.GetAlertingAsync(tenantId);

            if (!this.alertingContainer.SaJobExists(model) && createIfNotExists)
            {
                // If the tenant does not have an sa job, start creating it
                this.logger.LogInformation("The tenant does not already have alerting enabled and the createIfNotExists parameter was set to true. Creating a stream analytics job now. TenantId: {tenantId}", tenantId);
                return(await this.alertingContainer.AddAlertingAsync(tenantId));
            }
            else
            {
                return(model);
            }
        }
        public async Task RemoveAlertingAsyncTest()
        {
            // Arrange
            StreamAnalyticsJobModel streamAnalyticsModel = new StreamAnalyticsJobModel()
            {
                TenantId = TenantId,
                StreamAnalyticsJobName = "StreamAnalyticsJob",
                JobState = "Job State",
                IsActive = false,
            };

            this.mockAlertingContainer.Setup(x => x.RemoveAlertingAsync(It.IsAny <string>()))
            .ReturnsAsync(streamAnalyticsModel);

            // Act
            var result = await this.controller.RemoveAlertingAsync();

            // Assert
            Assert.False(result.IsActive);
            Assert.Equal(result.TenantId, TenantId);
        }
예제 #7
0
 public bool SaJobExists(StreamAnalyticsJobModel saJobModel)
 {
     return(!string.IsNullOrEmpty(saJobModel.StreamAnalyticsJobName) && !string.IsNullOrEmpty(saJobModel.JobState));
 }