github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/builtin/providers/aws/data_source_aws_ebs_volume_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 TestAccAWSEbsVolumeDataSource_basic(t *testing.T) {
    12  	resource.Test(t, resource.TestCase{
    13  		PreCheck:  func() { testAccPreCheck(t) },
    14  		Providers: testAccProviders,
    15  		Steps: []resource.TestStep{
    16  			{
    17  				Config: testAccCheckAwsEbsVolumeDataSourceConfig,
    18  				Check: resource.ComposeTestCheckFunc(
    19  					testAccCheckAwsEbsVolumeDataSourceID("data.aws_ebs_volume.ebs_volume"),
    20  					resource.TestCheckResourceAttr("data.aws_ebs_volume.ebs_volume", "size", "40"),
    21  				),
    22  			},
    23  		},
    24  	})
    25  }
    26  
    27  func TestAccAWSEbsVolumeDataSource_multipleFilters(t *testing.T) {
    28  	resource.Test(t, resource.TestCase{
    29  		PreCheck:  func() { testAccPreCheck(t) },
    30  		Providers: testAccProviders,
    31  		Steps: []resource.TestStep{
    32  			{
    33  				Config: testAccCheckAwsEbsVolumeDataSourceConfigWithMultipleFilters,
    34  				Check: resource.ComposeTestCheckFunc(
    35  					testAccCheckAwsEbsVolumeDataSourceID("data.aws_ebs_volume.ebs_volume"),
    36  					resource.TestCheckResourceAttr("data.aws_ebs_volume.ebs_volume", "size", "10"),
    37  					resource.TestCheckResourceAttr("data.aws_ebs_volume.ebs_volume", "volume_type", "gp2"),
    38  				),
    39  			},
    40  		},
    41  	})
    42  }
    43  
    44  func testAccCheckAwsEbsVolumeDataSourceID(n string) resource.TestCheckFunc {
    45  	return func(s *terraform.State) error {
    46  		rs, ok := s.RootModule().Resources[n]
    47  		if !ok {
    48  			return fmt.Errorf("Can't find Volume data source: %s", n)
    49  		}
    50  
    51  		if rs.Primary.ID == "" {
    52  			return fmt.Errorf("Volume data source ID not set")
    53  		}
    54  		return nil
    55  	}
    56  }
    57  
    58  const testAccCheckAwsEbsVolumeDataSourceConfig = `
    59  resource "aws_ebs_volume" "example" {
    60      availability_zone = "us-west-2a"
    61      type = "gp2"
    62      size = 40
    63      tags {
    64          Name = "External Volume"
    65      }
    66  }
    67  
    68  data "aws_ebs_volume" "ebs_volume" {
    69      most_recent = true
    70      filter {
    71  	name = "tag:Name"
    72  	values = ["External Volume"]
    73      }
    74      filter {
    75  	name = "volume-type"
    76  	values = ["${aws_ebs_volume.example.type}"]
    77      }
    78  }
    79  `
    80  
    81  const testAccCheckAwsEbsVolumeDataSourceConfigWithMultipleFilters = `
    82  resource "aws_ebs_volume" "external1" {
    83      availability_zone = "us-west-2a"
    84      type = "gp2"
    85      size = 10
    86      tags {
    87          Name = "External Volume 1"
    88      }
    89  }
    90  
    91  data "aws_ebs_volume" "ebs_volume" {
    92      most_recent = true
    93      filter {
    94  	name = "tag:Name"
    95  	values = ["External Volume 1"]
    96      }
    97      filter {
    98  	name = "size"
    99  	values = ["${aws_ebs_volume.external1.size}"]
   100      }
   101      filter {
   102  	name = "volume-type"
   103  	values = ["${aws_ebs_volume.external1.type}"]
   104      }
   105  }
   106  `