From a73085b048d0318be759c46102a01ec42612a94f Mon Sep 17 00:00:00 2001 From: Melody-Ann-Seda-Marotte Date: Fri, 1 Apr 2022 11:21:42 -0400 Subject: [PATCH] Attach detach cloud volume api --- .../api/cloud_volumes_controller.rb | 22 +++++++++++++ config/api.yml | 4 +++ spec/requests/cloud_volumes_spec.rb | 32 +++++++++++++++++++ 3 files changed, 58 insertions(+) diff --git a/app/controllers/api/cloud_volumes_controller.rb b/app/controllers/api/cloud_volumes_controller.rb index fb606a884d..b7a61602ea 100644 --- a/app/controllers/api/cloud_volumes_controller.rb +++ b/app/controllers/api/cloud_volumes_controller.rb @@ -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 diff --git a/config/api.yml b/config/api.yml index 04087ed97c..df120bdfb2 100644 --- a/config/api.yml +++ b/config/api.yml @@ -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 diff --git a/spec/requests/cloud_volumes_spec.rb b/spec/requests/cloud_volumes_spec.rb index 87ae86b87f..2eea098d6d 100644 --- a/spec/requests/cloud_volumes_spec.rb +++ b/spec/requests/cloud_volumes_spec.rb @@ -192,6 +192,38 @@ 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 end describe 'create backup' do