github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/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.Resource{
    32  				Schema: map[string]*schema.Schema{
    33  					"billing": &schema.Schema{
    34  						Type:     schema.TypeBool,
    35  						Required: true,
    36  					},
    37  				},
    38  			},
    39  		},
    40  		"teams": &schema.Schema{
    41  			Type:     schema.TypeList,
    42  			Optional: true,
    43  			Elem:     &schema.Schema{Type: schema.TypeString},
    44  		},
    45  	}
    46  	s = addPermsSchema(s)
    47  	return &schema.Resource{
    48  		Schema: s,
    49  		Create: UserCreate,
    50  		Read:   UserRead,
    51  		Update: UserUpdate,
    52  		Delete: UserDelete,
    53  	}
    54  }
    55  
    56  func userToResourceData(d *schema.ResourceData, u *account.User) error {
    57  	d.SetId(u.Username)
    58  	d.Set("name", u.Name)
    59  	d.Set("email", u.Email)
    60  	d.Set("teams", u.TeamIDs)
    61  	notify := make(map[string]bool)
    62  	notify["billing"] = u.Notify.Billing
    63  	d.Set("notify", notify)
    64  	permissionsToResourceData(d, u.Permissions)
    65  	return nil
    66  }
    67  
    68  func resourceDataToUser(u *account.User, d *schema.ResourceData) error {
    69  	u.Name = d.Get("name").(string)
    70  	u.Username = d.Get("username").(string)
    71  	u.Email = d.Get("email").(string)
    72  	if v, ok := d.GetOk("teams"); ok {
    73  		teamsRaw := v.([]interface{})
    74  		u.TeamIDs = make([]string, len(teamsRaw))
    75  		for i, team := range teamsRaw {
    76  			u.TeamIDs[i] = team.(string)
    77  		}
    78  	} else {
    79  		u.TeamIDs = make([]string, 0)
    80  	}
    81  	if v, ok := d.GetOk("notify"); ok {
    82  		notifyRaw := v.(map[string]interface{})
    83  		u.Notify.Billing = notifyRaw["billing"].(bool)
    84  	}
    85  	u.Permissions = resourceDataToPermissions(d)
    86  	return nil
    87  }
    88  
    89  // UserCreate creates the given user in ns1
    90  func UserCreate(d *schema.ResourceData, meta interface{}) error {
    91  	client := meta.(*ns1.Client)
    92  	u := account.User{}
    93  	if err := resourceDataToUser(&u, d); err != nil {
    94  		return err
    95  	}
    96  	if _, err := client.Users.Create(&u); err != nil {
    97  		return err
    98  	}
    99  	return userToResourceData(d, &u)
   100  }
   101  
   102  // UserRead  reads the given users data from ns1
   103  func UserRead(d *schema.ResourceData, meta interface{}) error {
   104  	client := meta.(*ns1.Client)
   105  	u, _, err := client.Users.Get(d.Id())
   106  	if err != nil {
   107  		return err
   108  	}
   109  	return userToResourceData(d, u)
   110  }
   111  
   112  // UserDelete deletes the given user from ns1
   113  func UserDelete(d *schema.ResourceData, meta interface{}) error {
   114  	client := meta.(*ns1.Client)
   115  	_, err := client.Users.Delete(d.Id())
   116  	d.SetId("")
   117  	return err
   118  }
   119  
   120  // UserUpdate updates the user with given parameters in ns1
   121  func UserUpdate(d *schema.ResourceData, meta interface{}) error {
   122  	client := meta.(*ns1.Client)
   123  	u := account.User{
   124  		Username: d.Id(),
   125  	}
   126  	if err := resourceDataToUser(&u, d); err != nil {
   127  		return err
   128  	}
   129  	if _, err := client.Users.Update(&u); err != nil {
   130  		return err
   131  	}
   132  	return userToResourceData(d, &u)
   133  }