Skip to content

Commit

Permalink
vcsim: add UpdateAssignedLicense method
Browse files Browse the repository at this point in the history
- govc object.save includes licenseAssignmentManager property

Signed-off-by: Doug MacEachern <dougm@broadcom.com>
  • Loading branch information
dougm committed Dec 5, 2024
1 parent 973a224 commit 5db2637
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 27 deletions.
11 changes: 8 additions & 3 deletions cli/object/save.go
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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{
Expand All @@ -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,
}},
})
}
Expand Down
12 changes: 11 additions & 1 deletion govc/test/license.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
50 changes: 27 additions & 23 deletions simulator/license_manager.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,18 @@
/*
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,
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

Expand Down Expand Up @@ -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{},
}
Expand All @@ -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{},
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}
}
Expand All @@ -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

Expand Down

0 comments on commit 5db2637

Please sign in to comment.