Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support replication specification for aws keyspaces #36331

3 changes: 3 additions & 0 deletions .changelog/36331.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/aws_keyspaces_keyspace: Add `replication_specification` argument
```
48 changes: 45 additions & 3 deletions internal/service/keyspaces/keyspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ import (
"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/enum"
"github.com/hashicorp/terraform-provider-aws/internal/errs"
"github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag"
"github.com/hashicorp/terraform-provider-aws/internal/flex"
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
"github.com/hashicorp/terraform-provider-aws/internal/verify"
Expand Down Expand Up @@ -59,6 +61,34 @@ func resourceKeyspace() *schema.Resource {
"The name can have up to 48 characters. It must begin with an alpha-numeric character and can only contain alpha-numeric characters and underscores.",
),
},
"replication_specification": {
Type: schema.TypeList,
Optional: true,
Computed: true,
ForceNew: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"replication_strategy": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
ValidateDiagFunc: enum.Validate[types.Rs](),
},
"region_list": {
Type: schema.TypeSet,
Optional: true,
ForceNew: true,
MaxItems: 6,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: verify.ValidRegionName,
},
},
},
},
},
names.AttrTags: tftags.TagsSchema(),
names.AttrTagsAll: tftags.TagsSchemaComputed(),
},
Expand All @@ -67,7 +97,6 @@ func resourceKeyspace() *schema.Resource {

func resourceKeyspaceCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics

conn := meta.(*conns.AWSClient).KeyspacesClient(ctx)

name := d.Get(names.AttrName).(string)
Expand All @@ -76,6 +105,17 @@ func resourceKeyspaceCreate(ctx context.Context, d *schema.ResourceData, meta in
Tags: getTagsIn(ctx),
}

if v, ok := d.GetOk("replication_specification"); ok && len(v.([]interface{})) > 0 && v.([]interface{})[0] != nil {
tfMap := v.([]interface{})[0].(map[string]interface{})
input.ReplicationSpecification = &types.ReplicationSpecification{
ReplicationStrategy: types.Rs(tfMap["replication_strategy"].(string)),
}

if v, ok := tfMap["region_list"].(*schema.Set); ok && v.Len() > 0 {
input.ReplicationSpecification.RegionList = flex.ExpandStringValueSet(v)
}
}

_, err := conn.CreateKeyspace(ctx, input)

if err != nil {
Expand All @@ -97,7 +137,6 @@ func resourceKeyspaceCreate(ctx context.Context, d *schema.ResourceData, meta in

func resourceKeyspaceRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics

conn := meta.(*conns.AWSClient).KeyspacesClient(ctx)

keyspace, err := findKeyspaceByName(ctx, conn, d.Id())
Expand All @@ -114,6 +153,10 @@ func resourceKeyspaceRead(ctx context.Context, d *schema.ResourceData, meta inte

d.Set(names.AttrARN, keyspace.ResourceArn)
d.Set(names.AttrName, keyspace.KeyspaceName)
d.Set("replication_specification", []interface{}{map[string]interface{}{
"region_list": keyspace.ReplicationRegions,
"replication_strategy": keyspace.ReplicationStrategy,
}})

return diags
}
Expand All @@ -125,7 +168,6 @@ func resourceKeyspaceUpdate(ctx context.Context, d *schema.ResourceData, meta in

func resourceKeyspaceDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics

conn := meta.(*conns.AWSClient).KeyspacesClient(ctx)

log.Printf("[DEBUG] Deleting Keyspaces Keyspace: (%s)", d.Id())
Expand Down
64 changes: 64 additions & 0 deletions internal/service/keyspaces/keyspace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"testing"

"github.com/aws/aws-sdk-go-v2/service/keyspaces/types"
"github.com/hashicorp/aws-sdk-go-base/v2/endpoints"
sdkacctest "github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
Expand Down Expand Up @@ -52,6 +53,44 @@ func TestAccKeyspacesKeyspace_basic(t *testing.T) {
})
}

func TestAccKeyspacesKeyspace_replicationSpecificationMulti(t *testing.T) {
ctx := acctest.Context(t)
rName := "tf_acc_test_" + sdkacctest.RandString(20)
resourceName := "aws_keyspaces_keyspace.test"
region1 := acctest.Region()
region2 := acctest.AlternateRegion()

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t); testAccPreCheck(t) },
ErrorCheck: acctest.ErrorCheck(t, names.KeyspacesServiceID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckKeyspaceDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccKeyspaceConfig_replicationSpecification(rName, string(types.RsSingleRegion)),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckKeyspaceExists(ctx, resourceName),
acctest.CheckResourceAttrRegionalARN(resourceName, "arn", "cassandra", "/keyspace/"+rName+"/"),
resource.TestCheckResourceAttr(resourceName, "name", rName),
resource.TestCheckResourceAttr(resourceName, "tags.%", "0"),
resource.TestCheckResourceAttr(resourceName, "replication_specification.0.replication_strategy", string(types.RsSingleRegion)),
),
},
{
Config: testAccKeyspaceConfig_multiReplicationSpecification(rName, string(types.RsMultiRegion), region1, region2),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckKeyspaceExists(ctx, resourceName),
acctest.CheckResourceAttrRegionalARN(resourceName, "arn", "cassandra", "/keyspace/"+rName+"/"),
resource.TestCheckResourceAttr(resourceName, "name", rName),
resource.TestCheckResourceAttr(resourceName, "tags.%", "0"),
resource.TestCheckResourceAttr(resourceName, "replication_specification.0.replication_strategy", string(types.RsMultiRegion)),
resource.TestCheckResourceAttr(resourceName, "replication_specification.0.region_list.#", "2"),
),
},
},
})
}

func TestAccKeyspacesKeyspace_disappears(t *testing.T) {
ctx := acctest.Context(t)
rName := "tf_acc_test_" + sdkacctest.RandString(20)
Expand Down Expand Up @@ -197,3 +236,28 @@ resource "aws_keyspaces_keyspace" "test" {
}
`, rName, tag1Key, tag1Value, tag2Key, tag2Value)
}

