Skip to content

Commit

Permalink
vscim: Implement UserDirectory
Browse files Browse the repository at this point in the history
  • Loading branch information
Yongkun Anfernee Gui committed Oct 4, 2017
1 parent 2418df7 commit 7477320
Show file tree
Hide file tree
Showing 3 changed files with 218 additions and 0 deletions.
1 change: 1 addition & 0 deletions simulator/service_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ func NewServiceInstance(content types.ServiceContent, folder mo.Folder) *Service
NewSearchIndex(*s.Content.SearchIndex),
NewViewManager(*s.Content.ViewManager),
NewTaskManager(*s.Content.TaskManager),
NewUserDirectory(*s.Content.UserDirectory),
NewOptionManager(s.Content.Setting, setting),
}

Expand Down
77 changes: 77 additions & 0 deletions simulator/user_directory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
Copyright (c) 2017 VMware, Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package simulator

import (
"strings"

"github.com/vmware/govmomi/object"
"github.com/vmware/govmomi/vim25/methods"
"github.com/vmware/govmomi/vim25/mo"
"github.com/vmware/govmomi/vim25/soap"
"github.com/vmware/govmomi/vim25/types"
)

var DefaultUserGroup = []*types.UserSearchResult{
{FullName: "root", Group: true, Principal: "root"},
{FullName: "root", Group: false, Principal: "root"},
{FullName: "administrator", Group: false, Principal: "admin"},
}

type UserDirectory struct {
mo.UserDirectory

userGroup []*types.UserSearchResult
}

func NewUserDirectory(ref types.ManagedObjectReference) object.Reference {
u := &UserDirectory{}

u.Self = ref
u.userGroup = DefaultUserGroup

return u
}

func (u *UserDirectory) RetrieveUserGroups(req *types.RetrieveUserGroups) soap.HasFault {
body := &methods.RetrieveUserGroupsBody{}
var res []types.BaseUserSearchResult

for _, ug := range u.userGroup {
if req.FindUsers && !ug.Group || req.FindGroups && ug.Group {
compare := compareFunc(req.SearchStr, req.ExactMatch)
if compare(ug.Principal) {
res = append(res, ug)
}
}
}

body.Res = &types.RetrieveUserGroupsResponse{
Returnval: res,
}

return body
}

func compareFunc(compared string, exactly bool) func(string) bool {
return func(s string) bool {
if exactly {
return s == compared
}
return strings.Contains(strings.ToLower(s), strings.ToLower(compared))
}
}
140 changes: 140 additions & 0 deletions simulator/user_directory_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/*
Copyright (c) 2017 VMware, Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package simulator

import (
"context"
"testing"

"github.com/vmware/govmomi"
"github.com/vmware/govmomi/simulator/esx"
"github.com/vmware/govmomi/vim25/methods"
"github.com/vmware/govmomi/vim25/types"
)

func TestUserDirectory(t *testing.T) {
s := New(NewServiceInstance(esx.ServiceContent, esx.RootFolder))

ts := s.NewServer()
defer ts.Close()

ctx := context.Background()

c, err := govmomi.NewClient(ctx, ts.URL, true)
if err != nil {
t.Fatal(err)
}

var tests = []struct {
findUsers bool
findGroups bool
}{
{true, true},
{true, false},
{false, true},
{false, false},
}

ref := *c.ServiceContent.UserDirectory

for _, test := range tests {
req := types.RetrieveUserGroups{
This: ref,
SearchStr: "root",
ExactMatch: true,
FindUsers: test.findUsers,
FindGroups: test.findGroups,
}

result, err := methods.RetrieveUserGroups(ctx, c.Client, &req)
if err != nil {
t.Fatal(err)
}

expectedSize := 0
if test.findGroups {
expectedSize++
}
if test.findUsers {
expectedSize++
}

if len(result.Returnval) != expectedSize {
t.Fatalf("expect search result for root is %d; got %d", expectedSize, len(result.Returnval))
}

for _, u := range result.Returnval {
if u.GetUserSearchResult().Principal != "root" {
t.Fatalf("expect principal to be root; got %s", u.GetUserSearchResult().Principal)
}
if !test.findGroups && u.GetUserSearchResult().Group {
t.Fatal("expect search result is non-group; got group")
}
if !test.findUsers && !u.GetUserSearchResult().Group {
t.Fatal("expect search result is non-user; got user")
}
}
}
}

func TestUserDirectoryExactlyMatch(t *testing.T) {
s := New(NewServiceInstance(esx.ServiceContent, esx.RootFolder))

ts := s.NewServer()
defer ts.Close()

ctx := context.Background()

c, err := govmomi.NewClient(ctx, ts.URL, true)
if err != nil {
t.Fatal(err)
}

var tests = []struct {
exactly bool
search string
expectedSize int
}{
{true, "root", 2},
{false, "ROO", 2},
}

ref := *c.ServiceContent.UserDirectory

for _, test := range tests {
req := types.RetrieveUserGroups{
This: ref,
SearchStr: test.search,
ExactMatch: test.exactly,
FindUsers: true,
FindGroups: true,
}

result, err := methods.RetrieveUserGroups(ctx, c.Client, &req)
if err != nil {
t.Fatal(err)
}

if err != nil {
t.Fatal(err)
}

if len(result.Returnval) != test.expectedSize {
t.Fatalf("expect result contains %d results; got %d", test.expectedSize, len(result.Returnval))
}
}
}

0 comments on commit 7477320

Please sign in to comment.