github.com/cloudfoundry-incubator/stembuild@v0.0.0-20211223202937-5b61d62226c6/iaas_cli/iaas_clients/factory/vcenter_manager_factory.go (about) 1 package vcenter_client_factory 2 3 import ( 4 "context" 5 "net/url" 6 "time" 7 8 "github.com/vmware/govmomi/session" 9 "github.com/vmware/govmomi/vim25" 10 11 "github.com/cloudfoundry-incubator/stembuild/iaas_cli/iaas_clients/vcenter_manager" 12 "github.com/vmware/govmomi" 13 "github.com/vmware/govmomi/find" 14 "github.com/vmware/govmomi/vim25/soap" 15 ) 16 17 //go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 . Vim25ClientCreator 18 type Vim25ClientCreator interface { 19 NewClient(ctx context.Context, rt soap.RoundTripper) (*vim25.Client, error) 20 } 21 22 type ClientCreator struct { 23 } 24 25 func (g *ClientCreator) NewClient(ctx context.Context, rt soap.RoundTripper) (*vim25.Client, error) { 26 27 vimClient, err := vim25.NewClient(ctx, rt) 28 if err != nil { 29 return nil, err 30 } 31 32 return vimClient, nil 33 } 34 35 //go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 . FinderCreator 36 type FinderCreator interface { 37 NewFinder(client *vim25.Client, all bool) *find.Finder 38 } 39 40 type GovmomiFinderCreator struct { 41 } 42 43 func (g *GovmomiFinderCreator) NewFinder(client *vim25.Client, all bool) *find.Finder { 44 return find.NewFinder(client, all) 45 } 46 47 type ManagerFactory struct { 48 Config FactoryConfig 49 } 50 51 type FactoryConfig struct { 52 VCenterServer string 53 Username string 54 Password string 55 ClientCreator Vim25ClientCreator 56 FinderCreator FinderCreator 57 RootCACertPath string 58 } 59 60 func (f *ManagerFactory) SetConfig(config FactoryConfig) { 61 f.Config = config 62 } 63 64 func (f *ManagerFactory) VCenterManager(ctx context.Context) (*vcenter_manager.VCenterManager, error) { 65 66 govmomiClient, err := f.govmomiClient(ctx) 67 if err != nil { 68 return nil, err 69 } 70 71 finder := f.Config.FinderCreator.NewFinder(govmomiClient.Client, false) 72 73 return vcenter_manager.NewVCenterManager(govmomiClient, govmomiClient.Client, finder, f.Config.Username, f.Config.Password) 74 75 } 76 77 func (f *ManagerFactory) govmomiClient(ctx context.Context) (*govmomi.Client, error) { 78 79 sc, err := f.soapClient() 80 if err != nil { 81 return nil, err 82 } 83 84 vc, err := f.vimClient(ctx, sc) 85 if err != nil { 86 return nil, err 87 } 88 89 return &govmomi.Client{ 90 Client: vc, 91 SessionManager: session.NewManager(vc), 92 }, nil 93 94 } 95 96 func (f *ManagerFactory) soapClient() (*soap.Client, error) { 97 vCenterURL, err := soap.ParseURL(f.Config.VCenterServer) 98 if err != nil { 99 return nil, err 100 } 101 credentials := url.UserPassword(f.Config.Username, f.Config.Password) 102 vCenterURL.User = credentials 103 104 soapClient := soap.NewClient(vCenterURL, false) 105 106 if f.Config.RootCACertPath != "" { 107 err = soapClient.SetRootCAs(f.Config.RootCACertPath) 108 if err != nil { 109 return nil, err 110 } 111 } 112 113 return soapClient, nil 114 } 115 116 func (f *ManagerFactory) vimClient(ctx context.Context, soapClient *soap.Client) (*vim25.Client, error) { 117 vimClient, err := f.Config.ClientCreator.NewClient(ctx, soapClient) 118 if err != nil { 119 return nil, err 120 } 121 122 vimClient.RoundTripper = session.KeepAlive(vimClient.RoundTripper, 10*time.Minute) 123 return vimClient, nil 124 }