Skip to content

Commit

Permalink
Merge pull request #14017 from terraform-providers/td-singular-data-s…
Browse files Browse the repository at this point in the history
…ource-elasticache-rg

data-source/elasticache_replication_group: adjust error formatting and handling
  • Loading branch information
anGie44 authored Jul 21, 2020
2 parents 4a0abae + a77e928 commit 2228798
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 15 deletions.
30 changes: 15 additions & 15 deletions aws/data_source_aws_elasticache_replication_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ func dataSourceAwsElasticacheReplicationGroup() *schema.Resource {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},
"node_type": {
Type: schema.TypeString,
Expand All @@ -69,31 +68,32 @@ func dataSourceAwsElasticacheReplicationGroup() *schema.Resource {

func dataSourceAwsElasticacheReplicationGroupRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).elasticacheconn

groupID := d.Get("replication_group_id").(string)
input := &elasticache.DescribeReplicationGroupsInput{
ReplicationGroupId: aws.String(d.Get("replication_group_id").(string)),
ReplicationGroupId: aws.String(groupID),
}

log.Printf("[DEBUG] Reading ElastiCache Replication Group: %s", input)
resp, err := conn.DescribeReplicationGroups(input)
if err != nil {
return err
}

var rg *elasticache.ReplicationGroup
for _, r := range resp.ReplicationGroups {
if *r.ReplicationGroupId == d.Get("replication_group_id").(string) {
rg = r
if isAWSErr(err, elasticache.ErrCodeReplicationGroupNotFoundFault, "") {
return fmt.Errorf("ElastiCache Replication Group (%s) not found", groupID)
}
return fmt.Errorf("error reading replication group (%s): %w", groupID, err)
}
if rg == nil {
return fmt.Errorf("Elasticache Replication Group (%s) not found", d.Get("replication_group_id").(string))

if resp == nil || len(resp.ReplicationGroups) == 0 {
return fmt.Errorf("error reading replication group (%s): empty output", groupID)
}

d.SetId(*rg.ReplicationGroupId)
rg := resp.ReplicationGroups[0]

d.SetId(aws.StringValue(rg.ReplicationGroupId))
d.Set("replication_group_description", rg.Description)
d.Set("auth_token_enabled", rg.AuthTokenEnabled)
if rg.AutomaticFailover != nil {
switch *rg.AutomaticFailover {
switch aws.StringValue(rg.AutomaticFailover) {
case elasticache.AutomaticFailoverStatusDisabled, elasticache.AutomaticFailoverStatusDisabling:
d.Set("automatic_failover_enabled", false)
case elasticache.AutomaticFailoverStatusEnabled, elasticache.AutomaticFailoverStatusEnabling:
Expand All @@ -106,14 +106,14 @@ func dataSourceAwsElasticacheReplicationGroupRead(d *schema.ResourceData, meta i
} else {
if rg.NodeGroups == nil {
d.SetId("")
return fmt.Errorf("Elasticache Replication Group (%s) doesn't have node groups.", d.Get("replication_group_id").(string))
return fmt.Errorf("Elasticache Replication Group (%s) doesn't have node groups.", aws.StringValue(rg.ReplicationGroupId))
}
d.Set("port", rg.NodeGroups[0].PrimaryEndpoint.Port)
d.Set("primary_endpoint_address", rg.NodeGroups[0].PrimaryEndpoint.Address)
}
d.Set("number_cache_clusters", len(rg.MemberClusters))
if err := d.Set("member_clusters", flattenStringList(rg.MemberClusters)); err != nil {
return fmt.Errorf("error setting member_clusters: %s", err)
return fmt.Errorf("error setting member_clusters: %w", err)
}
d.Set("node_type", rg.CacheNodeType)
d.Set("snapshot_window", rg.SnapshotWindow)
Expand Down
21 changes: 21 additions & 0 deletions aws/data_source_aws_elasticache_replication_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package aws

import (
"fmt"
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
Expand Down Expand Up @@ -61,6 +62,20 @@ func TestAccDataSourceAwsElasticacheReplicationGroup_ClusterMode(t *testing.T) {
})
}

func TestAccDataSourceAwsElasticacheReplicationGroup_NonExistent(t *testing.T) {

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAwsElasticacheReplicationGroupConfig_NonExistent,
ExpectError: regexp.MustCompile(`not found`),
},
},
})
}

func testAccDataSourceAwsElasticacheReplicationGroupConfig_basic(rName string) string {
return fmt.Sprintf(`
data "aws_availability_zones" "available" {
Expand Down Expand Up @@ -109,3 +124,9 @@ data "aws_elasticache_replication_group" "test" {
}
`, rName)
}

const testAccDataSourceAwsElasticacheReplicationGroupConfig_NonExistent = `
data "aws_elasticache_replication_group" "test" {
replication_group_id = "tf-acc-test-nonexistent"
}
`

0 comments on commit 2228798

Please sign in to comment.