github.com/vmware/govmomi@v0.43.0/simulator/extension_manager.go (about)

     1  /*
     2  Copyright (c) 2024-2024 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  
    17  package simulator
    18  
    19  import (
    20  	"time"
    21  
    22  	"github.com/vmware/govmomi/vim25/methods"
    23  	"github.com/vmware/govmomi/vim25/mo"
    24  	"github.com/vmware/govmomi/vim25/soap"
    25  	"github.com/vmware/govmomi/vim25/types"
    26  )
    27  
    28  var ExtensionList = []types.Extension{
    29  	{
    30  		Description: &types.Description{
    31  			Label:   "vcsim",
    32  			Summary: "Go vCenter simulator",
    33  		},
    34  		Key:         "com.vmware.govmomi.simulator",
    35  		Company:     "VMware, Inc.",
    36  		Type:        "",
    37  		Version:     "0.37.0",
    38  		SubjectName: "",
    39  		Server:      nil,
    40  		Client:      nil,
    41  		TaskList: []types.ExtensionTaskTypeInfo{
    42  			{
    43  				TaskID: "com.vmware.govmomi.simulator.test",
    44  			},
    45  		},
    46  		EventList:              nil,
    47  		FaultList:              nil,
    48  		PrivilegeList:          nil,
    49  		ResourceList:           nil,
    50  		LastHeartbeatTime:      time.Now(),
    51  		HealthInfo:             (*types.ExtensionHealthInfo)(nil),
    52  		OvfConsumerInfo:        (*types.ExtensionOvfConsumerInfo)(nil),
    53  		ExtendedProductInfo:    (*types.ExtExtendedProductInfo)(nil),
    54  		ManagedEntityInfo:      nil,
    55  		ShownInSolutionManager: types.NewBool(false),
    56  		SolutionManagerInfo:    (*types.ExtSolutionManagerInfo)(nil),
    57  	},
    58  }
    59  
    60  type ExtensionManager struct {
    61  	mo.ExtensionManager
    62  }
    63  
    64  func (m *ExtensionManager) init(r *Registry) {
    65  	if r.IsVPX() && len(m.ExtensionList) == 0 {
    66  		m.ExtensionList = ExtensionList
    67  	}
    68  }
    69  
    70  func (m *ExtensionManager) FindExtension(ctx *Context, req *types.FindExtension) soap.HasFault {
    71  	body := &methods.FindExtensionBody{
    72  		Res: new(types.FindExtensionResponse),
    73  	}
    74  
    75  	for _, x := range m.ExtensionList {
    76  		if x.Key == req.ExtensionKey {
    77  			body.Res.Returnval = &x
    78  			break
    79  		}
    80  	}
    81  
    82  	return body
    83  }
    84  
    85  func (m *ExtensionManager) RegisterExtension(ctx *Context, req *types.RegisterExtension) soap.HasFault {
    86  	body := &methods.RegisterExtensionBody{}
    87  
    88  	for _, x := range m.ExtensionList {
    89  		if x.Key == req.Extension.Key {
    90  			body.Fault_ = Fault("", &types.InvalidArgument{
    91  				InvalidProperty: "extension.key",
    92  			})
    93  			return body
    94  		}
    95  	}
    96  
    97  	body.Res = new(types.RegisterExtensionResponse)
    98  	m.ExtensionList = append(m.ExtensionList, req.Extension)
    99  
   100  	f := mo.Field{Path: "extensionList", Key: req.Extension.Key}
   101  	ctx.Map.Update(m, []types.PropertyChange{
   102  		{Name: f.Path, Val: m.ExtensionList},
   103  		{Name: f.String(), Val: req.Extension, Op: types.PropertyChangeOpAdd},
   104  	})
   105  
   106  	return body
   107  }
   108  
   109  func (m *ExtensionManager) UnregisterExtension(ctx *Context, req *types.UnregisterExtension) soap.HasFault {
   110  	body := &methods.UnregisterExtensionBody{}
   111  
   112  	for i, x := range m.ExtensionList {
   113  		if x.Key == req.ExtensionKey {
   114  			m.ExtensionList = append(m.ExtensionList[:i], m.ExtensionList[i+1:]...)
   115  
   116  			f := mo.Field{Path: "extensionList", Key: req.ExtensionKey}
   117  			ctx.Map.Update(m, []types.PropertyChange{
   118  				{Name: f.Path, Val: m.ExtensionList},
   119  				{Name: f.String(), Op: types.PropertyChangeOpRemove},
   120  			})
   121  
   122  			body.Res = new(types.UnregisterExtensionResponse)
   123  			return body
   124  		}
   125  	}
   126  
   127  	body.Fault_ = Fault("", new(types.NotFound))
   128  
   129  	return body
   130  }
   131  
   132  func (m *ExtensionManager) UpdateExtension(ctx *Context, req *types.UpdateExtension) soap.HasFault {
   133  	body := &methods.UpdateExtensionBody{}
   134  
   135  	for i, x := range m.ExtensionList {
   136  		if x.Key == req.Extension.Key {
   137  			m.ExtensionList[i] = req.Extension
   138  
   139  			f := mo.Field{Path: "extensionList", Key: req.Extension.Key}
   140  			ctx.Map.Update(m, []types.PropertyChange{
   141  				{Name: f.Path, Val: m.ExtensionList},
   142  				{Name: f.String(), Val: req.Extension},
   143  			})
   144  
   145  			body.Res = new(types.UpdateExtensionResponse)
   146  			return body
   147  		}
   148  	}
   149  
   150  	body.Fault_ = Fault("", new(types.NotFound))
   151  
   152  	return body
   153  }
   154  
   155  func (m *ExtensionManager) SetExtensionCertificate(ctx *Context, req *types.SetExtensionCertificate) soap.HasFault {
   156  	body := &methods.SetExtensionCertificateBody{}
   157  
   158  	for _, x := range m.ExtensionList {
   159  		if x.Key == req.ExtensionKey {
   160  			// TODO: save req.CertificatePem for use with SessionManager.LoginExtensionByCertificate()
   161  
   162  			body.Res = new(types.SetExtensionCertificateResponse)
   163  			return body
   164  		}
   165  	}
   166  
   167  	body.Fault_ = Fault("", new(types.NotFound))
   168  
   169  	return body
   170  }