-
Notifications
You must be signed in to change notification settings - Fork 8.9k
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 #26636 from DrFaust92/redshift-serverless-usage-limit
r/aws_redshiftserverless_usage_limit - new resource
- Loading branch information
Showing
7 changed files
with
382 additions
and
6 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_redshiftserverless_usage_limit | ||
``` |
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
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,157 @@ | ||
package redshiftserverless | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/redshiftserverless" | ||
"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/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 ResourceUsageLimit() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceUsageLimitCreate, | ||
Read: resourceUsageLimitRead, | ||
Update: resourceUsageLimitUpdate, | ||
Delete: resourceUsageLimitDelete, | ||
|
||
Importer: &schema.ResourceImporter{ | ||
State: schema.ImportStatePassthrough, | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"arn": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"amount": { | ||
Type: schema.TypeInt, | ||
Required: true, | ||
}, | ||
"breach_action": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Default: redshiftserverless.UsageLimitBreachActionLog, | ||
ValidateFunc: validation.StringInSlice(redshiftserverless.UsageLimitBreachAction_Values(), false), | ||
}, | ||
"period": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ForceNew: true, | ||
Default: redshiftserverless.UsageLimitPeriodMonthly, | ||
ValidateFunc: validation.StringInSlice(redshiftserverless.UsageLimitPeriod_Values(), false), | ||
}, | ||
"resource_arn": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: verify.ValidARN, | ||
}, | ||
"usage_type": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateFunc: validation.StringInSlice(redshiftserverless.UsageLimitUsageType_Values(), false), | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceUsageLimitCreate(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*conns.AWSClient).RedshiftServerlessConn | ||
|
||
input := redshiftserverless.CreateUsageLimitInput{ | ||
Amount: aws.Int64(int64(d.Get("amount").(int))), | ||
ResourceArn: aws.String(d.Get("resource_arn").(string)), | ||
UsageType: aws.String(d.Get("usage_type").(string)), | ||
} | ||
|
||
if v, ok := d.GetOk("period"); ok { | ||
input.Period = aws.String(v.(string)) | ||
} | ||
|
||
if v, ok := d.GetOk("breach_action"); ok { | ||
input.BreachAction = aws.String(v.(string)) | ||
} | ||
|
||
out, err := conn.CreateUsageLimit(&input) | ||
|
||
if err != nil { | ||
return fmt.Errorf("error creating Redshift Serverless Usage Limit : %w", err) | ||
} | ||
|
||
d.SetId(aws.StringValue(out.UsageLimit.UsageLimitId)) | ||
|
||
return resourceUsageLimitRead(d, meta) | ||
} | ||
|
||
func resourceUsageLimitRead(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*conns.AWSClient).RedshiftServerlessConn | ||
|
||
out, err := FindUsageLimitByName(conn, d.Id()) | ||
if !d.IsNewResource() && tfresource.NotFound(err) { | ||
log.Printf("[WARN] Redshift Serverless UsageLimit (%s) not found, removing from state", d.Id()) | ||
d.SetId("") | ||
return nil | ||
} | ||
|
||
if err != nil { | ||
return fmt.Errorf("error reading Redshift Serverless Usage Limit (%s): %w", d.Id(), err) | ||
} | ||
|
||
d.Set("arn", out.UsageLimitArn) | ||
d.Set("breach_action", out.BreachAction) | ||
d.Set("period", out.Period) | ||
d.Set("usage_type", out.UsageType) | ||
d.Set("resource_arn", out.ResourceArn) | ||
d.Set("amount", out.Amount) | ||
|
||
return nil | ||
} | ||
|
||
func resourceUsageLimitUpdate(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*conns.AWSClient).RedshiftServerlessConn | ||
|
||
input := &redshiftserverless.UpdateUsageLimitInput{ | ||
UsageLimitId: aws.String(d.Id()), | ||
} | ||
|
||
if d.HasChange("amount") { | ||
input.Amount = aws.Int64(int64(d.Get("amount").(int))) | ||
} | ||
|
||
if d.HasChange("breach_action") { | ||
input.BreachAction = aws.String(d.Get("breach_action").(string)) | ||
} | ||
|
||
_, err := conn.UpdateUsageLimit(input) | ||
if err != nil { | ||
return fmt.Errorf("error updating Redshift Serverless Usage Limit (%s): %w", d.Id(), err) | ||
} | ||
|
||
return resourceUsageLimitRead(d, meta) | ||
} | ||
|
||
func resourceUsageLimitDelete(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*conns.AWSClient).RedshiftServerlessConn | ||
|
||
log.Printf("[DEBUG] Deleting Redshift Serverless Usage Limit: %s", d.Id()) | ||
_, err := conn.DeleteUsageLimit(&redshiftserverless.DeleteUsageLimitInput{ | ||
UsageLimitId: aws.String(d.Id()), | ||
}) | ||
|
||
if tfawserr.ErrCodeEquals(err, redshiftserverless.ErrCodeResourceNotFoundException) { | ||
return nil | ||
} | ||
|
||
if err != nil { | ||
return fmt.Errorf("error deleting Redshift Serverless Usage Limit (%s): %w", d.Id(), err) | ||
} | ||
|
||
return nil | ||
} |
136 changes: 136 additions & 0 deletions
136
internal/service/redshiftserverless/usage_limit_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,136 @@ | ||
package redshiftserverless_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/aws/aws-sdk-go/service/redshiftserverless" | ||
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" | ||
tfredshiftserverless "github.com/hashicorp/terraform-provider-aws/internal/service/redshiftserverless" | ||
"github.com/hashicorp/terraform-provider-aws/internal/tfresource" | ||
) | ||
|
||
func TestAccRedshiftServerlessUsageLimit_basic(t *testing.T) { | ||
resourceName := "aws_redshiftserverless_usage_limit.test" | ||
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { acctest.PreCheck(t) }, | ||
ErrorCheck: acctest.ErrorCheck(t, redshiftserverless.EndpointsID), | ||
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, | ||
CheckDestroy: testAccCheckUsageLimitDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccUsageLimitConfig_basic(rName, 60), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckUsageLimitExists(resourceName), | ||
resource.TestCheckResourceAttrPair(resourceName, "resource_arn", "aws_redshiftserverless_workgroup.test", "arn"), | ||
resource.TestCheckResourceAttr(resourceName, "amount", "60"), | ||
resource.TestCheckResourceAttr(resourceName, "usage_type", "serverless-compute"), | ||
resource.TestCheckResourceAttr(resourceName, "breach_action", "log"), | ||
resource.TestCheckResourceAttr(resourceName, "period", "monthly"), | ||
), | ||
}, | ||
{ | ||
ResourceName: resourceName, | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
}, | ||
{ | ||
Config: testAccUsageLimitConfig_basic(rName, 120), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckUsageLimitExists(resourceName), | ||
resource.TestCheckResourceAttrPair(resourceName, "resource_arn", "aws_redshiftserverless_workgroup.test", "arn"), | ||
resource.TestCheckResourceAttr(resourceName, "amount", "120"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccRedshiftServerlessUsageLimit_disappears(t *testing.T) { | ||
resourceName := "aws_redshiftserverless_usage_limit.test" | ||
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { acctest.PreCheck(t) }, | ||
ErrorCheck: acctest.ErrorCheck(t, redshiftserverless.EndpointsID), | ||
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, | ||
CheckDestroy: testAccCheckUsageLimitDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccUsageLimitConfig_basic(rName, 60), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckUsageLimitExists(resourceName), | ||
acctest.CheckResourceDisappears(acctest.Provider, tfredshiftserverless.ResourceUsageLimit(), resourceName), | ||
), | ||
ExpectNonEmptyPlan: true, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCheckUsageLimitDestroy(s *terraform.State) error { | ||
conn := acctest.Provider.Meta().(*conns.AWSClient).RedshiftServerlessConn | ||
|
||
for _, rs := range s.RootModule().Resources { | ||
if rs.Type != "aws_redshiftserverless_usage_limit" { | ||
continue | ||
} | ||
_, err := tfredshiftserverless.FindUsageLimitByName(conn, rs.Primary.ID) | ||
|
||
if tfresource.NotFound(err) { | ||
continue | ||
} | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
return fmt.Errorf("Redshift Serverless Usage Limit %s still exists", rs.Primary.ID) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func testAccCheckUsageLimitExists(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("Redshift Serverless Usage Limit is not set") | ||
} | ||
|
||
conn := acctest.Provider.Meta().(*conns.AWSClient).RedshiftServerlessConn | ||
|
||
_, err := tfredshiftserverless.FindUsageLimitByName(conn, rs.Primary.ID) | ||
|
||
return err | ||
} | ||
} | ||
|
||
func testAccUsageLimitConfig_basic(rName string, amount int) string { | ||
return fmt.Sprintf(` | ||
resource "aws_redshiftserverless_namespace" "test" { | ||
namespace_name = %[1]q | ||
} | ||
resource "aws_redshiftserverless_workgroup" "test" { | ||
namespace_name = aws_redshiftserverless_namespace.test.namespace_name | ||
workgroup_name = %[1]q | ||
} | ||
resource "aws_redshiftserverless_usage_limit" "test" { | ||
resource_arn = aws_redshiftserverless_workgroup.test.arn | ||
usage_type = "serverless-compute" | ||
amount = %[2]d | ||
} | ||
`, rName, amount) | ||
} |
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.