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 Mar 31, 2022
1 parent fbc52f7 commit 209e14b
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
20 changes: 20 additions & 0 deletions app/controllers/api/cloud_volumes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,25 @@ def create_backup_resource(type, id, data)
{:task_id => cloud_volume.backup_create_queue(User.current_userid, data)}
end
end

def attach_resource(type, id, data = {})
api_resource(type, id, "Attaching Resource to", :supports => :attach) 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)}
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) 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 @@ -797,6 +797,10 @@
:identifier: cloud_volume_safe_delete
- :name: create_backup
:identifier: cloud_volume_backup_create
- :name: attach
:identifier: cloud_volume_edit
- :name: detach
:identifier: cloud_volume_edit
:tags_subcollection_actions:
:post:
- :name: assign
Expand Down
32 changes: 32 additions & 0 deletions spec/requests/cloud_volumes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)

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)
disk = 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)

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
Expand Down

0 comments on commit 209e14b

Please sign in to comment.