|
1 | 1 | package configdb |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "encoding/json" |
4 | 5 | "fmt" |
5 | 6 | "maps" |
6 | 7 | "slices" |
@@ -40,26 +41,24 @@ type ConfigDB struct { |
40 | 41 | VXLANTunnelMap `json:"VXLAN_TUNNEL_MAP,omitempty"` |
41 | 42 | } |
42 | 43 |
|
43 | | -func GenerateConfigDB(input *values.Values, platform *p.Platform) (*ConfigDB, error) { |
| 44 | +func GenerateConfigDB(input *values.Values, platform *p.Platform, currentDeviceMetadata DeviceMetadata) (*ConfigDB, error) { |
44 | 45 | ports, breakouts, err := getPortsAndBreakouts(input.Ports, input.Breakouts, input.PortsDefaultFEC, input.PortsDefaultMTU, platform) |
45 | 46 | if err != nil { |
46 | 47 | return nil, err |
47 | 48 | } |
48 | 49 |
|
| 50 | + deviceMetadata, err := getDeviceMetadata(input, currentDeviceMetadata) |
| 51 | + if err != nil { |
| 52 | + return nil, err |
| 53 | + } |
| 54 | + |
49 | 55 | rules, tables := getACLRulesAndTables(input.SSHSourceranges) |
50 | 56 |
|
51 | 57 | 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, |
63 | 62 | Features: map[string]Feature{ |
64 | 63 | "dhcp_relay": { |
65 | 64 | AutoRestart: FeatureModeEnabled, |
@@ -121,6 +120,16 @@ func GenerateConfigDB(input *values.Values, platform *p.Platform) (*ConfigDB, er |
121 | 120 | return &configdb, nil |
122 | 121 | } |
123 | 122 |
|
| 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 | + |
124 | 133 | func getACLRulesAndTables(sourceRanges []string) (map[string]ACLRule, map[string]ACLTable) { |
125 | 134 | if len(sourceRanges) == 0 { |
126 | 135 | return nil, nil |
@@ -170,6 +179,34 @@ func getACLRulesAndTables(sourceRanges []string) (map[string]ACLRule, map[string |
170 | 179 | return rules, tables |
171 | 180 | } |
172 | 181 |
|
| 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 | + |
173 | 210 | func getInterfaces(ports []values.Port, bgpPorts []string) map[string]Interface { |
174 | 211 | interfaces := make(map[string]Interface) |
175 | 212 |
|
|
0 commit comments