From 9ec310ac8ce53a209c14888a9272bcebb8984419 Mon Sep 17 00:00:00 2001 From: Doug MacEachern Date: Wed, 4 Dec 2024 20:11:23 -0800 Subject: [PATCH] vcsim: add UpdateAssignedLicense method - govc object.save includes licenseAssignmentManager property Signed-off-by: Doug MacEachern --- govc/object/save.go | 11 +++++--- govc/test/license.bats | 12 ++++++++- simulator/license_manager.go | 50 +++++++++++++++++++----------------- 3 files changed, 46 insertions(+), 27 deletions(-) diff --git a/govc/object/save.go b/govc/object/save.go index 0a88b6daa..38b8c1ac9 100644 --- a/govc/object/save.go +++ b/govc/object/save.go @@ -1,5 +1,5 @@ /* -Copyright (c) 2019-2024 VMware, Inc. All Rights Reserved. +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. @@ -290,10 +290,15 @@ func (cmd *save) Run(ctx context.Context, f *flag.FlagSet) error { for _, p := range content[0].PropSet { if c, ok := p.Val.(types.ServiceContent); ok { + var path []string for _, ref := range mo.References(c) { all := types.NewBool(true) switch ref.Type { - case "LicenseManager", "ServiceManager": + case "LicenseManager": + // avoid saving "licenses" property as it includes the keys + path = []string{"licenseAssignmentManager"} + all = nil + case "ServiceManager": all = nil } req.SpecSet = append(req.SpecSet, types.PropertyFilterSpec{ @@ -303,7 +308,7 @@ func (cmd *save) Run(ctx context.Context, f *flag.FlagSet) error { PropSet: []types.PropertySpec{{ Type: ref.Type, All: all, - PathSet: nil, + PathSet: path, }}, }) } diff --git a/govc/test/license.bats b/govc/test/license.bats index a97011f2e..e50dd6aa7 100755 --- a/govc/test/license.bats +++ b/govc/test/license.bats @@ -38,8 +38,18 @@ get_nlabel() { assert_equal "License is not valid for this product" "$(get_key 00000-00000-00000-00000-00002 <<<${output} | get_property diagnostic)" } +@test "license.assign" { + vcsim_env + + run govc license.assign -cluster DC0_C0 00000-00000-00000-00000-00000 + assert_success + + run govc license.assigned.ls + assert_success +} + @test "license.remove" { - esx_env + vcsim_env verify_evaluation diff --git a/simulator/license_manager.go b/simulator/license_manager.go index 609735966..749215955 100644 --- a/simulator/license_manager.go +++ b/simulator/license_manager.go @@ -1,11 +1,11 @@ /* -Copyright (c) 2017 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. You may obtain a copy of the License at - http://www.apache.org/licenses/LICENSE-2.0 +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, @@ -13,19 +13,6 @@ 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. */ -// Copyright 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 @@ -67,12 +54,19 @@ func (m *LicenseManager) init(r *Registry) { m.Licenses = []types.LicenseManagerLicenseInfo{EvalLicense} if r.IsVPX() { - am := Map.Put(&LicenseAssignmentManager{}).Reference() - m.LicenseAssignmentManager = &am + if m.LicenseAssignmentManager == nil { + m.LicenseAssignmentManager = &types.ManagedObjectReference{ + Type: "LicenseAssignmentManager", + Value: "LicenseAssignmentManager", + } + } + r.Put(&LicenseAssignmentManager{ + mo.LicenseAssignmentManager{Self: *m.LicenseAssignmentManager}, + }) } } -func (m *LicenseManager) AddLicense(req *types.AddLicense) soap.HasFault { +func (m *LicenseManager) AddLicense(ctx *Context, req *types.AddLicense) soap.HasFault { body := &methods.AddLicenseBody{ Res: &types.AddLicenseResponse{}, } @@ -94,7 +88,7 @@ func (m *LicenseManager) AddLicense(req *types.AddLicense) soap.HasFault { return body } -func (m *LicenseManager) RemoveLicense(req *types.RemoveLicense) soap.HasFault { +func (m *LicenseManager) RemoveLicense(ctx *Context, req *types.RemoveLicense) soap.HasFault { body := &methods.RemoveLicenseBody{ Res: &types.RemoveLicenseResponse{}, } @@ -108,7 +102,7 @@ func (m *LicenseManager) RemoveLicense(req *types.RemoveLicense) soap.HasFault { return body } -func (m *LicenseManager) UpdateLicenseLabel(req *types.UpdateLicenseLabel) soap.HasFault { +func (m *LicenseManager) UpdateLicenseLabel(ctx *Context, req *types.UpdateLicenseLabel) soap.HasFault { body := &methods.UpdateLicenseLabelBody{} for i := range m.Licenses { @@ -149,20 +143,20 @@ type LicenseAssignmentManager struct { mo.LicenseAssignmentManager } -func (m *LicenseAssignmentManager) QueryAssignedLicenses(req *types.QueryAssignedLicenses) soap.HasFault { +func (m *LicenseAssignmentManager) QueryAssignedLicenses(ctx *Context, req *types.QueryAssignedLicenses) soap.HasFault { body := &methods.QueryAssignedLicensesBody{ Res: &types.QueryAssignedLicensesResponse{}, } // EntityId can be a HostSystem or the vCenter InstanceUuid if req.EntityId != "" { - if req.EntityId != Map.content().About.InstanceUuid { + if req.EntityId != ctx.Map.content().About.InstanceUuid { id := types.ManagedObjectReference{ Type: "HostSystem", Value: req.EntityId, } - if Map.Get(id) == nil { + if ctx.Map.Get(id) == nil { return body } } @@ -178,6 +172,16 @@ func (m *LicenseAssignmentManager) QueryAssignedLicenses(req *types.QueryAssigne return body } +func (m *LicenseAssignmentManager) UpdateAssignedLicense(ctx *Context, req *types.UpdateAssignedLicense) soap.HasFault { + body := &methods.UpdateAssignedLicenseBody{ + Res: &types.UpdateAssignedLicenseResponse{ + Returnval: licenseInfo(req.LicenseKey, nil), + }, + } + + return body +} + func licenseInfo(key string, labels []types.KeyValue) types.LicenseManagerLicenseInfo { info := EvalLicense