-
Notifications
You must be signed in to change notification settings - Fork 358
/
cloud_subnet_controller.rb
278 lines (249 loc) · 9.38 KB
/
cloud_subnet_controller.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
class CloudSubnetController < ApplicationController
before_action :check_privileges
before_action :get_session_data
after_action :cleanup_action
after_action :set_session_data
include Mixins::GenericButtonMixin
include Mixins::GenericListMixin
include Mixins::GenericSessionMixin
include Mixins::GenericShowMixin
include Mixins::GenericFormMixin
def self.display_methods
%w(instances cloud_subnets)
end
def button
@edit = session[:edit] # Restore @edit for adv search box
params[:display] = @display if %w(vms instances images).include?(@display)
params[:page] = @current_page unless @current_page.nil? # Save current page for list refresh
@refresh_div = "main_div"
case params[:pressed]
when "cloud_subnet_tag"
return tag("CloudSubnet")
when 'cloud_subnet_delete'
delete_subnets
when "cloud_subnet_edit"
javascript_redirect :action => "edit", :id => checked_item_id
else
if params[:pressed] == "cloud_subnet_new"
javascript_redirect :action => "new"
elsif !flash_errors? && @refresh_div == "main_div" && @lastaction == "show_list"
replace_gtl_main_div
else
render_flash
end
end
end
def new
assert_privileges("cloud_subnet_new")
@subnet = CloudSubnet.new
@in_a_form = true
@network_provider_choices = {}
ExtManagementSystem.where(:type => "ManageIQ::Providers::Openstack::NetworkManager").find_each do |ems|
@network_provider_choices[ems.name] = ems.id
end
drop_breadcrumb(
:name => _("Add New Subnet"),
:url => "/cloud_subnet/new"
)
end
def create
assert_privileges("cloud_subnet_new")
case params[:button]
when "cancel"
javascript_redirect :action => 'show_list',
:flash_msg => _("Creation of a Cloud Subnet was cancelled by the user")
when "add"
@subnet = CloudSubnet.new
options = new_form_params
ems = ExtManagementSystem.find(options[:ems_id])
if CloudSubnet.class_by_ems(ems).supports_create?
options.delete(:ems_id)
task_id = ems.create_cloud_subnet_queue(session[:userid], options)
if task_id.kind_of?(Integer)
initiate_wait_for_task(:task_id => task_id, :action => "create_finished")
else
javascript_flash(
:text => _("Cloud Subnet creation: Task start failed: ID [%{id}]") % {:id => task_id.to_s},
:severity => :error,
:spinner_off => true
)
end
else
@in_a_form = true
add_flash(_(CloudSubnet.unsupported_reason(:create)), :error)
drop_breadcrumb(:name => _("Add new Cloud Subnet "), :url => "/subnet/new")
javascript_flash
end
end
end
def create_finished
task_id = session[:async][:params][:task_id]
subnet_name = session[:async][:params][:name]
task = MiqTask.find(task_id)
if MiqTask.status_ok?(task.status)
add_flash(_("Cloud Subnet \"%{name}\" created") % { :name => subnet_name })
else
add_flash(_("Unable to create Cloud Subnet: %{details}") %
{ :name => subnet_name, :details => task.message }, :error)
end
@breadcrumbs.pop if @breadcrumbs
session[:edit] = nil
session[:flash_msgs] = @flash_array.dup if @flash_array
javascript_redirect :action => "show_list"
end
def delete_subnets
assert_privileges("cloud_subnet_delete")
subnets = find_records_with_rbac(CloudSubnet, checked_or_params)
if subnets.empty?
add_flash(_("No Cloud Subnet were selected for deletion."), :error)
end
subnets_to_delete = []
subnets.each do |subnet|
if subnet.nil?
add_flash(_("Cloud Subnet no longer exists."), :error)
elsif subnet.supports_delete?
subnets_to_delete.push(subnet)
end
end
unless subnets_to_delete.empty?
process_cloud_subnets(subnets_to_delete, "destroy")
end
# refresh the list if applicable
if @lastaction == "show_list"
show_list
@refresh_partial = "layouts/gtl"
elsif @lastaction == "show" && @layout == "cloud_subnet"
@single_delete = true unless flash_errors?
if @flash_array.nil?
add_flash(_("The selected Cloud Subnet was deleted"))
end
javascript_redirect :action => 'show_list', :flash_msg => @flash_array[0][:message]
else
drop_breadcrumb(:name => 'dummy', :url => " ") # missing a bc to get correctly back so here's a dummy
session[:flash_msgs] = @flash_array.dup if @flash_array
redirect_to(previous_breadcrumb_url)
end
end
def edit
params[:id] = checked_item_id unless params[:id].present?
assert_privileges("cloud_subnet_edit")
@subnet = find_record_with_rbac(CloudSubnet, params[:id])
@in_a_form = true
drop_breadcrumb(
:name => _("Edit Subnet \"%{name}\"") % {:name => @subnet.name},
:url => "/cloud_subnet/edit/#{@subnet.id}"
)
end
def update
assert_privileges("cloud_subnet_edit")
@subnet = find_record_with_rbac(CloudSubnet, params[:id])
options = changed_form_params
case params[:button]
when "cancel"
cancel_action(_("Edit of Subnet \"%{name}\" was cancelled by the user") % {:name => @subnet.name})
when "save"
if @subnet.supports_create?
task_id = @subnet.update_cloud_subnet_queue(session[:userid], options)
if task_id.kind_of?(Integer)
initiate_wait_for_task(:task_id => task_id, :action => "update_finished")
else
javascript_flash(
:text => _("Cloud Subnet update failed: Task start failed: ID [%{id}]") % {:id => task_id.to_s},
:severity => :error,
:spinner_off => true
)
end
else
add_flash(_("Couldn't initiate update of Cloud Subnet \"%{name}\": %{details}") % {
:name => @subnet.name,
:details => @subnet.unsupported_reason(:update)
}, :error)
end
end
end
def update_finished
task_id = session[:async][:params][:task_id]
subnet_name = session[:async][:params][:name]
task = MiqTask.find(task_id)
if MiqTask.status_ok?(task.status)
add_flash(_("Cloud Subnet \"%{name}\" updated") % {:name => subnet_name })
else
add_flash(
_("Unable to update Cloud Subnet \"%{name}\": %{details}") % { :name => subnet_name,
:details => task.message }, :error)
end
session[:edit] = nil
session[:flash_msgs] = @flash_array.dup if @flash_array
javascript_redirect previous_breadcrumb_url
end
private
def textual_group_list
[%i(properties relationships), %i(tags)]
end
helper_method :textual_group_list
def switch_to_bol(option)
if option && option =~ /on|true/i
true
else
false
end
end
def changed_form_params
# Fields allowed for update are: name, enable_dhcp, dns_nameservers, allocation_pools, host_routes, gateway_ip
options = {}
options[:name] = params[:name] unless @subnet.name == params[:name]
# A gateway address is automatically assigned by Openstack when gateway is null
unless @subnet.gateway == params[:gateway]
options[:gateway_ip] = params[:gateway].blank? ? nil : params[:gateway]
end
unless @subnet.dhcp_enabled == switch_to_bol(params[:dhcp_enabled])
options[:enable_dhcp] = switch_to_bol(params[:dhcp_enabled])
end
# TODO: Add dns_nameservers, allocation_pools, host_routes
options
end
def new_form_params
params[:network_protocol] ||= "ipv4"
params[:dhcp_enabled] ||= false
options = {}
options[:name] = params[:name] if params[:name]
options[:ems_id] = params[:ems_id] if params[:ems_id]
options[:cidr] = params[:cidr] if params[:cidr]
# An address is automatically assigned by Openstack when gateway is null
if params[:gateway]
options[:gateway] = params[:gateway].blank? ? nil : params[:gateway]
end
options[:ip_version] = params[:network_protocol] =~ /4/ ? 4 : 6
options[:cloud_tenant] = find_record_with_rbac(CloudTenant, from_cid(params[:cloud_tenant_id])) if params[:cloud_tenant_id]
options[:network_id] = params[:network_id] if params[:network_id]
options[:enable_dhcp] = params[:dhcp_enabled]
# TODO: Add extra fields
options[:availability_zone_id] = params[:availability_zone_id] if params[:availability_zone_id]
if params[:ipv6_router_advertisement_mode]
options[:ipv6_router_advertisement_mode] = params[:ipv6_router_advertisement_mode]
end
options[:ipv6_address_mode] = params[:ipv6_address_mode] if params[:ipv6_address_mode]
options[:network_group_id] = params[:network_group_id] if params[:network_group_id]
options[:parent_cloud_subnet_id] = params[:parent_cloud_subnet_id] if params[:parent_cloud_subnet_id]
options
end
# dispatches operations to multiple subnets
def process_cloud_subnets(subnets, operation)
return if subnets.empty?
if operation == "destroy"
deleted_subnets = 0
subnets.each do |subnet|
audit = {
:event => "cloud_subnet_record_delete_initiated",
:message => "[#{subnet.name}] Record delete initiated",
:target_id => subnet.id,
:target_class => "CloudSubnet",
:userid => session[:userid]
}
AuditEvent.success(audit)
subnet.delete_cloud_subnet_queue(session[:userid])
end
end
end
menu_section :net
end