1
0
mirror of https://github.com/Oxalide/vsphere-influxdb-go.git synced 2023-10-10 13:36:51 +02:00

Only defer client.Logout if client has been successfully returned.

This commit is contained in:
Lucas Y.Z. Zhang 2016-09-08 17:40:49 +08:00
parent 482da3d2b9
commit 50c45f7d2a

View File

@ -131,12 +131,12 @@ func (vcenter *VCenter) Init(config Configuration) {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
client, err := vcenter.Connect() client, err := vcenter.Connect()
defer client.Logout(ctx)
if err != nil { if err != nil {
errlog.Println("Could not connect to vcenter: ", vcenter.Hostname) errlog.Println("Could not connect to vcenter: ", vcenter.Hostname)
errlog.Println("Error: ", err) errlog.Println("Error: ", err)
return return
} }
defer client.Logout(ctx)
var perfmanager mo.PerformanceManager var perfmanager mo.PerformanceManager
err = client.RetrieveOne(ctx, *client.ServiceContent.PerfManager, nil, &perfmanager) err = client.RetrieveOne(ctx, *client.ServiceContent.PerfManager, nil, &perfmanager)
if err != nil { if err != nil {
@ -182,12 +182,12 @@ func (vcenter *VCenter) Query(config Configuration, InfluxDBClient influxclient.
// Get the client // Get the client
client, err := vcenter.Connect() client, err := vcenter.Connect()
defer client.Logout(ctx)
if err != nil { if err != nil {
errlog.Println("Could not connect to vcenter: ", vcenter.Hostname) errlog.Println("Could not connect to vcenter: ", vcenter.Hostname)
errlog.Println("Error: ", err) errlog.Println("Error: ", err)
return return
} }
defer client.Logout(ctx)
// Create the view manager // Create the view manager
var viewManager mo.ViewManager var viewManager mo.ViewManager