github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/rabbitmq/resource_vhost.go (about)

     1  package rabbitmq
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  
     7  	"github.com/michaelklishin/rabbit-hole"
     8  
     9  	"github.com/hashicorp/terraform/helper/schema"
    10  )
    11  
    12  func resourceVhost() *schema.Resource {
    13  	return &schema.Resource{
    14  		Create: CreateVhost,
    15  		Read:   ReadVhost,
    16  		Delete: DeleteVhost,
    17  		Importer: &schema.ResourceImporter{
    18  			State: schema.ImportStatePassthrough,
    19  		},
    20  
    21  		Schema: map[string]*schema.Schema{
    22  			"name": &schema.Schema{
    23  				Type:     schema.TypeString,
    24  				Required: true,
    25  				ForceNew: true,
    26  			},
    27  		},
    28  	}
    29  }
    30  
    31  func CreateVhost(d *schema.ResourceData, meta interface{}) error {
    32  	rmqc := meta.(*rabbithole.Client)
    33  
    34  	vhost := d.Get("name").(string)
    35  
    36  	log.Printf("[DEBUG] RabbitMQ: Attempting to create vhost %s", vhost)
    37  
    38  	resp, err := rmqc.PutVhost(vhost, rabbithole.VhostSettings{})
    39  	log.Printf("[DEBUG] RabbitMQ: vhost creation response: %#v", resp)
    40  	if err != nil {
    41  		return err
    42  	}
    43  
    44  	d.SetId(vhost)
    45  
    46  	return ReadVhost(d, meta)
    47  }
    48  
    49  func ReadVhost(d *schema.ResourceData, meta interface{}) error {
    50  	rmqc := meta.(*rabbithole.Client)
    51  
    52  	vhost, err := rmqc.GetVhost(d.Id())
    53  	if err != nil {
    54  		return checkDeleted(d, err)
    55  	}
    56  
    57  	log.Printf("[DEBUG] RabbitMQ: Vhost retrieved: %#v", vhost)
    58  
    59  	d.Set("name", vhost.Name)
    60  
    61  	return nil
    62  }
    63  
    64  func DeleteVhost(d *schema.ResourceData, meta interface{}) error {
    65  	rmqc := meta.(*rabbithole.Client)
    66  
    67  	log.Printf("[DEBUG] RabbitMQ: Attempting to delete vhost %s", d.Id())
    68  
    69  	resp, err := rmqc.DeleteVhost(d.Id())
    70  	log.Printf("[DEBUG] RabbitMQ: vhost deletion response: %#v", resp)
    71  	if err != nil {
    72  		return err
    73  	}
    74  
    75  	if resp.StatusCode == 404 {
    76  		// the vhost was automatically deleted
    77  		return nil
    78  	}
    79  
    80  	if resp.StatusCode >= 400 {
    81  		return fmt.Errorf("Error deleting RabbitMQ user: %s", resp.Status)
    82  	}
    83  
    84  	return nil
    85  }