diff --git a/cmd/esxicli/main.go b/cmd/esxicli/main.go index 128d0eed6e..930147c6a3 100644 --- a/cmd/esxicli/main.go +++ b/cmd/esxicli/main.go @@ -88,7 +88,7 @@ func newClient(options *BaseOptions) (*esxi.SESXiClient, error) { return nil, fmt.Errorf("Missing password") } - return esxi.NewESXiClient("", "", options.Host, options.Port, options.Account, options.Password) + return esxi.NewESXiClient2("", "", options.Host, options.Port, options.Account, options.Password, false) } func main() { diff --git a/pkg/util/esxi/host.go b/pkg/util/esxi/host.go index 93557162bd..d13aa00d45 100644 --- a/pkg/util/esxi/host.go +++ b/pkg/util/esxi/host.go @@ -605,3 +605,11 @@ func (host *SHost) newLocalStorageCache() (*SDatastoreImageCache, error) { host: host, }, nil } + +func (host *SHost) GetManagementServerIp() string { + return host.getHostSystem().Summary.ManagementServerIp +} + +func (host *SHost) IsManagedByVCenter() bool { + return len(host.getHostSystem().Summary.ManagementServerIp) > 0 +} diff --git a/pkg/util/esxi/manager.go b/pkg/util/esxi/manager.go index 53ac4b7116..f404794698 100644 --- a/pkg/util/esxi/manager.go +++ b/pkg/util/esxi/manager.go @@ -56,6 +56,10 @@ type SESXiClient struct { } 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 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()} @@ -63,6 +67,18 @@ func NewESXiClient(providerId string, providerName string, host string, port int if err != nil { return nil, err } + + if !cli.IsVCenter() { + err := cli.checkHostManagedByVCenter() + if err != nil { + if managed { + cli.disconnect() + return nil, err + } else { + log.Warningf("%s", err) + } + } + } return cli, nil } @@ -261,6 +277,17 @@ func (cli *SESXiClient) getPrivateId(idStr string) string { return idStr } +func (cli *SESXiClient) checkHostManagedByVCenter() error { + host, err := cli.FindHostByIp(cli.host) + if err != nil { + return err + } + if host.IsManagedByVCenter() { + return fmt.Errorf("ESXi host is managed by vcenter %s, please connect to vcenter instead for full management functions!", host.GetManagementServerIp()) + } + return nil +} + func (cli *SESXiClient) FindHostByIp(hostIp string) (*SHost, error) { searchIndex := object.NewSearchIndex(cli.client.Client)