github.com/koding/terraform@v0.6.4-0.20170608090606-5d7e0339779d/builtin/providers/aws/data_source_aws_elasticache_cluster.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"strings"
     7  
     8  	"github.com/aws/aws-sdk-go/aws"
     9  	"github.com/aws/aws-sdk-go/service/elasticache"
    10  	"github.com/hashicorp/terraform/helper/schema"
    11  )
    12  
    13  func dataSourceAwsElastiCacheCluster() *schema.Resource {
    14  	return &schema.Resource{
    15  		Read: dataSourceAwsElastiCacheClusterRead,
    16  
    17  		Schema: map[string]*schema.Schema{
    18  			"cluster_id": {
    19  				Type:     schema.TypeString,
    20  				Required: true,
    21  				ForceNew: true,
    22  				StateFunc: func(v interface{}) string {
    23  					value := v.(string)
    24  					return strings.ToLower(value)
    25  				},
    26  			},
    27  
    28  			"node_type": {
    29  				Type:     schema.TypeString,
    30  				Computed: true,
    31  			},
    32  
    33  			"num_cache_nodes": {
    34  				Type:     schema.TypeInt,
    35  				Computed: true,
    36  			},
    37  
    38  			"subnet_group_name": {
    39  				Type:     schema.TypeString,
    40  				Computed: true,
    41  			},
    42  
    43  			"engine": {
    44  				Type:     schema.TypeString,
    45  				Computed: true,
    46  			},
    47  
    48  			"engine_version": {
    49  				Type:     schema.TypeString,
    50  				Computed: true,
    51  			},
    52  
    53  			"parameter_group_name": {
    54  				Type:     schema.TypeString,
    55  				Computed: true,
    56  			},
    57  
    58  			"replication_group_id": {
    59  				Type:     schema.TypeString,
    60  				Computed: true,
    61  			},
    62  
    63  			"security_group_names": {
    64  				Type:     schema.TypeSet,
    65  				Computed: true,
    66  				Elem:     &schema.Schema{Type: schema.TypeString},
    67  				Set:      schema.HashString,
    68  			},
    69  
    70  			"security_group_ids": {
    71  				Type:     schema.TypeSet,
    72  				Computed: true,
    73  				Elem:     &schema.Schema{Type: schema.TypeString},
    74  				Set:      schema.HashString,
    75  			},
    76  
    77  			"maintenance_window": {
    78  				Type:     schema.TypeString,
    79  				Computed: true,
    80  			},
    81  
    82  			"snapshot_window": {
    83  				Type:     schema.TypeString,
    84  				Computed: true,
    85  			},
    86  
    87  			"snapshot_retention_limit": {
    88  				Type:     schema.TypeInt,
    89  				Computed: true,
    90  			},
    91  
    92  			"availability_zone": {
    93  				Type:     schema.TypeString,
    94  				Computed: true,
    95  			},
    96  
    97  			"notification_topic_arn": {
    98  				Type:     schema.TypeString,
    99  				Computed: true,
   100  			},
   101  
   102  			"port": {
   103  				Type:     schema.TypeInt,
   104  				Computed: true,
   105  			},
   106  
   107  			"configuration_endpoint": {
   108  				Type:     schema.TypeString,
   109  				Computed: true,
   110  			},
   111  
   112  			"cluster_address": {
   113  				Type:     schema.TypeString,
   114  				Computed: true,
   115  			},
   116  
   117  			"arn": {
   118  				Type:     schema.TypeString,
   119  				Computed: true,
   120  			},
   121  
   122  			"cache_nodes": {
   123  				Type:     schema.TypeList,
   124  				Computed: true,
   125  				Elem: &schema.Resource{
   126  					Schema: map[string]*schema.Schema{
   127  						"id": {
   128  							Type:     schema.TypeString,
   129  							Computed: true,
   130  						},
   131  						"address": {
   132  							Type:     schema.TypeString,
   133  							Computed: true,
   134  						},
   135  						"port": {
   136  							Type:     schema.TypeInt,
   137  							Computed: true,
   138  						},
   139  						"availability_zone": {
   140  							Type:     schema.TypeString,
   141  							Computed: true,
   142  						},
   143  					},
   144  				},
   145  			},
   146  
   147  			"tags": tagsSchemaComputed(),
   148  		},
   149  	}
   150  }
   151  
   152  func dataSourceAwsElastiCacheClusterRead(d *schema.ResourceData, meta interface{}) error {
   153  	conn := meta.(*AWSClient).elasticacheconn
   154  
   155  	req := &elasticache.DescribeCacheClustersInput{
   156  		CacheClusterId:    aws.String(d.Get("cluster_id").(string)),
   157  		ShowCacheNodeInfo: aws.Bool(true),
   158  	}
   159  
   160  	resp, err := conn.DescribeCacheClusters(req)
   161  	if err != nil {
   162  		return err
   163  	}
   164  
   165  	if len(resp.CacheClusters) < 1 {
   166  		return fmt.Errorf("Your query returned no results. Please change your search criteria and try again.")
   167  	}
   168  	if len(resp.CacheClusters) > 1 {
   169  		return fmt.Errorf("Your query returned more than one result. Please try a more specific search criteria.")
   170  	}
   171  
   172  	cluster := resp.CacheClusters[0]
   173  
   174  	d.SetId(*cluster.CacheClusterId)
   175  
   176  	d.Set("cluster_id", cluster.CacheClusterId)
   177  	d.Set("node_type", cluster.CacheNodeType)
   178  	d.Set("num_cache_nodes", cluster.NumCacheNodes)
   179  	d.Set("subnet_group_name", cluster.CacheSubnetGroupName)
   180  	d.Set("engine", cluster.Engine)
   181  	d.Set("engine_version", cluster.EngineVersion)
   182  	d.Set("security_group_names", flattenElastiCacheSecurityGroupNames(cluster.CacheSecurityGroups))
   183  	d.Set("security_group_ids", flattenElastiCacheSecurityGroupIds(cluster.SecurityGroups))
   184  
   185  	if cluster.CacheParameterGroup != nil {
   186  		d.Set("parameter_group_name", cluster.CacheParameterGroup.CacheParameterGroupName)
   187  	}
   188  
   189  	if cluster.ReplicationGroupId != nil {
   190  		d.Set("replication_group_id", cluster.ReplicationGroupId)
   191  	}
   192  
   193  	d.Set("maintenance_window", cluster.PreferredMaintenanceWindow)
   194  	d.Set("snapshot_window", cluster.SnapshotWindow)
   195  	d.Set("snapshot_retention_limit", cluster.SnapshotRetentionLimit)
   196  	d.Set("availability_zone", cluster.PreferredAvailabilityZone)
   197  
   198  	if cluster.NotificationConfiguration != nil {
   199  		if *cluster.NotificationConfiguration.TopicStatus == "active" {
   200  			d.Set("notification_topic_arn", cluster.NotificationConfiguration.TopicArn)
   201  		}
   202  	}
   203  
   204  	if cluster.ConfigurationEndpoint != nil {
   205  		d.Set("port", cluster.ConfigurationEndpoint.Port)
   206  		d.Set("configuration_endpoint", aws.String(fmt.Sprintf("%s:%d", *cluster.ConfigurationEndpoint.Address, *cluster.ConfigurationEndpoint.Port)))
   207  		d.Set("cluster_address", aws.String(fmt.Sprintf("%s", *cluster.ConfigurationEndpoint.Address)))
   208  	}
   209  
   210  	if err := setCacheNodeData(d, cluster); err != nil {
   211  		return err
   212  	}
   213  
   214  	arn, err := buildECARN(d.Id(), meta.(*AWSClient).partition, meta.(*AWSClient).accountid, meta.(*AWSClient).region)
   215  	if err != nil {
   216  		log.Printf("[DEBUG] Error building ARN for ElastiCache Cluster %s", *cluster.CacheClusterId)
   217  	}
   218  	d.Set("arn", arn)
   219  
   220  	tagResp, err := conn.ListTagsForResource(&elasticache.ListTagsForResourceInput{
   221  		ResourceName: aws.String(arn),
   222  	})
   223  
   224  	if err != nil {
   225  		log.Printf("[DEBUG] Error retrieving tags for ARN: %s", arn)
   226  	}
   227  
   228  	var et []*elasticache.Tag
   229  	if len(tagResp.TagList) > 0 {
   230  		et = tagResp.TagList
   231  	}
   232  	d.Set("tags", tagsToMapEC(et))
   233  
   234  	return nil
   235  
   236  }