github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/resource_aws_iam_openid_connect_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/acctest"
    11  	"github.com/hashicorp/terraform/helper/resource"
    12  	"github.com/hashicorp/terraform/terraform"
    13  )
    14  
    15  func TestAccAWSIAMOpenIDConnectProvider_basic(t *testing.T) {
    16  	rString := acctest.RandString(5)
    17  	url := "accounts.google.com/" + rString
    18  
    19  	resource.Test(t, resource.TestCase{
    20  		PreCheck:     func() { testAccPreCheck(t) },
    21  		Providers:    testAccProviders,
    22  		CheckDestroy: testAccCheckIAMOpenIDConnectProviderDestroy,
    23  		Steps: []resource.TestStep{
    24  			resource.TestStep{
    25  				Config: testAccIAMOpenIDConnectProviderConfig(rString),
    26  				Check: resource.ComposeTestCheckFunc(
    27  					testAccCheckIAMOpenIDConnectProvider("aws_iam_openid_connect_provider.goog"),
    28  					resource.TestCheckResourceAttr("aws_iam_openid_connect_provider.goog", "url", url),
    29  					resource.TestCheckResourceAttr("aws_iam_openid_connect_provider.goog", "client_id_list.#", "1"),
    30  					resource.TestCheckResourceAttr("aws_iam_openid_connect_provider.goog", "client_id_list.0",
    31  						"266362248691-re108qaeld573ia0l6clj2i5ac7r7291.apps.googleusercontent.com"),
    32  					resource.TestCheckResourceAttr("aws_iam_openid_connect_provider.goog", "thumbprint_list.#", "0"),
    33  				),
    34  			},
    35  			resource.TestStep{
    36  				Config: testAccIAMOpenIDConnectProviderConfig_modified(rString),
    37  				Check: resource.ComposeTestCheckFunc(
    38  					testAccCheckIAMOpenIDConnectProvider("aws_iam_openid_connect_provider.goog"),
    39  					resource.TestCheckResourceAttr("aws_iam_openid_connect_provider.goog", "url", url),
    40  					resource.TestCheckResourceAttr("aws_iam_openid_connect_provider.goog", "client_id_list.#", "1"),
    41  					resource.TestCheckResourceAttr("aws_iam_openid_connect_provider.goog", "client_id_list.0",
    42  						"266362248691-re108qaeld573ia0l6clj2i5ac7r7291.apps.googleusercontent.com"),
    43  					resource.TestCheckResourceAttr("aws_iam_openid_connect_provider.goog", "thumbprint_list.#", "2"),
    44  					resource.TestCheckResourceAttr("aws_iam_openid_connect_provider.goog", "thumbprint_list.0", "cf23df2207d99a74fbe169e3eba035e633b65d94"),
    45  					resource.TestCheckResourceAttr("aws_iam_openid_connect_provider.goog", "thumbprint_list.1", "c784713d6f9cb67b55dd84f4e4af7832d42b8f55"),
    46  				),
    47  			},
    48  		},
    49  	})
    50  }
    51  
    52  func TestAccAWSIAMOpenIDConnectProvider_importBasic(t *testing.T) {
    53  	resourceName := "aws_iam_openid_connect_provider.goog"
    54  	rString := acctest.RandString(5)
    55  
    56  	resource.Test(t, resource.TestCase{
    57  		PreCheck:     func() { testAccPreCheck(t) },
    58  		Providers:    testAccProviders,
    59  		CheckDestroy: testAccCheckIAMOpenIDConnectProviderDestroy,
    60  		Steps: []resource.TestStep{
    61  			resource.TestStep{
    62  				Config: testAccIAMOpenIDConnectProviderConfig_modified(rString),
    63  			},
    64  
    65  			resource.TestStep{
    66  				ResourceName:      resourceName,
    67  				ImportState:       true,
    68  				ImportStateVerify: true,
    69  			},
    70  		},
    71  	})
    72  }
    73  
    74  func TestAccAWSIAMOpenIDConnectProvider_disappears(t *testing.T) {
    75  	rString := acctest.RandString(5)
    76  
    77  	resource.Test(t, resource.TestCase{
    78  		PreCheck:     func() { testAccPreCheck(t) },
    79  		Providers:    testAccProviders,
    80  		CheckDestroy: testAccCheckIAMOpenIDConnectProviderDestroy,
    81  		Steps: []resource.TestStep{
    82  			resource.TestStep{
    83  				Config: testAccIAMOpenIDConnectProviderConfig(rString),
    84  				Check: resource.ComposeTestCheckFunc(
    85  					testAccCheckIAMOpenIDConnectProvider("aws_iam_openid_connect_provider.goog"),
    86  					testAccCheckIAMOpenIDConnectProviderDisappears("aws_iam_openid_connect_provider.goog"),
    87  				),
    88  				ExpectNonEmptyPlan: true,
    89  			},
    90  		},
    91  	})
    92  }
    93  
    94  func testAccCheckIAMOpenIDConnectProviderDestroy(s *terraform.State) error {
    95  	iamconn := testAccProvider.Meta().(*AWSClient).iamconn
    96  
    97  	for _, rs := range s.RootModule().Resources {
    98  		if rs.Type != "aws_iam_openid_connect_provider" {
    99  			continue
   100  		}
   101  
   102  		input := &iam.GetOpenIDConnectProviderInput{
   103  			OpenIDConnectProviderArn: aws.String(rs.Primary.ID),
   104  		}
   105  		out, err := iamconn.GetOpenIDConnectProvider(input)
   106  		if err != nil {
   107  			if iamerr, ok := err.(awserr.Error); ok && iamerr.Code() == "NoSuchEntity" {
   108  				// none found, that's good
   109  				return nil
   110  			}
   111  			return fmt.Errorf("Error reading IAM OpenID Connect Provider, out: %s, err: %s", out, err)
   112  		}
   113  
   114  		if out != nil {
   115  			return fmt.Errorf("Found IAM OpenID Connect Provider, expected none: %s", out)
   116  		}
   117  	}
   118  
   119  	return nil
   120  }
   121  
   122  func testAccCheckIAMOpenIDConnectProviderDisappears(id string) resource.TestCheckFunc {
   123  	return func(s *terraform.State) error {
   124  		rs, ok := s.RootModule().Resources[id]
   125  		if !ok {
   126  			return fmt.Errorf("Not Found: %s", id)
   127  		}
   128  
   129  		if rs.Primary.ID == "" {
   130  			return fmt.Errorf("No ID is set")
   131  		}
   132  
   133  		iamconn := testAccProvider.Meta().(*AWSClient).iamconn
   134  		_, err := iamconn.DeleteOpenIDConnectProvider(&iam.DeleteOpenIDConnectProviderInput{
   135  			OpenIDConnectProviderArn: aws.String(rs.Primary.ID),
   136  		})
   137  		return err
   138  	}
   139  }
   140  
   141  func testAccCheckIAMOpenIDConnectProvider(id string) resource.TestCheckFunc {
   142  	return func(s *terraform.State) error {
   143  		rs, ok := s.RootModule().Resources[id]
   144  		if !ok {
   145  			return fmt.Errorf("Not Found: %s", id)
   146  		}
   147  
   148  		if rs.Primary.ID == "" {
   149  			return fmt.Errorf("No ID is set")
   150  		}
   151  
   152  		iamconn := testAccProvider.Meta().(*AWSClient).iamconn
   153  		_, err := iamconn.GetOpenIDConnectProvider(&iam.GetOpenIDConnectProviderInput{
   154  			OpenIDConnectProviderArn: aws.String(rs.Primary.ID),
   155  		})
   156  
   157  		if err != nil {
   158  			return err
   159  		}
   160  
   161  		return nil
   162  	}
   163  }
   164  
   165  func testAccIAMOpenIDConnectProviderConfig(rString string) string {
   166  	return fmt.Sprintf(`
   167  resource "aws_iam_openid_connect_provider" "goog" {
   168    url="https://accounts.google.com/%s"
   169    client_id_list = [
   170       "266362248691-re108qaeld573ia0l6clj2i5ac7r7291.apps.googleusercontent.com"
   171    ]
   172    thumbprint_list = []
   173  }
   174  `, rString)
   175  }
   176  
   177  func testAccIAMOpenIDConnectProviderConfig_modified(rString string) string {
   178  	return fmt.Sprintf(`
   179  resource "aws_iam_openid_connect_provider" "goog" {
   180    url="https://accounts.google.com/%s"
   181    client_id_list = [
   182       "266362248691-re108qaeld573ia0l6clj2i5ac7r7291.apps.googleusercontent.com"
   183    ]
   184    thumbprint_list = ["cf23df2207d99a74fbe169e3eba035e633b65d94", "c784713d6f9cb67b55dd84f4e4af7832d42b8f55"]
   185  }
   186  `, rString)
   187  }