-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfactory.go
100 lines (80 loc) · 2.17 KB
/
factory.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package scrapligocfg
import (
"errors"
"fmt"
"github.com/scrapli/scrapligo/logging"
"github.com/scrapli/scrapligocfg/cisconxos"
"github.com/scrapli/scrapligocfg/juniperjunos"
"github.com/scrapli/scrapligocfg/ciscoiosxr"
"github.com/scrapli/scrapligocfg/aristaeos"
"github.com/scrapli/scrapligo/driver/network"
"github.com/scrapli/scrapligocfg/ciscoiosxe"
"github.com/scrapli/scrapligocfg/util"
)
const (
// CiscoIOSXE is the Cisco IOS-XE scrapligocfg platform type.
CiscoIOSXE = "cisco_iosxe"
// CiscoNXOS is the Cisco NX-OS scrapligocfg platform type.
CiscoNXOS = "cisco_nxos"
// CiscoIOSXR is the Cisco IOS-XR scrapligocfg platform type.
CiscoIOSXR = "cisco_iosxr"
// AristaEOS is the Arista EOS scrapligocfg platform type.
AristaEOS = "arista_eos"
// JuniperJUNOS is the Juniper JunOS scrapligocfg platform type.
JuniperJUNOS = "juniper_junos"
)
// SupportedPlatforms returns a slice of all available supported "cfg" platforms.
func SupportedPlatforms() []string {
return []string{
CiscoIOSXE,
CiscoNXOS,
CiscoIOSXR,
AristaEOS,
JuniperJUNOS,
}
}
// NewCfg returns an instance of Cfg for the provided platform type.
func NewCfg(conn *network.Driver, platform string, opts ...util.Option) (*Cfg, error) {
var impl Platform
var err error
switch platform {
case CiscoIOSXE:
impl, err = ciscoiosxe.NewCiscoIOSXE(conn, opts...)
case CiscoNXOS:
impl, err = cisconxos.NewCiscoNXOS(conn, opts...)
case CiscoIOSXR:
impl, err = ciscoiosxr.NewCiscoIOSXR(conn, opts...)
case AristaEOS:
impl, err = aristaeos.NewAristaEOS(conn, opts...)
case JuniperJUNOS:
impl, err = juniperjunos.NewJuniperJunOS(conn, opts...)
default:
return nil, fmt.Errorf("%w: ", util.ErrUnsupportedPlatform)
}
if err != nil {
return nil, err
}
c := &Cfg{
Logger: nil,
Impl: impl,
Conn: conn,
}
for _, option := range opts {
err = option(c)
if err != nil {
if !errors.Is(err, util.ErrIgnoredOption) {
return nil, err
}
}
}
if c.Logger == nil {
// set a default logging instance w/ no assigned loggers (a noop basically)
var l *logging.Instance
l, err = logging.NewInstance()
if err != nil {
return nil, err
}
c.Logger = l
}
return c, nil
}