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

     1  /*
     2  Copyright (c) 2018 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/google/uuid"
    23  
    24  	"github.com/vmware/govmomi/pbm"
    25  	"github.com/vmware/govmomi/pbm/methods"
    26  	"github.com/vmware/govmomi/pbm/types"
    27  	"github.com/vmware/govmomi/simulator"
    28  	"github.com/vmware/govmomi/vim25/soap"
    29  	vim "github.com/vmware/govmomi/vim25/types"
    30  )
    31  
    32  var content = types.PbmServiceInstanceContent{
    33  	AboutInfo: types.PbmAboutInfo{
    34  		Name:         "PBM",
    35  		Version:      "2.0",
    36  		InstanceUuid: "df09f335-be97-4f33-8c27-315faaaad6fc",
    37  	},
    38  	SessionManager:            vim.ManagedObjectReference{Type: "PbmSessionManager", Value: "SessionManager"},
    39  	CapabilityMetadataManager: vim.ManagedObjectReference{Type: "PbmCapabilityMetadataManager", Value: "CapabilityMetadataManager"},
    40  	ProfileManager:            vim.ManagedObjectReference{Type: "PbmProfileProfileManager", Value: "ProfileManager"},
    41  	ComplianceManager:         vim.ManagedObjectReference{Type: "PbmComplianceManager", Value: "complianceManager"},
    42  	PlacementSolver:           vim.ManagedObjectReference{Type: "PbmPlacementSolver", Value: "placementSolver"},
    43  	ReplicationManager:        &vim.ManagedObjectReference{Type: "PbmReplicationManager", Value: "ReplicationManager"},
    44  }
    45  
    46  func init() {
    47  	simulator.RegisterEndpoint(func(s *simulator.Service, r *simulator.Registry) {
    48  		if r.IsVPX() {
    49  			s.RegisterSDK(New())
    50  		}
    51  	})
    52  }
    53  
    54  func New() *simulator.Registry {
    55  	r := simulator.NewRegistry()
    56  	r.Namespace = pbm.Namespace
    57  	r.Path = pbm.Path
    58  
    59  	r.Put(&ServiceInstance{
    60  		ManagedObjectReference: pbm.ServiceInstance,
    61  		Content:                content,
    62  	})
    63  
    64  	r.Put(&ProfileManager{
    65  		ManagedObjectReference: content.ProfileManager,
    66  	})
    67  
    68  	r.Put(&PlacementSolver{
    69  		ManagedObjectReference: content.PlacementSolver,
    70  	})
    71  
    72  	return r
    73  }
    74  
    75  type ServiceInstance struct {
    76  	vim.ManagedObjectReference
    77  
    78  	Content types.PbmServiceInstanceContent
    79  }
    80  
    81  func (s *ServiceInstance) PbmRetrieveServiceContent(_ *types.PbmRetrieveServiceContent) soap.HasFault {
    82  	return &methods.PbmRetrieveServiceContentBody{
    83  		Res: &types.PbmRetrieveServiceContentResponse{
    84  			Returnval: s.Content,
    85  		},
    86  	}
    87  }
    88  
    89  type ProfileManager struct {
    90  	vim.ManagedObjectReference
    91  }
    92  
    93  func (m *ProfileManager) PbmQueryProfile(req *types.PbmQueryProfile) soap.HasFault {
    94  	body := new(methods.PbmQueryProfileBody)
    95  	body.Res = new(types.PbmQueryProfileResponse)
    96  
    97  	for i := range profiles {
    98  		b, ok := profiles[i].(types.BasePbmCapabilityProfile)
    99  		if !ok {
   100  			continue
   101  		}
   102  		p := b.GetPbmCapabilityProfile()
   103  
   104  		if p.ResourceType != req.ResourceType {
   105  			continue
   106  		}
   107  
   108  		if req.ProfileCategory != "" {
   109  			if p.ProfileCategory != req.ProfileCategory {
   110  				continue
   111  			}
   112  		}
   113  
   114  		body.Res.Returnval = append(body.Res.Returnval, types.PbmProfileId{
   115  			UniqueId: p.ProfileId.UniqueId,
   116  		})
   117  	}
   118  
   119  	return body
   120  }
   121  
   122  func (m *ProfileManager) PbmQueryAssociatedProfile(req *types.PbmQueryAssociatedProfile) soap.HasFault {
   123  	body := new(methods.PbmQueryAssociatedProfileBody)
   124  	body.Res = new(types.PbmQueryAssociatedProfileResponse)
   125  
   126  	return body
   127  }
   128  
   129  func (m *ProfileManager) PbmQueryAssociatedProfiles(req *types.PbmQueryAssociatedProfiles) soap.HasFault {
   130  	body := new(methods.PbmQueryAssociatedProfilesBody)
   131  	body.Res = new(types.PbmQueryAssociatedProfilesResponse)
   132  
   133  	return body
   134  }
   135  
   136  func (m *ProfileManager) PbmRetrieveContent(req *types.PbmRetrieveContent) soap.HasFault {
   137  	body := new(methods.PbmRetrieveContentBody)
   138  	if len(req.ProfileIds) == 0 {
   139  		body.Fault_ = simulator.Fault("", new(vim.InvalidRequest))
   140  		return body
   141  	}
   142  
   143  	var res []types.BasePbmProfile
   144  
   145  	match := func(id string) bool {
   146  		for _, p := range profiles {
   147  			if id == p.GetPbmProfile().ProfileId.UniqueId {
   148  				res = append(res, p)
   149  				return true
   150  			}
   151  		}
   152  		return false
   153  	}
   154  
   155  	for _, p := range req.ProfileIds {
   156  		if match(p.UniqueId) {
   157  			continue
   158  		}
   159  
   160  		body.Fault_ = simulator.Fault("", &vim.InvalidArgument{InvalidProperty: "profileId"})
   161  		return body
   162  	}
   163  
   164  	body.Res = &types.PbmRetrieveContentResponse{Returnval: res}
   165  
   166  	return body
   167  }
   168  
   169  func (m *ProfileManager) PbmCreate(ctx *simulator.Context, req *types.PbmCreate) soap.HasFault {
   170  	body := new(methods.PbmCreateBody)
   171  	body.Res = new(types.PbmCreateResponse)
   172  
   173  	profile := &types.PbmCapabilityProfile{
   174  		PbmProfile: types.PbmProfile{
   175  			ProfileId: types.PbmProfileId{
   176  				UniqueId: uuid.New().String(),
   177  			},
   178  			Name:            req.CreateSpec.Name,
   179  			Description:     req.CreateSpec.Description,
   180  			CreationTime:    time.Now(),
   181  			CreatedBy:       ctx.Session.UserName,
   182  			LastUpdatedTime: time.Now(),
   183  			LastUpdatedBy:   ctx.Session.UserName,
   184  		},
   185  		ProfileCategory:          req.CreateSpec.Category,
   186  		ResourceType:             req.CreateSpec.ResourceType,
   187  		Constraints:              req.CreateSpec.Constraints,
   188  		GenerationId:             0,
   189  		IsDefault:                false,
   190  		SystemCreatedProfileType: "",
   191  		LineOfService:            "",
   192  	}
   193  
   194  	profiles = append(profiles, profile)
   195  	body.Res.Returnval.UniqueId = profile.PbmProfile.ProfileId.UniqueId
   196  
   197  	return body
   198  }
   199  
   200  func (m *ProfileManager) PbmDelete(req *types.PbmDelete) soap.HasFault {
   201  	body := new(methods.PbmDeleteBody)
   202  
   203  	for _, id := range req.ProfileId {
   204  		for i, p := range profiles {
   205  			pid := p.GetPbmProfile().ProfileId
   206  
   207  			if id == pid {
   208  				profiles = append(profiles[:i], profiles[i+1:]...)
   209  				break
   210  			}
   211  		}
   212  	}
   213  
   214  	body.Res = new(types.PbmDeleteResponse)
   215  
   216  	return body
   217  }
   218  
   219  type PlacementSolver struct {
   220  	vim.ManagedObjectReference
   221  }
   222  
   223  func (m *PlacementSolver) PbmCheckRequirements(req *types.PbmCheckRequirements) soap.HasFault {
   224  	body := new(methods.PbmCheckRequirementsBody)
   225  	body.Res = new(types.PbmCheckRequirementsResponse)
   226  
   227  	for _, ds := range simulator.Map.All("Datastore") {
   228  		// TODO: filter
   229  		ref := ds.Reference()
   230  		body.Res.Returnval = append(body.Res.Returnval, types.PbmPlacementCompatibilityResult{
   231  			Hub: types.PbmPlacementHub{
   232  				HubType: ref.Type,
   233  				HubId:   ref.Value,
   234  			},
   235  			MatchingResources: nil,
   236  			HowMany:           0,
   237  			Utilization:       nil,
   238  			Warning:           nil,
   239  			Error:             nil,
   240  		})
   241  	}
   242  
   243  	return body
   244  }
   245  
   246  func (m *PlacementSolver) PbmCheckCompatibility(req *types.PbmCheckCompatibility) soap.HasFault {
   247  	body := new(methods.PbmCheckCompatibilityBody)
   248  	body.Res = new(types.PbmCheckCompatibilityResponse)
   249  
   250  	for _, ds := range simulator.Map.All("Datastore") {
   251  		// TODO: filter
   252  		ref := ds.Reference()
   253  		body.Res.Returnval = append(body.Res.Returnval, types.PbmPlacementCompatibilityResult{
   254  			Hub: types.PbmPlacementHub{
   255  				HubType: ref.Type,
   256  				HubId:   ref.Value,
   257  			},
   258  			MatchingResources: nil,
   259  			HowMany:           0,
   260  			Utilization:       nil,
   261  			Warning:           nil,
   262  			Error:             nil,
   263  		})
   264  	}
   265  
   266  	return body
   267  }