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