forked from vmware/govmomi
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support CUD operations on users. Groups operations are not supported because they are deprecated in 5.1 Fix vmware#947
- Loading branch information
Yongkun Anfernee Gui
committed
Dec 13, 2017
1 parent
bb150d5
commit 42f9a13
Showing
3 changed files
with
174 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
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 ( | ||
"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" | ||
) | ||
|
||
// As of vSphere API 5.1, local groups operations are deprecated, so it's not supported here. | ||
|
||
type HostLocalAccountManager struct { | ||
mo.HostLocalAccountManager | ||
|
||
users map[string]*types.HostAccountSpec | ||
} | ||
|
||
func NewHostLocalAccountManager(ref types.ManagedObjectReference) object.Reference { | ||
m := &HostLocalAccountManager{} | ||
m.Self = ref | ||
m.users = make(map[string]*types.HostAccountSpec) | ||
return m | ||
} | ||
|
||
func (h *HostLocalAccountManager) CreateUser(req *types.CreateUser) soap.HasFault { | ||
spec := req.User.GetHostAccountSpec() | ||
if _, ok := h.users[spec.Id]; ok { | ||
return &methods.CreateUserBody{ | ||
Fault_: Fault("", &types.AlreadyExists{}), | ||
} | ||
} | ||
|
||
h.users[spec.Id] = spec | ||
|
||
return &methods.CreateUserBody{ | ||
Res: &types.CreateUserResponse{}, | ||
} | ||
} | ||
|
||
func (h *HostLocalAccountManager) RemoveUser(req *types.RemoveUser) soap.HasFault { | ||
if _, ok := h.users[req.UserName]; !ok { | ||
return &methods.RemoveUserBody{ | ||
Fault_: Fault("", &types.UserNotFound{}), | ||
} | ||
} | ||
|
||
delete(h.users, req.UserName) | ||
|
||
return &methods.RemoveUserBody{ | ||
Res: &types.RemoveUserResponse{}, | ||
} | ||
} | ||
|
||
func (h *HostLocalAccountManager) UpdateUser(req *types.UpdateUser) soap.HasFault { | ||
spec := req.User.GetHostAccountSpec() | ||
h.users[spec.Id] = spec | ||
|
||
return &methods.CreateUserBody{ | ||
Res: &types.CreateUserResponse{}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
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/vim25/methods" | ||
"github.com/vmware/govmomi/vim25/types" | ||
) | ||
|
||
func TestHostLocalAccountManager(t *testing.T) { | ||
ctx := context.Background() | ||
m := ESX() | ||
|
||
defer m.Remove() | ||
|
||
err := m.Create() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
ts := m.Service.NewServer() | ||
defer ts.Close() | ||
|
||
c, err := govmomi.NewClient(ctx, ts.URL, true) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
ref := types.ManagedObjectReference{Type: "HostLocalAccountManager", Value: "ha-localacctmgr"} | ||
|
||
createUserReq := &types.CreateUser{ | ||
This: ref, | ||
User: &types.HostAccountSpec{ | ||
Id: "userid", | ||
}, | ||
} | ||
|
||
_, err = methods.CreateUser(ctx, c.Client, createUserReq) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
_, err = methods.CreateUser(ctx, c.Client, createUserReq) | ||
if err == nil { | ||
t.Fatal("expect err; got nil") | ||
} | ||
|
||
updateUserReq := &types.UpdateUser{ | ||
This: ref, | ||
User: &types.HostAccountSpec{ | ||
Id: "userid", | ||
}, | ||
} | ||
|
||
_, err = methods.UpdateUser(ctx, c.Client, updateUserReq) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
removeUserReq := &types.RemoveUser{ | ||
This: ref, | ||
UserName: "userid", | ||
} | ||
|
||
_, err = methods.RemoveUser(ctx, c.Client, removeUserReq) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
_, err = methods.RemoveUser(ctx, c.Client, removeUserReq) | ||
if err == nil { | ||
t.Fatal("expect err; got nil") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters