From 544c21c5f1e2c0f8e4109ecbdaba712d6090935c Mon Sep 17 00:00:00 2001 From: Yves Blusseau <90z7oey02@sneakemail.com> Date: Fri, 27 Jan 2017 20:24:37 +0100 Subject: [PATCH] provider/ignition: Allow to add authorized keys without user creation (#11406) Fix #11405 --- .../ignition/resource_ignition_user.go | 37 ++++++++++++------- .../ignition/resource_ignition_user_test.go | 8 ++-- 2 files changed, 28 insertions(+), 17 deletions(-) diff --git a/builtin/providers/ignition/resource_ignition_user.go b/builtin/providers/ignition/resource_ignition_user.go index 70009c99bf..f6ce9f5bcf 100644 --- a/builtin/providers/ignition/resource_ignition_user.go +++ b/builtin/providers/ignition/resource_ignition_user.go @@ -1,6 +1,8 @@ package ignition import ( + "reflect" + "github.com/coreos/ignition/config/types" "github.com/hashicorp/terraform/helper/schema" ) @@ -107,20 +109,29 @@ func resourceUserRead(d *schema.ResourceData, meta interface{}) error { } func buildUser(d *schema.ResourceData, c *cache) (string, error) { - return c.addUser(&types.User{ + uc := types.UserCreate{ + Uid: getUInt(d, "uid"), + GECOS: d.Get("gecos").(string), + Homedir: d.Get("home_dir").(string), + NoCreateHome: d.Get("no_create_home").(bool), + PrimaryGroup: d.Get("primary_group").(string), + Groups: castSliceInterface(d.Get("groups").([]interface{})), + NoUserGroup: d.Get("no_user_group").(bool), + NoLogInit: d.Get("no_log_init").(bool), + Shell: d.Get("shell").(string), + } + + puc := &uc + if reflect.DeepEqual(uc, types.UserCreate{}) { // check if the struct is empty + puc = nil + } + + user := types.User{ Name: d.Get("name").(string), PasswordHash: d.Get("password_hash").(string), SSHAuthorizedKeys: castSliceInterface(d.Get("ssh_authorized_keys").([]interface{})), - Create: &types.UserCreate{ - Uid: getUInt(d, "uid"), - GECOS: d.Get("gecos").(string), - Homedir: d.Get("home_dir").(string), - NoCreateHome: d.Get("no_create_home").(bool), - PrimaryGroup: d.Get("primary_group").(string), - Groups: castSliceInterface(d.Get("groups").([]interface{})), - NoUserGroup: d.Get("no_user_group").(bool), - NoLogInit: d.Get("no_log_init").(bool), - Shell: d.Get("shell").(string), - }, - }), nil + Create: puc, + } + + return c.addUser(&user), nil } diff --git a/builtin/providers/ignition/resource_ignition_user_test.go b/builtin/providers/ignition/resource_ignition_user_test.go index d19f1f036a..b130b79d20 100644 --- a/builtin/providers/ignition/resource_ignition_user_test.go +++ b/builtin/providers/ignition/resource_ignition_user_test.go @@ -26,8 +26,8 @@ func TestIngnitionUser(t *testing.T) { resource "ignition_user" "qux" { name = "qux" - } - + } + resource "ignition_config" "test" { users = [ "${ignition_user.foo.id}", @@ -95,8 +95,8 @@ func TestIngnitionUser(t *testing.T) { return fmt.Errorf("name, found %q", u.Name) } - if u.Create.Uid != nil { - return fmt.Errorf("uid, found %d", *u.Create.Uid) + if u.Create != nil { + return fmt.Errorf("create struct found") } return nil