From d9af2a26f72081fca5a29613d900ef235c496a50 Mon Sep 17 00:00:00 2001 From: Doug MacEachern Date: Sun, 17 Mar 2024 13:19:12 -0700 Subject: [PATCH] vcsim: add ExtensionManager support --- govc/test/extension.bats | 15 +--- simulator/extension_manager.go | 137 +++++++++++++++++++++++++++++++++ simulator/model.go | 3 +- simulator/registry.go | 7 +- 4 files changed, 147 insertions(+), 15 deletions(-) create mode 100644 simulator/extension_manager.go diff --git a/govc/test/extension.bats b/govc/test/extension.bats index 6f8ad8373..0c20b8198 100755 --- a/govc/test/extension.bats +++ b/govc/test/extension.bats @@ -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 @@ -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} diff --git a/simulator/extension_manager.go b/simulator/extension_manager.go new file mode 100644 index 000000000..60a46c418 --- /dev/null +++ b/simulator/extension_manager.go @@ -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 +} diff --git a/simulator/model.go b/simulator/model.go index 1d65c5944..e14fd7508 100644 --- a/simulator/model.go +++ b/simulator/model.go @@ -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. @@ -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(), diff --git a/simulator/registry.go b/simulator/registry.go index 4b3e33060..7c3c7776b 100644 --- a/simulator/registry.go +++ b/simulator/registry.go @@ -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. @@ -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()