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

service/servicediscovery: Return operation ErrorCode and ErrorMessage when Status is FAIL #7118

Merged
merged 1 commit into from
Jan 14, 2019
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
43 changes: 43 additions & 0 deletions aws/resource_aws_service_discovery_private_dns_namespace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package aws

import (
"fmt"
"regexp"
"testing"

"github.com/aws/aws-sdk-go/aws"
Expand Down Expand Up @@ -51,6 +52,26 @@ func TestAccAWSServiceDiscoveryPrivateDnsNamespace_longname(t *testing.T) {
})
}

// This acceptance test ensures we properly send back error messaging. References:
// * https://github.com/terraform-providers/terraform-provider-aws/issues/2830
// * https://github.com/terraform-providers/terraform-provider-aws/issues/5532
func TestAccAWSServiceDiscoveryPrivateDnsNamespace_error_Overlap(t *testing.T) {
rName := acctest.RandString(5) + ".example.com"
subDomain := acctest.RandString(5) + "." + rName

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAwsServiceDiscoveryPrivateDnsNamespaceDestroy,
Steps: []resource.TestStep{
{
Config: testAccServiceDiscoveryPrivateDnsNamespaceConfigOverlapping(rName, subDomain),
ExpectError: regexp.MustCompile(`overlapping name space`),
},
},
})
}

func testAccCheckAwsServiceDiscoveryPrivateDnsNamespaceDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).sdconn

Expand Down Expand Up @@ -101,3 +122,25 @@ resource "aws_service_discovery_private_dns_namespace" "test" {
}
`, rName)
}

func testAccServiceDiscoveryPrivateDnsNamespaceConfigOverlapping(topDomain, subDomain string) string {
return fmt.Sprintf(`
resource "aws_vpc" "test" {
cidr_block = "10.0.0.0/16"
tags = {
Name = "terraform-testacc-service-discovery-private-dns-ns"
}
}

resource "aws_service_discovery_private_dns_namespace" "top" {
name = %q
vpc = "${aws_vpc.test.id}"
}

# Ensure ordering after first namespace
resource "aws_service_discovery_private_dns_namespace" "subdomain" {
name = %q
vpc = "${aws_service_discovery_private_dns_namespace.top.vpc}"
}
`, topDomain, subDomain)
}
16 changes: 14 additions & 2 deletions aws/resource_aws_service_discovery_public_dns_namespace.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package aws

import (
"fmt"
"time"

"github.com/aws/aws-sdk-go/aws"
Expand Down Expand Up @@ -138,9 +139,20 @@ func servicediscoveryOperationRefreshStatusFunc(conn *servicediscovery.ServiceDi
OperationId: aws.String(oid),
}
resp, err := conn.GetOperation(input)

if err != nil {
return nil, "failed", err
return nil, servicediscovery.OperationStatusFail, err
}
return resp, *resp.Operation.Status, nil

// Error messages can also be contained in the response with FAIL status
// "ErrorCode":"CANNOT_CREATE_HOSTED_ZONE",
// "ErrorMessage":"The VPC that you chose, vpc-xxx in region xxx, is already associated with another private hosted zone that has an overlapping name space, xxx.. (Service: AmazonRoute53; Status Code: 400; Error Code: ConflictingDomainExists; Request ID: xxx)"
// "Status":"FAIL",

if aws.StringValue(resp.Operation.Status) == servicediscovery.OperationStatusFail {
return resp, servicediscovery.OperationStatusFail, fmt.Errorf("%s: %s", aws.StringValue(resp.Operation.ErrorCode), aws.StringValue(resp.Operation.ErrorMessage))
}

return resp, aws.StringValue(resp.Operation.Status), nil
}
}