From ea97070fea1e4a810986d9184faa15b77e979887 Mon Sep 17 00:00:00 2001 From: Matthew Rich Date: Fri, 4 Oct 2024 10:35:27 -0700 Subject: [PATCH] add support for getting a user name from config --- internal/resource/user.go | 5 +++++ internal/resource/user_test.go | 20 ++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/internal/resource/user.go b/internal/resource/user.go index a7b5c19..67ce7ed 100644 --- a/internal/resource/user.go +++ b/internal/resource/user.go @@ -182,6 +182,11 @@ func (u *User) UseConfig(config data.ConfigurationValueGetter) { } func (u *User) ResolveId(ctx context.Context) string { + if u.config != nil { + if configUser, configUserErr := u.config.GetValue("user"); configUserErr == nil && u.Name == "" { + u.Name = configUser.(string) + } + } return LookupUIDString(u.Name) } diff --git a/internal/resource/user_test.go b/internal/resource/user_test.go index 803d746..39930d9 100644 --- a/internal/resource/user_test.go +++ b/internal/resource/user_test.go @@ -13,6 +13,7 @@ import ( _ "os" _ "strings" "testing" + "decl/internal/data" ) func TestNewUserResource(t *testing.T) { @@ -77,3 +78,22 @@ func TestCreateUser(t *testing.T) { applyDeleteErr := u.Apply() assert.Equal(t, nil, applyDeleteErr) } + +func TestSystemUser(t *testing.T) { + u := NewUser() + + u.UseConfig(MockConfigValueGetter(func(key string) (any, error) { + switch key { + case "user": + return "bar", nil + case "group": + return "foo", nil + case "uid": + return "", nil + } + return nil, fmt.Errorf("%w: %s", data.ErrUnknownConfigurationKey, key) + })) + + u.ResolveId(context.Background()) + assert.Equal(t, "bar", u.Name) +}