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

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