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

add_criterion method returning errors #34

Closed
sbrosnihan opened this issue Apr 16, 2015 · 6 comments
Closed

add_criterion method returning errors #34

sbrosnihan opened this issue Apr 16, 2015 · 6 comments

Comments

@sbrosnihan
Copy link

Thanks for all the hard work on this. It sure makes interacting with the JSS API easier! I have a problem that may be related to improper usage by me or a bug. I've created a variable:

jss_prefs = jss.JSSPrefs()
j = jss.JSS(jss_prefs, ssl_verify=False, suppress_warnings=True)
smartGroup = j.ComputerGroup(99)

When trying to add criteria to smartGroup (which is a smart group), I get the following error:

smartGroup.add_criterion(name="Username", priority=0, and_or="and", search_type="is", value="bobafett2")
self.criteria.append(criterion)
AttributeError: 'ComputerGroup' object has no attribute 'criteria'

I can see the contents of the group and criteria, also inspect.getargspec(smartGroup.add_criterion)
returns this:

ArgSpec(args=['self', 'name', 'priority', 'and_or', 'search_type', 'value'], varargs=None, keywords=None, defaults=None)
@sheagcraig
Copy link
Collaborator

Thanks!

Looking over the code, I can see what the issue is, and it's not you!

Python raises AttributeError, rightly so, because indeed the ComputerGroup has no attribute 'criteria'. This attribute is set in the new() method, but not for a ComputerGroup that you pull from the server.

I'll have to fix that!

In the meantime, if you add the attribute as a list, you'll be good to go.
e.g.

jss_prefs = jss.JSSPrefs()
j = jss.JSS(jss_prefs, ssl_verify=False, suppress_warnings=True)
smartGroup = j.ComputerGroup(99)
# Here's the only change...
smartGroup.criteria = []
smartGroup.add_criterion(name="Username", priority=0, and_or="and", search_type="is", value="bobafett2")

@sheagcraig
Copy link
Collaborator

I'm sorry, that's actually wrong.

criteria is not a list, it's an xml.etree.Element.

You should be able to do this to "find" it and assign it to ComputerGroup.criteria, and then append should work.

ss_prefs = jss.JSSPrefs()
j = jss.JSS(jss_prefs, ssl_verify=False, suppress_warnings=True)
smartGroup = j.ComputerGroup(99)
# Here's the only change... Finds the criteria element that comes 
# with the XML and assigns it to a convenient attribute.
smartGroup.criteria = smartGroup.find("criteria")
smartGroup.add_criterion(name="Username", priority=0, and_or="and", search_type="is", value="bobafett2")

@sbrosnihan
Copy link
Author

Bingo! Works like a charm.

@sheagcraig
Copy link
Collaborator

I'm going to keep this open so I remember to fix it!

@sbrosnihan
Copy link
Author

I am able to insert criteria with the add_criterion method, but all added criteria are set to and/or="and" even if I use "or" when I call it. The smartGroup.save() actually sets all criteria in the smart group to "and", even ones that were previously created on the JSS.

@sheagcraig
Copy link
Collaborator

@sbrosnihan I just added in the code to (at least) handle your first question about this issue. It's in testing now-should get released in the next 24 hours to production/master.

As for the issue about "or" getting switched to "and", I just tried it on jss v9.6 and it successfully lets me set "or" for the "and_or" part of a criterion.

Just playing around, I did see that I could add a criterion with wacky values and it just smoothes them over. For example:

g.add_criterion(name="Username", priority=1, and_or="tacos", search_type="is", value="bobafett2")
g.save()

results in a smart group with the above criteria's and_or value set to "and", despite clearly being asked to set it to tacos.

I'm sure this is a dumb question, but are you sure you're setting the and_or to the string value "or"? The python_jss end of it does zero validation, so it will happily let you assign any value to and_or, and it appears that the JSS will happily accept anything, and just turn it into "and".

Why this would then "and" all of your other criteria, I'm not sure!

If this is still an issue for you, let's open another issue to handle it, since the initial one here is now solved.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants