Skip to content

Commit 7f99e55

Browse files
fix vsphere group creation (#9458)
Co-authored-by: Charlie Large <largchar@amazon.com>
1 parent c91ea36 commit 7f99e55

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

pkg/executables/govc.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,11 +1078,13 @@ func (g *Govc) GroupExists(ctx context.Context, name string) (bool, error) {
10781078
}
10791079

10801080
response, err := g.exec(ctx, params...)
1081-
if err != nil {
1081+
if err != nil && strings.Contains(err.Error(), fmt.Sprintf("group %s doesn't exist", name)) {
1082+
return false, nil
1083+
} else if err != nil {
10821084
return false, err
10831085
}
1084-
1085-
return response.Len() > 0, nil
1086+
// govc returns empty response when group exists
1087+
return response.Len() == 0, nil
10861088
}
10871089

10881090
// AddUserToGroup adds a user to a group.

pkg/executables/govc_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1327,9 +1327,10 @@ func TestGovcCreateRole(t *testing.T) {
13271327
func TestGovcGroupExistsFalse(t *testing.T) {
13281328
ctx := context.Background()
13291329
_, g, executable, env := setup(t)
1330-
group := "EKSA"
1330+
group := "FakeGroup"
1331+
expectedErr := "govc: ServerFaultCode: The specified principal (FakeGroup@vsphere.local) is invalid.\nCaused by: group FakeGroup doesn't exist or multiple groups same name"
13311332

1332-
executable.EXPECT().ExecuteWithEnv(ctx, env, "sso.group.ls", group).Return(*bytes.NewBufferString(""), nil)
1333+
executable.EXPECT().ExecuteWithEnv(ctx, env, "sso.group.ls", group).Return(*bytes.NewBufferString(""), errors.New(expectedErr))
13331334

13341335
exists, err := g.GroupExists(ctx, group)
13351336
gt := NewWithT(t)
@@ -1341,8 +1342,8 @@ func TestGovcGroupExistsTrue(t *testing.T) {
13411342
ctx := context.Background()
13421343
_, g, executable, env := setup(t)
13431344
group := "EKSA"
1344-
1345-
executable.EXPECT().ExecuteWithEnv(ctx, env, "sso.group.ls", group).Return(*bytes.NewBufferString(group), nil)
1345+
// If group exists, govc should give empty response
1346+
executable.EXPECT().ExecuteWithEnv(ctx, env, "sso.group.ls", group).Return(*bytes.NewBufferString(""), nil)
13461347

13471348
exists, err := g.GroupExists(ctx, group)
13481349
gt := NewWithT(t)
@@ -1354,8 +1355,7 @@ func TestGovcGroupExistsError(t *testing.T) {
13541355
ctx := context.Background()
13551356
_, g, executable, env := setup(t)
13561357
group := "EKSA"
1357-
1358-
executable.EXPECT().ExecuteWithEnv(ctx, env, "sso.group.ls", group).Return(*bytes.NewBufferString(""), errors.New("operation failed"))
1358+
executable.EXPECT().ExecuteWithEnv(ctx, env, "sso.group.ls", group).Return(*bytes.NewBufferString(""), errors.New("unexpected error"))
13591359

13601360
_, err := g.GroupExists(ctx, group)
13611361
gt := NewWithT(t)

0 commit comments

Comments
 (0)