/
bmc.go
86 lines (70 loc) · 2.09 KB
/
bmc.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
86
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and IronCore contributors
// SPDX-License-Identifier: Apache-2.0
package bmc
import (
"context"
"fmt"
"regexp"
"time"
"github.com/ironcore-dev/oob/internal/log"
)
type BMC interface {
Type() string
Tags() map[string]string
Credentials() (Credentials, time.Time)
EnsureInitialCredentials(ctx context.Context, defaultCreds []Credentials, tempPassword string) error
Connect(ctx context.Context) error
CreateUser(ctx context.Context, creds Credentials, tempPassword string) error
DeleteUsers(ctx context.Context, regex *regexp.Regexp) error
ReadInfo(ctx context.Context) (Info, error)
}
type LEDControl interface {
SetLocatorLED(ctx context.Context, state string) (string, error)
}
type PowerControl interface {
PowerOn(ctx context.Context) error
PowerOff(ctx context.Context, immediate bool) error
}
type ResetControl interface {
Reset(ctx context.Context, immediate bool) error
}
type NTPControl interface {
SetNTPServers(ctx context.Context, ntpServers []string) error
}
type newBMCFunc func(tags map[string]string, host string, port int, creds Credentials, exp time.Time) BMC
var (
bmcs = make(map[string]newBMCFunc)
)
func registerBMC(newFunc newBMCFunc) {
bmcs[newFunc(nil, "", 0, Credentials{}, time.Time{}).Type()] = newFunc
}
func NewBMC(typ string, tags map[string]string, host string, port int, creds Credentials, exp time.Time) (BMC, error) {
newFunc, ok := bmcs[typ]
if !ok {
return nil, fmt.Errorf("BMC of type %s is not supported", typ)
}
return newFunc(tags, host, port, creds, exp), nil
}
type Credentials struct {
Username string `yaml:"username"`
Password string `yaml:"password"`
}
type Info struct {
UUID string
Type string
Capabilities []string
SerialNumber string
SKU string
Manufacturer string
LocatorLED string
Power string
OS string
OSReason string
Console string
FWVersion string
}
func must(ctx context.Context, err error) {
if err != nil {
log.Error(ctx, fmt.Errorf("impossible error (this should never happen lol): %w", err))
}
}