|
| 1 | +package redshift_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/aws/aws-sdk-go/service/redshift" |
| 8 | + sdkacctest "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" |
| 11 | + "github.com/hashicorp/terraform-provider-aws/internal/acctest" |
| 12 | + "github.com/hashicorp/terraform-provider-aws/internal/conns" |
| 13 | + tfredshift "github.com/hashicorp/terraform-provider-aws/internal/service/redshift" |
| 14 | + "github.com/hashicorp/terraform-provider-aws/internal/tfresource" |
| 15 | +) |
| 16 | + |
| 17 | +func TestAccRedshiftAuthenticationProfile_basic(t *testing.T) { |
| 18 | + resourceName := "aws_redshift_authentication_profile.test" |
| 19 | + rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) |
| 20 | + rNameUpdated := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) |
| 21 | + |
| 22 | + resource.ParallelTest(t, resource.TestCase{ |
| 23 | + PreCheck: func() { acctest.PreCheck(t) }, |
| 24 | + ErrorCheck: acctest.ErrorCheck(t, redshift.EndpointsID), |
| 25 | + ProviderFactories: acctest.ProviderFactories, |
| 26 | + CheckDestroy: testAccCheckAuthenticationProfileDestroy, |
| 27 | + Steps: []resource.TestStep{ |
| 28 | + { |
| 29 | + Config: testAccAuthenticationProfileBasic(rName, rName), |
| 30 | + Check: resource.ComposeTestCheckFunc( |
| 31 | + testAccCheckAuthenticationProfileExists(resourceName), |
| 32 | + resource.TestCheckResourceAttr(resourceName, "authentication_profile_name", rName), |
| 33 | + ), |
| 34 | + }, |
| 35 | + { |
| 36 | + ResourceName: resourceName, |
| 37 | + ImportState: true, |
| 38 | + ImportStateVerify: true, |
| 39 | + }, |
| 40 | + { |
| 41 | + Config: testAccAuthenticationProfileBasic(rName, rNameUpdated), |
| 42 | + Check: resource.ComposeTestCheckFunc( |
| 43 | + testAccCheckAuthenticationProfileExists(resourceName), |
| 44 | + resource.TestCheckResourceAttr(resourceName, "authentication_profile_name", rName), |
| 45 | + ), |
| 46 | + }, |
| 47 | + }, |
| 48 | + }) |
| 49 | +} |
| 50 | + |
| 51 | +func TestAccRedshiftAuthenticationProfile_disappears(t *testing.T) { |
| 52 | + resourceName := "aws_redshift_authentication_profile.test" |
| 53 | + rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) |
| 54 | + |
| 55 | + resource.ParallelTest(t, resource.TestCase{ |
| 56 | + PreCheck: func() { acctest.PreCheck(t) }, |
| 57 | + ErrorCheck: acctest.ErrorCheck(t, redshift.EndpointsID), |
| 58 | + ProviderFactories: acctest.ProviderFactories, |
| 59 | + CheckDestroy: testAccCheckAuthenticationProfileDestroy, |
| 60 | + Steps: []resource.TestStep{ |
| 61 | + { |
| 62 | + Config: testAccAuthenticationProfileBasic(rName, rName), |
| 63 | + Check: resource.ComposeTestCheckFunc( |
| 64 | + testAccCheckAuthenticationProfileExists(resourceName), |
| 65 | + acctest.CheckResourceDisappears(acctest.Provider, tfredshift.ResourceAuthenticationProfile(), resourceName), |
| 66 | + ), |
| 67 | + ExpectNonEmptyPlan: true, |
| 68 | + }, |
| 69 | + }, |
| 70 | + }) |
| 71 | +} |
| 72 | + |
| 73 | +func testAccCheckAuthenticationProfileDestroy(s *terraform.State) error { |
| 74 | + conn := acctest.Provider.Meta().(*conns.AWSClient).RedshiftConn |
| 75 | + |
| 76 | + for _, rs := range s.RootModule().Resources { |
| 77 | + if rs.Type != "aws_redshift_authentication_profile" { |
| 78 | + continue |
| 79 | + } |
| 80 | + |
| 81 | + _, err := tfredshift.FindAuthenticationProfileByID(conn, rs.Primary.ID) |
| 82 | + |
| 83 | + if tfresource.NotFound(err) { |
| 84 | + continue |
| 85 | + } |
| 86 | + |
| 87 | + if err != nil { |
| 88 | + return err |
| 89 | + } |
| 90 | + |
| 91 | + return fmt.Errorf("Redshift Authentication Profile %s still exists", rs.Primary.ID) |
| 92 | + } |
| 93 | + |
| 94 | + return nil |
| 95 | +} |
| 96 | + |
| 97 | +func testAccCheckAuthenticationProfileExists(name string) resource.TestCheckFunc { |
| 98 | + return func(s *terraform.State) error { |
| 99 | + rs, ok := s.RootModule().Resources[name] |
| 100 | + if !ok { |
| 101 | + return fmt.Errorf("not found: %s", name) |
| 102 | + } |
| 103 | + |
| 104 | + if rs.Primary.ID == "" { |
| 105 | + return fmt.Errorf("Authentication Profile ID is not set") |
| 106 | + } |
| 107 | + |
| 108 | + conn := acctest.Provider.Meta().(*conns.AWSClient).RedshiftConn |
| 109 | + |
| 110 | + _, err := tfredshift.FindAuthenticationProfileByID(conn, rs.Primary.ID) |
| 111 | + |
| 112 | + if err != nil { |
| 113 | + return err |
| 114 | + } |
| 115 | + |
| 116 | + return nil |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +func testAccAuthenticationProfileBasic(rName, id string) string { |
| 121 | + return fmt.Sprintf(` |
| 122 | +resource "aws_redshift_authentication_profile" "test" { |
| 123 | + authentication_profile_name = %[1]q |
| 124 | + authentication_profile_content = jsonencode( |
| 125 | + { |
| 126 | + AllowDBUserOverride = "1" |
| 127 | + Client_ID = "ExampleClientID" |
| 128 | + App_ID = %[2]q |
| 129 | + } |
| 130 | + ) |
| 131 | +} |
| 132 | +`, rName, id) |
| 133 | +} |
0 commit comments