github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/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  				ValidateFunc:     validateJsonString,
    31  				DiffSuppressFunc: suppressEquivalentAwsPolicyDiffs,
    32  			},
    33  		},
    34  	}
    35  }
    36  
    37  func resourceAwsSqsQueuePolicyUpsert(d *schema.ResourceData, meta interface{}) error {
    38  	conn := meta.(*AWSClient).sqsconn
    39  	url := d.Get("queue_url").(string)
    40  
    41  	_, err := conn.SetQueueAttributes(&sqs.SetQueueAttributesInput{
    42  		QueueUrl: aws.String(url),
    43  		Attributes: aws.StringMap(map[string]string{
    44  			"Policy": d.Get("policy").(string),
    45  		}),
    46  	})
    47  	if err != nil {
    48  		return fmt.Errorf("Error updating SQS attributes: %s", err)
    49  	}
    50  
    51  	d.SetId("sqs-policy-" + url)
    52  
    53  	return resourceAwsSqsQueuePolicyRead(d, meta)
    54  }
    55  
    56  func resourceAwsSqsQueuePolicyRead(d *schema.ResourceData, meta interface{}) error {
    57  	conn := meta.(*AWSClient).sqsconn
    58  	url := d.Get("queue_url").(string)
    59  	out, err := conn.GetQueueAttributes(&sqs.GetQueueAttributesInput{
    60  		QueueUrl:       aws.String(url),
    61  		AttributeNames: []*string{aws.String("Policy")},
    62  	})
    63  	if err != nil {
    64  		if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "AWS.SimpleQueueService.NonExistentQueue" {
    65  			log.Printf("[WARN] SQS Queue (%s) not found", d.Id())
    66  			d.SetId("")
    67  			return nil
    68  		}
    69  		return err
    70  	}
    71  	if out == nil {
    72  		return fmt.Errorf("Received empty response for SQS queue %s", d.Id())
    73  	}
    74  
    75  	policy, ok := out.Attributes["Policy"]
    76  	if !ok {
    77  		return fmt.Errorf("SQS Queue policy not found for %s", d.Id())
    78  	}
    79  
    80  	d.Set("policy", policy)
    81  
    82  	return nil
    83  }
    84  
    85  func resourceAwsSqsQueuePolicyDelete(d *schema.ResourceData, meta interface{}) error {
    86  	conn := meta.(*AWSClient).sqsconn
    87  
    88  	url := d.Get("queue_url").(string)
    89  	log.Printf("[DEBUG] Deleting SQS Queue Policy of %s", url)
    90  	_, err := conn.SetQueueAttributes(&sqs.SetQueueAttributesInput{
    91  		QueueUrl: aws.String(url),
    92  		Attributes: aws.StringMap(map[string]string{
    93  			"Policy": "",
    94  		}),
    95  	})
    96  	if err != nil {
    97  		return fmt.Errorf("Error deleting SQS Queue policy: %s", err)
    98  	}
    99  	return nil
   100  }