github.com/vmware/govmomi@v0.37.1/lookup/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  	"net/url"
    21  	"strings"
    22  	"sync"
    23  
    24  	"github.com/vmware/govmomi/lookup"
    25  	"github.com/vmware/govmomi/lookup/methods"
    26  	"github.com/vmware/govmomi/lookup/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.LookupServiceContent{
    33  	LookupService:                vim.ManagedObjectReference{Type: "LookupLookupService", Value: "lookupService"},
    34  	ServiceRegistration:          &vim.ManagedObjectReference{Type: "LookupServiceRegistration", Value: "ServiceRegistration"},
    35  	DeploymentInformationService: vim.ManagedObjectReference{Type: "LookupDeploymentInformationService", Value: "deploymentInformationService"},
    36  	L10n:                         vim.ManagedObjectReference{Type: "LookupL10n", Value: "l10n"},
    37  }
    38  
    39  func init() {
    40  	simulator.RegisterEndpoint(func(s *simulator.Service, r *simulator.Registry) {
    41  		if r.IsVPX() {
    42  			s.RegisterSDK(New())
    43  		}
    44  	})
    45  }
    46  
    47  func New() *simulator.Registry {
    48  	r := simulator.NewRegistry()
    49  	r.Namespace = lookup.Namespace
    50  	r.Path = lookup.Path
    51  
    52  	r.Put(&ServiceInstance{
    53  		ManagedObjectReference: lookup.ServiceInstance,
    54  		Content:                content,
    55  		register: func() {
    56  			r.Put(&ServiceRegistration{
    57  				ManagedObjectReference: *content.ServiceRegistration,
    58  				Info:                   registrationInfo(),
    59  			})
    60  		},
    61  	})
    62  
    63  	return r
    64  }
    65  
    66  type ServiceInstance struct {
    67  	vim.ManagedObjectReference
    68  
    69  	Content types.LookupServiceContent
    70  
    71  	instance sync.Once
    72  	register func()
    73  }
    74  
    75  func (s *ServiceInstance) RetrieveServiceContent(_ *types.RetrieveServiceContent) soap.HasFault {
    76  	// defer register to this point to ensure we can include vcsim's cert in ServiceEndpoints.SslTrust
    77  	// TODO: we should be able to register within New(), but this is the only place that currently depends on vcsim's cert.
    78  	s.instance.Do(s.register)
    79  
    80  	return &methods.RetrieveServiceContentBody{
    81  		Res: &types.RetrieveServiceContentResponse{
    82  			Returnval: s.Content,
    83  		},
    84  	}
    85  }
    86  
    87  type ServiceRegistration struct {
    88  	vim.ManagedObjectReference
    89  
    90  	Info []types.LookupServiceRegistrationInfo
    91  }
    92  
    93  func (s *ServiceRegistration) GetSiteId(_ *types.GetSiteId) soap.HasFault {
    94  	return &methods.GetSiteIdBody{
    95  		Res: &types.GetSiteIdResponse{
    96  			Returnval: siteID,
    97  		},
    98  	}
    99  }
   100  
   101  func matchServiceType(filter, info *types.LookupServiceRegistrationServiceType) bool {
   102  	if filter.Product != "" {
   103  		if filter.Product != info.Product {
   104  			return false
   105  		}
   106  	}
   107  
   108  	if filter.Type != "" {
   109  		if filter.Type != info.Type {
   110  			return false
   111  		}
   112  	}
   113  
   114  	return true
   115  }
   116  
   117  func matchEndpointType(filter, info *types.LookupServiceRegistrationEndpointType) bool {
   118  	if filter.Protocol != "" {
   119  		if filter.Protocol != info.Protocol {
   120  			return false
   121  		}
   122  	}
   123  
   124  	if filter.Type != "" {
   125  		if filter.Type != info.Type {
   126  			return false
   127  		}
   128  	}
   129  
   130  	return true
   131  }
   132  
   133  func (s *ServiceRegistration) List(req *types.List) soap.HasFault {
   134  	body := new(methods.ListBody)
   135  	filter := req.FilterCriteria
   136  
   137  	if filter == nil {
   138  		// This is what a real PSC returns if FilterCriteria is nil.
   139  		body.Fault_ = simulator.Fault("LookupFaultServiceFault", &vim.SystemError{
   140  			Reason: "Invalid fault",
   141  		})
   142  		return body
   143  	}
   144  	body.Res = new(types.ListResponse)
   145  
   146  	for _, info := range s.Info {
   147  		if filter.SiteId != "" {
   148  			if filter.SiteId != info.SiteId {
   149  				continue
   150  			}
   151  		}
   152  		if filter.NodeId != "" {
   153  			if filter.NodeId != info.NodeId {
   154  				continue
   155  			}
   156  		}
   157  		if filter.ServiceType != nil {
   158  			if !matchServiceType(filter.ServiceType, &info.ServiceType) {
   159  				continue
   160  			}
   161  		}
   162  		if filter.EndpointType != nil {
   163  			services := info.ServiceEndpoints
   164  			info.ServiceEndpoints = nil
   165  			for _, service := range services {
   166  				if !matchEndpointType(filter.EndpointType, &service.EndpointType) {
   167  					continue
   168  				}
   169  				info.ServiceEndpoints = append(info.ServiceEndpoints, service)
   170  			}
   171  			if len(info.ServiceEndpoints) == 0 {
   172  				continue
   173  			}
   174  		}
   175  		body.Res.Returnval = append(body.Res.Returnval, info)
   176  	}
   177  
   178  	return body
   179  }
   180  
   181  // BreakLookupServiceURLs makes the path of all lookup service urls invalid
   182  func BreakLookupServiceURLs() {
   183  	setting := simulator.Map.OptionManager().Setting
   184  
   185  	for _, s := range setting {
   186  		o := s.GetOptionValue()
   187  		if strings.HasSuffix(o.Key, ".uri") {
   188  			val := o.Value.(string)
   189  			u, _ := url.Parse(val)
   190  			u.Path = "/enoent" + u.Path
   191  			o.Value = u.String()
   192  		}
   193  	}
   194  }