Class: ForemanXen::SnapshotsController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/foreman_xen/snapshots_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject

POST = foreman_xen/snapshots/:id/create



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
# File 'app/controllers/foreman_xen/snapshots_controller.rb', line 97

def create
  id   = params[:id]
  name = params[:name]
  process_error(:error_msg => 'You must supply a name.') if name.nil? || name == ''
  @host = get_host_by_id(id)
  if !@host.nil?
    @compute_resource = get_compute_resource_by_host_id(id)
  else
    process_error(:error_msg => "Error retrieving host information for host id #{id}")
  end
  if !@compute_resource.nil?
    vm = @compute_resource.find_vm_by_uuid(@host.uuid)
    if !vm.nil?
      vm.snapshot(name)
      process_success(
        :success_msg      => "Successfully created snapshot #{name} for #{@host.name}",
        :success_redirect => "/foreman_xen/snapshots/#{id}"
      )
    else
      process_error(:error_msg => "Error retrieving compute resource information for #{@host.name}")
    end
  else
    process_error(:error_msg => "Error retrieving compute provider information for #{@host.name}")
  end
end

#destroyObject

GET = foreman_xen/snapshots/delete



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
# File 'app/controllers/foreman_xen/snapshots_controller.rb', line 51

def destroy
  ref               = params[:ref]
  id                = params[:id]
  @host             = get_host_by_id(id)
  @compute_resource = get_compute_resource_by_host_id(id)
  name              = nil
  if @compute_resource
    if @host
      snapshots = @compute_resource.find_snapshots
      snapshots.each do |snapshot|
        next unless snapshot.reference == ref

        name = snapshot.name
        snapshot.destroy
        notice "Successfully deleted snapshot #{snapshot.name}"
        break
      end
    else
      process_error(:error_msg => "Error retrieving host information for host id: #{id}")
    end
  else
    process_error(:error_msg => "Error retrieving compute resource information for host id: #{id}")
  end
  process_success(
    :success_msg      => "Successfully deleted snapshot: #{name}",
    :success_redirect => "/foreman_xen/snapshots/#{id}"
  )
end

#newObject

GET = foreman_xen/snapshots/:id/new



81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'app/controllers/foreman_xen/snapshots_controller.rb', line 81

def new
  id    = params[:id]
  @host = get_host_by_id(id)
  if !@host.nil?
    @compute_resource = get_compute_resource_by_host_id(id)
    if @compute_resource.nil?
      process_error(
        :error_msg => "Error retrieving compute information for compute resource id: #{@host.compute_resource_id}"
      )
    end
  else
    process_error(:error_msg => "Error retrieving host information for host id: #{id}")
  end
end

#revertObject

GET = foreman_xen/snapshots/revert



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/controllers/foreman_xen/snapshots_controller.rb', line 28

def revert
  id                = params[:id]
  ref               = params[:ref]
  @host             = get_host_by_id(id)
  @compute_resource = get_compute_resource_by_host_id(id)
  if @compute_resource
    if @host
      vm = @compute_resource.find_vm_by_uuid(@host.uuid)
      vm.revert(ref)
      vm.start
      process_success(
        :success_msg      => "Successfully reverted and powered on #{@host.name}",
        :success_redirect => "/foreman_xen/snapshots/#{id}"
      )
    else
      process_error(:error_msg => "Error retrieving host information for #{@host.name}")
    end
  else
    process_error(:error_msg => "Error retrieving compute resource information for #{@host.name}")
  end
end

#showObject

GET - foreman_xen/snapshots/:host_id



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/controllers/foreman_xen/snapshots_controller.rb', line 7

def show
  id    = params[:id]
  @host = get_host_by_id(id) if !id.nil? && id != ''
  if !@host.nil? && @host.compute_resource_id
    @compute_resource = get_compute_resource_for_host(@host)
    unless @compute_resource.nil?
      vm = @compute_resource.find_vm_by_uuid(@host.uuid)
      if !vm.nil?
        @snapshots = @compute_resource.find_snapshots_for_vm(vm)
      else
        process_error(:error_msg => "Error retrieving compute resource #{@host.compute_resource_id} from provider.")
      end
    end
  elsif @host.nil?
    process_error(:error_msg => "No host found with ID: #{id}.")
  else
    process_error(:error_msg => "No compute resource found for host with ID: #{id}.")
  end
end

#snapshots_urlObject



123
124
125
126
127
128
129
130
131
132
133
134
# File 'app/controllers/foreman_xen/snapshots_controller.rb', line 123

def snapshots_url
  case params[:action]
  when 'show'
    '/'
  when 'new'
    id = params[:id]
    id.nil? ? '/' : "/foreman_xen/snapshots/#{id}"
  when 'create'
    id = params[:id]
    id.nil? ? '/' : "/foreman_xen/snapshots/#{id}/new"
  end
end