public async Task PopulateResourceNames_NonExistantResourceSpecified_Ingnored() { // arrange var resourceSourceStub = new Mock <IResourceSource <ExampleServiceModel> >(); resourceSourceStub .Setup(x => x.GetResourceNamesAsync()) .ReturnsAsync(new List <string> { "ItemY" }); var sut = new ResourceNamePopulator <ExampleServiceModel, ResourceConfig>(new ConsoleAlarmLogger(false), resourceSourceStub.Object); var group = new ServiceAlertingGroup <ResourceConfig> { GroupParameters = new AlertingGroupParameters("name", "suffix"), Service = new AwsServiceAlarms <ResourceConfig> { Resources = new List <ResourceThresholds <ResourceConfig> > { new ResourceThresholds <ResourceConfig> { Name = "DoesNotExist" } } } }; // act await sut.PopulateResourceNames(group); // assert Assert.That(group.Service.Resources.Count, Is.EqualTo(0)); }
private async Task <IList <Alarm> > GetAlarms(IList <AlarmDefinition> alarms, ResourceThresholds awsResource, ServiceAlertingGroup group) { var result = new List <Alarm>(); var entity = await _tableSource.GetResourceAsync(awsResource.Name); if (entity == null) { throw new Exception($"Entity {awsResource.Name} not found"); } // expand dynamic thresholds foreach (var alarm in alarms) { alarm.Threshold = ExpandThreshold(entity.Resource, alarm.Threshold); var dimensions = _dimensions.GetDimensions(entity.Resource, alarm.DimensionNames); var model = new Alarm { AlarmName = GetAlarmName(entity, alarm.Name, group.AlarmNameSuffix), Resource = entity, AlarmDefinition = alarm, AlertingGroup = group, Dimensions = dimensions }; result.Add(model); } return(result); }
private async Task <IList <Alarm> > ExpandAlarmsToResources(ServiceAlertingGroup alertingGroup, IList <AlarmDefinition> defaults, ResourceThresholds resource, AwsServiceAlarms service) { // apply thresholds from resource or alerting group var expanded = ExpandDefaultAlarmsForResource(defaults, resource.Values, service.Values); return(await GetAlarms(expanded, resource, alertingGroup)); }
public async Task <string> EnsureSnsTopic(ServiceAlertingGroup alertingGroup, bool dryRun) { var snsTopicArn = await _snsTopicCreator.EnsureSnsTopic(alertingGroup.Name, dryRun); if (!dryRun) { await _snsSubscriptionCreator.EnsureSnsSubscriptions(alertingGroup.Targets, snsTopicArn); } return(snsTopicArn); }
public async Task ResourceEvaluationPeriodsCanOverrideServiceValueWithoutResettingThreshold() { // arrange var defaults = new List <AlarmDefinition> { new AlarmDefinition { Name = "AlarmName", EvaluationPeriods = 2, Threshold = new Threshold { ThresholdType = ThresholdType.Absolute, Value = 400 } } }; var alertingGroup = new ServiceAlertingGroup <ResourceConfig> { GroupParameters = new AlertingGroupParameters("TestAlarm", "Suffix"), Service = new AwsServiceAlarms <ResourceConfig> { Resources = new List <ResourceThresholds <ResourceConfig> > { new ResourceThresholds <ResourceConfig> { Name = "ResourceA", Values = new Dictionary <string, AlarmValues> { { "AlarmName", new AlarmValues(null, 17, null) } } } }, Values = new Dictionary <string, AlarmValues> { { "AlarmName", new AlarmValues(125, 5, null) } } } }; SetupFakeResources(new[] { "ResourceA" }); // act var result = await _generator.GenerateAlarmsFor(alertingGroup.Service, defaults, alertingGroup.GroupParameters.AlarmNameSuffix); // assert var resourceAlarmA = result.FirstOrDefault(x => x.Resource.Name == "ResourceA"); Assert.That(resourceAlarmA, Is.Not.Null); Assert.That(resourceAlarmA.AlarmDefinition.EvaluationPeriods, Is.EqualTo(17)); Assert.That(resourceAlarmA.AlarmDefinition.Threshold.Value, Is.EqualTo(125)); }
public async Task ResourceThresholdCanOverrideServiceValueWithoutResettingEvaluationPeriods() { // arrange var defaults = new List <AlarmDefinition> { new AlarmDefinition { Name = "AlarmName", EvaluationPeriods = 2, Threshold = new Threshold { ThresholdType = ThresholdType.Absolute, Value = 400 } } }; var alertingGroup = new ServiceAlertingGroup { AlarmNameSuffix = "Suffix", Name = "TestAlarm", Service = new AwsServiceAlarms { Resources = new List <ResourceThresholds> { new ResourceThresholds { Name = "ResourceA", Values = new Dictionary <string, AlarmValues> { { "AlarmName", new AlarmValues(200, null) } } } }, Values = new Dictionary <string, AlarmValues> { { "AlarmName", new AlarmValues(100, 5) } } } }; SetupFakeResources(new[] { "ResourceA" }); // act var result = await _generator.GenerateAlarmsFor(alertingGroup, defaults); // assert var resourceAlarmA = result.FirstOrDefault(x => x.Resource.Name == "ResourceA"); Assert.That(resourceAlarmA, Is.Not.Null); Assert.That(resourceAlarmA.AlarmDefinition.EvaluationPeriods, Is.EqualTo(5)); Assert.That(resourceAlarmA.AlarmDefinition.Threshold.Value, Is.EqualTo(200)); }
public async Task <IList <Alarm> > GenerateAlarmsFor(ServiceAlertingGroup alertingGroup, IList <AlarmDefinition> defaults) { var service = alertingGroup.Service; if (service?.Resources == null || service.Resources.Count == 0) { return(new List <Alarm>()); } var allAlarms = await Task.WhenAll(service.Resources .Select(r => ExpandAlarmsToResources(alertingGroup, defaults, r, service))); return(allAlarms.SelectMany(x => x).ToList()); }
public async Task ResourceAndGroupThresholdsTakePrecedenceOverDefault() { // arrange var defaults = DefineOneAlarm(); var alertingGroup = new ServiceAlertingGroup <ResourceConfig> { GroupParameters = new AlertingGroupParameters("TestAlarm", "Suffix"), Service = new AwsServiceAlarms <ResourceConfig> { Resources = new List <ResourceThresholds <ResourceConfig> > { new ResourceThresholds <ResourceConfig> { Name = "ResourceA", Values = new Dictionary <string, AlarmValues> { { "AlarmName", 200 } } }, new ResourceThresholds <ResourceConfig> { Name = "ResourceB" } }, Values = new Dictionary <string, AlarmValues> { { "AlarmName", 300 } } } }; SetupFakeResources(new[] { "ResourceA", "ResourceB" }); // act var result = await _generator.GenerateAlarmsFor(alertingGroup.Service, defaults, alertingGroup.GroupParameters); // assert var resourceAlarmA = result.FirstOrDefault(x => x.Resource.Name == "ResourceA"); var resourceAlarmB = result.FirstOrDefault(x => x.Resource.Name == "ResourceB"); Assert.That(resourceAlarmA, Is.Not.Null); Assert.That(resourceAlarmA.AlarmDefinition.Threshold.Value, Is.EqualTo(200)); Assert.That(resourceAlarmB, Is.Not.Null); Assert.That(resourceAlarmB.AlarmDefinition.Threshold.Value, Is.EqualTo(300)); }
public async Task EvaluationPeriodsAreSelectedFromResourceAndGroup() { // arrange var defaults = DefineOneAlarm(); var alertingGroup = new ServiceAlertingGroup <ResourceConfig> { GroupParameters = new AlertingGroupParameters("TestAlarm", "Suffix"), Service = new AwsServiceAlarms <ResourceConfig> { Resources = new List <ResourceThresholds <ResourceConfig> > { new ResourceThresholds <ResourceConfig> { Name = "ResourceA", Values = new Dictionary <string, AlarmValues> { { "AlarmName", new AlarmValues(200, 3, null) } } }, new ResourceThresholds <ResourceConfig> { Name = "ResourceB" } }, Values = new Dictionary <string, AlarmValues> { { "AlarmName", new AlarmValues(300, 4, null) } } } }; SetupFakeResources(new[] { "ResourceA", "ResourceB" }); // act var result = await _generator.GenerateAlarmsFor(alertingGroup.Service, defaults, alertingGroup.GroupParameters.AlarmNameSuffix); // assert var resourceAlarmA = result.FirstOrDefault(x => x.Resource.Name == "ResourceA"); var resourceAlarmB = result.FirstOrDefault(x => x.Resource.Name == "ResourceB"); Assert.That(resourceAlarmA, Is.Not.Null); Assert.That(resourceAlarmA.AlarmDefinition.EvaluationPeriods, Is.EqualTo(3)); Assert.That(resourceAlarmB, Is.Not.Null); Assert.That(resourceAlarmB.AlarmDefinition.EvaluationPeriods, Is.EqualTo(4)); }
public async Task PeriodLengthsAreSelectedFromResourceAndGroup() { // arrange var defaults = DefineOneAlarm(); var alertingGroup = new ServiceAlertingGroup <ResourceConfig> { GroupParameters = new AlertingGroupParameters("TestAlarm", "Suffix"), Service = new AwsServiceAlarms <ResourceConfig> { Resources = new List <ResourceThresholds <ResourceConfig> > { new ResourceThresholds <ResourceConfig> { Name = "ResourceA", Values = new Dictionary <string, AlarmValues> { { "AlarmName", new AlarmValues(200, periodMinutes: 10) } } }, new ResourceThresholds <ResourceConfig> { Name = "ResourceB" } }, Values = new Dictionary <string, AlarmValues> { { "AlarmName", new AlarmValues(300, periodMinutes: 7) } } } }; SetupFakeResources(new[] { "ResourceA", "ResourceB" }); // act var result = await CreateSut(defaults).GenerateAlarmsFor(alertingGroup.Service, alertingGroup.GroupParameters); // assert var resourceAlarmA = result.FirstOrDefault(x => x.Resource.Name == "ResourceA"); var resourceAlarmB = result.FirstOrDefault(x => x.Resource.Name == "ResourceB"); Assert.That(resourceAlarmA, Is.Not.Null); Assert.That(resourceAlarmA.AlarmDefinition.Period, Is.EqualTo(TimeSpan.FromMinutes(10))); Assert.That(resourceAlarmB, Is.Not.Null); Assert.That(resourceAlarmB.AlarmDefinition.Period, Is.EqualTo(TimeSpan.FromMinutes(7))); }
public async Task PopulateResourceNames_ExclusionPrefixSpecified_ExcludesResources() { // arrange var resourceSourceStub = new Mock <IResourceSource <ExampleServiceModel> >(); resourceSourceStub .Setup(x => x.GetResourceNamesAsync()) .ReturnsAsync(new List <string> { "ItemY", "Something" }); var sut = new ResourceNamePopulator <ExampleServiceModel, ResourceConfig> (new ConsoleAlarmLogger(false), resourceSourceStub.Object); var group = new ServiceAlertingGroup <ResourceConfig> { GroupParameters = new AlertingGroupParameters("name", "suffix"), Service = new AwsServiceAlarms <ResourceConfig> { Resources = new List <ResourceThresholds <ResourceConfig> > { new ResourceThresholds <ResourceConfig> { Name = "ItemY" }, new ResourceThresholds <ResourceConfig> { Pattern = "Item" }, new ResourceThresholds <ResourceConfig> { Name = "Something" } }, ExcludeResourcesPrefixedWith = new List <string> { "Item" } } }; // act await sut.PopulateResourceNames(group); // assert Assert.That(group.Service.Resources.Count, Is.EqualTo(1)); Assert.That(group.Service.Resources.First().Name, Is.EqualTo("Something")); }
public async Task DefaultThresholdIsUsedWhenThereAreNoOverrides() { // arrange var defaults = DefineOneAlarm(); var alertingGroup = new ServiceAlertingGroup <ResourceConfig> { GroupParameters = new AlertingGroupParameters("TestAlarm", "Suffix"), Service = new AwsServiceAlarms <ResourceConfig> { Resources = new List <ResourceThresholds <ResourceConfig> > { new ResourceThresholds <ResourceConfig> { Name = "ResourceA" }, new ResourceThresholds <ResourceConfig> { Name = "ResourceB" } } } }; SetupFakeResources(new[] { "ResourceA", "ResourceB" }); // act var result = await _generator.GenerateAlarmsFor(alertingGroup.Service, defaults, alertingGroup.GroupParameters.AlarmNameSuffix); // assert var resourceAlarmA = result.FirstOrDefault(x => x.Resource.Name == "ResourceA"); var resourceAlarmB = result.FirstOrDefault(x => x.Resource.Name == "ResourceB"); Assert.That(resourceAlarmA, Is.Not.Null); Assert.That(resourceAlarmA.AlarmDefinition.Threshold.Value, Is.EqualTo(400)); Assert.That(resourceAlarmB, Is.Not.Null); Assert.That(resourceAlarmB.AlarmDefinition.Threshold.Value, Is.EqualTo(400)); }
public async Task DefaultEvaluationPeriodsIsUsedWhenThereAreNoOverrides() { // arrange var defaults = DefineOneAlarm(); var alertingGroup = new ServiceAlertingGroup { AlarmNameSuffix = "Suffix", Name = "TestAlarm", Service = new AwsServiceAlarms { Resources = new List <ResourceThresholds> { new ResourceThresholds { Name = "ResourceA" }, new ResourceThresholds { Name = "ResourceB" } } } }; SetupFakeResources(new[] { "ResourceA", "ResourceB" }); // act var result = await _generator.GenerateAlarmsFor(alertingGroup, defaults); // assert var resourceAlarmA = result.FirstOrDefault(x => x.Resource.Name == "ResourceA"); var resourceAlarmB = result.FirstOrDefault(x => x.Resource.Name == "ResourceB"); Assert.That(resourceAlarmA, Is.Not.Null); Assert.That(resourceAlarmA.AlarmDefinition.EvaluationPeriods, Is.EqualTo(2)); Assert.That(resourceAlarmB, Is.Not.Null); Assert.That(resourceAlarmB.AlarmDefinition.EvaluationPeriods, Is.EqualTo(2)); }
public async Task DefaultPeriodLengthIsUsedWhenThereAreNoOverrides() { // arrange var defaults = DefineOneAlarm(); var alertingGroup = new ServiceAlertingGroup <ResourceConfig> { GroupParameters = new AlertingGroupParameters("TestAlarm", "Suffix"), Service = new AwsServiceAlarms <ResourceConfig> { Resources = new List <ResourceThresholds <ResourceConfig> > { new ResourceThresholds <ResourceConfig> { Name = "ResourceA" }, new ResourceThresholds <ResourceConfig> { Name = "ResourceB" } } } }; SetupFakeResources(new[] { "ResourceA", "ResourceB" }); // act var result = await CreateSut(defaults).GenerateAlarmsFor(alertingGroup.Service, alertingGroup.GroupParameters); // assert var resourceAlarmA = result.FirstOrDefault(x => x.Resource.Name == "ResourceA"); var resourceAlarmB = result.FirstOrDefault(x => x.Resource.Name == "ResourceB"); Assert.That(resourceAlarmA, Is.Not.Null); Assert.That(resourceAlarmA.AlarmDefinition.Period, Is.EqualTo(TimeSpan.FromMinutes(1))); Assert.That(resourceAlarmB, Is.Not.Null); Assert.That(resourceAlarmB.AlarmDefinition.Period, Is.EqualTo(TimeSpan.FromMinutes(1))); }
PopulateResourceNames(ServiceAlertingGroup <TConfig> alertingGroup) { var items = await ExpandTablePatterns(alertingGroup.Service, alertingGroup.GroupParameters.Name); // TODO: maybe in mapper var result = new PopulatedServiceAlertingGroup <TConfig, T>() { GroupParameters = alertingGroup.GroupParameters, Service = new PopulatedServiceAlarms <TConfig, T>() { ExcludeResourcesPrefixedWith = alertingGroup.Service.ExcludeResourcesPrefixedWith, Options = alertingGroup.Service.Options, Resources = items, Values = alertingGroup.Service.Values } }; return(result); }
public async Task PopulateResourceNames_DuplicatesMatched_DoesNotReturnDuplicates() { // arrange var resourceSourceStub = new Mock <IResourceSource <ExampleServiceModel> >(); resourceSourceStub .Setup(x => x.GetResourcesAsync()) .ReturnsAsync(WrapResourceNames(new List <string> { "ItemY" })); var sut = new ResourceNamePopulator <ExampleServiceModel, ResourceConfig>( new ConsoleAlarmLogger(false), resourceSourceStub.Object); var group = new ServiceAlertingGroup <ResourceConfig> { GroupParameters = new AlertingGroupParameters("name", "suffix"), Service = new AwsServiceAlarms <ResourceConfig> { Resources = new List <ResourceThresholds <ResourceConfig> > { new ResourceThresholds <ResourceConfig> { Name = "ItemY" }, new ResourceThresholds <ResourceConfig> { Pattern = "ItemY" }, new ResourceThresholds <ResourceConfig> { Pattern = "Item" } } } }; // act var result = await sut.PopulateResourceNames(group); // assert Assert.That(result.Service.Resources.Count, Is.EqualTo(1)); }
public async Task PopulateResourceNames_DuplicatesMatched_DoesNotReturnDuplicates() { // arrange var resourceSourceStub = new Mock <IResourceSource <ExampleServiceModel> >(); resourceSourceStub .Setup(x => x.GetResourceNamesAsync()) .ReturnsAsync(new List <string> { "ItemY" }); var sut = new ResourceNamePopulator <ExampleServiceModel>(new ConsoleAlarmLogger(false), resourceSourceStub.Object); var group = new ServiceAlertingGroup { Service = new AwsServiceAlarms { Resources = new List <ResourceThresholds> { new ResourceThresholds { Name = "ItemY" }, new ResourceThresholds { Pattern = "ItemY" }, new ResourceThresholds { Pattern = "Item" } } } }; // act await sut.PopulateResourceNames(group); // assert Assert.That(group.Service.Resources.Count, Is.EqualTo(1)); }
public async Task PopulateResourceNames(ServiceAlertingGroup alertingGroup) { alertingGroup.Service.Resources = await ExpandTablePatterns(alertingGroup.Service, alertingGroup.Name); }
public async Task PopulateResourceNames(ServiceAlertingGroup <TConfig> alertingGroup) { alertingGroup.Service.Resources = await ExpandTablePatterns(alertingGroup.Service, alertingGroup.GroupParameters.Name); }
private string ExpectedStackName(ServiceAlertingGroup group) { return($"Watchman-{group.Name.ToLowerInvariant()}"); }
public async Task PopulateResourceNames_ByPatternAndNamed_ResourcesExpandedWithCorrectEvaluationPeriods() { // arrange var resourceSourceStub = new Mock <IResourceSource <ExampleServiceModel> >(); resourceSourceStub .Setup(x => x.GetResourceNamesAsync()) .ReturnsAsync(new List <string> { "ItemX", "ItemY", "ItemZ" }); var sut = new ResourceNamePopulator <ExampleServiceModel, ResourceConfig>(new ConsoleAlarmLogger(false), resourceSourceStub.Object); var namedItem = new ResourceThresholds <ResourceConfig> { Name = "ItemX", Values = new Dictionary <string, AlarmValues> { { "SomeThreshold", new AlarmValues(500, 2) } } }; var patternMatchedItem = new ResourceThresholds <ResourceConfig> { Pattern = "Item", Values = new Dictionary <string, AlarmValues> { { "SomeThreshold", new AlarmValues(100, 3) } } }; var group = new ServiceAlertingGroup <ResourceConfig> { GroupParameters = new AlertingGroupParameters("name", "suffix"), Service = new AwsServiceAlarms <ResourceConfig> { Resources = new List <ResourceThresholds <ResourceConfig> > { namedItem, patternMatchedItem } } }; // act await sut.PopulateResourceNames(group); // assert var resources = group.Service.Resources; Assert.That(resources.Count, Is.EqualTo(3)); var itemXThreshold = resources.First(x => x.Name == "ItemX").Values["SomeThreshold"]; var itemYThreshold = resources.First(x => x.Name == "ItemY").Values["SomeThreshold"]; var itemZThreshold = resources.First(x => x.Name == "ItemZ").Values["SomeThreshold"]; Assert.That(itemXThreshold.EvaluationPeriods, Is.EqualTo(2)); Assert.That(itemYThreshold.EvaluationPeriods, Is.EqualTo(3)); Assert.That(itemZThreshold.EvaluationPeriods, Is.EqualTo(3)); }