github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/ns1/resource_user.go (about)

     1  package ns1
     2  
     3  import (
     4  	"github.com/hashicorp/terraform/helper/schema"
     5  
     6  	ns1 "gopkg.in/ns1/ns1-go.v2/rest"
     7  	"gopkg.in/ns1/ns1-go.v2/rest/model/account"
     8  )
     9  
    10  func userResource() *schema.Resource {
    11  	s := map[string]*schema.Schema{
    12  		"id": &schema.Schema{
    13  			Type:     schema.TypeString,
    14  			Computed: true,
    15  		},
    16  		"name": &schema.Schema{
    17  			Type:     schema.TypeString,
    18  			Required: true,
    19  		},
    20  		"username": &schema.Schema{
    21  			Type:     schema.TypeString,
    22  			Required: true,
    23  		},
    24  		"email": &schema.Schema{
    25  			Type:     schema.TypeString,
    26  			Required: true,
    27  		},
    28  		"notify": &schema.Schema{
    29  			Type:     schema.TypeMap,
    30  			Optional: true,
    31  			Elem:     schema.TypeBool,
    32  		},
    33  		"teams": &schema.Schema{
    34  			Type:     schema.TypeList,
    35  			Optional: true,
    36  			Elem:     &schema.Schema{Type: schema.TypeString},
    37  		},
    38  	}
    39  	s = addPermsSchema(s)
    40  	return &schema.Resource{
    41  		Schema: s,
    42  		Create: UserCreate,
    43  		Read:   UserRead,
    44  		Update: UserUpdate,
    45  		Delete: UserDelete,
    46  	}
    47  }
    48  
    49  func userToResourceData(d *schema.ResourceData, u *account.User) error {
    50  	d.SetId(u.Username)
    51  	d.Set("name", u.Name)
    52  	d.Set("email", u.Email)
    53  	d.Set("teams", u.TeamIDs)
    54  	notify := make(map[string]bool)
    55  	notify["billing"] = u.Notify.Billing
    56  	d.Set("notify", notify)
    57  	permissionsToResourceData(d, u.Permissions)
    58  	return nil
    59  }
    60  
    61  func resourceDataToUser(u *account.User, d *schema.ResourceData) error {
    62  	u.Name = d.Get("name").(string)
    63  	u.Username = d.Get("username").(string)
    64  	u.Email = d.Get("email").(string)
    65  	if v, ok := d.GetOk("teams"); ok {
    66  		teamsRaw := v.([]interface{})
    67  		u.TeamIDs = make([]string, len(teamsRaw))
    68  		for i, team := range teamsRaw {
    69  			u.TeamIDs[i] = team.(string)
    70  		}
    71  	} else {
    72  		u.TeamIDs = make([]string, 0)
    73  	}
    74  	if v, ok := d.GetOk("notify"); ok {
    75  		notifyRaw := v.(map[string]interface{})
    76  		u.Notify.Billing = notifyRaw["billing"].(bool)
    77  	}
    78  	u.Permissions = resourceDataToPermissions(d)
    79  	return nil
    80  }
    81  
    82  // UserCreate creates the given user in ns1
    83  func UserCreate(d *schema.ResourceData, meta interface{}) error {
    84  	client := meta.(*ns1.Client)
    85  	u := account.User{}
    86  	if err := resourceDataToUser(&u, d); err != nil {
    87  		return err
    88  	}
    89  	if _, err := client.Users.Create(&u); err != nil {
    90  		return err
    91  	}
    92  	return userToResourceData(d, &u)
    93  }
    94  
    95  // UserRead  reads the given users data from ns1
    96  func UserRead(d *schema.ResourceData, meta interface{}) error {
    97  	client := meta.(*ns1.Client)
    98  	u, _, err := client.Users.Get(d.Id())
    99  	if err != nil {
   100  		return err
   101  	}
   102  	return userToResourceData(d, u)
   103  }
   104  
   105  // UserDelete deletes the given user from ns1
   106  func UserDelete(d *schema.ResourceData, meta interface{}) error {
   107  	client := meta.(*ns1.Client)
   108  	_, err := client.Users.Delete(d.Id())
   109  	d.SetId("")
   110  	return err
   111  }
   112  
   113  // UserUpdate updates the user with given parameters in ns1
   114  func UserUpdate(d *schema.ResourceData, meta interface{}) error {
   115  	client := meta.(*ns1.Client)
   116  	u := account.User{
   117  		Username: d.Id(),
   118  	}
   119  	if err := resourceDataToUser(&u, d); err != nil {
   120  		return err
   121  	}
   122  	if _, err := client.Users.Update(&u); err != nil {
   123  		return err
   124  	}
   125  	return userToResourceData(d, &u)
   126  }