github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/rabbitmq/resource_queue.go (about)

     1  package rabbitmq
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"strings"
     7  
     8  	"github.com/michaelklishin/rabbit-hole"
     9  
    10  	"github.com/hashicorp/terraform/helper/schema"
    11  )
    12  
    13  func resourceQueue() *schema.Resource {
    14  	return &schema.Resource{
    15  		Create: CreateQueue,
    16  		Read:   ReadQueue,
    17  		Delete: DeleteQueue,
    18  		Importer: &schema.ResourceImporter{
    19  			State: schema.ImportStatePassthrough,
    20  		},
    21  
    22  		Schema: map[string]*schema.Schema{
    23  			"name": &schema.Schema{
    24  				Type:     schema.TypeString,
    25  				Required: true,
    26  				ForceNew: true,
    27  			},
    28  
    29  			"vhost": &schema.Schema{
    30  				Type:     schema.TypeString,
    31  				Optional: true,
    32  				Default:  "/",
    33  				ForceNew: true,
    34  			},
    35  
    36  			"settings": &schema.Schema{
    37  				Type:     schema.TypeList,
    38  				Required: true,
    39  				ForceNew: true,
    40  				MaxItems: 1,
    41  				Elem: &schema.Resource{
    42  					Schema: map[string]*schema.Schema{
    43  						"durable": &schema.Schema{
    44  							Type:     schema.TypeBool,
    45  							Optional: true,
    46  							Default:  false,
    47  						},
    48  
    49  						"auto_delete": &schema.Schema{
    50  							Type:     schema.TypeBool,
    51  							Optional: true,
    52  							Default:  false,
    53  						},
    54  
    55  						"arguments": &schema.Schema{
    56  							Type:     schema.TypeMap,
    57  							Optional: true,
    58  						},
    59  					},
    60  				},
    61  			},
    62  		},
    63  	}
    64  }
    65  
    66  func CreateQueue(d *schema.ResourceData, meta interface{}) error {
    67  	rmqc := meta.(*rabbithole.Client)
    68  
    69  	name := d.Get("name").(string)
    70  	vhost := d.Get("vhost").(string)
    71  	settingsList := d.Get("settings").([]interface{})
    72  
    73  	settingsMap, ok := settingsList[0].(map[string]interface{})
    74  	if !ok {
    75  		return fmt.Errorf("Unable to parse settings")
    76  	}
    77  
    78  	if err := declareQueue(rmqc, vhost, name, settingsMap); err != nil {
    79  		return err
    80  	}
    81  
    82  	id := fmt.Sprintf("%s@%s", name, vhost)
    83  	d.SetId(id)
    84  
    85  	return ReadQueue(d, meta)
    86  }
    87  
    88  func ReadQueue(d *schema.ResourceData, meta interface{}) error {
    89  	rmqc := meta.(*rabbithole.Client)
    90  
    91  	queueId := strings.Split(d.Id(), "@")
    92  	if len(queueId) < 2 {
    93  		return fmt.Errorf("Unable to determine Queue ID")
    94  	}
    95  
    96  	user := queueId[0]
    97  	vhost := queueId[1]
    98  
    99  	queueSettings, err := rmqc.GetQueue(vhost, user)
   100  	if err != nil {
   101  		return checkDeleted(d, err)
   102  	}
   103  
   104  	log.Printf("[DEBUG] RabbitMQ: Queue retrieved for %s: %#v", d.Id(), queueSettings)
   105  
   106  	d.Set("name", queueSettings.Name)
   107  	d.Set("vhost", queueSettings.Vhost)
   108  
   109  	queue := make([]map[string]interface{}, 1)
   110  	e := make(map[string]interface{})
   111  	e["durable"] = queueSettings.Durable
   112  	e["auto_delete"] = queueSettings.AutoDelete
   113  	e["arguments"] = queueSettings.Arguments
   114  	queue[0] = e
   115  
   116  	d.Set("settings", queue)
   117  
   118  	return nil
   119  }
   120  
   121  func DeleteQueue(d *schema.ResourceData, meta interface{}) error {
   122  	rmqc := meta.(*rabbithole.Client)
   123  
   124  	queueId := strings.Split(d.Id(), "@")
   125  	if len(queueId) < 2 {
   126  		return fmt.Errorf("Unable to determine Queue ID")
   127  	}
   128  
   129  	user := queueId[0]
   130  	vhost := queueId[1]
   131  
   132  	log.Printf("[DEBUG] RabbitMQ: Attempting to delete queue for %s", d.Id())
   133  
   134  	resp, err := rmqc.DeleteQueue(vhost, user)
   135  	log.Printf("[DEBUG] RabbitMQ: Queue delete response: %#v", resp)
   136  	if err != nil {
   137  		return err
   138  	}
   139  
   140  	if resp.StatusCode == 404 {
   141  		// the queue was automatically deleted
   142  		return nil
   143  	}
   144  
   145  	if resp.StatusCode >= 400 {
   146  		return fmt.Errorf("Error deleting RabbitMQ queue: %s", resp.Status)
   147  	}
   148  
   149  	return nil
   150  }
   151  
   152  func declareQueue(rmqc *rabbithole.Client, vhost string, name string, settingsMap map[string]interface{}) error {
   153  	queueSettings := rabbithole.QueueSettings{}
   154  
   155  	if v, ok := settingsMap["durable"].(bool); ok {
   156  		queueSettings.Durable = v
   157  	}
   158  
   159  	if v, ok := settingsMap["auto_delete"].(bool); ok {
   160  		queueSettings.AutoDelete = v
   161  	}
   162  
   163  	if v, ok := settingsMap["arguments"].(map[string]interface{}); ok {
   164  		queueSettings.Arguments = v
   165  	}
   166  
   167  	log.Printf("[DEBUG] RabbitMQ: Attempting to declare queue for %s@%s: %#v", name, vhost, queueSettings)
   168  
   169  	resp, err := rmqc.DeclareQueue(vhost, name, queueSettings)
   170  	log.Printf("[DEBUG] RabbitMQ: Queue declare response: %#v", resp)
   171  	if err != nil {
   172  		return err
   173  	}
   174  
   175  	if resp.StatusCode >= 400 {
   176  		return fmt.Errorf("Error declaring RabbitMQ queue: %s", resp.Status)
   177  	}
   178  
   179  	return nil
   180  }