forked from jacobweinstock/go-amt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
78 lines (65 loc) · 1.68 KB
/
client.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
package amt
import (
"context"
"fmt"
"github.com/go-logr/logr"
"github.com/jacobweinstock/wsman"
)
// Client used to perform actions on the machine
type Client struct {
logger logr.Logger
wsManClient *wsman.Client
}
// NewClient creates an amt client to use.
func NewClient(connection Connection) (*Client, error) {
port := int(connection.Port)
if port == 0 {
port = 16992
}
path := connection.Path
if path == "" {
path = "/wsman"
}
var target string
if connection.TLS {
target = fmt.Sprintf("https://%s:%d%s", connection.Host, port, path)
} else {
target = fmt.Sprintf("http://%s:%d%s", connection.Host, port, path)
}
wsmanClient, err := wsman.NewClient(target, connection.User, connection.Pass, true)
if err != nil {
return nil, err
}
wsmanClient.Debug = connection.Debug
if connection.Logger.GetSink() == nil {
connection.Logger = logr.Discard()
}
return &Client{
logger: connection.Logger,
wsManClient: wsmanClient,
}, nil
}
// Close the client.
func (c *Client) Close() error {
return nil
}
// PowerOn will power on a given machine.
func (c *Client) PowerOn(ctx context.Context) error {
return powerOn(ctx, c)
}
// PowerOff will power off a given machine.
func (c *Client) PowerOff(ctx context.Context) error {
return powerOff(ctx, c)
}
// PowerCycle will power cycle a given machine.
func (c *Client) PowerCycle(ctx context.Context) error {
return powerCycle(ctx, c)
}
// SetPXE makes sure the node will pxe boot next time.
func (c *Client) SetPXE(ctx context.Context) error {
return setPXE(ctx, c)
}
// IsPoweredOn checks current power state.
func (c *Client) IsPoweredOn(ctx context.Context) (bool, error) {
return isPoweredOn(ctx, c)
}