Skip to content

Commit 9a8eeae

Browse files
committed
include current device metadata
1 parent 6b93c04 commit 9a8eeae

File tree

15 files changed

+117
-3163
lines changed

15 files changed

+117
-3163
lines changed

cmd/generate.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,20 @@ var generateCmd = &cobra.Command{
4141
os.Exit(1)
4242
}
4343

44-
configDB, err := configdb.GenerateConfigDB(values, platform)
44+
configDBFile, _ := cmd.Flags().GetString("output")
45+
configDBBytes, err := os.ReadFile(configDBFile)
46+
if err != nil {
47+
fmt.Printf("failed to read current config file, %v\n", err)
48+
os.Exit(1)
49+
}
50+
51+
currentConfig, err := configdb.UnmarshalConfigDB(configDBBytes)
52+
if err != nil {
53+
fmt.Printf("failed to parse current config file, %v\n", err)
54+
os.Exit(1)
55+
}
56+
57+
configDB, err := configdb.GenerateConfigDB(values, platform, currentConfig.DeviceMetadata)
4558
if err != nil {
4659
fmt.Printf("failed to generate config, %v\n", err)
4760
os.Exit(1)
@@ -53,8 +66,7 @@ var generateCmd = &cobra.Command{
5366
os.Exit(1)
5467
}
5568

56-
outputFile, _ := cmd.Flags().GetString("output")
57-
err = os.WriteFile(outputFile, configBytes, 0644) //nolint:gosec
69+
err = os.WriteFile(configDBFile, configBytes, 0644) //nolint:gosec
5870
if err != nil {
5971
fmt.Printf("failed to write file, %v", err)
6072
os.Exit(1)

configdb/configdb.go

Lines changed: 49 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package configdb
22

33
import (
4+
"encoding/json"
45
"fmt"
56
"maps"
67
"slices"
@@ -40,26 +41,24 @@ type ConfigDB struct {
4041
VXLANTunnelMap `json:"VXLAN_TUNNEL_MAP,omitempty"`
4142
}
4243

43-
func GenerateConfigDB(input *values.Values, platform *p.Platform) (*ConfigDB, error) {
44+
func GenerateConfigDB(input *values.Values, platform *p.Platform, currentDeviceMetadata DeviceMetadata) (*ConfigDB, error) {
4445
ports, breakouts, err := getPortsAndBreakouts(input.Ports, input.Breakouts, input.PortsDefaultFEC, input.PortsDefaultMTU, platform)
4546
if err != nil {
4647
return nil, err
4748
}
4849

50+
deviceMetadata, err := getDeviceMetadata(input, currentDeviceMetadata)
51+
if err != nil {
52+
return nil, err
53+
}
54+
4955
rules, tables := getACLRulesAndTables(input.SSHSourceranges)
5056

5157
configdb := ConfigDB{
52-
ACLRules: rules,
53-
ACLTables: tables,
54-
Breakouts: breakouts,
55-
DeviceMetadata: DeviceMetadata{
56-
Localhost: Metadata{
57-
DockerRoutingConfigMode: DockerRoutingConfigMode(input.DockerRoutingConfigMode),
58-
FRRMgmtFrameworkConfig: strconv.FormatBool(input.FRRMgmtFrameworkConfig),
59-
Hostname: input.Hostname,
60-
RouterType: "LeafRouter",
61-
},
62-
},
58+
ACLRules: rules,
59+
ACLTables: tables,
60+
Breakouts: breakouts,
61+
DeviceMetadata: *deviceMetadata,
6362
Features: map[string]Feature{
6463
"dhcp_relay": {
6564
AutoRestart: FeatureModeEnabled,
@@ -121,6 +120,16 @@ func GenerateConfigDB(input *values.Values, platform *p.Platform) (*ConfigDB, er
121120
return &configdb, nil
122121
}
123122

123+
func UnmarshalConfigDB(in []byte) (*ConfigDB, error) {
124+
var configDB ConfigDB
125+
err := json.Unmarshal(in, &configDB)
126+
if err != nil {
127+
return nil, err
128+
}
129+
130+
return &configDB, nil
131+
}
132+
124133
func getACLRulesAndTables(sourceRanges []string) (map[string]ACLRule, map[string]ACLTable) {
125134
if len(sourceRanges) == 0 {
126135
return nil, nil
@@ -170,6 +179,34 @@ func getACLRulesAndTables(sourceRanges []string) (map[string]ACLRule, map[string
170179
return rules, tables
171180
}
172181

182+
func getDeviceMetadata(input *values.Values, currentMetadata DeviceMetadata) (*DeviceMetadata, error) {
183+
hint := "remove current config_db.json and run `config-setup factory` to generate an initial config_db.json with all the necessary information"
184+
185+
if currentMetadata.Localhost.Platform == "" {
186+
return nil, fmt.Errorf("missing platform from current device metadata\n%s", hint)
187+
}
188+
189+
if currentMetadata.Localhost.HWSKU == "" {
190+
return nil, fmt.Errorf("missing hwsku from current device metadata\n%s", hint)
191+
}
192+
193+
if currentMetadata.Localhost.MAC == "" {
194+
return nil, fmt.Errorf("missing mac from current device metadata\n%s", hint)
195+
}
196+
197+
return &DeviceMetadata{
198+
Localhost: Metadata{
199+
DockerRoutingConfigMode: DockerRoutingConfigMode(input.DockerRoutingConfigMode),
200+
FRRMgmtFrameworkConfig: strconv.FormatBool(input.FRRMgmtFrameworkConfig),
201+
Hostname: input.Hostname,
202+
HWSKU: currentMetadata.Localhost.HWSKU,
203+
MAC: currentMetadata.Localhost.MAC,
204+
Platform: currentMetadata.Localhost.Platform,
205+
RouterType: "LeafRouter",
206+
},
207+
}, nil
208+
}
209+
173210
func getInterfaces(ports []values.Port, bgpPorts []string) map[string]Interface {
174211
interfaces := make(map[string]Interface)
175212

configdb/fields.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ type BreakoutConfig struct {
3333
}
3434

3535
type DeviceMetadata struct {
36-
Localhost Metadata `json:"localhost,omitempty"`
36+
Localhost Metadata `json:"localhost"`
3737
}
3838

3939
type DockerRoutingConfigMode string
@@ -113,6 +113,9 @@ type Metadata struct {
113113
DockerRoutingConfigMode `json:"docker_routing_config_mode,omitempty"`
114114
FRRMgmtFrameworkConfig string `json:"frr_mgmt_framework_config,omitempty"`
115115
Hostname string `json:"hostname,omitempty"`
116+
HWSKU string `json:"hwsku,omitempty"`
117+
MAC string `json:"mac,omitempty"`
118+
Platform string `json:"platform,omitempty"`
116119
RouterType `json:"type,omitempty"`
117120
}
118121

@@ -127,15 +130,15 @@ type MgmtPort struct {
127130
}
128131

129132
type MgmtVRFConfig struct {
130-
VRFGlobal `json:"vrf_global,omitempty"`
133+
VRFGlobal `json:"vrf_global"`
131134
}
132135

133136
type NTP struct {
134-
NTPGlobal `json:"global,omitempty"`
137+
NTPGlobal `json:"global"`
135138
}
136139

137140
type NTPGlobal struct {
138-
SrcIntf string `json:"src_intf,omitempty"`
141+
SrcIntf string `json:"src_intf"`
139142
}
140143

141144
type PacketAction string
@@ -176,7 +179,7 @@ const (
176179
)
177180

178181
type SAG struct {
179-
SAGGlobal `json:"GLOBAL,omitempty"`
182+
SAGGlobal `json:"GLOBAL"`
180183
}
181184

182185
type SAGGlobal struct {
@@ -213,7 +216,7 @@ type VRFGlobal struct {
213216
}
214217

215218
type VXLANEVPN struct {
216-
VXLANEVPNNVO `json:"nvo,omitempty"`
219+
VXLANEVPNNVO `json:"nvo"`
217220
}
218221

219222
type VXLANEVPNNVO struct {

platform/platform.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,9 @@ func ParseSpeedOptions(breakoutMode string) (SpeedOptions, error) {
122122
return SpeedOptions{speed * 1000, altSpeed * 1000}, nil
123123
}
124124

125-
func UnmarshalPlatformJSON(input []byte) (*Platform, error) {
126-
platform := Platform{}
127-
err := json.Unmarshal(input, &platform)
125+
func UnmarshalPlatformJSON(in []byte) (*Platform, error) {
126+
var platform Platform
127+
err := json.Unmarshal(in, &platform)
128128
if err != nil {
129129
return nil, err
130130
}
@@ -135,8 +135,7 @@ func UnmarshalPlatformJSON(input []byte) (*Platform, error) {
135135
func stringToIntSlice(input string) ([]int, error) {
136136
ints := make([]int, 0)
137137

138-
numbers := strings.Split(input, ",")
139-
for _, n := range numbers {
138+
for n := range strings.SplitSeq(input, ",") {
140139
number, err := strconv.Atoi(n)
141140
if err != nil {
142141
return []int{}, err

tests/1/current-config_db.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"DEVICE_METADATA": {
3+
"localhost": {
4+
"hwsku": "Accton-AS7726-32X",
5+
"mac": "aa:aa:aa:aa:aa:aa",
6+
"platform": "x86_64-accton_as7726_32x-r0"
7+
}
8+
}
9+
}

tests/1/expected.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,9 @@
142142
"docker_routing_config_mode": "split",
143143
"frr_mgmt_framework_config": "false",
144144
"hostname": "leaf01",
145+
"hwsku": "Accton-AS7726-32X",
146+
"mac": "aa:aa:aa:aa:aa:aa",
147+
"platform": "x86_64-accton_as7726_32x-r0",
145148
"type": "LeafRouter"
146149
}
147150
},

tests/2/current-config_db.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"DEVICE_METADATA": {
3+
"localhost": {
4+
"hwsku": "Accton-AS7726-32X",
5+
"mac": "aa:aa:aa:aa:aa:aa",
6+
"platform": "x86_64-accton_as7726_32x-r0"
7+
}
8+
}
9+
}

tests/2/expected.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,9 @@
142142
"docker_routing_config_mode": "split",
143143
"frr_mgmt_framework_config": "false",
144144
"hostname": "leaf01",
145+
"hwsku": "Accton-AS7726-32X",
146+
"mac": "aa:aa:aa:aa:aa:aa",
147+
"platform": "x86_64-accton_as7726_32x-r0",
145148
"type": "LeafRouter"
146149
}
147150
},

tests/3/current-config_db.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"DEVICE_METADATA": {
3+
"localhost": {
4+
"hwsku": "Accton-AS4630-54TE",
5+
"mac": "aa:aa:aa:aa:aa:aa",
6+
"platform": "x86_64-accton_as4630_54te-r0"
7+
}
8+
}
9+
}

tests/3/expected.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,9 @@
208208
"docker_routing_config_mode": "split",
209209
"frr_mgmt_framework_config": "false",
210210
"hostname": "leaf01",
211+
"hwsku": "Accton-AS4630-54TE",
212+
"mac": "aa:aa:aa:aa:aa:aa",
213+
"platform": "x86_64-accton_as4630_54te-r0",
211214
"type": "LeafRouter"
212215
}
213216
},

0 commit comments

Comments
 (0)