func testAccKeyspaceConfig_replicationSpecification(rName, rSpecification string) string {
return fmt.Sprintf(`
resource "aws_keyspaces_keyspace" "test" {
name = %[1]q

replication_specification {
replication_strategy = %[2]q
}
}
`, rName, rSpecification)
}

func testAccKeyspaceConfig_multiReplicationSpecification(rName, rSpecification, region1, region2 string) string {
return fmt.Sprintf(`
resource "aws_keyspaces_keyspace" "test" {
name = %[1]q

replication_specification {
replication_strategy = %[2]q
region_list = [%[3]q, %[4]q]
}
}
`, rName, rSpecification, region1, region2)
}
7 changes: 0 additions & 7 deletions internal/service/keyspaces/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,6 @@ func resourceTable() *schema.Resource {

func resourceTableCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics

conn := meta.(*conns.AWSClient).KeyspacesClient(ctx)

keyspaceName := d.Get("keyspace_name").(string)
Expand Down Expand Up @@ -366,11 +365,9 @@ func resourceTableCreate(ctx context.Context, d *schema.ResourceData, meta inter

func resourceTableRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics

conn := meta.(*conns.AWSClient).KeyspacesClient(ctx)

keyspaceName, tableName, err := tableParseResourceID(d.Id())

if err != nil {
return sdkdiag.AppendFromErr(diags, err)
}
Expand Down Expand Up @@ -446,11 +443,9 @@ func resourceTableRead(ctx context.Context, d *schema.ResourceData, meta interfa

func resourceTableUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics

conn := meta.(*conns.AWSClient).KeyspacesClient(ctx)

keyspaceName, tableName, err := tableParseResourceID(d.Id())

if err != nil {
return sdkdiag.AppendFromErr(diags, err)
}
Expand Down Expand Up @@ -618,11 +613,9 @@ func resourceTableUpdate(ctx context.Context, d *schema.ResourceData, meta inter

func resourceTableDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics

conn := meta.(*conns.AWSClient).KeyspacesClient(ctx)

keyspaceName, tableName, err := tableParseResourceID(d.Id())

if err != nil {
return sdkdiag.AppendFromErr(diags, err)
}
Expand Down
3 changes: 3 additions & 0 deletions website/docs/r/keyspaces_keyspace.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ The following arguments are required:

The following arguments are optional:

* `replication_specification` - (Optional) The replication specification of the keyspace.
* `region_list` - (Optional) Replication regions. If `replication_strategy` is `MULTI_REGION`, `region_list` requires the current Region and at least one additional AWS Region where the keyspace is going to be replicated in.
* `replication_strategy` - (Required) Replication strategy. Valid values: `SINGLE_REGION` and `MULTI_REGION`.
* `tags` - (Optional) A map of tags to assign to the resource. If configured with a provider [`default_tags` configuration block](https://registry.terraform.io/providers/hashicorp/aws/latest/docs#default_tags-configuration-block) present, tags with matching keys will overwrite those defined at the provider-level.

## Attribute Reference
Expand Down
Loading