public static async Task <MetricAlarm> UpsertAELBMetricAlarmAsync( this CloudWatchHelper cwh, ELBHelper elb, string name, string loadBalancer, string targetGroup, ELBMetricName metric, ComparisonOperator comparisonOperator, int treshold, Statistic statistic = null, int dataPointToAlarm = 1, int evaluationPeriod = 1, int requestDelay = 1000, CancellationToken cancellationToken = default(CancellationToken)) { var alb = (await elb.GetLoadBalancersByName(loadBalancer, throwIfNotFound: true)).Single(); var tg = await elb.GetTargetGroupByName(targetGroup, alb, throwIfNotFound : true); var loadBalanceValue = alb.LoadBalancerArn.SplitByLast(':')[1].TrimStartSingle("loadbalancer/"); var targetGroupValue = tg.TargetGroupArn.SplitByLast(':')[1]; var stat = statistic != null ? statistic : (comparisonOperator == ComparisonOperator.GreaterThanOrEqualToThreshold || comparisonOperator == ComparisonOperator.GreaterThanThreshold) ? Statistic.Maximum : Statistic.Minimum; var deleteAlarm = await cwh.DeleteMetricAlarmAsync(name, throwIfNotFound : false); if (deleteAlarm.HttpStatusCode != System.Net.HttpStatusCode.NotFound) { await Task.Delay(requestDelay); //lets ensure metric was removed before we push new request } var mar = await cwh.PutAELBMetricAlarmAsync( name, loadBalanceValue, targetGroupValue, metric : metric, comparisonOperator : comparisonOperator, statistic : stat, treshold : treshold, dataPointToAlarm : dataPointToAlarm, evaluationPeriod : evaluationPeriod, cancellationToken : cancellationToken); await Task.Delay(requestDelay); //lets ensure metric exists before we search for it return(await cwh.GetMetricAlarmAsync(name : name, throwIfNotFound : true, cancellationToken : cancellationToken)); }
public static async Task <StateValue> WaitForMetricState(this CloudWatchHelper cwh, string name, StateValue stateValue, int timeout_s) { var sw = Stopwatch.StartNew(); MetricAlarm ma; do { ma = await cwh.GetMetricAlarmAsync(name, throwIfNotFound : true); if (ma.StateValue == stateValue) { return(ma.StateValue); } await Task.Delay(1000); }while (sw.ElapsedMilliseconds < (timeout_s * 1000)); throw new Exception($"Metric '{name}' coudn't reach '{stateValue}' state within {timeout_s} [s], last state was: '{ma.StateValue}'."); }