-
Notifications
You must be signed in to change notification settings - Fork 98
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
Fixes #21674 - Add abstraction for subcommand searching #342
Conversation
@xprazak2, the Redmine ticket used is for a different project than the one associated with this GitHub repository. Please either:
If changing the ticket number used, remember to update the PR title and the commit message (using This message was auto-generated by Foreman's prprocessor |
Limitation:
if I create a subcommand in hammer_cli_plugin_a, where |
What's the status here? It blocks linked PR |
Could someone please take a look? This blocks further hammer openscap improvements and it's been sitting untouched here more than 2 months. |
any progress? |
ping @theforeman/hammer-admin |
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.
I tried this abstraction in hammer-cli-foreman-ansible
plugin and it worked fine for hammer host ansible-roles
and hammer ansible roles hosts
commands. I'd like to have it merged ASAP :)
I made updates as suggested. |
@xprazak2, needs rebase |
Rebased. |
Can we keep this moving? we should get this into 1.18 repos soon since the ansible hammer plugin is already there. |
@ares, just to be clear, I'm not using this approach in the latest version of ansible hammer plugin. By deafult it's waiting for theforeman/foreman_ansible#161 to be merged. If this is merged before 161, then a new version of ansible hammer plugin needs to be released. |
@ofedoren oh, based on this last comment Don't merge this for now, please. I guess there is another solution via #342. |
@ares are you sure we can't assign roles? I just checked via Since there is no easier way to get list of host's roles than searching like |
You're right, I was playing with hammer host ansible-roles and always got a reply that server does not support this. But one can use the command you mentioned, thanks. Still it would be nice to get this PR merged :-) |
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.
@xprazak2 thanks for adding this and sorry for ignoring the PR for so long.
Looks good overall just see my two concerns inline.
lib/hammer_cli_foreman/commands.rb
Outdated
@@ -279,6 +279,86 @@ def should_retrieve_all? | |||
|
|||
end | |||
|
|||
class ListSearchCommand < ListCommand |
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.
Could we have some more descriptive name? It took me while until I figured out what this class does. Would make AssociatedResourceListCommand
more sense?
lib/hammer_cli_foreman/commands.rb
Outdated
end | ||
|
||
def self.default_search_options | ||
option("--#{module_resource.singular_name}-id".tr('_', '-'), "ID", _("%s Id") % module_resource.singular_name) |
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.
I tried to convert host reports
into descendant of this class. The resulting command looks good and easy to read. However in hammer we traditionally use --id
and --name
to refer to the "module resource" in this type of commands i.e. host reports --id
and host reports --name
. Would you mind to change the option names accordingly? It may make sense to add a mapping to change it if you have a use case for that.
Updated, I changed the class name and the options |
@xprazak2 could you please check the tests? |
I fixed tests, all green now. |
any update here? @ofedoren / @mbacovsky |
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.
Sorry, @xprazak2, @ares for the delay.
Looks good now (my inline comments are just for keeping code style the same as we have now or at least similar everywhere :) ), if @mbacovsky has nothing to add I'd merge it.
One more thing though: have you taken a look at
class AssociatedResourceListCommand < ListCommand |
The purpose of these two looks very similar to me. Have you tried that command as a solution of the problem you had in other project?
P.S. I've run the test locally and only one failed: https://github.com/theforeman/hammer-cli-foreman/pull/342/files#diff-4c8a0b9258b2e44a8be27890bc78591eR156
There are no quotation marks in real output.
@@ -279,6 +279,86 @@ def should_retrieve_all? | |||
|
|||
end | |||
|
|||
class AssociatedListSearchCommand < ListCommand | |||
def self.search_resource(res, action = :index) | |||
resource res, action |
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.
Usually if we have method called with arguments, we use parenthesis.
|
||
def validate_options | ||
super | ||
validator.any("option_name".to_sym, "option_id".to_sym).required |
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.
Why use .to_sym
instead of just passing a symbol right away? Like here:
validator.any(:option_domain_name, :option_domain_id).required |
end | ||
|
||
def self.search_options_mapping(mapping = {}) | ||
{ "name" => module_resource.singular_name, |
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.
Rubocop style nitpick :)
Doesn't look better?
{
'name' => module_resource.singular_name,
'id' => "#{module_resource.singular_name}_id"
}.merge(mapping)
[test] |
Rebased. @ofedoren, I was looking at
Policy and host are not nested so the
I need to specify a search somehow, so that only the associated hosts are returned:
@mbacovsky, I had to revert the option naming back, having just |
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.
@xprazak2, you might want to return --id
and --name
options, because there is a way to omit id resolving that caused problems you described:
If you don't need to resolve id by name (I presume you want to skip it since you're using search by name instead), you can remove the SelfParam
option source, redefining the option_sources
method like
def option_sources
sources = super
sources.find_by_name('IdResolution').insert_relative(
:replace,
'SelfParam',
HammerCLI::Options::Sources::Base.new
)
sources
end
Otherwise, it seems to be working pretty well :)
test/unit/commands_test.rb
Outdated
end | ||
end | ||
comm = DomainOuter::HostsCommand.new("", { :adapter => :csv, :interactive => false }) | ||
out, err = capture_io { comm.run(["--host-id=5"]) } |
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.
--host-id
is wrong parameter in this case (it just doesn't exist, since parent command has domains
resource, so it should be rather --domain-id
as you designed).
test/unit/commands_test.rb
Outdated
end | ||
comm = DomainOuter::HostsCommand.new("", { :adapter => :csv, :interactive => false }) | ||
out, err = capture_io { comm.run(["--host-id=5"]) } | ||
out.must_equal "Id,Name,Operating System,Host Group,IP,MAC\n2,random-host,\"\",\"\",192.168.100.112,6e:4b:3c:2c:8a:0a\n" |
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.
There are no quotes in real output, so test will fail.
I pushed changes, thanks for suggestions @ofedoren! |
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.
Thanks for being patient, @xprazak2! Finally we are merging it :) |
No description provided.