Beispiel #1
0
        public static async Task DestroyLoadBalancer(this ELBHelper elbh, string loadBalancerName, bool throwIfNotFound, CancellationToken cancellationToken = default(CancellationToken))
        {
            var loadbalancers = await elbh.GetLoadBalancersByName(loadBalancerName, throwIfNotFound, cancellationToken);

            if (loadbalancers.Count() != 1)
            {
                if (throwIfNotFound)
                {
                    throw new Exception($"DestroyLoadBalancer, LoadBalancer '{loadBalancerName}' was not found, or multiple load balancers with the same name were found.");
                }
                else
                {
                    return;
                }
            }

            var arn       = loadbalancers.First().LoadBalancerArn;
            var listeners = await elbh.ListListenersAsync(arn, cancellationToken);

            var targetGroups = await elbh.ListTargetGroupsAsync(arn, cancellationToken : cancellationToken);

            //kill listeners
            await elbh.DeleteListenersAsync(listeners, cancellationToken);

            //kill target groups
            await elbh.DeleteTargetGroupsAsync(targetGroups, cancellationToken);

            //kill loadbalancer
            await elbh.DeleteLoadBalancersAsync(new List <string>() { arn }, cancellationToken);
        }
Beispiel #2
0
 public static async Task <IEnumerable <string> > ListTargetGroupsAsync(
     this ELBHelper elbh,
     string loadBalancerArn,
     IEnumerable <string> names           = null,
     IEnumerable <string> targetGroupArns = null,
     CancellationToken cancellationToken  = default(CancellationToken))
 => (await elbh.DescribeTargetGroupsAsync(loadBalancerArn, names, targetGroupArns, cancellationToken)).Select(x => x.TargetGroupArn);
Beispiel #3
0
 public static async Task <LoadBalancer> CreateApplicationLoadBalancerAsync(
     this ELBHelper elbh,
     string name,
     IEnumerable <string> subnets,
     IEnumerable <string> securityGroups,
     bool isInternal,
     CancellationToken cancellationToken = default(CancellationToken))
 => (await elbh.CreateLoadBalancerAsync(
         name, subnets, securityGroups, LoadBalancerTypeEnum.Application,
         isInternal ? LoadBalancerSchemeEnum.Internal : LoadBalancerSchemeEnum.InternetFacing,
         cancellationToken).EnsureSuccessAsync()).LoadBalancers.Single();
Beispiel #4
0
        public static async Task <TargetGroup> GetTargetGroupByName(this ELBHelper elbh, string name, LoadBalancer lb, bool throwIfNotFound, CancellationToken cancellationToken = default(CancellationToken))
        {
            var tgs = await elbh.DescribeTargetGroupsAsync(lb.LoadBalancerArn);

            var tg = tgs.SingleOrDefault(x => x.TargetGroupName == name);

            if (throwIfNotFound && tg == null)
            {
                throw new Exception($"Could not find Target Group '{name}' for Load Balancer '{lb.LoadBalancerName}'");
            }

            return(tg);
        }
Beispiel #5
0
        /*public static async Task<TargetGroup> GetTargetGroupByNameAsync(
         *  this ELBHelper elbh,
         *  string loadBalancerName,
         *  string targetGroupName,
         *  bool throwIfNotFound)
         * {
         *  var albs = await elbh.GetLoadBalancersByName(loadBalancerName, throwIfNotFound: throwIfNotFound);
         *  var albsCount = (albs?.Count() ?? 0);
         *
         *  if (albsCount > 1)
         *      throw new Exception($"Found more then one Loadbalancer with name '{loadBalancerName}'.");
         *  else if(albsCount <= 0)
         *  {
         *      if (throwIfNotFound)
         *          throw new Exception($"Loadbalancer with name '{loadBalancerName}' was not found.");
         *      else
         *          return null;
         *  }
         *
         *  var alb = albs.Single();
         *  IEnumerable<TargetGroup> tgs;
         *
         *  if (!throwIfNotFound)
         *  {
         *      try
         *      {
         *          tgs = await elbh.DescribeTargetGroupsAsync(alb.LoadBalancerArn, names: new List<string>() { targetGroupName });
         *      }
         *      catch (TargetGroupNotFoundException ex)
         *      {
         *          return null;
         *      }
         *  }
         *  else
         *      tgs = await elbh.DescribeTargetGroupsAsync(alb.LoadBalancerArn, names: new List<string>() { targetGroupName });
         *
         *  var tgsCount = (tgs?.Count() ?? 0);
         *
         *  if (tgsCount > 1)
         *      throw new Exception($"Found more then one Target Group with name '{targetGroupName}'.");
         *  else if (tgsCount <= 0)
         *  {
         *      if (throwIfNotFound)
         *          throw new Exception($"Target Group with name '{targetGroupName}' was not found.");
         *      else
         *          return null;
         *  }
         *
         *  return tgs.Single();
         * }*/

        public static async Task <TargetGroup> GetTargetGroupByNameAsync(
            this ELBHelper elbh,
            string targetGroupName,
            bool throwIfNotFound)
        {
            IEnumerable <TargetGroup> tgs;

            if (!throwIfNotFound)
            {
                try
                {
                    tgs = await elbh.DescribeTargetGroupsAsync(loadBalancerArn : null, names : new List <string>()
                    {
                        targetGroupName
                    });
                }
                catch (TargetGroupNotFoundException)
                {
                    return(null);
                }
            }
            else
            {
                tgs = await elbh.DescribeTargetGroupsAsync(loadBalancerArn : null, names : new List <string>()
                {
                    targetGroupName
                });
            }

            var tgsCount = (tgs?.Count() ?? 0);

            if (tgsCount > 1)
            {
                throw new Exception($"Found more then one Target Group with name '{targetGroupName}'.");
            }
            else if (tgsCount <= 0)
            {
                if (throwIfNotFound)
                {
                    throw new Exception($"Target Group with name '{targetGroupName}' was not found.");
                }
                else
                {
                    return(null);
                }
            }

            return(tgs.Single());
        }
