github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/resource_aws_iam_saml_provider_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/aws/aws-sdk-go/aws"
     8  	"github.com/aws/aws-sdk-go/aws/awserr"
     9  	"github.com/aws/aws-sdk-go/service/iam"
    10  	"github.com/hashicorp/terraform/helper/resource"
    11  	"github.com/hashicorp/terraform/terraform"
    12  )
    13  
    14  func TestAccAWSIAMSamlProvider_basic(t *testing.T) {
    15  	resource.Test(t, resource.TestCase{
    16  		PreCheck:     func() { testAccPreCheck(t) },
    17  		Providers:    testAccProviders,
    18  		CheckDestroy: testAccCheckIAMSamlProviderDestroy,
    19  		Steps: []resource.TestStep{
    20  			{
    21  				Config: testAccIAMSamlProviderConfig,
    22  				Check: resource.ComposeTestCheckFunc(
    23  					testAccCheckIAMSamlProvider("aws_iam_saml_provider.salesforce"),
    24  				),
    25  			},
    26  			{
    27  				Config: testAccIAMSamlProviderConfigUpdate,
    28  				Check: resource.ComposeTestCheckFunc(
    29  					testAccCheckIAMSamlProvider("aws_iam_saml_provider.salesforce"),
    30  				),
    31  			},
    32  		},
    33  	})
    34  }
    35  
    36  func testAccCheckIAMSamlProviderDestroy(s *terraform.State) error {
    37  	iamconn := testAccProvider.Meta().(*AWSClient).iamconn
    38  
    39  	for _, rs := range s.RootModule().Resources {
    40  		if rs.Type != "aws_iam_saml_provider" {
    41  			continue
    42  		}
    43  
    44  		input := &iam.GetSAMLProviderInput{
    45  			SAMLProviderArn: aws.String(rs.Primary.ID),
    46  		}
    47  		out, err := iamconn.GetSAMLProvider(input)
    48  		if err != nil {
    49  			if iamerr, ok := err.(awserr.Error); ok && iamerr.Code() == "NoSuchEntity" {
    50  				// none found, that's good
    51  				return nil
    52  			}
    53  			return fmt.Errorf("Error reading IAM SAML Provider, out: %s, err: %s", out, err)
    54  		}
    55  
    56  		if out != nil {
    57  			return fmt.Errorf("Found IAM SAML Provider, expected none: %s", out)
    58  		}
    59  	}
    60  
    61  	return nil
    62  }
    63  
    64  func testAccCheckIAMSamlProvider(id string) resource.TestCheckFunc {
    65  	return func(s *terraform.State) error {
    66  		rs, ok := s.RootModule().Resources[id]
    67  		if !ok {
    68  			return fmt.Errorf("Not Found: %s", id)
    69  		}
    70  
    71  		if rs.Primary.ID == "" {
    72  			return fmt.Errorf("No ID is set")
    73  		}
    74  
    75  		iamconn := testAccProvider.Meta().(*AWSClient).iamconn
    76  		_, err := iamconn.GetSAMLProvider(&iam.GetSAMLProviderInput{
    77  			SAMLProviderArn: aws.String(rs.Primary.ID),
    78  		})
    79  
    80  		if err != nil {
    81  			return err
    82  		}
    83  
    84  		return nil
    85  	}
    86  }
    87  
    88  const testAccIAMSamlProviderConfig = `
    89  resource "aws_iam_saml_provider" "salesforce" {
    90      name = "tf-salesforce-test"
    91      saml_metadata_document = "${file("./test-fixtures/saml-metadata.xml")}"
    92  }
    93  `
    94  
    95  const testAccIAMSamlProviderConfigUpdate = `
    96  resource "aws_iam_saml_provider" "salesforce" {
    97      name = "tf-salesforce-test"
    98      saml_metadata_document = "${file("./test-fixtures/saml-metadata-modified.xml")}"
    99  }
   100  `