Skip to content

Commit

Permalink
Add delete automate domain support
Browse files Browse the repository at this point in the history
  • Loading branch information
jvlcek committed Jan 30, 2019
1 parent 3d33b03 commit a39a2f4
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 1 deletion.
23 changes: 23 additions & 0 deletions app/controllers/api/automate_domains_controller.rb
@@ -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
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
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

0 comments on commit a39a2f4

Please sign in to comment.