Skip to content

Commit 760220e

Browse files
vasant17Vasant Patil
authored and
Vasant Patil
committed
[sonic-cfggen]: add --key option to show a specific key (sonic-net#3248)
* Adding --key option to sonic-cfggen script. This will help to display config DB with more granularity. Signed-off-by: Vasant Patil <[email protected]>
1 parent db2ea6f commit 760220e

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

src/sonic-config-engine/sonic-cfggen

+16-2
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,18 @@ TODO(taoyl): Current version of config db only supports BGP admin states.
194194
return db_data
195195

196196
@staticmethod
197-
def to_serialized(data):
197+
def to_serialized(data, lookup_key = None):
198198
if type(data) is dict:
199199
data = OrderedDict(natsorted(data.items()))
200+
201+
if lookup_key != None:
202+
newData = {}
203+
for key in data.keys():
204+
if ((type(key) is unicode and lookup_key == key) or (type(key) is tuple and lookup_key in key)):
205+
newData[ConfigDBConnector.serialize_key(key)] = data.pop(key)
206+
break
207+
return newData
208+
200209
for key in data.keys():
201210
new_key = ConfigDBConnector.serialize_key(key)
202211
if new_key != key:
@@ -252,6 +261,8 @@ def main():
252261
group.add_argument("--write-to-db", help="write config into configdb", action='store_true')
253262
group.add_argument("--print-data", help="print all data", action='store_true')
254263
group.add_argument("--preset", help="generate sample configuration from a preset template", choices=get_available_config())
264+
group = parser.add_mutually_exclusive_group()
265+
group.add_argument("-K", "--key", help="Lookup for a specific key")
255266
args = parser.parse_args()
256267

257268
platform = get_platform_info(get_machine_info())
@@ -331,7 +342,10 @@ def main():
331342
print(template.render(data))
332343

333344
if args.var_json != None:
334-
print(json.dumps(FormatConverter.to_serialized(data[args.var_json]), indent=4, cls=minigraph_encoder))
345+
if args.key != None:
346+
print(json.dumps(FormatConverter.to_serialized(data[args.var_json], args.key), indent=4, cls=minigraph_encoder))
347+
else:
348+
print(json.dumps(FormatConverter.to_serialized(data[args.var_json]), indent=4, cls=minigraph_encoder))
335349

336350
if args.write_to_db:
337351
configdb = ConfigDBConnector(**db_kwargs)

src/sonic-config-engine/tests/test_cfggen.py

+10
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,16 @@ def test_additional_json_data(self):
6565
output = self.run_script(argument)
6666
self.assertEqual(output.strip(), 'value1')
6767

68+
def test_additional_json_data_level1_key(self):
69+
argument = '-a \'{"k1":{"k11":"v11","k12":"v12"}, "k2":{"k22":"v22"}}\' --var-json k1'
70+
output = self.run_script(argument)
71+
self.assertEqual(output.strip(), '{\n "k11": "v11", \n "k12": "v12"\n}')
72+
73+
def test_additional_json_data_level2_key(self):
74+
argument = '-a \'{"k1":{"k11":"v11","k12":"v12"},"k2":{"k22":"v22"}}\' --var-json k1 -K k11'
75+
output = self.run_script(argument)
76+
self.assertEqual(output.strip(), '{\n "k11": "v11"\n}')
77+
6878
def test_read_yaml(self):
6979
argument = '-v yml_item -y ' + os.path.join(self.test_dir, 'test.yml')
7080
output = self.run_script(argument)

0 commit comments

Comments
 (0)