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



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

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

#destroyObject

GET = foreman_xen/snapshots/delete



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

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.get_snapshots
      snapshots.each do | snapshot |
        if snapshot.reference == ref
          name = snapshot.name
          snapshot.destroy
          notice ("Succesfully deleted snapshot #{snapshot.name}")
          break
        end
      end
    else
      process_error({:error_msg =>  ("Error retrieving host information for host id: #{id}")})
      return
    end
  else
    process_error({:error_msg =>  ("Error retrieving compute resource information for host id: #{id}")})
    return
  end
  process_success({:success_msg => ("Succesfully deleted snapshot: #{name}"), :success_redirect => "/foreman_xen/snapshots/#{id}"})
  return
end

#newObject

GET = foreman_xen/snapshots/:id/new



89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'app/controllers/foreman_xen/snapshots_controller.rb', line 89

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}"})
      return
    end
  else
    process_error({:error_msg => "Error retrieving host information for host id: #{id}"})
    return
  end
end

#revertObject

GET = foreman_xen/snapshots/revert



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/controllers/foreman_xen/snapshots_controller.rb', line 34

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 => "Succesfully reverted and powered on #{@host.name}", :success_redirect => "/foreman_xen/snapshots/#{id}"})
      return
    else
      process_error({:error_msg =>  "Error retrieving host information for #{@host.name}" })
      return
    end
  else
    process_error({:error_msg =>  "Error retrieving compute resource information for #{@host.name}" })
    return
  end
  process_success({:success_msg => ("Succesfully reverted #{@host.name}"), :success_redirect => "/foreman_xen/snapshots/#{id}"})
  return
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
26
27
28
29
30
31
# File 'app/controllers/foreman_xen/snapshots_controller.rb', line 7

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

#snapshots_urlObject



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'app/controllers/foreman_xen/snapshots_controller.rb', line 135

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