-
Notifications
You must be signed in to change notification settings - Fork 144
Added PxeServer create, update and delete actions #594
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| module Api | ||
| class PxeMenusController < BaseController | ||
| end | ||
| end |
This file contains hidden or 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,5 +1,78 @@ | ||
| module Api | ||
| class PxeServersController < BaseController | ||
| include Subcollections::PxeImages | ||
| include Subcollections::PxeMenus | ||
| INVALID_ATTRIBUTES = { | ||
| "PxeServer" => %w[id href], # Cannot update or create these | ||
| }.freeze | ||
| PXE_ATTRIBUTES = { | ||
| "PxeServer" => %w[name uri], | ||
| "PxeMenu" => %w[file_name], | ||
| "Authentication" => %w[userid password] | ||
| }.freeze | ||
|
|
||
| def create_resource(_type, _id, data = {}) | ||
| authentication = data.delete('authentication') | ||
| menus = data.delete('pxe_menus') | ||
| validate_data_for('PxeServer', data) | ||
|
|
||
| PxeServer.transaction do | ||
| server = collection_class(:pxe_servers).new(data) | ||
| # generates uir_prefix which checks if server needs authentication or not | ||
| server.verify_uri_prefix_before_save | ||
|
|
||
| if server.requires_credentials? && server.missing_credentials? | ||
| validate_data_for('Authentication', authentication || {}) | ||
| server.update_authentication({:default => authentication.compact}, {:save => true}) | ||
| end | ||
|
|
||
| server.pxe_menus = create_pxe_menus(menus) if menus | ||
|
|
||
| if server.invalid? | ||
| raise BadRequestError, "Failed to add a pxe server - #{server.errors.full_messages.join(', ')}" | ||
| end | ||
|
|
||
| server.save | ||
| server | ||
| end | ||
| end | ||
|
|
||
| def delete_resource(_type, id = nil, data = nil) | ||
| raise BadRequestError, "Must specify an id for deleting a pxe server" unless id | ||
|
|
||
| super | ||
| end | ||
|
|
||
| def edit_resource(type, id, data) | ||
| server = resource_search(id, type, collection_class(:pxe_servers)) | ||
| menus = data.delete('pxe_menus') | ||
| authentication = data.delete('authentication') | ||
| PxeServer.transaction do | ||
| if menus | ||
| server.pxe_menus.clear | ||
| data['pxe_menus'] = create_pxe_menus(menus) | ||
| end | ||
| server.update!(data) | ||
| server.update_authentication({:default => authentication.transform_keys(&:to_sym)}, {:save => true}) if authentication && server.requires_credentials? | ||
| server | ||
| end | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def create_pxe_menus(menus) | ||
| menus.each do |menu| | ||
| validate_data_for('PxeMenu', menu) | ||
| end | ||
| menus.map do |menu| | ||
| collection_class(:pxe_menus).create(menu) | ||
| end | ||
| end | ||
|
|
||
| def validate_data_for(klass, data) | ||
| bad_attrs = [] | ||
| PXE_ATTRIBUTES[klass].each { |attr| bad_attrs << attr if data[attr].blank? } | ||
| raise BadRequestError, "Missing attribute(s) #{bad_attrs.join(', ')} for creating a #{klass}" if bad_attrs.present? | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| module Api | ||
| module Subcollections | ||
| module PxeMenus | ||
| def pxe_menus_query_resource(object) | ||
| object.pxe_menus | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or 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
This file contains hidden or 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 should work also
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.
in this case, what about to put line ^ on places where
create_pxe_menusis called and put validation just before it ?