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

Fixes #38241 - Extend userdata API by MAC address endpoint #912

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 6 additions & 0 deletions modules/templates/templates_userdata_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,10 @@ class Proxy::TemplatesUserdataApi < Sinatra::Base
Proxy::Templates::UserdataProxyRequest.new.get(kind, request.env, params)
end
end

get "/:mac/:kind" do |mac, kind|
log_halt(500, "Failed to retrieve #{kind} userdata template for #{params.inspect}: ") do
Proxy::Templates::UserdataProxyRequest.new.get([mac, kind], request.env, params)
Copy link
Member

Choose a reason for hiding this comment

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

Looking at the method this feels like an abuse of the API:

Technically it may work since it calls flatten, but I really don't like flatten because it ends up being really unpredictable what gets called.

But that's probably not the real issue. If you look at ProxyRequest you can see it copies the parameters while removing a blacklist:

opts = params.clone.merge(:url => template_url)
BLACKLIST_PARAMETERS.each do |blacklisted_parameter|
opts.delete(blacklisted_parameter)
end

Looking at that blacklist, we can see kind is in there:

BLACKLIST_PARAMETERS = ['path', 'template', 'kind', 'hostgroup', 'splat', 'captures']

I suspect both mac and kind are in params. Not entirely sure what the correct thing to do here, but this whole API feels awkward.

Copy link
Author

Choose a reason for hiding this comment

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

Alright, I see why hostgroup and template don't need to be added in the test - thanks!

However, to make this API feel less awkward I see two improvements currently:

a.) Add another function in UserdataProxyRequest to mitigate the flatten here?
Like a def get_mac and then put the mac as mandatory function parameter? But, this makes it less generic..

b.) Overwrite BLACKLIST_PARAMETERS when inheriting ProxyRequest and add mac there?

What do you think?

end
end
end
9 changes: 9 additions & 0 deletions test/templates/templates_userdata_api_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,13 @@ def test_api_can_ask_for_a_cloud_init_template
assert last_response.ok?, "Last response was ok"
assert_match("A user-data template", last_response.body)
end

def test_api_can_ask_for_a_cloud_init_template_by_mac
stub_request(:get, "#{@foreman_url}/userdata/00:a1:b2:c3:d4:f5/user-data")
.with(query: {"url" => @template_url, "mac" => "00:a1:b2:c3:d4:f5"})
.to_return(:body => 'A user-data template')
get "/00:a1:b2:c3:d4:f5/user-data"
assert last_response.ok?, "Last response was ok"
assert_equal("A user-data template", last_response.body)
end
end