From 10e08c1c46a1cbf7db2091cbbf37dd50dc25ad80 Mon Sep 17 00:00:00 2001 From: Yousong Zhou Date: Thu, 19 Mar 2020 15:00:06 +0800 Subject: [PATCH] esxi: use ESXiClientConfig --- cmd/esxicli/main.go | 9 ++- pkg/multicloud/esxi/manager.go | 73 ++++++++++++++++++------ pkg/multicloud/esxi/provider/provider.go | 6 +- 3 files changed, 68 insertions(+), 20 deletions(-) diff --git a/cmd/esxicli/main.go b/cmd/esxicli/main.go index c7ad77b712..7114b230a5 100644 --- a/cmd/esxicli/main.go +++ b/cmd/esxicli/main.go @@ -88,7 +88,14 @@ func newClient(options *BaseOptions) (*esxi.SESXiClient, error) { return nil, fmt.Errorf("Missing password") } - return esxi.NewESXiClient2("", "", options.Host, options.Port, options.Account, options.Password, false) + return esxi.NewESXiClient2( + esxi.NewESXiClientConfig( + options.Host, + options.Port, + options.Account, + options.Password, + ), + ) } func main() { diff --git a/pkg/multicloud/esxi/manager.go b/pkg/multicloud/esxi/manager.go index 823ead0212..d9b7569871 100644 --- a/pkg/multicloud/esxi/manager.go +++ b/pkg/multicloud/esxi/manager.go @@ -57,30 +57,60 @@ func init() { defaultDc.ManagedEntity.ExtensibleManagedObject.Self.Value = defaultDcId } +type ESXiClientConfig struct { + cpcfg cloudprovider.ProviderConfig + + host string + port int + account string + password string + + managed bool +} + +func NewESXiClientConfig(host string, port int, account, password string) *ESXiClientConfig { + cfg := &ESXiClientConfig{ + host: host, + port: port, + account: account, + password: password, + } + return cfg +} + +func (cfg *ESXiClientConfig) CloudproviderConfig(cpcfg cloudprovider.ProviderConfig) *ESXiClientConfig { + cfg.cpcfg = cpcfg + return cfg +} + +func (cfg *ESXiClientConfig) Managed(managed bool) *ESXiClientConfig { + cfg.managed = managed + return cfg +} + type SESXiClient struct { + *ESXiClientConfig + cloudprovider.SFakeOnPremiseRegion multicloud.SRegion multicloud.SNoObjectStorageRegion - providerId string - providerName string - host string - port int - account string - password string - client *govmomi.Client - context context.Context + client *govmomi.Client + context context.Context datacenters []*SDatacenter } -func NewESXiClient(providerId string, providerName string, host string, port int, account string, passwd string) (*SESXiClient, error) { - return NewESXiClient2(providerId, providerName, host, port, account, passwd, true) +func NewESXiClient(cfg *ESXiClientConfig) (*SESXiClient, error) { + cfg.Managed(true) + return NewESXiClient2(cfg) } -func NewESXiClient2(providerId string, providerName string, host string, port int, account string, passwd string, managed bool) (*SESXiClient, error) { - cli := &SESXiClient{providerId: providerId, providerName: providerName, - host: host, port: port, account: account, password: passwd, context: context.Background()} +func NewESXiClient2(cfg *ESXiClientConfig) (*SESXiClient, error) { + cli := &SESXiClient{ + ESXiClientConfig: cfg, + context: context.Background(), + } err := cli.connect() if err != nil { @@ -90,7 +120,7 @@ func NewESXiClient2(providerId string, providerName string, host string, port in if !cli.IsVCenter() { err := cli.checkHostManagedByVCenter() if err != nil { - if managed { + if cfg.managed { cli.disconnect() return nil, err } else { @@ -118,7 +148,14 @@ func NewESXiClientFromAccessInfo(ctx context.Context, accessInfo *models.SVCente accessInfo.Password = tmp } } - client, err := NewESXiClient("", "", accessInfo.Host, accessInfo.Port, accessInfo.Account, accessInfo.Password) + client, err := NewESXiClient( + NewESXiClientConfig( + accessInfo.Host, + accessInfo.Port, + accessInfo.Account, + accessInfo.Password, + ).Managed(true), + ) if err != nil { return nil, err } @@ -180,7 +217,7 @@ func (cli *SESXiClient) GetSubAccounts() ([]cloudprovider.SSubAccount, error) { } subAccount := cloudprovider.SSubAccount{ Account: cli.account, - Name: cli.providerName, + Name: cli.cpcfg.Name, HealthStatus: api.CLOUD_PROVIDER_HEALTH_NORMAL, } return []cloudprovider.SSubAccount{subAccount}, nil @@ -382,8 +419,8 @@ func (cli *SESXiClient) FindHostByMoId(moId string) (cloudprovider.ICloudHost, e } func (cli *SESXiClient) getPrivateId(idStr string) string { - if len(cli.providerId) > 0 && strings.HasPrefix(idStr, cli.providerId) { - idStr = idStr[len(cli.providerId)+1:] + if len(cli.cpcfg.Id) > 0 && strings.HasPrefix(idStr, cli.cpcfg.Id) { + idStr = idStr[len(cli.cpcfg.Id)+1:] } return idStr } diff --git a/pkg/multicloud/esxi/provider/provider.go b/pkg/multicloud/esxi/provider/provider.go index 5b0175f13f..31a55308af 100644 --- a/pkg/multicloud/esxi/provider/provider.go +++ b/pkg/multicloud/esxi/provider/provider.go @@ -111,7 +111,11 @@ func (self *SESXiProviderFactory) GetProvider(cfg cloudprovider.ProviderConfig) return nil, err } - client, err := esxi.NewESXiClient(cfg.Id, cfg.Name, host, port, cfg.Account, cfg.Secret) + client, err := esxi.NewESXiClient( + esxi.NewESXiClientConfig( + host, port, cfg.Account, cfg.Secret, + ).CloudproviderConfig(cfg), + ) if err != nil { return nil, err }