Merge pull request #646 from swordqiu/hotfix/qj-no-direct-manage-vcenter-managed-esxi

fix: do not directly manage ESXi host that is managed by vcenter
This commit is contained in:
yunion-ci-robot
2019-04-26 21:19:11 +08:00
committed by GitHub
3 changed files with 36 additions and 1 deletions
+1 -1
View File
@@ -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() {
+8
View File
@@ -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
}
+27
View File
@@ -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)