github.com/vmware/govmomi@v0.37.2/simulator/license_manager.go (about)

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