Esempio n. 1
0
        public void FromSnsEventReturnsRequest()
        {
            var targetGroupArn = "arn:aws:elb:us-east-1:1:targetgroup/test/test";
            var dnsName        = "http://example.com";
            var alarm          = new AlarmEvent
            {
                Trigger = new Trigger
                {
                    Metrics = new List <MetricDataQuery> {
                        new MetricDataQuery {
                            Id = "healthy",
                        },
                        new MetricDataQuery {
                            Id         = "customdata",
                            MetricStat = new MetricStat {
                                Metric = new Metric {
                                    Dimensions = new List <Dimension> {
                                        new Dimension {
                                            Name  = "TargetGroupArn",
                                            Value = targetGroupArn
                                        },
                                        new Dimension {
                                            Name  = "TargetDnsName",
                                            Value = dnsName
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            };

            var evnt = new SNSEvent
            {
                Records = new List <SNSRecord> {
                    new SNSRecord {
                        Sns = new SNSMessage {
                            Message = Serialize(alarm)
                        }
                    }
                }
            };

            var factory = new UpdateTargetsRequestFactory();
            var request = factory.CreateFromSnsEvent(evnt);

            Assert.That(request.TargetGroupArn, Is.EqualTo(targetGroupArn));
            Assert.That(request.TargetDnsName, Is.EqualTo(dnsName));
        }
Esempio n. 2
0
        public async Task <Response> Handle(SNSEvent snsRequest, CancellationToken cancellationToken = default)
        {
            var request = requestFactory.CreateFromSnsEvent(snsRequest);

            logger.LogInformation($"Received transformed request: {JsonSerializer.Serialize(request)}");

            var addresses           = dnsResolver.Resolve(request.TargetDnsName).AddressList ?? new IPAddress[] { };
            var targetHealthRequest = new DescribeTargetHealthRequest {
                TargetGroupArn = request.TargetGroupArn
            };
            var targetHealthResponse = await elbClient.DescribeTargetHealthAsync(targetHealthRequest);

            logger.LogInformation($"Got target health response: {JsonSerializer.Serialize(targetHealthResponse)}");

            var targetHealthDescriptions = targetHealthResponse.TargetHealthDescriptions;
            var healthyTargets           = from target in targetHealthDescriptions where target.TargetHealth.State != TargetHealthStateEnum.Unhealthy select target.Target;
            var unhealthyTargets         = from target in targetHealthDescriptions where target.TargetHealth.State == TargetHealthStateEnum.Unhealthy select target.Target;
            var newTargets = from address in addresses
                             where healthyTargets.All(target => !IPAddress.Parse(target.Id).Equals(address))
                             select new TargetDescription
            {
                Id = address.ToString(),
                AvailabilityZone = "all",
                Port             = 80
            };

            await Task.WhenAll(new Task[]
            {
                DeregisterTargets(request.TargetGroupArn, unhealthyTargets),
                RegisterTargets(request.TargetGroupArn, newTargets),
            });

            return(new Response
            {
                Success = true
            });
        }