-
Notifications
You must be signed in to change notification settings - Fork 81
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
Add value key to return data #359
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -151,6 +151,11 @@ | |
type: int | ||
default: 1000 | ||
version_added: 2.5.0 | ||
show_values: | ||
description: | ||
- Whether to include the valu' field for dictionary-type columns in returned. | ||
type: bool | ||
default: false | ||
|
||
""" | ||
|
||
|
@@ -364,7 +369,7 @@ def construct_sysparm_query(query, is_encoded_query): | |
def fetch_records(table_client, table, query, fields=None, is_encoded_query=False): | ||
snow_query = dict( | ||
# Make references and choice fields human-readable | ||
sysparm_display_value=True, | ||
sysparm_display_value="all", | ||
) | ||
if query: | ||
snow_query["sysparm_query"] = construct_sysparm_query(query, is_encoded_query) | ||
|
@@ -426,6 +431,13 @@ def add_host(self, record, name_source): | |
raise AnsibleParserError(msg.format(name_source)) | ||
|
||
inventory_hostname = record[name_source] | ||
if isinstance(inventory_hostname, dict): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You don't need this. You already set |
||
if "display_value" in inventory_hostname: | ||
inventory_hostname = inventory_hostname["display_value"] | ||
else: | ||
msg = "Inventory hostname source column '{0}' is a dict but does not contain 'display_value'." | ||
raise AnsibleParserError(msg.format(name_source)) | ||
|
||
if inventory_hostname: | ||
host = self.inventory.add_host(inventory_hostname) | ||
return host | ||
|
@@ -442,8 +454,23 @@ def set_hostvars(self, host, record, columns): | |
"Invalid column names: {0}.".format(", ".join(missing)) | ||
) | ||
|
||
show_values = self.get_option("show_values") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please pass show_values to I will advise passing a default value like |
||
|
||
for k in columns: | ||
self.inventory.set_variable(host, k.replace(".", "_"), record[k]) | ||
value = record.get(k) | ||
if isinstance(value, dict): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what if the value is not a dict? Where do you set the value to host_var if value is str? |
||
main_value = value.get("display_value") or value.get("value") or value | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. either use display_value or value but like this is dangerous. You imply that you can use the whole dict if display_value or value are not present but I don't think you need this. The problem with this code is that you hardcoded |
||
self.inventory.set_variable(host, k.replace(".", "_"), main_value) | ||
|
||
if show_values: | ||
for sub_key in ["display_value", "value"]: | ||
sub_value = value.get(sub_key) | ||
if sub_value and sub_value != main_value: | ||
self.inventory.set_variable( | ||
host, f"{k}_{sub_key}", sub_value | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please use |
||
) | ||
else: | ||
self.inventory.set_variable(host, k.replace(".", "_"), value) | ||
|
||
def fill_constructed( | ||
self, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,21 +50,21 @@ def test_no_query(self, table_client): | |
now.fetch_records(table_client, "table_name", None) | ||
|
||
table_client.list_records.assert_called_once_with( | ||
"table_name", dict(sysparm_display_value=True) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the tests need to take into account the new option |
||
"table_name", dict(sysparm_display_value="all") | ||
) | ||
|
||
def test_query(self, table_client): | ||
now.fetch_records(table_client, "table_name", [dict(my="!= value")]) | ||
|
||
table_client.list_records.assert_called_once_with( | ||
"table_name", dict(sysparm_display_value=True, sysparm_query="my!=value") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as above |
||
"table_name", dict(sysparm_display_value="all", sysparm_query="my!=value") | ||
) | ||
|
||
def test_no_query_with_fields(self, table_client): | ||
now.fetch_records(table_client, "table_name", None, fields=["a", "b", "c"]) | ||
|
||
table_client.list_records.assert_called_once_with( | ||
"table_name", dict(sysparm_display_value=True, sysparm_fields="a,b,c") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as above |
||
"table_name", dict(sysparm_display_value="all", sysparm_fields="a,b,c") | ||
) | ||
|
||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use the value of
show_values
to set this parameter. You risk breaking the inventories of other users.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By default it needs to be
True
. Change it toall
only ifshow_values
istrue