<commit_before>package aws

import (
	"fmt"
	"log"
	"testing"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/awserr"
	"github.com/aws/aws-sdk-go/service/ec2"
	"github.com/hashicorp/terraform/helper/resource"
	"github.com/hashicorp/terraform/terraform"
)

func init() {
	resource.AddTestSweepers("aws_vpc_dhcp_options", &resource.Sweeper{
		Name: "aws_vpc_dhcp_options",
		F:    testSweepVpcDhcpOptions,
	})
}

func testSweepVpcDhcpOptions(region string) error {
	client, err := sharedClientForRegion(region)
	if err != nil {
		return fmt.Errorf("error getting client: %s", err)
	}
	conn := client.(*AWSClient).ec2conn

	input := &ec2.DescribeDhcpOptionsInput{}

	err = conn.DescribeDhcpOptionsPages(input, func(page *ec2.DescribeDhcpOptionsOutput, lastPage bool) bool {
		for _, dhcpOption := range page.DhcpOptions {
			var defaultDomainNameFound, defaultDomainNameServersFound bool

			domainName := region + ".compute.internal"
			if region == "us-east-1" {
				domainName = "ec2.internal"
			}

			// This skips the default dhcp configurations so they don't get deleted
			for _, dhcpConfiguration := range dhcpOption.DhcpConfigurations {
				if aws.StringValue(dhcpConfiguration.Key) == "domain-name" {
					if len(dhcpConfiguration.Values) != 1 || dhcpConfiguration.Values[0] == nil {
						continue
					}

					if aws.StringValue(dhcpConfiguration.Values[0].Value) == domainName {
						defaultDomainNameFound = true
					}
				} else if aws.StringValue(dhcpConfiguration.Key) == "domain-name-servers" {
					if len(dhcpConfiguration.Values) != 1 || dhcpConfiguration.Values[0] == nil {
						continue
					}

					if aws.StringValue(dhcpConfiguration.Values[0].Value) == "AmazonProvidedDNS" {
						defaultDomainNameServersFound = true
					}
				}
			}

			if defaultDomainNameFound && defaultDomainNameServersFound {
				continue
			}

			input := &ec2.DeleteDhcpOptionsInput{
				DhcpOptionsId: dhcpOption.DhcpOptionsId,
			}

			_, err := conn.DeleteDhcpOptions(input)

			if err != nil {
				log.Printf("[ERROR] Error deleting EC2 DHCP Option (%s): %s", aws.StringValue(dhcpOption.DhcpOptionsId), err)
			}
		}
		return !lastPage
	})

	if testSweepSkipSweepError(err) {
		log.Printf("[WARN] Skipping EC2 DHCP Option sweep for %s: %s", region, err)
		return nil
	}

	if err != nil {
		return fmt.Errorf("error describing DHCP Options: %s", err)
	}

	return nil
}

func TestAccAWSDHCPOptions_importBasic(t *testing.T) {
	resourceName := "aws_vpc_dhcp_options.foo"

	resource.ParallelTest(t, resource.TestCase{
		PreCheck:  func() { testAccPreCheck(t) },
		Providers: testAccProviders,
		Steps: []resource.TestStep{
			{
				Config: testAccDHCPOptionsConfig,
			},<commit_msg>Add Missing CheckDestroy for aws_vpc_dhcp_options resource<commit_after>package aws

import (
	"fmt"
	"log"
	"testing"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/awserr"
	"github.com/aws/aws-sdk-go/service/ec2"
	"github.com/hashicorp/terraform/helper/resource"
	"github.com/hashicorp/terraform/terraform"
)

func init() {
	resource.AddTestSweepers("aws_vpc_dhcp_options", &resource.Sweeper{
		Name: "aws_vpc_dhcp_options",
		F:    testSweepVpcDhcpOptions,
	})
}

func testSweepVpcDhcpOptions(region string) error {
	client, err := sharedClientForRegion(region)
	if err != nil {
		return fmt.Errorf("error getting client: %s", err)
	}
	conn := client.(*AWSClient).ec2conn

	input := &ec2.DescribeDhcpOptionsInput{}

	err = conn.DescribeDhcpOptionsPages(input, func(page *ec2.DescribeDhcpOptionsOutput, lastPage bool) bool {
		for _, dhcpOption := range page.DhcpOptions {
			var defaultDomainNameFound, defaultDomainNameServersFound bool

			domainName := region + ".compute.internal"
			if region == "us-east-1" {
				domainName = "ec2.internal"
			}

			// This skips the default dhcp configurations so they don't get deleted
			for _, dhcpConfiguration := range dhcpOption.DhcpConfigurations {
				if aws.StringValue(dhcpConfiguration.Key) == "domain-name" {
					if len(dhcpConfiguration.Values) != 1 || dhcpConfiguration.Values[0] == nil {
						continue
					}

					if aws.StringValue(dhcpConfiguration.Values[0].Value) == domainName {
						defaultDomainNameFound = true
					}
				} else if aws.StringValue(dhcpConfiguration.Key) == "domain-name-servers" {
					if len(dhcpConfiguration.Values) != 1 || dhcpConfiguration.Values[0] == nil {
						continue
					}

					if aws.StringValue(dhcpConfiguration.Values[0].Value) == "AmazonProvidedDNS" {
						defaultDomainNameServersFound = true
					}
				}
			}

			if defaultDomainNameFound && defaultDomainNameServersFound {
				continue
			}

			input := &ec2.DeleteDhcpOptionsInput{
				DhcpOptionsId: dhcpOption.DhcpOptionsId,
			}

			_, err := conn.DeleteDhcpOptions(input)

			if err != nil {
				log.Printf("[ERROR] Error deleting EC2 DHCP Option (%s): %s", aws.StringValue(dhcpOption.DhcpOptionsId), err)
			}
		}
		return !lastPage
	})

	if testSweepSkipSweepError(err) {
		log.Printf("[WARN] Skipping EC2 DHCP Option sweep for %s: %s", region, err)
		return nil
	}

	if err != nil {
		return fmt.Errorf("error describing DHCP Options: %s", err)
	}

	return nil
}

func TestAccAWSDHCPOptions_importBasic(t *testing.T) {
	resourceName := "aws_vpc_dhcp_options.foo"

	resource.ParallelTest(t, resource.TestCase{
		PreCheck:     func() { testAccPreCheck(t) },
		Providers:    testAccProviders,
		CheckDestroy: testAccCheckDHCPOptionsDestroy,
		Steps: []resource.TestStep{
			{
				Config: testAccDHCPOptionsConfig,
			},
{'fraction_non_alphanumeric': 0.12121212121212122, 'fraction_numerical': 0.005443658138268917, 'mean_word_length': 4.029197080291971, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '920182', 'n_tokens_mistral': 2198, 'n_tokens_neox': 1927, 'n_words': 355}