forked from hashicorp/terraform-provider-vsphere
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
85 lines (70 loc) · 1.66 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package vsphere
import (
"fmt"
"log"
"net/url"
"os"
"path/filepath"
"time"
"github.com/vmware/govmomi"
"github.com/vmware/govmomi/vim25/debug"
"golang.org/x/net/context"
)
type Config struct {
User string
Password string
VSphereServer string
InsecureFlag bool
Debug bool
DebugPath string
DebugPathRun string
}
// Client() returns a new client for accessing VMWare vSphere.
func (c *Config) Client() (*govmomi.Client, error) {
u, err := url.Parse("https://" + c.VSphereServer + "/sdk")
if err != nil {
return nil, fmt.Errorf("Error parse url: %s", err)
}
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", 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
}