Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Govomi Debug Logging Capability and refactoring of integration tests #6893

Merged
merged 8 commits into from
Jun 2, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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