Adding Govomi Debug Logging Capability and refactoring of integration 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
This commit is contained in:
parent
6d54d0643d
commit
7b449b66a0
|
@ -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"
|
||||
)
|
||||
|
||||
|
@ -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.
|
||||
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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{
|
||||
|
@ -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()
|
||||
|
|
|
@ -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) {
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -83,6 +83,14 @@ The following arguments are used to configure the VMware vSphere Provider:
|
|||
could allow an attacker to intercept your auth token. If omitted, default
|
||||
value is `false`. Can also be specified with the `VSPHERE_ALLOW_UNVERIFIED_SSL`
|
||||
environment variable.
|
||||
* `client_debug` - (Optional) Boolean to set the govomomi api to log soap calls
|
||||
to disk. The log files are logged to `${HOME}/.govc`, the same path used by
|
||||
`govc`. Can also be specified with the `VSPHERE_CLIENT_DEBUG` environment
|
||||
variable.
|
||||
* `client_debug_path` - (Optional) Override the default log path. Can also
|
||||
be specified with the `VSPHERE_CLIENT_DEBUG_PATH` environment variable.
|
||||
* `client_debug_path_run` - (Optional) Client debug file path for a single run. Can also
|
||||
be specified with the `VSPHERE_CLIENT_DEBUG_PATH_RUN` environment variable.
|
||||
|
||||
## Required Privileges
|
||||
|
||||
|
|
Loading…
Reference in New Issue