1/* 2Copyright (c) 2017 VMware, Inc. All Rights Reserved. 3 4Licensed under the Apache License, Version 2.0 (the "License"); 5you may not use this file except in compliance with the License. 6You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10Unless required by applicable law or agreed to in writing, software 11distributed under the License is distributed on an "AS IS" BASIS, 12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13See the License for the specific language governing permissions and 14limitations under the License. 15*/ 16// Copyright 2017 VMware, Inc. All Rights Reserved. 17// 18// Licensed under the Apache License, Version 2.0 (the "License"); 19// you may not use this file except in compliance with the License. 20// You may obtain a copy of the License at 21// 22// http://www.apache.org/licenses/LICENSE-2.0 23// 24// Unless required by applicable law or agreed to in writing, software 25// distributed under the License is distributed on an "AS IS" BASIS, 26// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 27// See the License for the specific language governing permissions and 28// limitations under the License. 29 30package simulator 31 32import ( 33 "github.com/vmware/govmomi/object" 34 "github.com/vmware/govmomi/vim25/methods" 35 "github.com/vmware/govmomi/vim25/mo" 36 "github.com/vmware/govmomi/vim25/soap" 37 "github.com/vmware/govmomi/vim25/types" 38) 39 40// EvalLicense is the default license 41var EvalLicense = types.LicenseManagerLicenseInfo{ 42 LicenseKey: "00000-00000-00000-00000-00000", 43 EditionKey: "eval", 44 Name: "Evaluation Mode", 45 Properties: []types.KeyAnyValue{ 46 { 47 Key: "feature", 48 Value: types.KeyValue{ 49 Key: "serialuri:2", 50 Value: "Remote virtual Serial Port Concentrator", 51 }, 52 }, 53 { 54 Key: "feature", 55 Value: types.KeyValue{ 56 Key: "dvs", 57 Value: "vSphere Distributed Switch", 58 }, 59 }, 60 }, 61} 62 63type LicenseManager struct { 64 mo.LicenseManager 65} 66 67func NewLicenseManager(ref types.ManagedObjectReference) object.Reference { 68 m := &LicenseManager{} 69 m.Self = ref 70 m.Licenses = []types.LicenseManagerLicenseInfo{EvalLicense} 71 72 if Map.IsVPX() { 73 am := Map.Put(&LicenseAssignmentManager{}).Reference() 74 m.LicenseAssignmentManager = &am 75 } 76 77 return m 78} 79 80func (m *LicenseManager) AddLicense(req *types.AddLicense) soap.HasFault { 81 body := &methods.AddLicenseBody{ 82 Res: &types.AddLicenseResponse{}, 83 } 84 85 for _, license := range m.Licenses { 86 if license.LicenseKey == req.LicenseKey { 87 body.Res.Returnval = licenseInfo(license.LicenseKey, license.Labels) 88 return body 89 } 90 } 91 92 m.Licenses = append(m.Licenses, types.LicenseManagerLicenseInfo{ 93 LicenseKey: req.LicenseKey, 94 Labels: req.Labels, 95 }) 96 97 body.Res.Returnval = licenseInfo(req.LicenseKey, req.Labels) 98 99 return body 100} 101 102func (m *LicenseManager) RemoveLicense(req *types.RemoveLicense) soap.HasFault { 103 body := &methods.RemoveLicenseBody{ 104 Res: &types.RemoveLicenseResponse{}, 105 } 106 107 for i, license := range m.Licenses { 108 if req.LicenseKey == license.LicenseKey { 109 m.Licenses = append(m.Licenses[:i], m.Licenses[i+1:]...) 110 return body 111 } 112 } 113 return body 114} 115 116type LicenseAssignmentManager struct { 117 mo.LicenseAssignmentManager 118} 119 120func (m *LicenseAssignmentManager) QueryAssignedLicenses(req *types.QueryAssignedLicenses) soap.HasFault { 121 body := &methods.QueryAssignedLicensesBody{ 122 Res: &types.QueryAssignedLicensesResponse{}, 123 } 124 125 // EntityId can be a HostSystem or the vCenter InstanceUuid 126 if req.EntityId != "" { 127 if req.EntityId != Map.content().About.InstanceUuid { 128 id := types.ManagedObjectReference{ 129 Type: "HostSystem", 130 Value: req.EntityId, 131 } 132 133 if Map.Get(id) == nil { 134 return body 135 } 136 } 137 } 138 139 body.Res.Returnval = []types.LicenseAssignmentManagerLicenseAssignment{ 140 { 141 EntityId: req.EntityId, 142 AssignedLicense: EvalLicense, 143 }, 144 } 145 146 return body 147} 148 149func licenseInfo(key string, labels []types.KeyValue) types.LicenseManagerLicenseInfo { 150 info := EvalLicense 151 152 info.LicenseKey = key 153 info.Labels = labels 154 155 return info 156} 157