github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/aws/data_source_aws_vpc_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/helper/resource"
     8  	"github.com/hashicorp/terraform/terraform"
     9  )
    10  
    11  func TestAccDataSourceAwsVpc_basic(t *testing.T) {
    12  	resource.Test(t, resource.TestCase{
    13  		PreCheck:  func() { testAccPreCheck(t) },
    14  		Providers: testAccProviders,
    15  		Steps: []resource.TestStep{
    16  			resource.TestStep{
    17  				Config: testAccDataSourceAwsVpcConfig,
    18  				Check: resource.ComposeTestCheckFunc(
    19  					testAccDataSourceAwsVpcCheck("data.aws_vpc.by_id"),
    20  					testAccDataSourceAwsVpcCheck("data.aws_vpc.by_cidr"),
    21  					testAccDataSourceAwsVpcCheck("data.aws_vpc.by_tag"),
    22  					testAccDataSourceAwsVpcCheck("data.aws_vpc.by_filter"),
    23  				),
    24  			},
    25  		},
    26  	})
    27  }
    28  
    29  func testAccDataSourceAwsVpcCheck(name string) resource.TestCheckFunc {
    30  	return func(s *terraform.State) error {
    31  		rs, ok := s.RootModule().Resources[name]
    32  		if !ok {
    33  			return fmt.Errorf("root module has no resource called %s", name)
    34  		}
    35  
    36  		vpcRs, ok := s.RootModule().Resources["aws_vpc.test"]
    37  		if !ok {
    38  			return fmt.Errorf("can't find aws_vpc.test in state")
    39  		}
    40  
    41  		attr := rs.Primary.Attributes
    42  
    43  		if attr["id"] != vpcRs.Primary.Attributes["id"] {
    44  			return fmt.Errorf(
    45  				"id is %s; want %s",
    46  				attr["id"],
    47  				vpcRs.Primary.Attributes["id"],
    48  			)
    49  		}
    50  
    51  		if attr["cidr_block"] != "172.16.0.0/16" {
    52  			return fmt.Errorf("bad cidr_block %s", attr["cidr_block"])
    53  		}
    54  		if attr["tags.Name"] != "terraform-testacc-vpc-data-source" {
    55  			return fmt.Errorf("bad Name tag %s", attr["tags.Name"])
    56  		}
    57  
    58  		return nil
    59  	}
    60  }
    61  
    62  const testAccDataSourceAwsVpcConfig = `
    63  provider "aws" {
    64    region = "us-west-2"
    65  }
    66  
    67  resource "aws_vpc" "test" {
    68    cidr_block = "172.16.0.0/16"
    69  
    70    tags {
    71      Name = "terraform-testacc-vpc-data-source"
    72    }
    73  }
    74  
    75  data "aws_vpc" "by_id" {
    76    id = "${aws_vpc.test.id}"
    77  }
    78  
    79  data "aws_vpc" "by_cidr" {
    80    cidr_block = "${aws_vpc.test.cidr_block}"
    81  }
    82  
    83  data "aws_vpc" "by_tag" {
    84    tags {
    85      Name = "${aws_vpc.test.tags["Name"]}"
    86    }
    87  }
    88  
    89  data "aws_vpc" "by_filter" {
    90    filter {
    91      name = "cidr"
    92      values = ["${aws_vpc.test.cidr_block}"]
    93    }
    94  }
    95  `