public static async Task <List <Exception> > Destroy( FargateResourceV2 resource, ELBHelper elb, Route53Helper e53, ECSHelper ecs, CloudWatchHelper cw, KMSHelper kms, IAMHelper iam, bool throwOnFailure, bool catchDisable) { var errList = new List <Exception>(); int maxRepeats = throwOnFailure ? 1 : 3; int delay_ms = throwOnFailure ? 500 : 10000; Console.WriteLine($"Destroying Role '{resource.RoleName}'..."); (await iam.DeleteRoleAsync(resource.RoleName, detachPolicies: true) .TryCatchRetryAsync(maxRepeats: maxRepeats, delay: delay_ms).CatchExceptionAsync()).PrintResult(); Console.WriteLine($"Destroying Policy '{resource.PolicyNameAccessS3}'..."); errList.Add(iam.DeletePolicyByNameAsync(resource.PolicyNameAccessS3, throwIfNotFound: false) .TryCatchRetryAsync(maxRepeats: maxRepeats, delay: delay_ms) .CatchExceptionAsync(catchDisable: catchDisable).Result.PrintResult()); Console.WriteLine($"Destroying Default Grant '{resource.StorageGrantDefaultS3}' for key '{resource.StorageKeyDefaultS3}'..."); errList.Add(kms.RemoveGrantsByName(keyName: resource.StorageKeyDefaultS3, grantName: resource.StorageGrantDefaultS3, throwIfNotFound: false) .TryCatchRetryAsync(maxRepeats: maxRepeats, delay: delay_ms) .CatchExceptionAsync(catchDisable: catchDisable).Result.PrintResult()); Console.WriteLine($"Destroying Internal Grant '{resource.StorageGrantInternalS3}' for key '{resource.StorageKeyInternalS3}'..."); errList.Add(kms.RemoveGrantsByName(keyName: resource.StorageKeyInternalS3, grantName: resource.StorageGrantInternalS3, throwIfNotFound: false) .TryCatchRetryAsync(maxRepeats: maxRepeats, delay: delay_ms) .CatchExceptionAsync(catchDisable: catchDisable).Result.PrintResult()); Console.WriteLine($"Destroying Application Load Balancer '{resource.LoadBalancerName}'..."); errList.Add(elb.DestroyLoadBalancer(loadBalancerName: resource.LoadBalancerName, throwIfNotFound: false) .TryCatchRetryAsync(maxRepeats: maxRepeats, delay: delay_ms) .CatchExceptionAsync(catchDisable: catchDisable).Result.PrintResult()); if (resource.IsPublic && !resource.ZonePublic.IsNullOrWhitespace()) { Console.WriteLine($"Destroying Route53 DNS Record: '{resource.DNSCName}' of '{resource.ZonePublic}' zone..."); errList.Add(e53.DestroyCNameRecord(resource.ZonePublic, resource.DNSCName, throwIfNotFound: false) .TryCatchRetryAsync(maxRepeats: maxRepeats, delay: delay_ms) .CatchExceptionAsync(catchDisable: catchDisable).Result.PrintResult()); } if (!resource.ZonePrivate.IsNullOrWhitespace()) { Console.WriteLine($"Destroying Route53 DNS Record: '{resource.DNSCName}' of '{resource.ZonePrivate}' zone..."); errList.Add(e53.DestroyCNameRecord(resource.ZonePrivate, resource.DNSCName, throwIfNotFound: false) .TryCatchRetryAsync(maxRepeats: maxRepeats, delay: delay_ms) .CatchExceptionAsync(catchDisable: catchDisable).Result.PrintResult()); } Console.WriteLine($"Destroying Log Group '{resource.LogGroupName}'..."); errList.Add(cw.DeleteLogGroupAsync(resource.LogGroupName, throwIfNotFound: false) .TryCatchRetryAsync(maxRepeats: maxRepeats, delay: delay_ms) .CatchExceptionAsync(catchDisable: catchDisable).Result.PrintResult()); Console.WriteLine($"Destroying Task Definitions of Family'{resource.TaskFamily}'..."); errList.Add(ecs.DestroyTaskDefinitions(familyPrefix: resource.TaskFamily) .TryCatchRetryAsync(maxRepeats: maxRepeats, delay: delay_ms) .CatchExceptionAsync(catchDisable: catchDisable).Result.PrintResult()); Console.WriteLine($"Destroying Service '{resource.ServiceName}'..."); errList.Add(ecs.DestroyService(cluster: resource.ClusterName, serviceName: resource.ServiceName, throwIfNotFound: false) .TryCatchRetryAsync(maxRepeats: maxRepeats, delay: delay_ms) .CatchExceptionAsync(catchDisable: catchDisable).Result.PrintResult()); Console.WriteLine($"Destroying Cluster '{resource.ClusterName}'..."); errList.Add(ecs.DeleteClusterAsync(name: resource.ClusterName, throwIfNotFound: false) .TryCatchRetryAsync(maxRepeats: maxRepeats, delay: delay_ms) .CatchExceptionAsync(catchDisable: catchDisable).Result.PrintResult()); Console.WriteLine($"Destroying Metric Alarm '{resource.ELBHealthyMetricAlarmName}'..."); errList.Add(cw.DeleteMetricAlarmAsync(resource.ELBHealthyMetricAlarmName, throwIfNotFound: false) .TryCatchRetryAsync(maxRepeats: maxRepeats, delay: delay_ms) .CatchExceptionAsync(catchDisable: catchDisable).Result.PrintResult()); if (throwOnFailure && errList.Any(x => x != null)) { throw new AggregateException("Failed Fargate Resource Destruction", errList.ToArray()); } return(errList); }
private static void executeELB(string[] args) { var nArgs = CLIHelper.GetNamedArguments(args); var elb = new ELBHelper(); var ec2 = new EC2Helper(); switch (args[1]) { case "destroy-load-balancer": elb.DestroyLoadBalancer(nArgs["name"], throwIfNotFound: true).Wait(); ; break; case "register-target-instance": { var tgName = nArgs["tg-name"]; var instanceName = nArgs["instance"]; var port = nArgs["port"].ToInt32(); var tg = elb.GetTargetGroupByNameAsync( targetGroupName: tgName, throwIfNotFound: true).Result; var instance = ec2.ListInstancesByName(name: instanceName, stateExclude: new List <Amazon.EC2.InstanceStateName>() { Amazon.EC2.InstanceStateName.ShuttingDown, Amazon.EC2.InstanceStateName.Terminated } ).Result.SingleOrDefault(); if (instance == null) { throw new Exception($"Could not find instance with name '{instanceName}' or found more then one."); } var result = elb.RegisterTargetAsync(tg, instance, port: port).Result; Console.WriteLine($"Successfully Registered Instance '{instance.InstanceId}' into Target Group '{tg.TargetGroupArn}', response metadata: {result.ResponseMetadata?.JsonSerialize() ?? "undefined"}"); } ; break; case "deregister-target-instance": { var tgName = nArgs["tg-name"]; var instanceName = nArgs["instance"]; var tg = elb.GetTargetGroupByNameAsync( targetGroupName: tgName, throwIfNotFound: true).Result; var instance = ec2.ListInstancesByName(name: instanceName, stateExclude: new List <Amazon.EC2.InstanceStateName>() { Amazon.EC2.InstanceStateName.ShuttingDown, Amazon.EC2.InstanceStateName.Terminated } ).Result.SingleOrDefault(); if (instance == null) { throw new Exception($"Could not find instance with name '{instanceName}' or found more then one."); } var result = elb.DeregisterTargetAsync(tg, instance).Result; Console.WriteLine($"Successfully Deregistered Instance '{instance.InstanceId}' from Target Group '{tg.TargetGroupArn}', response metadata: {result.ResponseMetadata?.JsonSerialize() ?? "undefined"}"); } ; break; case "help": case "--help": case "-help": case "-h": case "h": HelpPrinter($"{args[0]}", "Amazon Elastic Load Balancer", ("destroy-load-balancer", "Accepts params: name"), ("register-target-instance", "Accepts params: tg-name, instance, port"), ("deregister-target-instance", "Accepts params: tg-name, instance")); break; default: throw new Exception($"Unknown ELB command: '{args[1]}'"); } }