github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/aws/resource_aws_elasticsearch_domain_policy.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "log" 6 "time" 7 8 "github.com/aws/aws-sdk-go/aws" 9 "github.com/aws/aws-sdk-go/aws/awserr" 10 elasticsearch "github.com/aws/aws-sdk-go/service/elasticsearchservice" 11 "github.com/hashicorp/terraform/helper/resource" 12 "github.com/hashicorp/terraform/helper/schema" 13 ) 14 15 func resourceAwsElasticSearchDomainPolicy() *schema.Resource { 16 return &schema.Resource{ 17 Create: resourceAwsElasticSearchDomainPolicyUpsert, 18 Read: resourceAwsElasticSearchDomainPolicyRead, 19 Update: resourceAwsElasticSearchDomainPolicyUpsert, 20 Delete: resourceAwsElasticSearchDomainPolicyDelete, 21 22 Schema: map[string]*schema.Schema{ 23 "domain_name": { 24 Type: schema.TypeString, 25 Required: true, 26 }, 27 "access_policies": { 28 Type: schema.TypeString, 29 Required: true, 30 DiffSuppressFunc: suppressEquivalentAwsPolicyDiffs, 31 }, 32 }, 33 } 34 } 35 36 func resourceAwsElasticSearchDomainPolicyRead(d *schema.ResourceData, meta interface{}) error { 37 conn := meta.(*AWSClient).esconn 38 name := d.Get("domain_name").(string) 39 out, err := conn.DescribeElasticsearchDomain(&elasticsearch.DescribeElasticsearchDomainInput{ 40 DomainName: aws.String(name), 41 }) 42 if err != nil { 43 if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "ResourceNotFound" { 44 log.Printf("[WARN] ElasticSearch Domain %q not found, removing", name) 45 d.SetId("") 46 return nil 47 } 48 return err 49 } 50 51 log.Printf("[DEBUG] Received ElasticSearch domain: %s", out) 52 53 ds := out.DomainStatus 54 d.Set("access_policies", ds.AccessPolicies) 55 56 return nil 57 } 58 59 func resourceAwsElasticSearchDomainPolicyUpsert(d *schema.ResourceData, meta interface{}) error { 60 conn := meta.(*AWSClient).esconn 61 domainName := d.Get("domain_name").(string) 62 _, err := conn.UpdateElasticsearchDomainConfig(&elasticsearch.UpdateElasticsearchDomainConfigInput{ 63 DomainName: aws.String(domainName), 64 AccessPolicies: aws.String(d.Get("access_policies").(string)), 65 }) 66 if err != nil { 67 return err 68 } 69 70 d.SetId("esd-policy-" + domainName) 71 72 err = resource.Retry(50*time.Minute, func() *resource.RetryError { 73 out, err := conn.DescribeElasticsearchDomain(&elasticsearch.DescribeElasticsearchDomainInput{ 74 DomainName: aws.String(d.Get("domain_name").(string)), 75 }) 76 if err != nil { 77 return resource.NonRetryableError(err) 78 } 79 80 if *out.DomainStatus.Processing == false { 81 return nil 82 } 83 84 return resource.RetryableError( 85 fmt.Errorf("%q: Timeout while waiting for changes to be processed", d.Id())) 86 }) 87 if err != nil { 88 return err 89 } 90 91 return resourceAwsElasticSearchDomainPolicyRead(d, meta) 92 } 93 94 func resourceAwsElasticSearchDomainPolicyDelete(d *schema.ResourceData, meta interface{}) error { 95 conn := meta.(*AWSClient).esconn 96 97 _, err := conn.UpdateElasticsearchDomainConfig(&elasticsearch.UpdateElasticsearchDomainConfigInput{ 98 DomainName: aws.String(d.Get("domain_name").(string)), 99 AccessPolicies: aws.String(""), 100 }) 101 if err != nil { 102 return err 103 } 104 105 log.Printf("[DEBUG] Waiting for ElasticSearch domain policy %q to be deleted", d.Get("domain_name").(string)) 106 err = resource.Retry(60*time.Minute, func() *resource.RetryError { 107 out, err := conn.DescribeElasticsearchDomain(&elasticsearch.DescribeElasticsearchDomainInput{ 108 DomainName: aws.String(d.Get("domain_name").(string)), 109 }) 110 if err != nil { 111 return resource.NonRetryableError(err) 112 } 113 114 if *out.DomainStatus.Processing == false { 115 return nil 116 } 117 118 return resource.RetryableError( 119 fmt.Errorf("%q: Timeout while waiting for policy to be deleted", d.Id())) 120 }) 121 if err != nil { 122 return err 123 } 124 125 d.SetId("") 126 return nil 127 }