-
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
Conversation
|
cc @lpichler |
498dcbc to
9a621e3
Compare
|
you need to call |
|
@Hyperkid123 : please, link the BZ for visibility |
e294bfd to
1b34362
Compare
|
Ping @lpichler |
| menus.each do |menu| | ||
| validate_pxe_create_data(menu, REQUIRED_PXE_MENU_ATTRS, 'pxe menu') | ||
| end | ||
| menus.map do |menu| |
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
collection_class(:pxe_menus).create(menu)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_menus is called and put validation just before it ?
if menus
server.pxe_menus.clear
menus.each{|menu| validate_data_for(collection_class(:pxe_menus), menu) }
data['pxe_menus'] = collection_class(:pxe_menus).create(menu)
end
def validate_data_for(klass, data)
klass_attributes = PXE_ATTRIBUTES[klass]
MISSING_ATTRIBUTES = ...
INVALID_ATTRIBUTES = ....
end |
|
|
||
| def edit_resource(type, id, data) | ||
| PxeServer.transaction do | ||
| server = resource_search(id, type, collection_class(:pxe_servers)) |
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.
are other validations missing here ?
| server.update_authentication(prepare_credentials(authentication), :save => true) | ||
| end | ||
|
|
||
| # assign pxe menus if needed |
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 think that comment like this is not needed
| module Api | ||
| class PxeServersController < BaseController | ||
| INVALID_PXE_SERVER_ATTRS = %w[id href].freeze # Cannot update or create these | ||
| REQUIRED_PXE_SERVER_ATTRS = %w[name uri].freeze |
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.
as I wrote bellow:
you can pust this to the hash:
PXE_ATTRIBUTES = {
PxeServer => %w[name uri],
...
}.freeze| validate_pxe_create_data(data, REQUIRED_PXE_SERVER_ATTRS, 'pxe server') | ||
|
|
||
| server = collection_class(:pxe_servers).new(data) | ||
| # generates uir_prefix which is ched if server needs authentication or not |
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 is good comment 👍 (it is not obvious from verify_uri_prefix_before_save why we need it)
|
|
||
| def create_resource(_type, _id, data = {}) | ||
| PxeServer.transaction do | ||
| authentication = data.delete('authentication') |
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.
is it possible to put code outside of PxeServer.transaction block which is not related to the transaction ? (same for edit)
| private | ||
|
|
||
| def prepare_credentials(authentication) | ||
| creds = {:default => {}} |
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.
can be this just?
{:default => authentication.compact}
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 if we need method prepare_credentials in this case
|
overall looks good 👍 and I added some comments - we can discuss it 👍 👾 |
1b34362 to
e6538f6
Compare
@lpichler I've addressed most of your feedback but i cannot use the classes as keys in hashes because they are not auto loaded correctly. I used string constants instead. |
e6538f6 to
d6a2c9d
Compare
|
Checked commits Hyperkid123/manageiq-api@2647011~...d6a2c9d with ruby 2.3.3, rubocop 0.69.0, haml-lint 0.20.0, and yamllint 1.10.0 |
Can that be done in a separate PR? @Hyperkid123 : can you, please, add some API call examples to the Description? |
|
@djberg96 can you review also please ? thanks! |
d6a2c9d to
eef07dc
Compare
spec/requests/pxe_servers_spec.rb
Outdated
| @@ -1,7 +1,13 @@ | |||
| RSpec.describe 'PxeServers API' do | |||
| let!(:pxe_server) { FactoryBot.create(:pxe_server) } | |||
| let(:pxe_server) do | |||
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.
@Hyperkid123 you can do this:
let(:pxe_server) { FactoryBot.build(:pxe_server).tap { |x| x.update_authentication(:default => {:userid => 'foo', :password => 'bar'}) } }b13b3fd to
459ad80
Compare
|
@Hyperkid123 we need to improve authorisation tied with product features.
and eventually you need to add it related blocks in api.yml to the diff --git a/config/api.yml b/config/api.yml
index 6d6e9cc3..97c12862 100644
--- a/config/api.yml
+++ b/config/api.yml
@@ -2445,6 +2445,9 @@
:get:
- :name: read
:identifier: pxe_server_view
+ :patch:
+ - :name: edit
+ :identifier: pxe_server_edit
:quotas:
:description: TenantQuotas
:options:
diff --git a/spec/requests/pxe_servers_spec.rb b/spec/requests/pxe_servers_spec.rb
index 5166427a..c39b9c6e 100644
--- a/spec/requests/pxe_servers_spec.rb
+++ b/spec/requests/pxe_servers_spec.rb
@@ -1,10 +1,6 @@
RSpec.describe 'PxeServers API' do
- let(:pxe_server) do
- pxe_server = FactoryBot.build(:pxe_server)
- pxe_server.update_authentication(:default => {:userid => 'foo', :password => 'bar'})
- pxe_server.save
- pxe_server
- end
+ let(:pxe_server) { FactoryBot.build(:pxe_server).tap { |x| x.update_authentication(:default => {:userid => 'foo', :password => 'bar'}) } }
+
let!(:pxe_image_1) { FactoryBot.create(:pxe_image, :pxe_server => pxe_server) }
let!(:pxe_image_2) { FactoryBot.create(:pxe_image, :pxe_server => pxe_server) }
let!(:pxe_menu_1) { FactoryBot.create(:pxe_menu, :pxe_server => pxe_server) }
@@ -187,6 +183,14 @@ RSpec.describe 'PxeServers API' do
post(url, :params => {:name => 'test server', :uri => 'smb://quax', :authentication => {:userid => 'user'}})
expect(response).to have_http_status(:bad_request)
end
+
+ it 'will not ... ' do
+ api_basic_authorize
+ post(url, :params => {:name => 'foo', :uri => 'bar/quax',
+ :pxe_menus => [{:file_name => 'menu_1'}, {:file_name => 'menu_2'}],
+ :authentication => {:userid => 'foo', :password => 'bar' }})
+ expect(response).to have_http_status(:forbidden)
+ end
end
describe 'patch /api/pxe_servers/:id' do
@@ -200,6 +204,12 @@ RSpec.describe 'PxeServers API' do
expect(response.parsed_body['uri']).to eq('updated/url')
expect(PxeServer.find(response.parsed_body['id']).pxe_menus.first[:file_name]).to eq('updated menu')
end
+
+ it 'will not .. ' do
+ api_basic_authorize
+ patch(url, :params => {:name => 'updated name', :uri => 'updated/url', :pxe_menus => [{:file_name => 'updated menu'}]})
+ expect(response).to have_http_status(:forbidden)
+ end
end
describe 'delete /api/pxe_servers/:id' do
@@ -211,5 +221,11 @@ RSpec.describe 'PxeServers API' do
expect(response).to have_http_status(:no_content)
expect(PxeServer.exists?(pxe_server.id)).to be_falsey
end
+
+ it 'will not show ...' do
+ api_basic_authorize
+ delete(url)
+ expect(response).to have_http_status(:forbidden)
+ end
end
end |
459ad80 to
021be88
Compare
lpichler
left a comment
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 @Hyperkid123
We've got a BZ for add/edit PxeServer in ui-classic which will require us to rewrite the UI. Part of that is removing current code from ui-classic and let the API handle these actions instead.
https://bugzilla.redhat.com/show_bug.cgi?id=1537267
TODO
file depot credentials validation id model: [WIP] Add validation to model for FileDepots manageiq#18795