github.com/openshift/installer@v1.4.17/pkg/asset/installconfig/vsphere/mock/vsphere_sim.go (about) 1 package mock 2 3 import ( 4 "context" 5 "crypto/tls" 6 "encoding/pem" 7 "errors" 8 "io/fs" 9 "os" 10 "strconv" 11 12 "github.com/vmware/govmomi/find" 13 "github.com/vmware/govmomi/session" 14 "github.com/vmware/govmomi/simulator" 15 "github.com/vmware/govmomi/simulator/esx" 16 "github.com/vmware/govmomi/vim25" 17 "github.com/vmware/govmomi/vim25/soap" 18 19 // required to initialize the REST endpoint. 20 _ "github.com/vmware/govmomi/vapi/rest" 21 // required to initialize the VAPI endpoint. 22 _ "github.com/vmware/govmomi/vapi/simulator" 23 ) 24 25 const ( 26 esxi7U2BuildNumber int = 17630552 27 esxi7U2Version string = "7.0.2" 28 vcenter7U2BuildNumber int = 17694817 29 vcenter7U2Version string = "7.0.2" 30 ) 31 32 // StartSimulator starts an instance of the simulator which listens on 127.0.0.1. 33 // Call GetClient to retrieve a vim25.client which will connect to and trust this 34 // simulator 35 func StartSimulator(setVersionToSupported bool) (*simulator.Server, error) { 36 model := simulator.VPX() 37 38 // Change the simulated vCenter and ESXi hosts 39 // to the version and build we support. 40 if setVersionToSupported { 41 esx.HostSystem.Config.Product.Build = strconv.Itoa(esxi7U2BuildNumber) 42 esx.HostSystem.Config.Product.Version = esxi7U2Version 43 model.ServiceContent.About.Build = strconv.Itoa(vcenter7U2BuildNumber) 44 model.ServiceContent.About.Version = vcenter7U2Version 45 } 46 47 model.Folder = 1 48 model.Datacenter = 5 49 model.OpaqueNetwork = 1 50 err := model.Create() 51 if err != nil { 52 return nil, err 53 } 54 55 model.Service.TLS = new(tls.Config) 56 57 model.Service.TLS.ServerName = "127.0.0.1" 58 model.Service.RegisterEndpoints = true 59 server := model.Service.NewServer() 60 return server, nil 61 } 62 63 // GetClient returns a vim25 client which connects to and trusts the simulator 64 func GetClient(server *simulator.Server) (*vim25.Client, *session.Manager, error) { 65 tmpCAdir := "/tmp/vcsimca" 66 err := os.Mkdir(tmpCAdir, os.ModePerm) 67 68 if err != nil { 69 // If the error is not file existing return err 70 if !errors.Is(err, fs.ErrExist) { 71 return nil, nil, err 72 } 73 } 74 pemBlock := pem.Block{ 75 Type: "CERTIFICATE", 76 Headers: nil, 77 Bytes: server.TLS.Certificates[0].Certificate[0], 78 } 79 tempFile, err := os.CreateTemp(tmpCAdir, "*.pem") 80 if err != nil { 81 return nil, nil, err 82 } 83 _, err = tempFile.Write(pem.EncodeToMemory(&pemBlock)) 84 if err != nil { 85 return nil, nil, err 86 } 87 88 soapClient := soap.NewClient(server.URL, false) 89 err = soapClient.SetRootCAs(tempFile.Name()) 90 if err != nil { 91 return nil, nil, err 92 } 93 vimClient, err := vim25.NewClient(context.TODO(), soapClient) 94 if err != nil { 95 return nil, nil, err 96 } 97 sessionMgr := session.NewManager(vimClient) 98 if sessionMgr == nil { 99 return nil, nil, errors.New("unable to retrieve session manager") 100 } 101 if server.URL.User != nil { 102 err = sessionMgr.Login(context.TODO(), server.URL.User) 103 if err != nil { 104 return nil, nil, err 105 } 106 } 107 return vimClient, sessionMgr, err 108 } 109 110 // GetFinder returns an object finder 111 func GetFinder(server *simulator.Server) (*find.Finder, error) { 112 client, _, err := GetClient(server) 113 if err != nil { 114 return nil, err 115 } 116 return find.NewFinder(client), nil 117 }