Skip to content

Commit

Permalink
Moves common code setting values from cache cluster
Browse files Browse the repository at this point in the history
  • Loading branch information
gdavison committed Apr 23, 2021
1 parent 6f32ea2 commit dd525b3
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 45 deletions.
68 changes: 47 additions & 21 deletions aws/resource_aws_elasticache_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,20 +455,12 @@ func resourceAwsElasticacheClusterRead(d *schema.ResourceData, meta interface{})
}

d.Set("cluster_id", c.CacheClusterId)
d.Set("node_type", c.CacheNodeType)
d.Set("num_cache_nodes", c.NumCacheNodes)
d.Set("engine", c.Engine)

engineVersion, err := gversion.NewVersion(aws.StringValue(c.EngineVersion))
if err != nil {
return fmt.Errorf("error reading ElastiCache Cache Cluster (%s) engine version: %w", d.Id(), err)
}
if engineVersion.Segments()[0] < 6 {
d.Set("engine_version", engineVersion.String())
} else {
d.Set("engine_version", fmt.Sprintf("%d.x", engineVersion.Segments()[0]))
if err := elasticacheSetResourceDataFromCacheCluster(d, c); err != nil {
return err
}
d.Set("engine_version_actual", engineVersion.String())

d.Set("num_cache_nodes", c.NumCacheNodes)

if c.ConfigurationEndpoint != nil {
d.Set("port", c.ConfigurationEndpoint.Port)
Expand All @@ -482,15 +474,6 @@ func resourceAwsElasticacheClusterRead(d *schema.ResourceData, meta interface{})
d.Set("replication_group_id", c.ReplicationGroupId)
}

d.Set("subnet_group_name", c.CacheSubnetGroupName)
d.Set("security_group_names", flattenElastiCacheSecurityGroupNames(c.CacheSecurityGroups))
d.Set("security_group_ids", flattenElastiCacheSecurityGroupIds(c.SecurityGroups))
if c.CacheParameterGroup != nil {
d.Set("parameter_group_name", c.CacheParameterGroup.CacheParameterGroupName)
}
d.Set("maintenance_window", c.PreferredMaintenanceWindow)
d.Set("snapshot_window", c.SnapshotWindow)
d.Set("snapshot_retention_limit", c.SnapshotRetentionLimit)
if c.NotificationConfiguration != nil {
if *c.NotificationConfiguration.TopicStatus == "active" {
d.Set("notification_topic_arn", c.NotificationConfiguration.TopicArn)
Expand Down Expand Up @@ -529,6 +512,49 @@ func resourceAwsElasticacheClusterRead(d *schema.ResourceData, meta interface{})
return nil
}

func elasticacheSetResourceDataFromCacheCluster(d *schema.ResourceData, c *elasticache.CacheCluster) error {
d.Set("node_type", c.CacheNodeType)

d.Set("engine", c.Engine)
if err := elasticacheSetResourceDataEngineVersionFromCacheCluster(d, c); err != nil {
return err
}

d.Set("subnet_group_name", c.CacheSubnetGroupName)
if err := d.Set("security_group_names", flattenElastiCacheSecurityGroupNames(c.CacheSecurityGroups)); err != nil {
return fmt.Errorf("error setting security_group_names: %w", err)
}
if err := d.Set("security_group_ids", flattenElastiCacheSecurityGroupIds(c.SecurityGroups)); err != nil {
return fmt.Errorf("error setting security_group_ids: %w", err)
}

if c.CacheParameterGroup != nil {
d.Set("parameter_group_name", c.CacheParameterGroup.CacheParameterGroupName)
}

d.Set("maintenance_window", c.PreferredMaintenanceWindow)

d.Set("snapshot_window", c.SnapshotWindow)
d.Set("snapshot_retention_limit", c.SnapshotRetentionLimit)

return nil
}

func elasticacheSetResourceDataEngineVersionFromCacheCluster(d *schema.ResourceData, c *elasticache.CacheCluster) error {
engineVersion, err := gversion.NewVersion(aws.StringValue(c.EngineVersion))
if err != nil {
return fmt.Errorf("error reading ElastiCache Cache Cluster (%s) engine version: %w", d.Id(), err)
}
if engineVersion.Segments()[0] < 6 {
d.Set("engine_version", engineVersion.String())
} else {
d.Set("engine_version", fmt.Sprintf("%d.x", engineVersion.Segments()[0]))
}
d.Set("engine_version_actual", engineVersion.String())

return nil
}

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

Expand Down
26 changes: 2 additions & 24 deletions aws/resource_aws_elasticache_replication_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/service/elasticache"
"github.com/hashicorp/aws-sdk-go-base/tfawserr"
gversion "github.com/hashicorp/go-version"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -530,32 +529,11 @@ func resourceAwsElasticacheReplicationGroupRead(d *schema.ResourceData, meta int
}

c := res.CacheClusters[0]
d.Set("node_type", c.CacheNodeType)
d.Set("engine", c.Engine)

engineVersion, err := gversion.NewVersion(aws.StringValue(c.EngineVersion))
if err != nil {
return fmt.Errorf("error reading ElastiCache Cache Cluster (%s) engine version: %w", d.Id(), err)
}
if engineVersion.Segments()[0] < 6 {
d.Set("engine_version", engineVersion.String())
} else {
d.Set("engine_version", fmt.Sprintf("%d.x", engineVersion.Segments()[0]))
}
d.Set("engine_version_actual", engineVersion.String())

d.Set("subnet_group_name", c.CacheSubnetGroupName)
d.Set("security_group_names", flattenElastiCacheSecurityGroupNames(c.CacheSecurityGroups))
d.Set("security_group_ids", flattenElastiCacheSecurityGroupIds(c.SecurityGroups))

if c.CacheParameterGroup != nil {
d.Set("parameter_group_name", c.CacheParameterGroup.CacheParameterGroupName)
if err := elasticacheSetResourceDataFromCacheCluster(d, c); err != nil {
return err
}

d.Set("maintenance_window", c.PreferredMaintenanceWindow)
d.Set("snapshot_window", rgp.SnapshotWindow)
d.Set("snapshot_retention_limit", rgp.SnapshotRetentionLimit)

if rgp.ConfigurationEndpoint != nil {
d.Set("port", rgp.ConfigurationEndpoint.Port)
d.Set("configuration_endpoint_address", rgp.ConfigurationEndpoint.Address)
Expand Down

0 comments on commit dd525b3

Please sign in to comment.