Skip to content

Commit

Permalink
Attach detach cloud volume api
Browse files Browse the repository at this point in the history
  • Loading branch information
MelsHyrule committed Apr 4, 2022
1 parent 58066bf commit fd8cef4
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
22 changes: 22 additions & 0 deletions app/controllers/api/cloud_volumes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,27 @@ def restore_backup_resource(type, id, data)
{:task_id => cloud_volume.backup_restore_queue(User.current_userid, data.symbolize_keys)}
end
end

def attach_resource(type, id, data = {})
api_resource(type, id, "Attaching Resource to", :supports => :attach_volume) do |cloud_volume|
raise BadRequestError, "Must specify a vm_id" if data["vm_id"].blank?

vm = resource_search(data["vm_id"], :vms)
{:task_id => cloud_volume.attach_volume_queue(User.current_userid, vm.ems_ref, data["device"].presence)}
end
rescue => err
action_result(false, err.to_s)
end

def detach_resource(type, id, data = {})
api_resource(type, id, "Detaching Resource from", :supports => :detach_volume) do |cloud_volume|
raise BadRequestError, "Must specify a vm_id" if data["vm_id"].blank?

vm = resource_search(data["vm_id"], :vms)
{:task_id => cloud_volume.detach_volume_queue(User.current_userid, vm.ems_ref)}
end
rescue => err
action_result(false, err.to_s)
end
end
end
4 changes: 4 additions & 0 deletions config/api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,10 @@
:identifier: cloud_volume_backup_create
- :name: restore_backup
:identifier: cloud_volume_backup_restore
- :name: attach
:identifier: cloud_volume_edit
- :name: detach
:identifier: cloud_volume_edit
:tags_subcollection_actions:
:post:
- :name: assign
Expand Down
63 changes: 63 additions & 0 deletions spec/requests/cloud_volumes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,69 @@
expect(response.parsed_body['data']).to match("form_schema" => {"fields" => []})
expect(response).to have_http_status(:ok)
end

it 'attaches Cloud Volume to an instance' do
zone = FactoryBot.create(:zone)
ems = FactoryBot.create(:ems_autosde, :zone => zone)
vm = FactoryBot.create(:vm_vmware)
cloud_volume = FactoryBot.create(:cloud_volume_autosde, :ext_management_system => ems)

api_basic_authorize(action_identifier(:cloud_volumes, :attach, :resource_actions, :post))
stub_supports(cloud_volume.class, :attach_volume)

payload = {:action => "attach", :resources => {:vm_id => vm.id.to_s}}
post(api_cloud_volume_url(nil, cloud_volume), :params => payload)

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

it 'detaches Cloud Volume from an instance' do
zone = FactoryBot.create(:zone)
ems = FactoryBot.create(:ems_autosde, :zone => zone)
hw = FactoryBot.create(:hardware)
vm = FactoryBot.create(:vm_vmware, :hardware => hw)
cloud_volume = FactoryBot.create(:cloud_volume_autosde, :ext_management_system => ems)
FactoryBot.create(:disk, :hardware => hw, :backing => cloud_volume)

api_basic_authorize(action_identifier(:cloud_volumes, :detach, :resource_actions, :post))
stub_supports(cloud_volume.class, :detach_volume)

payload = {:action => "detach", :resources => {:vm_id => vm.id.to_s}}
post(api_cloud_volume_url(nil, cloud_volume), :params => payload)

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

it "attach raise an error if the cloud volume does not support attach_volume" do
cloud_volume = FactoryBot.create(:cloud_volume_autosde)
stub_supports_not(:cloud_volumes, :attach_volume)

api_basic_authorize(action_identifier(:cloud_volumes, :attach, :resource_actions, :post))

post(api_cloud_volume_url(nil, cloud_volume), :params => {:action => "attach"})

expected = {
"success" => false,
"message" => a_string_including("Attach Volume for Cloud Volume id: #{cloud_volume.id} name: '': Feature not available\/supported")
}
expect(response.parsed_body).to include(expected)
expect(response).to have_http_status(:bad_request)
end

it "detach raise an error if the cloud volume does not support detach_volume" do
cloud_volume = FactoryBot.create(:cloud_volume_autosde)
stub_supports_not(:cloud_volumes, :detach_volume)

api_basic_authorize(action_identifier(:cloud_volumes, :detach, :resource_actions, :post))

post(api_cloud_volume_url(nil, cloud_volume), :params => {:action => "detach"})
expected = {
"success" => false,
"message" => a_string_including("Detach Volume for Cloud Volume id: #{cloud_volume.id} name: '': Feature not available\/supported")
}
expect(response.parsed_body).to include(expected)
expect(response).to have_http_status(:bad_request)
end
end

describe 'create backup' do
Expand Down

0 comments on commit fd8cef4

Please sign in to comment.