Beispiel #6
0
 public static async Task <Listener> CreateHttpListenerAsync(
     this ELBHelper elbh,
     string loadBalancerArn,
     string targetGroupArn,
     int port,
     CancellationToken cancellationToken = default(CancellationToken))
 => (await elbh.CreateListenerAsync(
         port,
         ProtocolEnum.HTTP,
         loadBalancerArn,
         targetGroupArn,
         ActionTypeEnum.Forward,
         null,
         null,
         cancellationToken).EnsureSuccessAsync()).Listeners.Single();
Beispiel #7
0
        public static async Task <IEnumerable <TargetGroup> > DescribeLoadBalancerTargetGroupsAsync(
            this ELBHelper elbh,
            string loadBalancerName,
            bool thowIfNotFound,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            var loadbalancer = (await elbh.GetLoadBalancersByName(loadBalancerName, thowIfNotFound, cancellationToken)).SingleOrDefault();

            if (!thowIfNotFound)
            {
                return(null);
            }

            return(await elbh.DescribeTargetGroupsAsync(loadBalancerArn : loadbalancer.LoadBalancerArn, cancellationToken : cancellationToken));
        }
Beispiel #8
0
 public static async Task <Listener> CreateHttpsListenerAsync(
     this ELBHelper elbh,
     string loadBalancerArn,
     string targetGroupArn,
     string certificateArn,
     CancellationToken cancellationToken = default(CancellationToken))
 => (await elbh.CreateListenerAsync(
         443,
         ProtocolEnum.HTTPS,
         loadBalancerArn,
         targetGroupArn,
         ActionTypeEnum.Forward,
         new Certificate[] { new Certificate()
                             {
                                 CertificateArn = certificateArn
                             } },
         "ELBSecurityPolicy-2016-08",
         cancellationToken).EnsureSuccessAsync()).Listeners.Single();
Beispiel #9
0
 public static async Task <TargetGroup> CreateHttpTargetGroupAsync(
     this ELBHelper elbh,
     string name,
     int port,
     string vpcId,
     string healthCheckPath,
     CancellationToken cancellationToken = default(CancellationToken))
 => (await elbh.CreateTargetGroupAsync(
         name,
         port,
         ProtocolEnum.HTTP,
         vpcId,
         TargetTypeEnum.Ip,
         healthCheckPath: healthCheckPath,
         healthCheckIntervalSeconds: 65,
         healthyThresholdCount: 2,
         unhealthyThresholdCount: 3,
         healthCheckTimeoutSeconds: 60,
         healthCheckProtocol: ProtocolEnum.HTTP,
         healthCheckPort: null, //traffic port
         cancellationToken: cancellationToken).EnsureSuccessAsync()).TargetGroups.Single();
Beispiel #10
0
 public static async Task <IEnumerable <string> > ListListenersAsync(this ELBHelper elbh, string loadBalancerArn, CancellationToken cancellationToken = default(CancellationToken))
 => (await elbh.DescribeListenersAsync(loadBalancerArn)).Select(x => x.ListenerArn);
Beispiel #11
0
 public static async Task <IEnumerable <LoadBalancer> > GetLoadBalancersByName(this ELBHelper elbh, string loadBalancerName, bool throwIfNotFound, CancellationToken cancellationToken = default(CancellationToken))
 {
     if (!throwIfNotFound)
     {
         try
         {
             return(await elbh.DescribeLoadBalancersAsync(new List <string>() { loadBalancerName }));
         }
         catch (LoadBalancerNotFoundException)
         {
             return(new LoadBalancer[0]);
         }
     }
     else
     {
         return(await elbh.DescribeLoadBalancersAsync(new List <string>() { loadBalancerName }));
     }
 }