-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
provider/aws: Add aws_elasticsearch_domain_policy
- Loading branch information
1 parent
09d6d2c
commit bc5b29f
Showing
6 changed files
with
240 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
builtin/providers/aws/resource_aws_elasticsearch_domain_policy.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"time" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
elasticsearch "github.com/aws/aws-sdk-go/service/elasticsearchservice" | ||
"github.com/hashicorp/terraform/helper/resource" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
) | ||
|
||
func resourceAwsElasticSearchDomainPolicy() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceAwsElasticSearchDomainPolicyUpsert, | ||
Read: resourceAwsElasticSearchDomainPolicyRead, | ||
Update: resourceAwsElasticSearchDomainPolicyUpsert, | ||
Delete: resourceAwsElasticSearchDomainPolicyDelete, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"domain_name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"access_policies": { | ||
Type: schema.TypeString, | ||
StateFunc: normalizeJson, | ||
Required: true, | ||
DiffSuppressFunc: suppressEquivalentAwsPolicyDiffs, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceAwsElasticSearchDomainPolicyRead(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).esconn | ||
|
||
out, err := conn.DescribeElasticsearchDomain(&elasticsearch.DescribeElasticsearchDomainInput{ | ||
DomainName: aws.String(d.Get("domain_name").(string)), | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
log.Printf("[DEBUG] Received ElasticSearch domain: %s", out) | ||
|
||
ds := out.DomainStatus | ||
|
||
if ds.AccessPolicies != nil && *ds.AccessPolicies != "" { | ||
d.Set("access_policies", normalizeJson(*ds.AccessPolicies)) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func resourceAwsElasticSearchDomainPolicyUpsert(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).esconn | ||
domainName := d.Get("domain_name").(string) | ||
_, err := conn.UpdateElasticsearchDomainConfig(&elasticsearch.UpdateElasticsearchDomainConfigInput{ | ||
DomainName: aws.String(domainName), | ||
AccessPolicies: aws.String(d.Get("access_policies").(string)), | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
d.SetId("esd-policy-" + domainName) | ||
|
||
err = resource.Retry(50*time.Minute, func() *resource.RetryError { | ||
out, err := conn.DescribeElasticsearchDomain(&elasticsearch.DescribeElasticsearchDomainInput{ | ||
DomainName: aws.String(d.Get("domain_name").(string)), | ||
}) | ||
if err != nil { | ||
return resource.NonRetryableError(err) | ||
} | ||
|
||
if *out.DomainStatus.Processing == false { | ||
return nil | ||
} | ||
|
||
return resource.RetryableError( | ||
fmt.Errorf("%q: Timeout while waiting for changes to be processed", d.Id())) | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return resourceAwsElasticSearchDomainPolicyRead(d, meta) | ||
} | ||
|
||
func resourceAwsElasticSearchDomainPolicyDelete(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).esconn | ||
|
||
_, err := conn.UpdateElasticsearchDomainConfig(&elasticsearch.UpdateElasticsearchDomainConfigInput{ | ||
DomainName: aws.String(d.Get("domain_name").(string)), | ||
AccessPolicies: aws.String(""), | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
log.Printf("[DEBUG] Waiting for ElasticSearch domain policy %q to be deleted", d.Get("domain_name").(string)) | ||
err = resource.Retry(60*time.Minute, func() *resource.RetryError { | ||
out, err := conn.DescribeElasticsearchDomain(&elasticsearch.DescribeElasticsearchDomainInput{ | ||
DomainName: aws.String(d.Get("domain_name").(string)), | ||
}) | ||
if err != nil { | ||
return resource.NonRetryableError(err) | ||
} | ||
|
||
if *out.DomainStatus.Processing == false { | ||
return nil | ||
} | ||
|
||
return resource.RetryableError( | ||
fmt.Errorf("%q: Timeout while waiting for policy to be deleted", d.Id())) | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
d.SetId("") | ||
return nil | ||
} |
62 changes: 62 additions & 0 deletions
62
builtin/providers/aws/resource_aws_elasticsearch_domain_policy_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"testing" | ||
|
||
elasticsearch "github.com/aws/aws-sdk-go/service/elasticsearchservice" | ||
"github.com/hashicorp/terraform/helper/acctest" | ||
"github.com/hashicorp/terraform/helper/resource" | ||
) | ||
|
||
func TestAccAWSElasticSearchDomainPolicy_basic(t *testing.T) { | ||
var domain elasticsearch.ElasticsearchDomainStatus | ||
ri := acctest.RandInt() | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckESDomainDestroy, | ||
Steps: []resource.TestStep{ | ||
resource.TestStep{ | ||
Config: testAccESDomainPolicyConfig(ri), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckESDomainExists("aws_elasticsearch_domain.example", &domain), | ||
resource.TestCheckResourceAttr("aws_elasticsearch_domain.example", "elasticsearch_version", "2.3"), | ||
resource.TestMatchResourceAttr("aws_elasticsearch_domain_policy.main", "access_policies", | ||
regexp.MustCompile("^{\"Statement\":.+")), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccESDomainPolicyConfig(randInt int) string { | ||
return fmt.Sprintf(` | ||
resource "aws_elasticsearch_domain" "example" { | ||
domain_name = "tf-test-%d" | ||
elasticsearch_version = "2.3" | ||
} | ||
resource "aws_elasticsearch_domain_policy" "main" { | ||
domain_name = "${aws_elasticsearch_domain.example.domain_name}" | ||
access_policies = <<POLICIES | ||
{ | ||
"Version": "2012-10-17", | ||
"Statement": [ | ||
{ | ||
"Action": "es:*", | ||
"Principal": "*", | ||
"Effect": "Allow", | ||
"Condition": { | ||
"IpAddress": {"aws:SourceIp": "127.0.0.1/32"} | ||
}, | ||
"Resource": "${aws_elasticsearch_domain.example.arn}" | ||
} | ||
] | ||
} | ||
POLICIES | ||
} | ||
`, randInt) | ||
} |
47 changes: 47 additions & 0 deletions
47
website/source/docs/providers/aws/r/elasticsearch_domain_policy.html.markdown
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
--- | ||
layout: "aws" | ||
page_title: "AWS: aws_elasticsearch_domain" | ||
sidebar_current: "docs-aws-resource-elasticsearch-domain" | ||
description: |- | ||
Provides an ElasticSearch Domain. | ||
--- | ||
|
||
# aws\_elasticsearch\_domain\_policy | ||
|
||
Allows setting policy to an ElasticSearch domain while referencing domain attributes (e.g. ARN) | ||
|
||
## Example Usage | ||
|
||
``` | ||
resource "aws_elasticsearch_domain" "example" { | ||
domain_name = "tf-test" | ||
elasticsearch_version = "2.3" | ||
} | ||
resource "aws_elasticsearch_domain_policy" "main" { | ||
domain_name = "${aws_elasticsearch_domain.example.domain_name}" | ||
access_policies = <<POLICIES | ||
{ | ||
"Version": "2012-10-17", | ||
"Statement": [ | ||
{ | ||
"Action": "es:*", | ||
"Principal": "*", | ||
"Effect": "Allow", | ||
"Condition": { | ||
"IpAddress": {"aws:SourceIp": "127.0.0.1/32"} | ||
}, | ||
"Resource": "${aws_elasticsearch_domain.example.arn}" | ||
} | ||
] | ||
} | ||
POLICIES | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
The following arguments are supported: | ||
|
||
* `domain_name` - (Required) Name of the domain. | ||
* `access_policies` - (Optional) IAM policy document specifying the access policies for the domain |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters