github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/rabbitmq/resource_exchange.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 resourceExchange() *schema.Resource {
    14  	return &schema.Resource{
    15  		Create: CreateExchange,
    16  		Read:   ReadExchange,
    17  		Delete: DeleteExchange,
    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  						"type": &schema.Schema{
    44  							Type:     schema.TypeString,
    45  							Required: true,
    46  						},
    47  
    48  						"durable": &schema.Schema{
    49  							Type:     schema.TypeBool,
    50  							Optional: true,
    51  							Default:  false,
    52  						},
    53  
    54  						"auto_delete": &schema.Schema{
    55  							Type:     schema.TypeBool,
    56  							Optional: true,
    57  							Default:  false,
    58  						},
    59  
    60  						"arguments": &schema.Schema{
    61  							Type:     schema.TypeMap,
    62  							Optional: true,
    63  						},
    64  					},
    65  				},
    66  			},
    67  		},
    68  	}
    69  }
    70  
    71  func CreateExchange(d *schema.ResourceData, meta interface{}) error {
    72  	rmqc := meta.(*rabbithole.Client)
    73  
    74  	name := d.Get("name").(string)
    75  	vhost := d.Get("vhost").(string)
    76  	settingsList := d.Get("settings").([]interface{})
    77  
    78  	settingsMap, ok := settingsList[0].(map[string]interface{})
    79  	if !ok {
    80  		return fmt.Errorf("Unable to parse settings")
    81  	}
    82  
    83  	if err := declareExchange(rmqc, vhost, name, settingsMap); err != nil {
    84  		return err
    85  	}
    86  
    87  	id := fmt.Sprintf("%s@%s", name, vhost)
    88  	d.SetId(id)
    89  
    90  	return ReadExchange(d, meta)
    91  }
    92  
    93  func ReadExchange(d *schema.ResourceData, meta interface{}) error {
    94  	rmqc := meta.(*rabbithole.Client)
    95  
    96  	exchangeId := strings.Split(d.Id(), "@")
    97  	if len(exchangeId) < 2 {
    98  		return fmt.Errorf("Unable to determine exchange ID")
    99  	}
   100  
   101  	name := exchangeId[0]
   102  	vhost := exchangeId[1]
   103  
   104  	exchangeSettings, err := rmqc.GetExchange(vhost, name)
   105  	if err != nil {
   106  		return checkDeleted(d, err)
   107  	}
   108  
   109  	log.Printf("[DEBUG] RabbitMQ: Exchange retrieved %s: %#v", d.Id(), exchangeSettings)
   110  
   111  	d.Set("name", exchangeSettings.Name)
   112  	d.Set("vhost", exchangeSettings.Vhost)
   113  
   114  	exchange := make([]map[string]interface{}, 1)
   115  	e := make(map[string]interface{})
   116  	e["type"] = exchangeSettings.Type
   117  	e["durable"] = exchangeSettings.Durable
   118  	e["auto_delete"] = exchangeSettings.AutoDelete
   119  	e["arguments"] = exchangeSettings.Arguments
   120  	exchange[0] = e
   121  	d.Set("settings", exchange)
   122  
   123  	return nil
   124  }
   125  
   126  func DeleteExchange(d *schema.ResourceData, meta interface{}) error {
   127  	rmqc := meta.(*rabbithole.Client)
   128  
   129  	exchangeId := strings.Split(d.Id(), "@")
   130  	if len(exchangeId) < 2 {
   131  		return fmt.Errorf("Unable to determine exchange ID")
   132  	}
   133  
   134  	name := exchangeId[0]
   135  	vhost := exchangeId[1]
   136  
   137  	log.Printf("[DEBUG] RabbitMQ: Attempting to delete exchange %s", d.Id())
   138  
   139  	resp, err := rmqc.DeleteExchange(vhost, name)
   140  	log.Printf("[DEBUG] RabbitMQ: Exchange delete response: %#v", resp)
   141  	if err != nil {
   142  		return err
   143  	}
   144  
   145  	if resp.StatusCode == 404 {
   146  		// The exchange was automatically deleted
   147  		return nil
   148  	}
   149  
   150  	if resp.StatusCode >= 400 {
   151  		return fmt.Errorf("Error deleting RabbitMQ exchange: %s", resp.Status)
   152  	}
   153  
   154  	return nil
   155  }
   156  
   157  func declareExchange(rmqc *rabbithole.Client, vhost string, name string, settingsMap map[string]interface{}) error {
   158  	exchangeSettings := rabbithole.ExchangeSettings{}
   159  
   160  	if v, ok := settingsMap["type"].(string); ok {
   161  		exchangeSettings.Type = v
   162  	}
   163  
   164  	if v, ok := settingsMap["durable"].(bool); ok {
   165  		exchangeSettings.Durable = v
   166  	}
   167  
   168  	if v, ok := settingsMap["auto_delete"].(bool); ok {
   169  		exchangeSettings.AutoDelete = v
   170  	}
   171  
   172  	if v, ok := settingsMap["arguments"].(map[string]interface{}); ok {
   173  		exchangeSettings.Arguments = v
   174  	}
   175  
   176  	log.Printf("[DEBUG] RabbitMQ: Attempting to declare exchange %s@%s: %#v", name, vhost, exchangeSettings)
   177  
   178  	resp, err := rmqc.DeclareExchange(vhost, name, exchangeSettings)
   179  	log.Printf("[DEBUG] RabbitMQ: Exchange declare response: %#v", resp)
   180  	if err != nil {
   181  		return err
   182  	}
   183  
   184  	if resp.StatusCode >= 400 {
   185  		return fmt.Errorf("Error declaring RabbitMQ exchange: %s", resp.Status)
   186  	}
   187  
   188  	return nil
   189  }