/
compute_resources_helper.rb
115 lines (97 loc) · 3.74 KB
/
compute_resources_helper.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
module ComputeResourcesHelper
include LookupKeysHelper
def vm_state(vm)
if vm.state == 'PAUSED'
' ' + _("Paused")
else
vm.ready? ? _("On") : _("Off")
end
end
def action_string(vm)
vm.ready? ? ' ' + _("Off") : ' ' + _("On")
end
def vm_power_class(s)
"class='label #{s ? 'label-success' : 'label-default'}'".html_safe
end
def vm_power_action(vm, authorizer = nil)
opts = hash_for_power_compute_resource_vm_path(:compute_resource_id => @compute_resource, :id => vm.identity).merge(:auth_object => @compute_resource, :permission => 'power_compute_resources_vms', :authorizer => authorizer)
html = power_action_html(vm)
display_link_if_authorized "Power #{action_string(vm)}", opts, html.merge(:method => :put)
end
def power_action_html(vm)
if vm.ready?
{
:data => { :confirm => _("Are you sure you want to power %{act} %{vm}?") % { :act => action_string(vm).downcase.strip, :vm => vm }},
:class => "btn btn-danger",
}
else
{ :class => "btn btn-info" }
end
end
def vm_pause_action(vm, authorizer = nil)
opts = hash_for_pause_compute_resource_vm_path(:compute_resource_id => @compute_resource, :id => vm.identity).merge(:auth_object => @compute_resource, :permission => 'power_compute_resources_vms', :authorizer => authorizer)
html = pause_action_html(vm)
display_link_if_authorized pause_action_string(vm), opts, html.merge(:method => :put)
end
def pause_action_html(vm)
pause_action = pause_action_string(vm).downcase
if vm.state.downcase == 'paused'
{ :class => "btn btn-info" }
else
{
:data => { :confirm => _("Are you sure you want to %{act} %{vm}?") % { :act => pause_action, :vm => vm } },
:class => "btn btn-danger",
}
end
end
def pause_action_string(vm)
vm.ready? ? _('Pause') : _('Resume')
end
def password_placeholder(obj, attr = nil)
pass = obj.read_attribute(attr).present? || obj.read_attribute(:password_hash).present?
pass ? "********" : ''
end
def list_datacenters(compute)
return [] unless compute.uuid || controller.action_name == 'test_connection'
compute.datacenters
rescue Foreman::FingerprintException => e
compute.errors.add(:pubkey_hash, e.message)
[]
rescue => e
Foreman::Logging.exception("Failed listing datacenters", e)
[]
end
def list_providers
providers = ComputeResource.providers.map do |provider_name, provider_class|
[provider_class.constantize.provider_friendly_name, provider_name]
end
providers.sort_by { |provider| provider.first }
end
def unset_password?
action_name == "edit" || (action_name == "test_connection" && params[:cr_id].present?)
end
def test_connection_button_f(f, success, caption = nil)
caption ||= _("Test Connection")
btn_class = success ? 'btn-success' : 'btn-default'
spinner_class = success ? 'spinner-inverse' : nil
spinner_button_f(f, caption, "tfm.computeResource.testConnection(this)",
:id => 'test_connection_button',
:spinner_id => 'test_connection_indicator',
:class => btn_class,
:spinner_class => spinner_class,
:'data-url' => test_connection_compute_resources_path)
end
def load_button_f(f, success, failure_caption)
caption = success ? _("Test Connection") : failure_caption
test_connection_button_f(f, success, caption)
end
def load_datacenters_button_f(f, success)
load_button_f(f, success, _("Load Datacenters"))
end
def http_proxy_field(f)
select_f(f, :http_proxy_id, HttpProxy.all, :id, :name,
{:include_blank => true },
{ :label => _("HTTP Proxy") }
)
end
end