Skip to content

Commit 5dacc20

Browse files
authored
Merge pull request #1006 from Autosde/introduced_volume_mapping
Added creation of new VolumeMapping
2 parents 5de68ca + 2efa46b commit 5dacc20

File tree

3 files changed

+207
-0
lines changed

3 files changed

+207
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
module Api
2+
class VolumeMappingsController < BaseController
3+
def refresh_resource(type, id, _data = nil)
4+
raise BadRequestError, "Must specify an id for refreshing a #{type} resource" if id.blank?
5+
6+
ensure_resource_exists(type, id) if single_resource?
7+
8+
api_action(type, id) do |klass|
9+
volume_mapping = resource_search(id, type, klass)
10+
api_log_info("Refreshing #{volume_mapping_ident(volume_mapping)}")
11+
refresh_volume_mapping(volume_mapping)
12+
end
13+
end
14+
15+
def create_resource(_type, _id = nil, data = {})
16+
ext_management_system = ExtManagementSystem.find(data['ems_id'])
17+
18+
klass = VolumeMapping.class_by_ems(ext_management_system)
19+
raise BadRequestError, klass.unsupported_reason(:create) unless klass.supports?(:create)
20+
21+
task_id = VolumeMapping.create_volume_mapping_queue(session[:userid], ext_management_system, data)
22+
action_result(true, "Creating Volume Mapping for Provider: #{ext_management_system.name}", :task_id => task_id)
23+
rescue => err
24+
action_result(false, err.to_s)
25+
end
26+
27+
private
28+
29+
def ensure_resource_exists(type, id)
30+
raise NotFoundError, "#{type} with id:#{id} not found" unless collection_class(type).exists?(id)
31+
end
32+
33+
def refresh_volume_mapping(volume_mapping)
34+
desc = "#{volume_mapping_ident(volume_mapping)} refreshing"
35+
task_id = queue_object_action(volume_mapping, desc, :method_name => "refresh_ems", :role => "ems_operations")
36+
action_result(true, "#{volume_mapping_ident(volume_mapping)} refreshing", :task_id => task_id)
37+
rescue => err
38+
action_result(false, err.to_s)
39+
end
40+
41+
def volume_mapping_ident(volume_mapping)
42+
"Volume Mapping id:#{volume_mapping.id}"
43+
end
44+
end
45+
end

