Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

provider/aws: Fixes elasticsearch error setting policy (#11) #12

Merged
merged 1 commit into from
Jun 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion aws/resource_aws_elasticsearch_domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/hashicorp/errwrap"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"strings"
)

func resourceAwsElasticSearchDomain() *schema.Resource {
Expand Down Expand Up @@ -220,7 +221,24 @@ func resourceAwsElasticSearchDomainCreate(d *schema.ResourceData, meta interface
}

log.Printf("[DEBUG] Creating ElasticSearch domain: %s", input)
out, err := conn.CreateElasticsearchDomain(&input)

// IAM Roles can take some time to propagate if set in AccessPolicies and created in the same terraform
var out *elasticsearch.CreateElasticsearchDomainOutput
err := resource.Retry(30*time.Second, func() *resource.RetryError {
var err error
out, err = conn.CreateElasticsearchDomain(&input)
if err != nil {
if awsErr, ok := err.(awserr.Error); ok {
if awsErr.Code() == "InvalidTypeException" && strings.Contains(awsErr.Message(), "Error setting policy") {
log.Printf("[DEBUG] Retrying creation of ElasticSearch domain %s", *input.DomainName)
return resource.RetryableError(err)
}
}
return resource.NonRetryableError(err)
}
return nil
})

if err != nil {
return err
}
Expand Down
58 changes: 58 additions & 0 deletions aws/resource_aws_elasticsearch_domain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,24 @@ func TestAccAWSElasticSearchDomain_complex(t *testing.T) {
})
}

func TestAccAWSElasticSearchDomain_policy(t *testing.T) {
var domain elasticsearch.ElasticsearchDomainStatus

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckESDomainDestroy,
Steps: []resource.TestStep{
{
Config: testAccESDomainConfigWithPolicy(acctest.RandInt(), acctest.RandInt()),
Check: resource.ComposeTestCheckFunc(
testAccCheckESDomainExists("aws_elasticsearch_domain.example", &domain),
),
},
},
})
}

func TestAccAWSElasticSearchDomain_tags(t *testing.T) {
var domain elasticsearch.ElasticsearchDomainStatus
var td elasticsearch.ListTagsOutput
Expand Down Expand Up @@ -223,6 +241,46 @@ resource "aws_elasticsearch_domain" "example" {
`, randInt)
}

func testAccESDomainConfigWithPolicy(randESId int, randRoleId int) string {
return fmt.Sprintf(`
resource "aws_elasticsearch_domain" "example" {
domain_name = "tf-test-%d"
ebs_options {
ebs_enabled = true
volume_size = 10
}
access_policies = <<CONFIG
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"AWS": "${aws_iam_role.example_role.arn}"
},
"Action": "es:*",
"Resource": "arn:aws:es:*"
}
]
}
CONFIG
}
resource "aws_iam_role" "example_role" {
name = "es-domain-role-%d"
assume_role_policy = "${data.aws_iam_policy_document.instance-assume-role-policy.json}"
}
data "aws_iam_policy_document" "instance-assume-role-policy" {
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["ec2.amazonaws.com"]
}
}
}
`, randESId, randRoleId)
}

func testAccESDomainConfig_complex(randInt int) string {
return fmt.Sprintf(`
resource "aws_elasticsearch_domain" "example" {
Expand Down