Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 5 commits into from
Aug 6, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, find_key = ""):
Copy link
Collaborator

@lguohan lguohan Aug 5, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-> lookup_key = None

if type(data) is dict:
data = OrderedDict(natsorted(data.items()))

if find_key != "":
newData = {}
for key in data.keys():
if ((type(key) is unicode and find_key == key) or (type(key) is tuple and find_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("-f", "--find", help="Find/Search for a specific key")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is the difference between --var-json.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

--var-json is used to query one level higher keys. For example: BGP_NEIGHBOR, DEVICE_METADATA, INTERFACE, PORT, etc.. where as --find is used query one level down. For example Ethernet0 under PORT.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

then, why not use -k, --key option which is more specific?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I though so as well, since -k is taken by hwSKU, I used -f. Please suggest if you have any other better alternative.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-K

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good. Let me change that!

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.find != None:
print(json.dumps(FormatConverter.to_serialized(data[args.var_json], args.find), 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