config/api.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4897,6 +4897,30 @@
48974897
:identifier:
48984898
- vm_snapshot_delete
48994899
- sui_vm_snapshot_delete
4900+
:volume_mappings:
4901+
:description: Volume Mappings
4902+
:identifier: volume_mapping
4903+
:options:
4904+
- :collection
4905+
:verbs: *gp
4906+
:klass: VolumeMapping
4907+
:subcollections:
4908+
:collection_actions:
4909+
:get:
4910+
- :name: read
4911+
:identifier: volume_mapping_show_list
4912+
:post:
4913+
- :name: create
4914+
:identifier: volume_mapping_new
4915+
- :name: refresh
4916+
:identifier: volume_mapping_refresh
4917+
:resource_actions:
4918+
:get:
4919+
- :name: read
4920+
:identifier: volume_mapping_show
4921+
:post:
4922+
- :name: refresh
4923+
:identifier: volume_mapping_refresh
49004924
:widgets:
49014925
:description: Miq Widgets
49024926
:identifier: miq_report_widget_admin
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
describe "Volume Mappings API" do
2+
context "POST /api/volume_mappings" do
3+
it "with an invalid ems_id it responds with 404 Not Found" do
4+
api_basic_authorize(collection_action_identifier(:volume_mappings, :create))
5+
6+
request = {
7+
"action" => "create",
8+
"resource" => {
9+
"ems_id" => nil,
10+
"cloud_volume_id" => "1",
11+
"host_initiator_id" => "1",
12+
}
13+
}
14+
15+
post(api_volume_mappings_url, :params => request)
16+
17+
expect(response).to have_http_status(:bad_request)
18+
end
19+
20+
it "creates new Volume Mapping" do
21+
api_basic_authorize(collection_action_identifier(:volume_mappings, :create))
22+
provider = FactoryBot.create(:ems_autosde, :name => 'Autosde')
23+
cloud_volume = FactoryBot.create(:cloud_volume)
24+
host_initiator = FactoryBot.create(:host_initiator)
25+
26+
request = {
27+
"action" => "create",
28+
"resource" => {
29+
"ems_id" => provider.id,
30+
"cloud_volume_id" => cloud_volume.id,
31+
"host_initiator_id" => host_initiator.id,
32+
}
33+
}
34+
35+
post(api_volume_mappings_url, :params => request)
36+
37+
expect_multiple_action_result(1, :success => true, :message => "Creating Host Initiator test_host_initiator for Provider: #{provider.name}", :task => true)
38+
end
39+
end
40+
41+
context "GET /api/volume_mappings" do
42+
it "returns all volume mappings" do
43+
volume_mapping = FactoryBot.create(:volume_mapping)
44+
api_basic_authorize('volume_mapping_show_list')
45+
46+
get(api_volume_mappings_url)
47+
48+
expected = {
49+
"name" => "volume_mappings",
50+
"resources" => [{"href" => api_volume_mapping_url(nil, volume_mapping)}]
51+
}
52+
expect(response).to have_http_status(:ok)
53+
expect(response.parsed_body).to include(expected)
54+
end
55+
end
56+
57+
context "GET /api/volume_mappings/:id" do
58+
it "returns one volume_mapping" do
59+
volume_mapping = FactoryBot.create(:volume_mapping)
60+
api_basic_authorize('volume_mapping_show')
61+
62+
get(api_volume_mapping_url(nil, volume_mapping))
63+
64+
expected = {
65+
"href" => api_volume_mapping_url(nil, volume_mapping)
66+
}
67+
expect(response).to have_http_status(:ok)
68+
expect(response.parsed_body).to include(expected)
69+
end
70+
end
71+
72+
describe "Volume Mapping refresh action" do
73+
context "with an invalid id" do
74+
it "it responds with 404 Not Found" do
75+
api_basic_authorize(action_identifier(:volume_mappings, :refresh, :resource_actions, :post))
76+
77+
post(api_volume_mapping_url(nil, 999_999), :params => gen_request(:refresh))
78+
79+
expect(response).to have_http_status(:not_found)
80+
end
81+
end
82+
83+
context "without an appropriate role" do
84+
it "it responds with 403 Forbidden" do
85+
volume_mapping = FactoryBot.create(:volume_mapping)
86+
api_basic_authorize
87+
88+
post(api_volume_mapping_url(nil, volume_mapping), :params => gen_request(:refresh))
89+
90+
expect(response).to have_http_status(:forbidden)
91+
end
92+
end
93+
94+
context "with an appropriate role" do
95+
it "rejects refresh for unspecified volume mapping" do
96+
api_basic_authorize(action_identifier(:volume_mappings, :refresh, :resource_actions, :post))
97+
98+
post(api_volume_mappings_url, :params => gen_request(:refresh, [{"href" => "/api/volume_mappings/"}, {"href" => "/api/volume_mappings/"}]))
99+
100+
expect_bad_request(/Must specify an id/i)
101+
end
102+
103+
it "refresh of a single Volume Mapping" do
104+
volume_mapping = FactoryBot.create(:volume_mapping)
105+
api_basic_authorize('volume_mapping_refresh')
106+
107+
post(api_volume_mapping_url(nil, volume_mapping), :params => gen_request(:refresh))
108+
109+
expect_single_action_result(:success => true, :message => /#{volume_mapping.id}.* refreshing/i, :href => api_volume_mapping_url(nil, volume_mapping))
110+
end
111+
112+
it "refresh of multiple Host Initiators" do
113+
volume_mapping = FactoryBot.create(:volume_mapping)
114+
volume_mapping_two = FactoryBot.create(:volume_mapping)
115+
api_basic_authorize('volume_mapping_refresh')
116+
117+
post(api_volume_mappings_url, :params => gen_request(:refresh, [{"href" => api_volume_mapping_url(nil, volume_mapping)}, {"href" => api_volume_mapping_url(nil, volume_mapping_two)}]))
118+
119+
expected = {
120+
"results" => a_collection_containing_exactly(
121+
a_hash_including(
122+
"message" => a_string_matching(/#{volume_mapping.id}.* refreshing/i),
123+
"success" => true,
124+
"href" => api_volume_mapping_url(nil, volume_mapping)
125+
),
126+
a_hash_including(
127+
"message" => a_string_matching(/#{volume_mapping_two.id}.* refreshing/i),
128+
"success" => true,
129+
"href" => api_volume_mapping_url(nil, volume_mapping_two)
130+
)
131+
)
132+
}
133+
expect(response.parsed_body).to include(expected)
134+
expect(response).to have_http_status(:ok)
135+
end
136+
end
137+
end
138+
end

0 commit comments

Comments
 (0)