-
Notifications
You must be signed in to change notification settings - Fork 898
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
API Enhancement to support fine-grained settings whitelisting #13948
Merged
gtanzillo
merged 4 commits into
ManageIQ:master
from
abellotti:api_fine_grained_settings
Mar 16, 2017
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
7df0015
- Adding support for arbitrary path categories, can include top level
abellotti 0354870
Updated rspecs so a well defined set of the settings gets merged
abellotti 69203c8
Simplified logic of settings_entry_to_hash by leveraging store_path
abellotti 86cd8ff
- Reduced the scope of the stub_api_collection_config to just the
abellotti File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,36 @@ | ||
module Api | ||
class SettingsController < BaseController | ||
def index | ||
render_resource :settings, slice_settings(exposed_settings) | ||
render_resource :settings, whitelist_settings(settings_hash) | ||
end | ||
|
||
def show | ||
raise NotFoundError, "Settings category #{@req.c_id} not found" unless exposed_settings.include?(@req.c_id) | ||
render_resource :settings, slice_settings(@req.c_id) | ||
settings_value = entry_value(whitelist_settings(settings_hash), @req.c_suffix) | ||
|
||
raise NotFoundError, "Settings entry #{@req.c_suffix} not found" if settings_value.nil? | ||
render_resource :settings, settings_entry_to_hash(@req.c_suffix, settings_value) | ||
end | ||
|
||
private | ||
|
||
def exposed_settings | ||
ApiConfig.collections[:settings][:categories] | ||
def whitelist_settings(settings) | ||
result_hash = {} | ||
ApiConfig.collections[:settings][:categories].each do |category_path| | ||
result_hash.deep_merge!(settings_entry_to_hash(category_path, entry_value(settings, category_path))) | ||
end | ||
result_hash | ||
end | ||
|
||
def settings_hash | ||
@settings_hash ||= Settings.to_hash.deep_stringify_keys | ||
end | ||
|
||
def entry_value(settings, path) | ||
settings.fetch_path(path.split('/')) | ||
end | ||
|
||
def slice_settings(keys) | ||
::Settings.to_hash.slice(*Array(keys).collect(&:to_sym)) | ||
def settings_entry_to_hash(path, value) | ||
{}.tap { |h| h.store_path(path.split("/"), value) } | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This seems (a) sufficiently complicated, and (b) already abstracted away from details of the request to warrant a service object that could be perhaps better tested in isolation. WDYT?
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.
This method is coded as it's needed as such by a following PR. Some of this code will be moving to an API Settings mixin, will be used by servers, regions and zones.
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.
Not sure I follow. Is there a problem with delegating?