forked from hashicorp/terraform-provider-aws
/
data_source_aws_elasticache_replication_group.go
122 lines (115 loc) · 3.48 KB
/
data_source_aws_elasticache_replication_group.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package aws
import (
"fmt"
"log"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/elasticache"
"github.com/hashicorp/terraform/helper/schema"
)
func dataSourceAwsElasticacheReplicationGroup() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsElasticacheReplicationGroupRead,
Schema: map[string]*schema.Schema{
"replication_group_id": {
Type: schema.TypeString,
Required: true,
},
"replication_group_description": {
Type: schema.TypeString,
Computed: true,
},
"auth_token_enabled": {
Type: schema.TypeBool,
Computed: true,
},
"automatic_failover_enabled": {
Type: schema.TypeBool,
Computed: true,
},
"port": {
Type: schema.TypeInt,
Computed: true,
},
"configuration_endpoint_address": {
Type: schema.TypeString,
Computed: true,
},
"primary_endpoint_address": {
Type: schema.TypeString,
Computed: true,
},
"number_cache_clusters": {
Type: schema.TypeInt,
Computed: true,
},
"member_clusters": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},
"node_type": {
Type: schema.TypeString,
Computed: true,
},
"snapshot_window": {
Type: schema.TypeString,
Computed: true,
},
"snapshot_retention_limit": {
Type: schema.TypeInt,
Computed: true,
},
},
}
}
func dataSourceAwsElasticacheReplicationGroupRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).elasticacheconn
input := &elasticache.DescribeReplicationGroupsInput{
ReplicationGroupId: aws.String(d.Get("replication_group_id").(string)),
}
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 rg == nil {
return fmt.Errorf("Elasticache Replication Group (%s) not found", d.Get("replication_group_id").(string))
}
d.SetId(*rg.ReplicationGroupId)
d.Set("replication_group_description", rg.Description)
d.Set("auth_token_enabled", rg.AuthTokenEnabled)
if rg.AutomaticFailover != nil {
switch *rg.AutomaticFailover {
case elasticache.AutomaticFailoverStatusDisabled, elasticache.AutomaticFailoverStatusDisabling:
d.Set("automatic_failover_enabled", false)
case elasticache.AutomaticFailoverStatusEnabled, elasticache.AutomaticFailoverStatusEnabling:
d.Set("automatic_failover_enabled", true)
}
}
if rg.ConfigurationEndpoint != nil {
d.Set("port", rg.ConfigurationEndpoint.Port)
d.Set("configuration_endpoint_address", rg.ConfigurationEndpoint.Address)
} 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))
}
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)
}
d.Set("node_type", rg.CacheNodeType)
d.Set("snapshot_window", rg.SnapshotWindow)
d.Set("snapshot_retention_limit", rg.SnapshotRetentionLimit)
return nil
}