github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/ignition/resource_ignition_user.go (about)

     1  package ignition
     2  
     3  import (
     4  	"reflect"
     5  
     6  	"github.com/coreos/ignition/config/types"
     7  	"github.com/hashicorp/terraform/helper/schema"
     8  )
     9  
    10  func resourceUser() *schema.Resource {
    11  	return &schema.Resource{
    12  		Exists: resourceUserExists,
    13  		Read:   resourceUserRead,
    14  		Schema: map[string]*schema.Schema{
    15  			"name": &schema.Schema{
    16  				Type:     schema.TypeString,
    17  				Required: true,
    18  				ForceNew: true,
    19  			},
    20  			"password_hash": &schema.Schema{
    21  				Type:     schema.TypeString,
    22  				Optional: true,
    23  				ForceNew: true,
    24  			},
    25  			"ssh_authorized_keys": &schema.Schema{
    26  				Type:     schema.TypeList,
    27  				Optional: true,
    28  				ForceNew: true,
    29  				Elem:     &schema.Schema{Type: schema.TypeString},
    30  			},
    31  			"uid": &schema.Schema{
    32  				Type:     schema.TypeInt,
    33  				Optional: true,
    34  				ForceNew: true,
    35  			},
    36  			"gecos": &schema.Schema{
    37  				Type:     schema.TypeString,
    38  				Optional: true,
    39  				ForceNew: true,
    40  			},
    41  			"home_dir": &schema.Schema{
    42  				Type:     schema.TypeString,
    43  				Optional: true,
    44  				ForceNew: true,
    45  			},
    46  			"no_create_home": &schema.Schema{
    47  				Type:     schema.TypeBool,
    48  				Optional: true,
    49  				ForceNew: true,
    50  			},
    51  			"primary_group": &schema.Schema{
    52  				Type:     schema.TypeString,
    53  				Optional: true,
    54  				ForceNew: true,
    55  			},
    56  			"groups": &schema.Schema{
    57  				Type:     schema.TypeList,
    58  				Optional: true,
    59  				ForceNew: true,
    60  				Elem:     &schema.Schema{Type: schema.TypeString},
    61  			},
    62  			"no_user_group": &schema.Schema{
    63  				Type:     schema.TypeBool,
    64  				Optional: true,
    65  				ForceNew: true,
    66  			},
    67  			"no_log_init": &schema.Schema{
    68  				Type:     schema.TypeBool,
    69  				Optional: true,
    70  				ForceNew: true,
    71  			},
    72  			"shell": &schema.Schema{
    73  				Type:     schema.TypeString,
    74  				Optional: true,
    75  				ForceNew: true,
    76  			},
    77  		},
    78  	}
    79  }
    80  
    81  func resourceUserRead(d *schema.ResourceData, meta interface{}) error {
    82  	id, err := buildUser(d, meta.(*cache))
    83  	if err != nil {
    84  		return err
    85  	}
    86  
    87  	d.SetId(id)
    88  	return nil
    89  }
    90  
    91  func resourceUserExists(d *schema.ResourceData, meta interface{}) (bool, error) {
    92  	id, err := buildUser(d, meta.(*cache))
    93  	if err != nil {
    94  		return false, err
    95  	}
    96  
    97  	return id == d.Id(), nil
    98  }
    99  
   100  func buildUser(d *schema.ResourceData, c *cache) (string, error) {
   101  	uc := types.UserCreate{
   102  		Uid:          getUInt(d, "uid"),
   103  		GECOS:        d.Get("gecos").(string),
   104  		Homedir:      d.Get("home_dir").(string),
   105  		NoCreateHome: d.Get("no_create_home").(bool),
   106  		PrimaryGroup: d.Get("primary_group").(string),
   107  		Groups:       castSliceInterface(d.Get("groups").([]interface{})),
   108  		NoUserGroup:  d.Get("no_user_group").(bool),
   109  		NoLogInit:    d.Get("no_log_init").(bool),
   110  		Shell:        d.Get("shell").(string),
   111  	}
   112  
   113  	puc := &uc
   114  	if reflect.DeepEqual(uc, types.UserCreate{}) { // check if the struct is empty
   115  		puc = nil
   116  	}
   117  
   118  	user := types.User{
   119  		Name:              d.Get("name").(string),
   120  		PasswordHash:      d.Get("password_hash").(string),
   121  		SSHAuthorizedKeys: castSliceInterface(d.Get("ssh_authorized_keys").([]interface{})),
   122  		Create:            puc,
   123  	}
   124  
   125  	return c.addUser(&user), nil
   126  }