Merge pull request #7538 from hashicorp/import-aws-sqs
provider/aws: Support Import `aws_sqs_queue`
This commit is contained in:
commit
082c4fc2c8
|
@ -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"},
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
|
@ -5,10 +5,13 @@ import (
|
|||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/url"
|
||||
"strconv"
|
||||
|
||||
"github.com/hashicorp/terraform/helper/schema"
|
||||
|
||||
"strings"
|
||||
|
||||
"github.com/aws/aws-sdk-go/aws"
|
||||
"github.com/aws/aws-sdk-go/aws/awserr"
|
||||
"github.com/aws/aws-sdk-go/service/sqs"
|
||||
|
@ -34,6 +37,9 @@ func resourceAwsSqsQueue() *schema.Resource {
|
|||
Read: resourceAwsSqsQueueRead,
|
||||
Update: resourceAwsSqsQueueUpdate,
|
||||
Delete: resourceAwsSqsQueueDelete,
|
||||
Importer: &schema.ResourceImporter{
|
||||
State: schema.ImportStatePassthrough,
|
||||
},
|
||||
|
||||
Schema: map[string]*schema.Schema{
|
||||
"name": &schema.Schema{
|
||||
|
@ -189,6 +195,12 @@ func resourceAwsSqsQueueRead(d *schema.ResourceData, meta interface{}) error {
|
|||
return err
|
||||
}
|
||||
|
||||
name, err := extractNameFromSqsQueueUrl(d.Id())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
d.Set("name", name)
|
||||
|
||||
if attributeOutput.Attributes != nil && len(attributeOutput.Attributes) > 0 {
|
||||
attrmap := attributeOutput.Attributes
|
||||
resource := *resourceAwsSqsQueue()
|
||||
|
@ -225,3 +237,18 @@ func resourceAwsSqsQueueDelete(d *schema.ResourceData, meta interface{}) error {
|
|||
}
|
||||
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
|
||||
|
||||
}
|
||||
|
|
|
@ -13,13 +13,14 @@ import (
|
|||
)
|
||||
|
||||
func TestAccAWSSQSQueue_basic(t *testing.T) {
|
||||
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,
|
||||
Config: testAccAWSSQSConfigWithDefaults(queueName),
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
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" {
|
||||
name = "test-sqs-queue-with-defaults"
|
||||
name = "%s"
|
||||
}
|
||||
`, r)
|
||||
}
|
||||
`
|
||||
|
||||
const testAccAWSSQSConfigWithOverrides = `
|
||||
resource "aws_sqs_queue" "queue-with-overrides" {
|
||||
|
|
Loading…
Reference in New Issue