-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
proxmox_compute_resources_vms_helper.rb
113 lines (95 loc) · 3.79 KB
/
proxmox_compute_resources_vms_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
# frozen_string_literal: true
# Copyright 2021 Tristan Robert
# This file is part of ForemanFogProxmox.
# ForemanFogProxmox is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# ForemanFogProxmox is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with ForemanFogProxmox. If not, see <http://www.gnu.org/licenses/>.
module ProxmoxComputeResourcesVmsHelper
def proxmox_vm_id(compute_resource, vm)
id = vm.identity
id = vm.unique_cluster_identity(compute_resource) if compute_resource.instance_of?(ForemanFogProxmox::Proxmox)
id
end
def vm_host_action(vm)
host = Host.for_vm_uuid(@compute_resource, vm).first
return unless host
display_link_if_authorized(_("Host"), hash_for_host_path(:id => host), :class => 'btn btn-default')
end
def vm_power_action(vm, authorizer = nil)
opts = hash_for_power_compute_resource_vm_path(:compute_resource_id => @compute_resource, :id => proxmox_vm_id(@compute_resource, vm)).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 vm_associate_action(vm)
vm_associate_link(vm, link_class: "btn btn-default")
end
def vm_associate_link(vm, link_class: "")
return unless @compute_resource.supports_host_association?
display_link_if_authorized(
_('Associate VM'),
hash_for_associate_compute_resource_vm_path(
:compute_resource_id => @compute_resource,
:id => proxmox_vm_id(@compute_resource, vm)
).merge(
:auth_object => @compute_resource,
:permission => 'edit_compute_resources'
),
:title => _('Associate VM to a Foreman host'),
:method => :put,
:class => link_class
)
end
def vm_import_action(vm, html_options = {})
@_linked_hosts_cache ||= Host.where(:compute_resource_id => @compute_resource.id).pluck(:uuid)
return if @_linked_hosts_cache.include?(proxmox_vm_id(@compute_resource, vm).to_s)
import_managed_link = display_link_if_authorized(
_('Import as managed Host'),
hash_for_import_compute_resource_vm_path(
:compute_resource_id => @compute_resource,
:id => proxmox_vm_id(@compute_resource, vm),
:type => 'managed'
),
html_options
)
import_unmanaged_link = display_link_if_authorized(
_('Import as unmanaged Host'),
hash_for_import_compute_resource_vm_path(
:compute_resource_id => @compute_resource,
:id => proxmox_vm_id(@compute_resource, vm),
:type => 'unmanaged'
),
html_options
)
import_managed_link + import_unmanaged_link
end
def vm_console_action(vm)
return unless vm.ready?
link_to_if_authorized(
_('Console'),
hash_for_console_compute_resource_vm_path.merge(
:auth_object => @compute_resource,
:id => proxmox_vm_id(@compute_resource, vm)
),
{
:id => 'console-button',
:class => 'btn btn-info',
}
)
end
def vm_delete_action(vm, authorizer = nil)
display_delete_if_authorized(
hash_for_compute_resource_vm_path(:compute_resource_id => @compute_resource, :id => proxmox_vm_id(@compute_resource, vm)).merge(
:auth_object => @compute_resource, :authorizer => authorizer
), :class => 'btn btn-danger'
)
end
end