github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/builtin/providers/rabbitmq/resource_user.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 resourceUser() *schema.Resource {
    14  	return &schema.Resource{
    15  		Create: CreateUser,
    16  		Update: UpdateUser,
    17  		Read:   ReadUser,
    18  		Delete: DeleteUser,
    19  		Importer: &schema.ResourceImporter{
    20  			State: schema.ImportStatePassthrough,
    21  		},
    22  
    23  		Schema: map[string]*schema.Schema{
    24  			"name": &schema.Schema{
    25  				Type:     schema.TypeString,
    26  				Required: true,
    27  				ForceNew: true,
    28  			},
    29  
    30  			"password": &schema.Schema{
    31  				Type:      schema.TypeString,
    32  				Required:  true,
    33  				Sensitive: true,
    34  			},
    35  
    36  			"tags": &schema.Schema{
    37  				Type:     schema.TypeList,
    38  				Required: true,
    39  				Elem:     &schema.Schema{Type: schema.TypeString},
    40  			},
    41  		},
    42  	}
    43  }
    44  
    45  func CreateUser(d *schema.ResourceData, meta interface{}) error {
    46  	rmqc := meta.(*rabbithole.Client)
    47  
    48  	name := d.Get("name").(string)
    49  
    50  	var tagList []string
    51  	for _, v := range d.Get("tags").([]interface{}) {
    52  		tagList = append(tagList, v.(string))
    53  	}
    54  	tags := strings.Join(tagList, ",")
    55  
    56  	userSettings := rabbithole.UserSettings{
    57  		Password: d.Get("password").(string),
    58  		Tags:     tags,
    59  	}
    60  
    61  	log.Printf("[DEBUG] RabbitMQ: Attempting to create user %s", name)
    62  
    63  	resp, err := rmqc.PutUser(name, userSettings)
    64  	log.Printf("[DEBUG] RabbitMQ: user creation response: %#v", resp)
    65  	if err != nil {
    66  		return err
    67  	}
    68  
    69  	if resp.StatusCode >= 400 {
    70  		return fmt.Errorf("Error creating RabbitMQ user: %s", resp.Status)
    71  	}
    72  
    73  	d.SetId(name)
    74  
    75  	return ReadUser(d, meta)
    76  }
    77  
    78  func ReadUser(d *schema.ResourceData, meta interface{}) error {
    79  	rmqc := meta.(*rabbithole.Client)
    80  
    81  	user, err := rmqc.GetUser(d.Id())
    82  	if err != nil {
    83  		return checkDeleted(d, err)
    84  	}
    85  
    86  	log.Printf("[DEBUG] RabbitMQ: User retrieved: %#v", user)
    87  
    88  	d.Set("name", user.Name)
    89  
    90  	tags := strings.Split(user.Tags, ",")
    91  	d.Set("tags", tags)
    92  
    93  	return nil
    94  }
    95  
    96  func UpdateUser(d *schema.ResourceData, meta interface{}) error {
    97  	rmqc := meta.(*rabbithole.Client)
    98  
    99  	name := d.Id()
   100  
   101  	if d.HasChange("password") {
   102  		_, newPassword := d.GetChange("password")
   103  
   104  		userSettings := rabbithole.UserSettings{
   105  			Password: newPassword.(string),
   106  		}
   107  
   108  		log.Printf("[DEBUG] RabbitMQ: Attempting to update password for %s", name)
   109  
   110  		resp, err := rmqc.PutUser(name, userSettings)
   111  		log.Printf("[DEBUG] RabbitMQ: Password update response: %#v", resp)
   112  		if err != nil {
   113  			return err
   114  		}
   115  
   116  		if resp.StatusCode >= 400 {
   117  			return fmt.Errorf("Error updating RabbitMQ user: %s", resp.Status)
   118  		}
   119  
   120  	}
   121  
   122  	if d.HasChange("tags") {
   123  		_, newTags := d.GetChange("tags")
   124  
   125  		var tagList []string
   126  		for _, v := range newTags.([]interface{}) {
   127  			tagList = append(tagList, v.(string))
   128  		}
   129  		tags := strings.Join(tagList, ",")
   130  
   131  		userSettings := rabbithole.UserSettings{
   132  			Tags: tags,
   133  		}
   134  
   135  		log.Printf("[DEBUG] RabbitMQ: Attempting to update tags for %s", name)
   136  
   137  		resp, err := rmqc.PutUser(name, userSettings)
   138  		log.Printf("[DEBUG] RabbitMQ: Tags update response: %#v", resp)
   139  		if err != nil {
   140  			return err
   141  		}
   142  
   143  		if resp.StatusCode >= 400 {
   144  			return fmt.Errorf("Error updating RabbitMQ user: %s", resp.Status)
   145  		}
   146  
   147  	}
   148  
   149  	return ReadUser(d, meta)
   150  }
   151  
   152  func DeleteUser(d *schema.ResourceData, meta interface{}) error {
   153  	rmqc := meta.(*rabbithole.Client)
   154  
   155  	name := d.Id()
   156  	log.Printf("[DEBUG] RabbitMQ: Attempting to delete user %s", name)
   157  
   158  	resp, err := rmqc.DeleteUser(name)
   159  	log.Printf("[DEBUG] RabbitMQ: User delete response: %#v", resp)
   160  	if err != nil {
   161  		return err
   162  	}
   163  
   164  	if resp.StatusCode == 404 {
   165  		// the user was automatically deleted
   166  		return nil
   167  	}
   168  
   169  	if resp.StatusCode >= 400 {
   170  		return fmt.Errorf("Error deleting RabbitMQ user: %s", resp.Status)
   171  	}
   172  
   173  	return nil
   174  }