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

Add delete automate domain support #548

Merged
merged 1 commit into from
Feb 1, 2019
Merged
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
23 changes: 23 additions & 0 deletions app/controllers/api/automate_domains_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
module Api
class AutomateDomainsController < BaseController
def delete_resource(type, id = nil, _data = {})
raise BadRequestError, "Must specify an id for deleting a #{type} resource" unless id

delete_resource_action(type, id)
end

def refresh_from_source_resource(type, id = nil, data = nil)
raise BadRequestError, "Must specify an id for refreshing a #{type} resource from source" unless id

Expand Down Expand Up @@ -29,6 +35,23 @@ def refresh_from_source_resource(type, id = nil, data = nil)

private

def delete_resource_action(type, id)
api_action(type, id) do |klass|
domain = resource_search(id, type, klass)
api_log_info("Delete will be queued for #{automate_domain_ident(domain)}")

begin
# Only delete unlocked user domains. System or GIT based domains will not be deleted.
MiqAeDomain.where(:name => domain.name).each { |d| raise "Not deleting. Domain is locked." if d.contents_locked? }

MiqAeDomain.where(:name => domain.name).each(&:destroy_queue)
action_result(true, "Delete queued for #{automate_domain_ident(domain)}")
rescue => err
action_result(false, err.to_s)
end
end
end

def automate_domain_ident(domain)
"Automate Domain id:#{domain.id} name:'#{domain.name}'"
end
Expand Down
9 changes: 8 additions & 1 deletion config/api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@
:description: Automate Domains
:options:
- :collection
:verbs: *gp
:verbs: *gpd
:klass: MiqAeDomain
:collection_actions:
:get:
Expand All @@ -253,13 +253,20 @@
:identifier: miq_ae_domain_view
- :name: refresh_from_source
:identifier: miq_ae_git_refresh
- :name: delete
:identifier: miq_ae_domain_delete
:resource_actions:
:get:
- :name: read
:identifier: miq_ae_domain_view
:post:
- :name: refresh_from_source
:identifier: miq_ae_git_refresh
- :name: delete
:identifier: miq_ae_domain_delete
:delete:
- :name: delete
:identifier: miq_ae_domain_delete
:automate_workspaces:
:description: Automate Workspaces
:options:
Expand Down
45 changes: 45 additions & 0 deletions spec/requests/automate_domains_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,51 @@
end
end

describe 'delete action' do
let(:automate_domain) { FactoryBot.create(:miq_ae_domain) }
let(:automate_domain_locked) { FactoryBot.create(:miq_ae_domain_user_locked, :enabled => true) }
let(:automate_domain_system) { FactoryBot.create(:miq_ae_system_domain_enabled) }

it 'forbids access for users without proper permissions' do
api_basic_authorize

post(api_automate_domain_url(nil, automate_domain), :params => gen_request(:delete))

expect(response).to have_http_status(:forbidden)
end

it 'does not delete locked domains' do
api_basic_authorize action_identifier(:automate_domains, :delete)

post(api_automate_domain_url(nil, automate_domain_locked), :params => gen_request(:delete))
expect_single_action_result(
:success => false,
:message => a_string_matching(/Not deleting.*locked/)
)
end

it 'does not delete system domains' do
api_basic_authorize action_identifier(:automate_domains, :delete)

post(api_automate_domain_url(nil, automate_domain_system), :params => gen_request(:delete))
expect_single_action_result(
:success => false,
:message => a_string_matching(/Not deleting.*locked/)
)
end

it 'deletes domains' do
api_basic_authorize action_identifier(:automate_domains, :delete)

post(api_automate_domain_url(nil, automate_domain), :params => gen_request(:delete))
expect_single_action_result(
:success => true,
:message => a_string_matching(/Delete queued for .*/),
:href => api_automate_domain_url(nil, automate_domain)
)
end
end

describe 'refresh_from_source action' do
let(:git_domain) { FactoryBot.create(:miq_ae_git_domain) }
it 'forbids access for users without proper permissions' do
Expand Down