Skip to content

Commit

Permalink
Adding Govomi Debug Logging Capability and refactoring of integration…
Browse files Browse the repository at this point in the history
… tests (#6893)

* Adding debug functionality to log debug api calls

* adding debug and refactoring tests

* more tweaks with tests

* updating documentation

* more refactoring of tests

* working through factor for testing

* removing logging that displays username and password

* more work on getting tests stable
  • Loading branch information
chrislovecnm authored and stack72 committed Jun 2, 2016
1 parent 6d54d06 commit 7b449b6
Show file tree
Hide file tree
Showing 5 changed files with 805 additions and 1,024 deletions.
51 changes: 50 additions & 1 deletion builtin/providers/vsphere/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ import (
"fmt"
"log"
"net/url"
"os"
"path/filepath"
"time"

"github.com/vmware/govmomi"
"github.com/vmware/govmomi/vim25/debug"
"golang.org/x/net/context"
)

Expand All @@ -14,6 +18,9 @@ type Config struct {
Password string
VSphereServer string
InsecureFlag bool
Debug bool
DebugPath string
DebugPathRun string
}

// Client() returns a new client for accessing VMWare vSphere.
Expand All @@ -25,12 +32,54 @@ func (c *Config) Client() (*govmomi.Client, error) {

u.User = url.UserPassword(c.User, c.Password)

err = c.EnableDebug()
if err != nil {
return nil, fmt.Errorf("Error setting up client debug: %s", err)
}

client, err := govmomi.NewClient(context.TODO(), u, c.InsecureFlag)
if err != nil {
return nil, fmt.Errorf("Error setting up client: %s", err)
}

log.Printf("[INFO] VMWare vSphere Client configured for URL: %s", u)
log.Printf("[INFO] VMWare vSphere Client configured for URL: %s", c.VSphereServer)

return client, nil
}

func (c *Config) EnableDebug() error {
if !c.Debug {
return nil
}

// Base path for storing debug logs.
r := c.DebugPath
if r == "" {
r = filepath.Join(os.Getenv("HOME"), ".govmomi")
}
r = filepath.Join(r, "debug")

// Path for this particular run.
run := c.DebugPathRun
if run == "" {
now := time.Now().Format("2006-01-02T15-04-05.999999999")
r = filepath.Join(r, now)
} else {
// reuse the same path
r = filepath.Join(r, run)
_ = os.RemoveAll(r)
}

err := os.MkdirAll(r, 0700)
if err != nil {
log.Printf("[ERROR] Client debug setup failed: %v", err)
return err
}

p := debug.FileProvider{
Path: r,
}

debug.SetProvider(&p)
return nil
}
21 changes: 21 additions & 0 deletions builtin/providers/vsphere/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,24 @@ func Provider() terraform.ResourceProvider {
DefaultFunc: schema.EnvDefaultFunc("VSPHERE_VCENTER", nil),
Deprecated: "This field has been renamed to vsphere_server.",
},
"client_debug": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("VSPHERE_CLIENT_DEBUG", false),
Description: "govomomi debug",
},
"client_debug_path_run": &schema.Schema{
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("VSPHERE_CLIENT_DEBUG_PATH_RUN", nil),
Description: "govomomi debug path for a single run",
},
"client_debug_path": &schema.Schema{
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("VSPHERE_CLIENT_DEBUG_PATH", nil),
Description: "govomomi debug path for debug",
},
},

ResourcesMap: map[string]*schema.Resource{
Expand Down Expand Up @@ -76,6 +94,9 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
Password: d.Get("password").(string),
InsecureFlag: d.Get("allow_unverified_ssl").(bool),
VSphereServer: server,
Debug: d.Get("client_debug").(bool),
DebugPathRun: d.Get("client_debug_path_run").(string),
DebugPath: d.Get("client_debug_path").(string),
}

return config.Client()
Expand Down
1 change: 1 addition & 0 deletions builtin/providers/vsphere/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func TestProvider(t *testing.T) {
if err := Provider().(*schema.Provider).InternalValidate(); err != nil {
t.Fatalf("err: %s", err)
}

}

func TestProvider_impl(t *testing.T) {
Expand Down
Loading

0 comments on commit 7b449b6

Please sign in to comment.