Skip to content

Commit

Permalink
vcsim: add ExtensionManager support
Browse files Browse the repository at this point in the history
  • Loading branch information
dougm committed Mar 18, 2024
1 parent d62c0ac commit d9af2a2
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 15 deletions.
15 changes: 2 additions & 13 deletions govc/test/extension.bats
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
load test_helper

@test "extension" {
vcsim_env_todo

govc extension.info | grep Name: | grep govc-test | awk '{print $2}' | $xargs -r govc extension.unregister
vcsim_env

run govc extension.info enoent
assert_failure
Expand Down Expand Up @@ -51,16 +49,7 @@ EOS
run govc extension.setcert -cert-pem '+' $id
assert_success

# test client certificate authentication
(
# remove password from env, set user to extension id and turn of session cache
govc_url_to_vars
unset GOVC_PASSWORD
GOVC_USERNAME=$id
export GOVC_PERSIST_SESSION=false
run govc about -cert "${id}.crt" -key "${id}.key"
assert_success
)
# client certificate authentication is tested in session.bats

# remove generated cert and key
rm ${id}.{crt,key}
Expand Down
137 changes: 137 additions & 0 deletions simulator/extension_manager.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/*
Copyright (c) 2024-2024 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 (
"time"

"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 ExtensionList = []types.Extension{
{
Description: &types.Description{
Label: "vcsim",
Summary: "Go vCenter simulator",
},
Key: "com.vmware.govmomi.simulator",
Company: "VMware, Inc.",
Type: "",
Version: "0.37.0",
SubjectName: "",
Server: nil,
Client: nil,
TaskList: []types.ExtensionTaskTypeInfo{
{
TaskID: "com.vmware.govmomi.simulator.test",
},
},
EventList: nil,
FaultList: nil,
PrivilegeList: nil,
ResourceList: nil,
LastHeartbeatTime: time.Now(),
HealthInfo: (*types.ExtensionHealthInfo)(nil),
OvfConsumerInfo: (*types.ExtensionOvfConsumerInfo)(nil),
ExtendedProductInfo: (*types.ExtExtendedProductInfo)(nil),
ManagedEntityInfo: nil,
ShownInSolutionManager: types.NewBool(false),
SolutionManagerInfo: (*types.ExtSolutionManagerInfo)(nil),
},
}

type ExtensionManager struct {
mo.ExtensionManager
}

func (m *ExtensionManager) init(r *Registry) {
if r.IsVPX() && len(m.ExtensionList) == 0 {
m.ExtensionList = ExtensionList
}
}

func (m *ExtensionManager) RegisterExtension(ctx *Context, req *types.RegisterExtension) soap.HasFault {
body := &methods.RegisterExtensionBody{}

for _, x := range m.ExtensionList {
if x.Key == req.Extension.Key {
body.Fault_ = Fault("", &types.InvalidArgument{
InvalidProperty: "extension.key",
})
return body
}
}

body.Res = new(types.RegisterExtensionResponse)
m.ExtensionList = append(m.ExtensionList, req.Extension)

return body
}

func (m *ExtensionManager) UnregisterExtension(ctx *Context, req *types.UnregisterExtension) soap.HasFault {
body := &methods.UnregisterExtensionBody{}

for i, x := range m.ExtensionList {
if x.Key == req.ExtensionKey {
m.ExtensionList = append(m.ExtensionList[:i], m.ExtensionList[i+1:]...)

body.Res = new(types.UnregisterExtensionResponse)
return body
}
}

body.Fault_ = Fault("", new(types.NotFound))

return body
}

func (m *ExtensionManager) UpdateExtension(ctx *Context, req *types.UpdateExtension) soap.HasFault {
body := &methods.UpdateExtensionBody{}

for i, x := range m.ExtensionList {
if x.Key == req.Extension.Key {
m.ExtensionList[i] = req.Extension

body.Res = new(types.UpdateExtensionResponse)
return body
}
}

body.Fault_ = Fault("", new(types.NotFound))

return body
}

func (m *ExtensionManager) SetExtensionCertificate(ctx *Context, req *types.SetExtensionCertificate) soap.HasFault {
body := &methods.SetExtensionCertificateBody{}

for _, x := range m.ExtensionList {
if x.Key == req.ExtensionKey {
// TODO: save req.CertificatePem for use with SessionManager.LoginExtensionByCertificate()

body.Res = new(types.SetExtensionCertificateResponse)
return body
}
}

body.Fault_ = Fault("", new(types.NotFound))

return body
}
3 changes: 2 additions & 1 deletion simulator/model.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2017-2023 VMware, Inc. All Rights Reserved.
Copyright (c) 2017-2024 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.
Expand Down Expand Up @@ -243,6 +243,7 @@ var kinds = map[string]reflect.Type{
"DistributedVirtualSwitchManager": reflect.TypeOf((*DistributedVirtualSwitchManager)(nil)).Elem(),
"EnvironmentBrowser": reflect.TypeOf((*EnvironmentBrowser)(nil)).Elem(),
"EventManager": reflect.TypeOf((*EventManager)(nil)).Elem(),
"ExtensionManager": reflect.TypeOf((*ExtensionManager)(nil)).Elem(),
"FileManager": reflect.TypeOf((*FileManager)(nil)).Elem(),
"Folder": reflect.TypeOf((*Folder)(nil)).Elem(),
"GuestOperationsManager": reflect.TypeOf((*GuestOperationsManager)(nil)).Elem(),
Expand Down
7 changes: 6 additions & 1 deletion simulator/registry.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2017-2023 VMware, Inc. All Rights Reserved.
Copyright (c) 2017-2024 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.
Expand Down Expand Up @@ -572,6 +572,11 @@ func (r *Registry) TenantManager() *TenantManager {
return r.Get(r.content().TenantManager.Reference()).(*TenantManager)
}

// ExtensionManager returns the ExtensionManager singleton
func (r *Registry) ExtensionManager() *ExtensionManager {
return r.Get(r.content().ExtensionManager.Reference()).(*ExtensionManager)
}

func (r *Registry) MarshalJSON() ([]byte, error) {
r.m.Lock()
defer r.m.Unlock()
Expand Down

0 comments on commit d9af2a2

Please sign in to comment.