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
116func (m *LicenseManager) UpdateLicenseLabel(req *types.UpdateLicenseLabel) soap.HasFault {
117	body := &methods.UpdateLicenseLabelBody{}
118
119	for i := range m.Licenses {
120		license := &m.Licenses[i]
121
122		if req.LicenseKey != license.LicenseKey {
123			continue
124		}
125
126		body.Res = new(types.UpdateLicenseLabelResponse)
127
128		for j := range license.Labels {
129			label := &license.Labels[j]
130
131			if label.Key == req.LabelKey {
132				if req.LabelValue == "" {
133					license.Labels = append(license.Labels[:i], license.Labels[i+1:]...)
134				} else {
135					label.Value = req.LabelValue
136				}
137				return body
138			}
139		}
140
141		license.Labels = append(license.Labels, types.KeyValue{
142			Key:   req.LabelKey,
143			Value: req.LabelValue,
144		})
145
146		return body
147	}
148
149	body.Fault_ = Fault("", &types.InvalidArgument{InvalidProperty: "licenseKey"})
150	return body
151}
152
153type LicenseAssignmentManager struct {
154	mo.LicenseAssignmentManager
155}
156
157func (m *LicenseAssignmentManager) QueryAssignedLicenses(req *types.QueryAssignedLicenses) soap.HasFault {
158	body := &methods.QueryAssignedLicensesBody{
159		Res: &types.QueryAssignedLicensesResponse{},
160	}
161
162	// EntityId can be a HostSystem or the vCenter InstanceUuid
163	if req.EntityId != "" {
164		if req.EntityId != Map.content().About.InstanceUuid {
165			id := types.ManagedObjectReference{
166				Type:  "HostSystem",
167				Value: req.EntityId,
168			}
169
170			if Map.Get(id) == nil {
171				return body
172			}
173		}
174	}
175
176	body.Res.Returnval = []types.LicenseAssignmentManagerLicenseAssignment{
177		{
178			EntityId:        req.EntityId,
179			AssignedLicense: EvalLicense,
180		},
181	}
182
183	return body
184}
185
186func licenseInfo(key string, labels []types.KeyValue) types.LicenseManagerLicenseInfo {
187	info := EvalLicense
188
189	info.LicenseKey = key
190	info.Labels = labels
191
192	return info
193}
194