github.com/vmware/govmomi@v0.51.0/vsan/simulator/simulator.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  	"github.com/google/uuid"
     9  
    10  	"github.com/vmware/govmomi/simulator"
    11  	"github.com/vmware/govmomi/vim25/soap"
    12  	vim "github.com/vmware/govmomi/vim25/types"
    13  	"github.com/vmware/govmomi/vsan"
    14  	"github.com/vmware/govmomi/vsan/methods"
    15  	"github.com/vmware/govmomi/vsan/types"
    16  )
    17  
    18  func init() {
    19  	simulator.RegisterEndpoint(func(s *simulator.Service, r *simulator.Registry) {
    20  		if r.IsVPX() {
    21  			s.RegisterSDK(New())
    22  		}
    23  	})
    24  }
    25  
    26  func New() *simulator.Registry {
    27  	r := simulator.NewRegistry()
    28  	r.Namespace = vsan.Namespace
    29  	r.Path = vsan.Path
    30  
    31  	r.Put(&StretchedClusterSystem{
    32  		ManagedObjectReference: vsan.VsanVcStretchedClusterSystem,
    33  	})
    34  
    35  	r.Put(&ClusterConfigSystem{
    36  		ManagedObjectReference: vsan.VsanVcClusterConfigSystemInstance,
    37  	})
    38  
    39  	return r
    40  }
    41  
    42  type StretchedClusterSystem struct {
    43  	vim.ManagedObjectReference
    44  }
    45  
    46  func (s *StretchedClusterSystem) VSANVcConvertToStretchedCluster(ctx *simulator.Context, req *types.VSANVcConvertToStretchedCluster) soap.HasFault {
    47  	task := simulator.CreateTask(s, "convertToStretchedCluster", func(*simulator.Task) (vim.AnyType, vim.BaseMethodFault) {
    48  		// TODO: validate req fields
    49  		return nil, nil
    50  	})
    51  
    52  	return &methods.VSANVcConvertToStretchedClusterBody{
    53  		Res: &types.VSANVcConvertToStretchedClusterResponse{
    54  			Returnval: task.Run(ctx),
    55  		},
    56  	}
    57  }
    58  
    59  type ClusterConfigSystem struct {
    60  	vim.ManagedObjectReference
    61  
    62  	Config map[vim.ManagedObjectReference]*types.VsanConfigInfoEx
    63  }
    64  
    65  func (s *ClusterConfigSystem) info(ref vim.ManagedObjectReference) *types.VsanConfigInfoEx {
    66  	if s.Config == nil {
    67  		s.Config = make(map[vim.ManagedObjectReference]*types.VsanConfigInfoEx)
    68  	}
    69  
    70  	info := s.Config[ref]
    71  	if info == nil {
    72  		info = &types.VsanConfigInfoEx{}
    73  		info.DefaultConfig = &vim.VsanClusterConfigInfoHostDefaultInfo{
    74  			Uuid: uuid.New().String(),
    75  		}
    76  		s.Config[ref] = info
    77  	}
    78  
    79  	return info
    80  }
    81  
    82  func (s *ClusterConfigSystem) VsanClusterGetConfig(ctx *simulator.Context, req *types.VsanClusterGetConfig) soap.HasFault {
    83  	return &methods.VsanClusterGetConfigBody{
    84  		Res: &types.VsanClusterGetConfigResponse{
    85  			Returnval: *s.info(req.Cluster),
    86  		},
    87  	}
    88  }
    89  
    90  func (s *ClusterConfigSystem) VsanClusterReconfig(ctx *simulator.Context, req *types.VsanClusterReconfig) soap.HasFault {
    91  	task := simulator.CreateTask(s, "vsanClusterReconfig", func(*simulator.Task) (vim.AnyType, vim.BaseMethodFault) {
    92  		// TODO: validate req fields
    93  		info := s.info(req.Cluster)
    94  		if req.VsanReconfigSpec.UnmapConfig != nil {
    95  			info.UnmapConfig = req.VsanReconfigSpec.UnmapConfig
    96  		}
    97  		if req.VsanReconfigSpec.FileServiceConfig != nil {
    98  			info.FileServiceConfig = req.VsanReconfigSpec.FileServiceConfig
    99  		}
   100  		return nil, nil
   101  	})
   102  
   103  	return &methods.VsanClusterReconfigBody{
   104  		Res: &types.VsanClusterReconfigResponse{
   105  			Returnval: task.Run(ctx),
   106  		},
   107  	}
   108  }