<commit_before>package awspurge

import (
	"fmt"

	"github.com/aws/aws-sdk-go/service/ec2"
	"github.com/aws/aws-sdk-go/service/elb"
	"github.com/hashicorp/go-multierror"
)

// resourceLimit defines the number of resources that can be terminated at once
// for a given resource
const resourceLimit = 100

func (p *Purge) terminateEC2Resources(fn func(*ec2.EC2) error) {
	for r, s := range p.services.ec2 {
		p.deleteWg.Add(1)

		go func(region string, svc *ec2.EC2) {
			err := fn(svc)
			if err != nil {
				p.deleteMu.Lock()
				p.deleteErrs = multierror.Append(p.deleteErrs, err)
				p.deleteMu.Unlock()
			}
			p.deleteWg.Done()
		}(r, s)
	}
}

func (p *Purge) terminateELBResources(fn func(*elb.ELB) error) {
	for r, s := range p.services.elb {
		p.deleteWg.Add(1)

		go func(region string, svc *elb.ELB) {
			err := fn(svc)
			if err != nil {
				p.deleteMu.Lock()
				p.deleteErrs = multierror.Append(p.deleteErrs, err)
				p.deleteMu.Unlock()
			}
			p.deleteWg.Done()
		}(r, s)
	}
}

// DeleteInstances terminates all instances on all regions
func (p *Purge) DeleteInstances() {
	fn := func(svc *ec2.EC2) error {
		region := *svc.Config.Region

		resources, ok := p.resources[region]
		if !ok {
			return fmt.Errorf("Couldn't find resources for region %s", region)
		}

		if resources.instances == nil || len(resources.instances) == 0 {
			return fmt.Errorf("instances are not fetched for region %s", region)
		}

		instanceIds := make([]*string, len(resources.instances))
		for i, instance := range resources.instances {
			instanceIds[i] = instance.InstanceId
		}

		if len(instanceIds) > resourceLimit {
			return fmt.Errorf("Too many instances(%d) found for region '%s'. Aborting", len(instanceIds), region)
		}

		input := &ec2.TerminateInstancesInput{
			InstanceIds: instanceIds,
		}

		_, err := svc.TerminateInstances(input)<commit_msg>awspurge: change awsterminate for upcoming changes<commit_after>package awspurge

import (
	"fmt"

	"github.com/aws/aws-sdk-go/service/ec2"
	"github.com/aws/aws-sdk-go/service/elb"
	"github.com/hashicorp/go-multierror"
)

// resourceLimit defines the number of resources that can be terminated at once
// for a given resource
const resourceLimit = 100

func (p *Purge) terminateEC2Resources(fn func(*ec2.EC2) error) {
	for _, s := range p.services.ec2 {
		p.deleteWg.Add(1)

		deleteFunc := func(svc *ec2.EC2) {
			err := fn(svc)
			if err != nil {
				p.deleteMu.Lock()
				p.deleteErrs = multierror.Append(p.deleteErrs, err)
				p.deleteMu.Unlock()
			}
			p.deleteWg.Done()
		}

		go deleteFunc(s)
	}
}

func (p *Purge) terminateELBResources(fn func(*elb.ELB) error) {
	for _, s := range p.services.elb {
		p.deleteWg.Add(1)

		deleteFunc := func(svc *elb.ELB) {
			err := fn(svc)
			if err != nil {
				p.deleteMu.Lock()
				p.deleteErrs = multierror.Append(p.deleteErrs, err)
				p.deleteMu.Unlock()
			}
			p.deleteWg.Done()
		}

		go deleteFunc(s)
	}
}

// DeleteInstances terminates all instances on all regions
func (p *Purge) DeleteInstances() {
	fn := func(svc *ec2.EC2) error {
		region := *svc.Config.Region

		resources, ok := p.resources[region]
		if !ok {
			return fmt.Errorf("Couldn't find resources for region %s", region)
		}

		if resources.instances == nil || len(resources.instances) == 0 {
			return fmt.Errorf("instances are not fetched for region %s", region)
		}

		instanceIds := make([]*string, len(resources.instances))
		for i, instance := range resources.instances {
			instanceIds[i] = instance.InstanceId
		}

		if len(instanceIds) > resourceLimit {
			return fmt.Errorf("Too many instances(%d) found for region '%s'. Aborting", len(instanceIds), region)
		}

		input := &ec2.TerminateInstancesInput{
			InstanceIds: instanceIds,
		}

		_, err := svc.TerminateInstances(input)
{'fraction_non_alphanumeric': 0.14164230667020994, 'fraction_numerical': 0.008503853308530427, 'mean_word_length': 3.5679611650485437, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6951905', 'n_tokens_mistral': 1543, 'n_tokens_neox': 1367, 'n_words': 318}