license_manager.go 4.7 KB

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