github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/builtin/providers/aws/resource_aws_sqs_queue_policy.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  
     7  	"github.com/aws/aws-sdk-go/aws"
     8  	"github.com/aws/aws-sdk-go/aws/awserr"
     9  	"github.com/aws/aws-sdk-go/service/sqs"
    10  	"github.com/hashicorp/terraform/helper/schema"
    11  )
    12  
    13  func resourceAwsSqsQueuePolicy() *schema.Resource {
    14  	return &schema.Resource{
    15  		Create: resourceAwsSqsQueuePolicyUpsert,
    16  		Read:   resourceAwsSqsQueuePolicyRead,
    17  		Update: resourceAwsSqsQueuePolicyUpsert,
    18  		Delete: resourceAwsSqsQueuePolicyDelete,
    19  
    20  		Schema: map[string]*schema.Schema{
    21  			"queue_url": &schema.Schema{
    22  				Type:     schema.TypeString,
    23  				Required: true,
    24  				ForceNew: true,
    25  			},
    26  
    27  			"policy": &schema.Schema{
    28  				Type:             schema.TypeString,
    29  				Required:         true,
    30  				DiffSuppressFunc: suppressEquivalentAwsPolicyDiffs,
    31  			},
    32  		},
    33  	}
    34  }
    35  
    36  func resourceAwsSqsQueuePolicyUpsert(d *schema.ResourceData, meta interface{}) error {
    37  	conn := meta.(*AWSClient).sqsconn
    38  	url := d.Get("queue_url").(string)
    39  
    40  	_, err := conn.SetQueueAttributes(&sqs.SetQueueAttributesInput{
    41  		QueueUrl: aws.String(url),
    42  		Attributes: aws.StringMap(map[string]string{
    43  			"Policy": d.Get("policy").(string),
    44  		}),
    45  	})
    46  	if err != nil {
    47  		return fmt.Errorf("Error updating SQS attributes: %s", err)
    48  	}
    49  
    50  	d.SetId("sqs-policy-" + url)
    51  
    52  	return resourceAwsSqsQueuePolicyRead(d, meta)
    53  }
    54  
    55  func resourceAwsSqsQueuePolicyRead(d *schema.ResourceData, meta interface{}) error {
    56  	conn := meta.(*AWSClient).sqsconn
    57  	url := d.Get("queue_url").(string)
    58  	out, err := conn.GetQueueAttributes(&sqs.GetQueueAttributesInput{
    59  		QueueUrl:       aws.String(url),
    60  		AttributeNames: []*string{aws.String("Policy")},
    61  	})
    62  	if err != nil {
    63  		if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "AWS.SimpleQueueService.NonExistentQueue" {
    64  			log.Printf("[WARN] SQS Queue (%s) not found", d.Id())
    65  			d.SetId("")
    66  			return nil
    67  		}
    68  		return err
    69  	}
    70  	if out == nil {
    71  		return fmt.Errorf("Received empty response for SQS queue %s", d.Id())
    72  	}
    73  
    74  	policy, ok := out.Attributes["Policy"]
    75  	if !ok {
    76  		return fmt.Errorf("SQS Queue policy not found for %s", d.Id())
    77  	}
    78  
    79  	d.Set("policy", policy)
    80  
    81  	return nil
    82  }
    83  
    84  func resourceAwsSqsQueuePolicyDelete(d *schema.ResourceData, meta interface{}) error {
    85  	conn := meta.(*AWSClient).sqsconn
    86  
    87  	url := d.Get("queue_url").(string)
    88  	log.Printf("[DEBUG] Deleting SQS Queue Policy of %s", url)
    89  	_, err := conn.SetQueueAttributes(&sqs.SetQueueAttributesInput{
    90  		QueueUrl: aws.String(url),
    91  		Attributes: aws.StringMap(map[string]string{
    92  			"Policy": "",
    93  		}),
    94  	})
    95  	if err != nil {
    96  		return fmt.Errorf("Error deleting SQS Queue policy: %s", err)
    97  	}
    98  	return nil
    99  }