[sonic-cfggen]: add --key option to show a specific key (#3248)

* Adding --key option to sonic-cfggen script. This will help to display config DB with more granularity.

Signed-off-by: Vasant Patil <vapatil@linkedin.com>
This commit is contained in:
Vasant Patil 2019-08-06 08:04:33 -07:00 committed by lguohan
parent d80d3d60db
commit 63b5e0e14c
2 changed files with 26 additions and 2 deletions

View File

@ -124,9 +124,18 @@ TODO(taoyl): Current version of config db only supports BGP admin states.
return db_data
@staticmethod
def to_serialized(data):
def to_serialized(data, lookup_key = None):
if type(data) is dict:
data = OrderedDict(natsorted(data.items()))
if lookup_key != None:
newData = {}
for key in data.keys():
if ((type(key) is unicode and lookup_key == key) or (type(key) is tuple and lookup_key in key)):
newData[ConfigDBConnector.serialize_key(key)] = data.pop(key)
break
return newData
for key in data.keys():
new_key = ConfigDBConnector.serialize_key(key)
if new_key != key:
@ -181,6 +190,8 @@ def main():
group.add_argument("-w", "--write-to-db", help="write config into configdb", action='store_true')
group.add_argument("--print-data", help="print all data", action='store_true')
group.add_argument("--preset", help="generate sample configuration from a preset template", choices=get_available_config())
group = parser.add_mutually_exclusive_group()
group.add_argument("-K", "--key", help="Lookup for a specific key")
args = parser.parse_args()
platform = get_platform_info(get_machine_info())
@ -261,7 +272,10 @@ def main():
print(template.render(data))
if args.var_json != None:
print(json.dumps(FormatConverter.to_serialized(data[args.var_json]), indent=4, cls=minigraph_encoder))
if args.key != None:
print(json.dumps(FormatConverter.to_serialized(data[args.var_json], args.key), indent=4, cls=minigraph_encoder))
else:
print(json.dumps(FormatConverter.to_serialized(data[args.var_json]), indent=4, cls=minigraph_encoder))
if args.write_to_db:
configdb = ConfigDBConnector(**db_kwargs)

View File

@ -65,6 +65,16 @@ class TestCfgGen(TestCase):
output = self.run_script(argument)
self.assertEqual(output.strip(), 'value1')
def test_additional_json_data_level1_key(self):
argument = '-a \'{"k1":{"k11":"v11","k12":"v12"}, "k2":{"k22":"v22"}}\' --var-json k1'
output = self.run_script(argument)
self.assertEqual(output.strip(), '{\n "k11": "v11", \n "k12": "v12"\n}')
def test_additional_json_data_level2_key(self):
argument = '-a \'{"k1":{"k11":"v11","k12":"v12"},"k2":{"k22":"v22"}}\' --var-json k1 -K k11'
output = self.run_script(argument)
self.assertEqual(output.strip(), '{\n "k11": "v11"\n}')
def test_var_json_data(self):
argument = '-m "' + self.sample_graph_simple + '" -p "' + self.port_config + '" --var-json VLAN_MEMBER'
output = self.run_script(argument)