-
Notifications
You must be signed in to change notification settings - Fork 9.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24907 from DrFaust92/redshift-auth-profile
r/redshift_authentication_profile - new resource
- Loading branch information
Showing
7 changed files
with
385 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```release-note:new-resource | ||
aws_redshift_authentication_profile | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
package redshift | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/redshift" | ||
"github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/structure" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" | ||
"github.com/hashicorp/terraform-provider-aws/internal/conns" | ||
"github.com/hashicorp/terraform-provider-aws/internal/tfresource" | ||
"github.com/hashicorp/terraform-provider-aws/internal/verify" | ||
) | ||
|
||
func ResourceAuthenticationProfile() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceAuthenticationProfileCreate, | ||
Read: resourceAuthenticationProfileRead, | ||
Update: resourceAuthenticationProfileUpdate, | ||
Delete: resourceAuthenticationProfileDelete, | ||
|
||
Importer: &schema.ResourceImporter{ | ||
State: schema.ImportStatePassthrough, | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"authentication_profile_name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
"authentication_profile_content": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateFunc: validation.StringIsJSON, | ||
DiffSuppressFunc: verify.SuppressEquivalentJSONDiffs, | ||
StateFunc: func(v interface{}) string { | ||
json, _ := structure.NormalizeJsonString(v) | ||
return json | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceAuthenticationProfileCreate(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*conns.AWSClient).RedshiftConn | ||
|
||
authProfileName := d.Get("authentication_profile_name").(string) | ||
|
||
input := redshift.CreateAuthenticationProfileInput{ | ||
AuthenticationProfileName: aws.String(authProfileName), | ||
AuthenticationProfileContent: aws.String(d.Get("authentication_profile_content").(string)), | ||
} | ||
|
||
out, err := conn.CreateAuthenticationProfile(&input) | ||
|
||
if err != nil { | ||
return fmt.Errorf("error creating Redshift Authentication Profile (%s): %s", authProfileName, err) | ||
} | ||
|
||
d.SetId(aws.StringValue(out.AuthenticationProfileName)) | ||
|
||
return resourceAuthenticationProfileRead(d, meta) | ||
} | ||
|
||
func resourceAuthenticationProfileRead(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*conns.AWSClient).RedshiftConn | ||
|
||
out, err := FindAuthenticationProfileByID(conn, d.Id()) | ||
if !d.IsNewResource() && tfresource.NotFound(err) { | ||
log.Printf("[WARN] Redshift Authentication Profile (%s) not found, removing from state", d.Id()) | ||
d.SetId("") | ||
return nil | ||
} | ||
|
||
if err != nil { | ||
return fmt.Errorf("error reading Redshift Authentication Profile (%s): %w", d.Id(), err) | ||
} | ||
|
||
d.Set("authentication_profile_content", out.AuthenticationProfileContent) | ||
d.Set("authentication_profile_name", out.AuthenticationProfileName) | ||
|
||
return nil | ||
} | ||
|
||
func resourceAuthenticationProfileUpdate(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*conns.AWSClient).RedshiftConn | ||
|
||
input := &redshift.ModifyAuthenticationProfileInput{ | ||
AuthenticationProfileName: aws.String(d.Id()), | ||
AuthenticationProfileContent: aws.String(d.Get("authentication_profile_content").(string)), | ||
} | ||
|
||
_, err := conn.ModifyAuthenticationProfile(input) | ||
|
||
if err != nil { | ||
return fmt.Errorf("error modifying Redshift Authentication Profile (%s): %w", d.Id(), err) | ||
} | ||
|
||
return resourceAuthenticationProfileRead(d, meta) | ||
} | ||
|
||
func resourceAuthenticationProfileDelete(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*conns.AWSClient).RedshiftConn | ||
|
||
deleteInput := redshift.DeleteAuthenticationProfileInput{ | ||
AuthenticationProfileName: aws.String(d.Id()), | ||
} | ||
|
||
log.Printf("[DEBUG] Deleting Redshift Authentication Profile: %s", d.Id()) | ||
_, err := conn.DeleteAuthenticationProfile(&deleteInput) | ||
|
||
if err != nil { | ||
if tfawserr.ErrCodeEquals(err, redshift.ErrCodeAuthenticationProfileNotFoundFault) { | ||
return nil | ||
} | ||
return err | ||
} | ||
|
||
return err | ||
} |
133 changes: 133 additions & 0 deletions
133
internal/service/redshift/authentication_profile_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
package redshift_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/aws/aws-sdk-go/service/redshift" | ||
sdkacctest "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform" | ||
"github.com/hashicorp/terraform-provider-aws/internal/acctest" | ||
"github.com/hashicorp/terraform-provider-aws/internal/conns" | ||
tfredshift "github.com/hashicorp/terraform-provider-aws/internal/service/redshift" | ||
"github.com/hashicorp/terraform-provider-aws/internal/tfresource" | ||
) | ||
|
||
func TestAccRedshiftAuthenticationProfile_basic(t *testing.T) { | ||
resourceName := "aws_redshift_authentication_profile.test" | ||
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) | ||
rNameUpdated := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { acctest.PreCheck(t) }, | ||
ErrorCheck: acctest.ErrorCheck(t, redshift.EndpointsID), | ||
ProviderFactories: acctest.ProviderFactories, | ||
CheckDestroy: testAccCheckAuthenticationProfileDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccAuthenticationProfileBasic(rName, rName), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckAuthenticationProfileExists(resourceName), | ||
resource.TestCheckResourceAttr(resourceName, "authentication_profile_name", rName), | ||
), | ||
}, | ||
{ | ||
ResourceName: resourceName, | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
}, | ||
{ | ||
Config: testAccAuthenticationProfileBasic(rName, rNameUpdated), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckAuthenticationProfileExists(resourceName), | ||
resource.TestCheckResourceAttr(resourceName, "authentication_profile_name", rName), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccRedshiftAuthenticationProfile_disappears(t *testing.T) { | ||
resourceName := "aws_redshift_authentication_profile.test" | ||
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { acctest.PreCheck(t) }, | ||
ErrorCheck: acctest.ErrorCheck(t, redshift.EndpointsID), | ||
ProviderFactories: acctest.ProviderFactories, | ||
CheckDestroy: testAccCheckAuthenticationProfileDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccAuthenticationProfileBasic(rName, rName), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckAuthenticationProfileExists(resourceName), | ||
acctest.CheckResourceDisappears(acctest.Provider, tfredshift.ResourceAuthenticationProfile(), resourceName), | ||
), | ||
ExpectNonEmptyPlan: true, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCheckAuthenticationProfileDestroy(s *terraform.State) error { | ||
conn := acctest.Provider.Meta().(*conns.AWSClient).RedshiftConn | ||
|
||
for _, rs := range s.RootModule().Resources { | ||
if rs.Type != "aws_redshift_authentication_profile" { | ||
continue | ||
} | ||
|
||
_, err := tfredshift.FindAuthenticationProfileByID(conn, rs.Primary.ID) | ||
|
||
if tfresource.NotFound(err) { | ||
continue | ||
} | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
return fmt.Errorf("Redshift Authentication Profile %s still exists", rs.Primary.ID) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func testAccCheckAuthenticationProfileExists(name string) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
rs, ok := s.RootModule().Resources[name] | ||
if !ok { | ||
return fmt.Errorf("not found: %s", name) | ||
} | ||
|
||
if rs.Primary.ID == "" { | ||
return fmt.Errorf("Authentication Profile ID is not set") | ||
} | ||
|
||
conn := acctest.Provider.Meta().(*conns.AWSClient).RedshiftConn | ||
|
||
_, err := tfredshift.FindAuthenticationProfileByID(conn, rs.Primary.ID) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
func testAccAuthenticationProfileBasic(rName, id string) string { | ||
return fmt.Sprintf(` | ||
resource "aws_redshift_authentication_profile" "test" { | ||
authentication_profile_name = %[1]q | ||
authentication_profile_content = jsonencode( | ||
{ | ||
AllowDBUserOverride = "1" | ||
Client_ID = "ExampleClientID" | ||
App_ID = %[2]q | ||
} | ||
) | ||
} | ||
`, rName, id) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.