|
| 1 | +package aws |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/aws/aws-sdk-go/aws" |
| 7 | + "github.com/aws/aws-sdk-go/service/elasticsearchservice" |
| 8 | + "github.com/hashicorp/terraform/helper/schema" |
| 9 | + "github.com/hashicorp/terraform/helper/structure" |
| 10 | +) |
| 11 | + |
| 12 | +func dataSourceAwsElasticSearchDomain() *schema.Resource { |
| 13 | + return &schema.Resource{ |
| 14 | + Read: dataSourceAwsElasticSearchDomainRead, |
| 15 | + |
| 16 | + Schema: map[string]*schema.Schema{ |
| 17 | + "access_policies": { |
| 18 | + Type: schema.TypeString, |
| 19 | + Computed: true, |
| 20 | + }, |
| 21 | + "advanced_options": { |
| 22 | + Type: schema.TypeMap, |
| 23 | + Computed: true, |
| 24 | + }, |
| 25 | + "domain_name": { |
| 26 | + Type: schema.TypeString, |
| 27 | + Required: true, |
| 28 | + }, |
| 29 | + "arn": { |
| 30 | + Type: schema.TypeString, |
| 31 | + Computed: true, |
| 32 | + }, |
| 33 | + "domain_id": { |
| 34 | + Type: schema.TypeString, |
| 35 | + Computed: true, |
| 36 | + }, |
| 37 | + "endpoint": { |
| 38 | + Type: schema.TypeString, |
| 39 | + Computed: true, |
| 40 | + }, |
| 41 | + "kibana_endpoint": { |
| 42 | + Type: schema.TypeString, |
| 43 | + Computed: true, |
| 44 | + }, |
| 45 | + "ebs_options": { |
| 46 | + Type: schema.TypeList, |
| 47 | + Computed: true, |
| 48 | + Elem: &schema.Resource{ |
| 49 | + Schema: map[string]*schema.Schema{ |
| 50 | + "ebs_enabled": { |
| 51 | + Type: schema.TypeBool, |
| 52 | + Computed: true, |
| 53 | + }, |
| 54 | + "iops": { |
| 55 | + Type: schema.TypeInt, |
| 56 | + Computed: true, |
| 57 | + }, |
| 58 | + "volume_size": { |
| 59 | + Type: schema.TypeInt, |
| 60 | + Computed: true, |
| 61 | + }, |
| 62 | + "volume_type": { |
| 63 | + Type: schema.TypeString, |
| 64 | + Computed: true, |
| 65 | + }, |
| 66 | + }, |
| 67 | + }, |
| 68 | + }, |
| 69 | + "encryption_at_rest": { |
| 70 | + Type: schema.TypeList, |
| 71 | + Computed: true, |
| 72 | + Elem: &schema.Resource{ |
| 73 | + Schema: map[string]*schema.Schema{ |
| 74 | + "enabled": { |
| 75 | + Type: schema.TypeBool, |
| 76 | + Computed: true, |
| 77 | + }, |
| 78 | + "kms_key_id": { |
| 79 | + Type: schema.TypeString, |
| 80 | + Computed: true, |
| 81 | + }, |
| 82 | + }, |
| 83 | + }, |
| 84 | + }, |
| 85 | + "node_to_node_encryption": { |
| 86 | + Type: schema.TypeList, |
| 87 | + Computed: true, |
| 88 | + Elem: &schema.Resource{ |
| 89 | + Schema: map[string]*schema.Schema{ |
| 90 | + "enabled": { |
| 91 | + Type: schema.TypeBool, |
| 92 | + Computed: true, |
| 93 | + }, |
| 94 | + }, |
| 95 | + }, |
| 96 | + }, |
| 97 | + "cluster_config": { |
| 98 | + Type: schema.TypeList, |
| 99 | + Computed: true, |
| 100 | + Elem: &schema.Resource{ |
| 101 | + Schema: map[string]*schema.Schema{ |
| 102 | + "dedicated_master_count": { |
| 103 | + Type: schema.TypeInt, |
| 104 | + Computed: true, |
| 105 | + }, |
| 106 | + "dedicated_master_enabled": { |
| 107 | + Type: schema.TypeBool, |
| 108 | + Computed: true, |
| 109 | + }, |
| 110 | + "dedicated_master_type": { |
| 111 | + Type: schema.TypeString, |
| 112 | + Computed: true, |
| 113 | + }, |
| 114 | + "instance_count": { |
| 115 | + Type: schema.TypeInt, |
| 116 | + Computed: true, |
| 117 | + }, |
| 118 | + "instance_type": { |
| 119 | + Type: schema.TypeString, |
| 120 | + Computed: true, |
| 121 | + }, |
| 122 | + "zone_awareness_config": { |
| 123 | + Type: schema.TypeList, |
| 124 | + Computed: true, |
| 125 | + Elem: &schema.Resource{ |
| 126 | + Schema: map[string]*schema.Schema{ |
| 127 | + "availability_zone_count": { |
| 128 | + Type: schema.TypeInt, |
| 129 | + Computed: true, |
| 130 | + }, |
| 131 | + }, |
| 132 | + }, |
| 133 | + }, |
| 134 | + "zone_awareness_enabled": { |
| 135 | + Type: schema.TypeBool, |
| 136 | + Computed: true, |
| 137 | + }, |
| 138 | + }, |
| 139 | + }, |
| 140 | + }, |
| 141 | + "snapshot_options": { |
| 142 | + Type: schema.TypeList, |
| 143 | + Computed: true, |
| 144 | + Elem: &schema.Resource{ |
| 145 | + Schema: map[string]*schema.Schema{ |
| 146 | + "automated_snapshot_start_hour": { |
| 147 | + Type: schema.TypeInt, |
| 148 | + Computed: true, |
| 149 | + }, |
| 150 | + }, |
| 151 | + }, |
| 152 | + }, |
| 153 | + "vpc_options": { |
| 154 | + Type: schema.TypeList, |
| 155 | + Computed: true, |
| 156 | + Elem: &schema.Resource{ |
| 157 | + Schema: map[string]*schema.Schema{ |
| 158 | + "availability_zones": { |
| 159 | + Type: schema.TypeSet, |
| 160 | + Computed: true, |
| 161 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 162 | + //Set: schema.HashString, |
| 163 | + }, |
| 164 | + "security_group_ids": { |
| 165 | + Type: schema.TypeSet, |
| 166 | + Computed: true, |
| 167 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 168 | + }, |
| 169 | + "subnet_ids": { |
| 170 | + Type: schema.TypeSet, |
| 171 | + Computed: true, |
| 172 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 173 | + }, |
| 174 | + "vpc_id": { |
| 175 | + Type: schema.TypeString, |
| 176 | + Computed: true, |
| 177 | + }, |
| 178 | + }, |
| 179 | + }, |
| 180 | + }, |
| 181 | + "log_publishing_options": { |
| 182 | + Type: schema.TypeSet, |
| 183 | + Computed: true, |
| 184 | + Elem: &schema.Resource{ |
| 185 | + Schema: map[string]*schema.Schema{ |
| 186 | + "log_type": { |
| 187 | + Type: schema.TypeString, |
| 188 | + Computed: true, |
| 189 | + }, |
| 190 | + "cloudwatch_log_group_arn": { |
| 191 | + Type: schema.TypeString, |
| 192 | + Computed: true, |
| 193 | + }, |
| 194 | + "enabled": { |
| 195 | + Type: schema.TypeBool, |
| 196 | + Computed: true, |
| 197 | + }, |
| 198 | + }, |
| 199 | + }, |
| 200 | + }, |
| 201 | + "elasticsearch_version": { |
| 202 | + Type: schema.TypeString, |
| 203 | + Computed: true, |
| 204 | + }, |
| 205 | + "cognito_options": { |
| 206 | + Type: schema.TypeList, |
| 207 | + Computed: true, |
| 208 | + Elem: &schema.Resource{ |
| 209 | + Schema: map[string]*schema.Schema{ |
| 210 | + "enabled": { |
| 211 | + Type: schema.TypeBool, |
| 212 | + Computed: true, |
| 213 | + }, |
| 214 | + "user_pool_id": { |
| 215 | + Type: schema.TypeString, |
| 216 | + Computed: true, |
| 217 | + }, |
| 218 | + "identity_pool_id": { |
| 219 | + Type: schema.TypeString, |
| 220 | + Computed: true, |
| 221 | + }, |
| 222 | + "role_arn": { |
| 223 | + Type: schema.TypeString, |
| 224 | + Computed: true, |
| 225 | + }, |
| 226 | + }, |
| 227 | + }, |
| 228 | + }, |
| 229 | + |
| 230 | + "created": { |
| 231 | + Type: schema.TypeBool, |
| 232 | + Computed: true, |
| 233 | + }, |
| 234 | + "deleted": { |
| 235 | + Type: schema.TypeBool, |
| 236 | + Computed: true, |
| 237 | + }, |
| 238 | + "processing": { |
| 239 | + Type: schema.TypeString, |
| 240 | + Computed: true, |
| 241 | + }, |
| 242 | + |
| 243 | + "tags": tagsSchemaComputed(), |
| 244 | + }, |
| 245 | + } |
| 246 | +} |
| 247 | + |
| 248 | +func dataSourceAwsElasticSearchDomainRead(d *schema.ResourceData, meta interface{}) error { |
| 249 | + esconn := meta.(*AWSClient).esconn |
| 250 | + |
| 251 | + req := &elasticsearchservice.DescribeElasticsearchDomainInput{ |
| 252 | + DomainName: aws.String(d.Get("domain_name").(string)), |
| 253 | + } |
| 254 | + |
| 255 | + resp, err := esconn.DescribeElasticsearchDomain(req) |
| 256 | + if err != nil { |
| 257 | + return fmt.Errorf("error querying elasticsearch_domain: %s", err) |
| 258 | + } |
| 259 | + |
| 260 | + if resp.DomainStatus == nil { |
| 261 | + return fmt.Errorf("your query returned no results") |
| 262 | + } |
| 263 | + |
| 264 | + ds := resp.DomainStatus |
| 265 | + |
| 266 | + d.SetId(*ds.ARN) |
| 267 | + |
| 268 | + if ds.AccessPolicies != nil && *ds.AccessPolicies != "" { |
| 269 | + policies, err := structure.NormalizeJsonString(*ds.AccessPolicies) |
| 270 | + if err != nil { |
| 271 | + return fmt.Errorf("access policies contain an invalid JSON: %s", err) |
| 272 | + } |
| 273 | + d.Set("access_policies", policies) |
| 274 | + } |
| 275 | + |
| 276 | + if err := d.Set("advanced_options", pointersMapToStringList(ds.AdvancedOptions)); err != nil { |
| 277 | + return fmt.Errorf("error setting advanced_options: %s", err) |
| 278 | + } |
| 279 | + |
| 280 | + d.Set("arn", ds.ARN) |
| 281 | + d.Set("domain_id", ds.DomainId) |
| 282 | + d.Set("endpoint", ds.Endpoint) |
| 283 | + d.Set("kibana_endpoint", getKibanaEndpoint(d)) |
| 284 | + |
| 285 | + if err := d.Set("ebs_options", flattenESEBSOptions(ds.EBSOptions)); err != nil { |
| 286 | + return fmt.Errorf("error setting ebs_options: %s", err) |
| 287 | + } |
| 288 | + |
| 289 | + if err := d.Set("encryption_at_rest", flattenESEncryptAtRestOptions(ds.EncryptionAtRestOptions)); err != nil { |
| 290 | + return fmt.Errorf("error setting encryption_at_rest: %s", err) |
| 291 | + } |
| 292 | + |
| 293 | + if err := d.Set("node_to_node_encryption", flattenESNodeToNodeEncryptionOptions(ds.NodeToNodeEncryptionOptions)); err != nil { |
| 294 | + return fmt.Errorf("error setting node_to_node_encryption: %s", err) |
| 295 | + } |
| 296 | + |
| 297 | + if err := d.Set("cluster_config", flattenESClusterConfig(ds.ElasticsearchClusterConfig)); err != nil { |
| 298 | + return fmt.Errorf("error setting cluster_config: %s", err) |
| 299 | + } |
| 300 | + |
| 301 | + if err := d.Set("snapshot_options", flattenESSnapshotOptions(ds.SnapshotOptions)); err != nil { |
| 302 | + return fmt.Errorf("error setting snapshot_options: %s", err) |
| 303 | + } |
| 304 | + |
| 305 | + if ds.VPCOptions != nil { |
| 306 | + if err := d.Set("vpc_options", flattenESVPCDerivedInfo(ds.VPCOptions)); err != nil { |
| 307 | + return fmt.Errorf("error setting vpc_options: %s", err) |
| 308 | + } |
| 309 | + |
| 310 | + endpoints := pointersMapToStringList(ds.Endpoints) |
| 311 | + if err := d.Set("endpoint", endpoints["vpc"]); err != nil { |
| 312 | + return fmt.Errorf("error setting endpoint: %s", err) |
| 313 | + } |
| 314 | + d.Set("kibana_endpoint", getKibanaEndpoint(d)) |
| 315 | + if ds.Endpoint != nil { |
| 316 | + return fmt.Errorf("%q: Elasticsearch domain in VPC expected to have null Endpoint value", d.Id()) |
| 317 | + } |
| 318 | + } else { |
| 319 | + if ds.Endpoint != nil { |
| 320 | + d.Set("endpoint", aws.StringValue(ds.Endpoint)) |
| 321 | + d.Set("kibana_endpoint", getKibanaEndpoint(d)) |
| 322 | + } |
| 323 | + if ds.Endpoints != nil { |
| 324 | + return fmt.Errorf("%q: Elasticsearch domain not in VPC expected to have null Endpoints value", d.Id()) |
| 325 | + } |
| 326 | + } |
| 327 | + |
| 328 | + if ds.LogPublishingOptions != nil { |
| 329 | + m := make([]map[string]interface{}, 0) |
| 330 | + for k, val := range ds.LogPublishingOptions { |
| 331 | + mm := map[string]interface{}{} |
| 332 | + mm["log_type"] = k |
| 333 | + if val.CloudWatchLogsLogGroupArn != nil { |
| 334 | + mm["cloudwatch_log_group_arn"] = aws.StringValue(val.CloudWatchLogsLogGroupArn) |
| 335 | + } |
| 336 | + mm["enabled"] = aws.BoolValue(val.Enabled) |
| 337 | + m = append(m, mm) |
| 338 | + } |
| 339 | + d.Set("log_publishing_options", m) |
| 340 | + } |
| 341 | + |
| 342 | + d.Set("elasticsearch_version", ds.ElasticsearchVersion) |
| 343 | + |
| 344 | + if err := d.Set("cognito_options", flattenESCognitoOptions(ds.CognitoOptions)); err != nil { |
| 345 | + return fmt.Errorf("error setting cognito_options: %s", err) |
| 346 | + } |
| 347 | + |
| 348 | + d.Set("created", ds.Created) |
| 349 | + d.Set("deleted", ds.Deleted) |
| 350 | + |
| 351 | + d.Set("processing", ds.Processing) |
| 352 | + |
| 353 | + tagResp, err := esconn.ListTags(&elasticsearchservice.ListTagsInput{ |
| 354 | + ARN: ds.ARN, |
| 355 | + }) |
| 356 | + |
| 357 | + if err != nil { |
| 358 | + return fmt.Errorf("error retrieving tags for elasticsearch_domain: %s", err) |
| 359 | + } |
| 360 | + |
| 361 | + if err := d.Set("tags", tagsToMapElasticsearchService(tagResp.TagList)); err != nil { |
| 362 | + return fmt.Errorf("error setting tags: %s", err) |
| 363 | + } |
| 364 | + |
| 365 | + return nil |
| 366 | +} |
0 commit comments