Merge pull request #7538 from hashicorp/import-aws-sqs

provider/aws: Support Import `aws_sqs_queue`
This commit is contained in:
James Nugent 2016-07-14 13:29:19 -06:00 committed by GitHub
commit 082c4fc2c8
3 changed files with 69 additions and 4 deletions

View File

@ -0,0 +1,35 @@
package aws
import (
"testing"
"fmt"
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
)
func TestAccAWSSQSQueue_importBasic(t *testing.T) {
resourceName := "aws_sqs_queue.queue-with-defaults"
queueName := fmt.Sprintf("sqs-queue-%s", acctest.RandString(5))
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSSQSQueueDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccAWSSQSConfigWithDefaults(queueName),
},
resource.TestStep{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
//The name is never returned after the initial create of the queue.
//It is part of the URL and can be split down if needed
//ImportStateVerifyIgnore: []string{"name"},
},
},
})
}

View File

@ -5,10 +5,13 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"log" "log"
"net/url"
"strconv" "strconv"
"github.com/hashicorp/terraform/helper/schema" "github.com/hashicorp/terraform/helper/schema"
"strings"
"github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr" "github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/sqs" "github.com/aws/aws-sdk-go/service/sqs"
@ -34,6 +37,9 @@ func resourceAwsSqsQueue() *schema.Resource {
Read: resourceAwsSqsQueueRead, Read: resourceAwsSqsQueueRead,
Update: resourceAwsSqsQueueUpdate, Update: resourceAwsSqsQueueUpdate,
Delete: resourceAwsSqsQueueDelete, Delete: resourceAwsSqsQueueDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{ Schema: map[string]*schema.Schema{
"name": &schema.Schema{ "name": &schema.Schema{
@ -189,6 +195,12 @@ func resourceAwsSqsQueueRead(d *schema.ResourceData, meta interface{}) error {
return err return err
} }
name, err := extractNameFromSqsQueueUrl(d.Id())
if err != nil {
return err
}
d.Set("name", name)
if attributeOutput.Attributes != nil && len(attributeOutput.Attributes) > 0 { if attributeOutput.Attributes != nil && len(attributeOutput.Attributes) > 0 {
attrmap := attributeOutput.Attributes attrmap := attributeOutput.Attributes
resource := *resourceAwsSqsQueue() resource := *resourceAwsSqsQueue()
@ -225,3 +237,18 @@ func resourceAwsSqsQueueDelete(d *schema.ResourceData, meta interface{}) error {
} }
return nil return nil
} }
func extractNameFromSqsQueueUrl(queue string) (string, error) {
//http://sqs.us-west-2.amazonaws.com/123456789012/queueName
u, err := url.Parse(queue)
if err != nil {
return "", err
}
segments := strings.Split(u.Path, "/")
if len(segments) != 3 {
return "", fmt.Errorf("SQS Url not parsed correctly")
}
return segments[2], nil
}

View File

@ -13,13 +13,14 @@ import (
) )
func TestAccAWSSQSQueue_basic(t *testing.T) { func TestAccAWSSQSQueue_basic(t *testing.T) {
queueName := fmt.Sprintf("sqs-queue-%s", acctest.RandString(5))
resource.Test(t, resource.TestCase{ resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) }, PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders, Providers: testAccProviders,
CheckDestroy: testAccCheckAWSSQSQueueDestroy, CheckDestroy: testAccCheckAWSSQSQueueDestroy,
Steps: []resource.TestStep{ Steps: []resource.TestStep{
resource.TestStep{ resource.TestStep{
Config: testAccAWSSQSConfigWithDefaults, Config: testAccAWSSQSConfigWithDefaults(queueName),
Check: resource.ComposeTestCheckFunc( Check: resource.ComposeTestCheckFunc(
testAccCheckAWSSQSExistsWithDefaults("aws_sqs_queue.queue-with-defaults"), testAccCheckAWSSQSExistsWithDefaults("aws_sqs_queue.queue-with-defaults"),
), ),
@ -194,11 +195,13 @@ func testAccCheckAWSSQSExistsWithOverrides(n string) resource.TestCheckFunc {
} }
} }
const testAccAWSSQSConfigWithDefaults = ` func testAccAWSSQSConfigWithDefaults(r string) string {
return fmt.Sprintf(`
resource "aws_sqs_queue" "queue-with-defaults" { resource "aws_sqs_queue" "queue-with-defaults" {
name = "test-sqs-queue-with-defaults" name = "%s"
}
`, r)
} }
`
const testAccAWSSQSConfigWithOverrides = ` const testAccAWSSQSConfigWithOverrides = `
resource "aws_sqs_queue" "queue-with-overrides" { resource "aws_sqs_queue" "queue-with-overrides" {