Class: ComputeResourcesController

Inherits:
ApplicationController show all
Includes:
Foreman::Controller::AutoCompleteSearch, Foreman::Controller::Parameters::ComputeResource
Defined in:
app/controllers/compute_resources_controller.rb

Constant Summary collapse

AJAX_REQUESTS =
[:template_selected, :instance_type_selected, :cluster_selected, :resource_pools]

Instance Attribute Summary

Attributes inherited from ApplicationController

#original_search_parameter

Instance Method Summary collapse

Methods included from Foreman::Controller::AutoCompleteSearch

#auto_complete_search, #invalid_search_query, #reset_redirect_to_url, #store_redirect_to_url

Methods inherited from ApplicationController

#api_request?, #current_user, parameter_filter_context, #resource_path, #welcome

Methods included from ApplicationShared

#current_permission, #find_default_taxonomy, #find_session_taxonomy, #scope_by_resource_id, #set_current_taxonomy, #set_current_user, #set_taxonomy, #store_default_taxonomy, #store_taxonomy

Methods included from FindCommon

#controller_permission, #find_resource, #resource_class, #resource_class_for, #resource_finder, #resource_name, #resource_scope, #scope_for

Methods included from Foreman::ThreadSession::Cleaner

#clear_thread

Methods included from Foreman::Controller::Timezone

#set_timezone

Methods included from Foreman::Controller::TopbarSweeper

#set_topbar_sweeper_controller

Methods included from Foreman::Controller::Session

#backup_session_content, #expire_session, #ignore_api_request?, #session_expiry, #set_activity_time, #update_activity_time

Methods included from Foreman::Controller::Authentication

#authenticate, #authorized, #available_sso, #check_user_enabled, #is_admin?, #path_to_authenticate, #require_login

Methods included from Foreman::Controller::MigrationChecker

needs_migration?

Instance Method Details

#associateObject


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'app/controllers/compute_resources_controller.rb', line 49

def associate
  if @compute_resource.supports_host_association?
    associator = ComputeResourceHostAssociator.new(@compute_resource)
    associator.associate_hosts
    messages = []
    if associator.hosts.empty?
      messages << _('No VMs matched any host.')
    else
      messages << n_('%s VM was associated to a host.', '%s VMs were each associated to hosts.', associator.hosts.count) % associator.hosts.count
    end
    if associator.fail_count > 0
      messages << n_('%s VM failed while processing: check logs for more details.',
        '%s VMs failed while processing: check logs for more details.',
        associator.fail_count) % associator.fail_count
      process_error(:error_msg => messages.join(' '))
    else
      process_success(:success_msg => messages.join(' '))
    end
  else
    process_error(:error_msg => 'Associating VMs is not supported for this compute resource.')
  end
end

#cluster_selectedObject


152
153
154
155
156
157
# File 'app/controllers/compute_resources_controller.rb', line 152

def cluster_selected
  networks = @compute_resource.networks(:cluster_id => params[:cluster_id])
  respond_to do |format|
    format.json { render :json => networks }
  end
end

#createObject


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/controllers/compute_resources_controller.rb', line 26

def create
  if params[:compute_resource].present? && compute_resource_params[:provider].present?
    @compute_resource = ComputeResource.new_provider compute_resource_params
    if @compute_resource.save
      process_success :success_redirect => @compute_resource
    else
      process_error
    end
  else
    @compute_resource = ComputeResource.new compute_resource_params
    @compute_resource.valid?
    process_error
  end
rescue Fog::Errors::Error, Excon::Error => e
  Foreman::Logging.exception("Error while creating a resource", e)
  process_error(
    error_msg: _('Error while trying to create resource: %s') % e.message
  )
end

#destroyObject


85
86
87
88
89
90
91
# File 'app/controllers/compute_resources_controller.rb', line 85

def destroy
  if @compute_resource.destroy
    process_success
  else
    process_error
  end
end

#editObject


46
47
# File 'app/controllers/compute_resources_controller.rb', line 46

def edit
end

#indexObject


15
16
17
# File 'app/controllers/compute_resources_controller.rb', line 15

def index
  @compute_resources = resource_base_search_and_page.live_descendants
end

#instance_type_selectedObject


145
146
147
148
149
150
# File 'app/controllers/compute_resources_controller.rb', line 145

def instance_type_selected
  compute = @compute_resource.instance_type(params[:instance_type_id])
  respond_to do |format|
    format.json { render :json => compute }
  end
end

#newObject


19
20
21
# File 'app/controllers/compute_resources_controller.rb', line 19

def new
  @compute_resource = ComputeResource.new
end

#pingObject


120
121
122
123
124
# File 'app/controllers/compute_resources_controller.rb', line 120

def ping
  respond_to do |format|
    format.json { render :json => errors_hash(@compute_resource.ping) }
  end
end

#provider_selectedObject

ajax methods


115
116
117
118
# File 'app/controllers/compute_resources_controller.rb', line 115

def provider_selected
  @compute_resource = ComputeResource.new_provider :provider => params[:provider]
  render :partial => "compute_resources/form", :locals => { :compute_resource => @compute_resource }
end

#refresh_cacheObject


93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'app/controllers/compute_resources_controller.rb', line 93

def refresh_cache
  if @compute_resource.respond_to?(:refresh_cache)
    if @compute_resource.refresh_cache
      process_success(
        :success_msg => _('Successfully refreshed the cache.'),
        :success_redirect => @compute_resource
      )
    else
      process_error(
        :error_msg => _('Failed to refresh the cache.'),
        :redirect => @compute_resource
      )
    end
  else
    process_error(
      :error_msg => _("Cache refreshing is not supported for %s") % @compute_resource.provider_friendly_name,
      :redirect => @compute_resource
    )
  end
end

#resource_poolsObject


159
160
161
162
163
164
165
# File 'app/controllers/compute_resources_controller.rb', line 159

def resource_pools
  return head(:method_not_allowed) unless @compute_resource.is_a? Foreman::Model::Vmware
  pools = @compute_resource.available_resource_pools(:cluster_id => params[:cluster_id])
  respond_to do |format|
    format.json { render :json => pools }
  end
end

#showObject


23
24
# File 'app/controllers/compute_resources_controller.rb', line 23

def show
end

#template_selectedObject


138
139
140
141
142
143
# File 'app/controllers/compute_resources_controller.rb', line 138

def template_selected
  compute = @compute_resource.template(params[:template_id])
  respond_to do |format|
    format.json { render :json => compute }
  end
end

#test_connectionObject


126
127
128
129
130
131
132
133
134
135
136
# File 'app/controllers/compute_resources_controller.rb', line 126

def test_connection
  # cr_id is posted from AJAX function. cr_id is nil if new
  if params[:cr_id].present?
    @compute_resource = ComputeResource.authorized(:edit_compute_resources).find(params[:cr_id])
    @compute_resource.attributes = compute_resource_params.reject { |k, v| k == :password && v.blank? }
  else
    @compute_resource = ComputeResource.new_provider(compute_resource_params)
  end
  @compute_resource.test_connection :force => true
  render :partial => "compute_resources/form", :locals => { :compute_resource => @compute_resource }
end

#updateObject


72
73
74
75
76
77
78
79
80
81
82
83
# File 'app/controllers/compute_resources_controller.rb', line 72

def update
  if @compute_resource.update(compute_resource_params)
    process_success :success_redirect => compute_resources_path
  else
    process_error
  end
rescue Fog::Errors::Error, Excon::Error => e
  Foreman::Logging.exception("Error while updating resource", e)
  process_error(
    error_msg: _('Error while trying to update resource: %s') % e.message
  )
end