Skip to content

Commit

Permalink
[sonic-cfggen]: add --key option to show a specific key (#3248)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
vasant17 authored and lguohan committed Aug 6, 2019
1 parent d80d3d6 commit 63b5e0e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/sonic-config-engine/sonic-cfggen
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions src/sonic-config-engine/tests/test_cfggen.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,16 @@ def test_additional_json_data(self):
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)
Expand Down

0 comments on commit 63b5e0e

Please sign in to comment.