Class: JobInvocationComposer::ApiParams

Inherits:
Object
  • Object
show all
Defined in:
app/models/job_invocation_composer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_params) ⇒ ApiParams

Returns a new instance of ApiParams.



106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'app/models/job_invocation_composer.rb', line 106

def initialize(api_params)
  @api_params = api_params

  if api_params[:feature]
    # set a default targeting type for backward compatibility
    # when `for_feature` was used by the API it automatically set a default
    api_params[:targeting_type] = Targeting::STATIC_TYPE
  end

  if api_params[:search_query].blank? && api_params[:host_ids].present?
    translator = HostIdsTranslator.new(api_params[:host_ids])
    api_params[:search_query] = translator.scoped_search
  end
end

Instance Attribute Details

#api_paramsObject (readonly)

Returns the value of attribute api_params.



104
105
106
# File 'app/models/job_invocation_composer.rb', line 104

def api_params
  @api_params
end

Instance Method Details

#concurrency_control_paramsObject



175
176
177
178
179
180
# File 'app/models/job_invocation_composer.rb', line 175

def concurrency_control_params
  {
    :level => api_params.fetch(:concurrency_control, {})[:concurrency_level],
    :time_span => api_params.fetch(:concurrency_control, {})[:time_span],
  }
end

#featureObject



203
204
205
206
207
# File 'app/models/job_invocation_composer.rb', line 203

def feature
  @feature ||= RemoteExecutionFeature.feature(api_params[:feature]) if api_params[:feature]
rescue => e
  raise(FeatureNotFound, e.message)
end

#filter_provider_inputs(api_params) ⇒ Object



196
197
198
199
200
201
# File 'app/models/job_invocation_composer.rb', line 196

def filter_provider_inputs(api_params)
  return [] if template.provider.provider_input_namespace.empty?
  inputs = api_params[template.provider.provider_input_namespace].to_h
  provider_input_names = template.provider.provider_inputs.map(&:name)
  inputs.select { |key, value| provider_input_names.include? key }.map { |key, value| { :name => key, :value => value } }
end

#job_template_idObject



209
210
211
# File 'app/models/job_invocation_composer.rb', line 209

def job_template_id
  feature&.job_template_id || api_params[:job_template_id]
end

#paramsObject



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'app/models/job_invocation_composer.rb', line 121

def params
  { :job_category => template.job_category,
    :targeting => targeting_params,
    :triggering => triggering_params,
    :description_format => api_params[:description_format],
    :ssh_user => api_params[:ssh_user],
    :password => api_params[:password],
    :remote_execution_feature_id => remote_execution_feature_id,
    :effective_user_password => api_params[:effective_user_password],
    :key_passphrase => api_params[:key_passphrase],
    :concurrency_control => concurrency_control_params,
    :execution_timeout_interval => api_params[:execution_timeout_interval] || template.execution_timeout_interval,
    :time_to_pickup => api_params[:time_to_pickup],
    :template_invocations => template_invocations_params }.with_indifferent_access
end

#recurring_mode_paramsObject



164
165
166
167
168
169
170
171
172
173
# File 'app/models/job_invocation_composer.rb', line 164

def recurring_mode_params
  {
    :mode => :recurring,
    :cronline => api_params[:recurrence][:cron_line],
    :end_time => format_datetime(api_params[:recurrence][:end_time]),
    :input_type => :cronline,
    :max_iteration => api_params[:recurrence][:max_iteration],
    :purpose => api_params[:recurrence][:purpose],
  }
end

#remote_execution_feature_idObject



137
138
139
# File 'app/models/job_invocation_composer.rb', line 137

def remote_execution_feature_id
  feature&.id || api_params[:remote_execution_feature_id]
end

#targeting_paramsObject

Raises:

  • (::Foreman::Exception)


141
142
143
144
145
# File 'app/models/job_invocation_composer.rb', line 141

def targeting_params
  raise ::Foreman::Exception, _('Cannot specify both bookmark_id and search_query') if api_params[:bookmark_id] && api_params[:search_query]

  api_params.slice(:targeting_type, :bookmark_id, :search_query, :randomized_ordering).merge(:user_id => User.current.id)
end

#templateObject



213
214
215
216
217
# File 'app/models/job_invocation_composer.rb', line 213

def template
  @template ||= JobTemplate.authorized(:view_job_templates).find(job_template_id)
rescue ActiveRecord::RecordNotFound
  raise(JobTemplateNotFound, _("Template with id '%{id}' was not found") % { id: job_template_id })
end

#template_invocations_paramsObject



182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'app/models/job_invocation_composer.rb', line 182

def template_invocations_params
  template_invocation_params = { :template_id => template.id, :effective_user => api_params[:effective_user] }
  template_invocation_params[:provider_input_values] = filter_provider_inputs api_params
  template_invocation_params[:input_values] = api_params.fetch(:inputs, {}).to_h.map do |name, value|
    input = template.template_inputs_with_foreign.find { |i| i.name == name }
    unless input
      raise ::Foreman::Exception, _('Unknown input %{input_name} for template %{template_name}') %
        { :input_name => name, :template_name => template.name }
    end
    { :template_input_id => input.id, :value => value }
  end
  [template_invocation_params]
end

#triggering_paramsObject



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'app/models/job_invocation_composer.rb', line 147

def triggering_params
  if api_params[:recurrence].present? && api_params[:scheduling].present?
    recurring_mode_params.merge :start_at_raw => format_datetime(api_params[:scheduling][:start_at])
  elsif api_params[:recurrence].present? && api_params[:scheduling].empty?
    recurring_mode_params
  elsif api_params[:recurrence].empty? && api_params[:scheduling].present?
    {
      :mode => :future,
      :start_at_raw => format_datetime(api_params[:scheduling][:start_at]),
      :start_before_raw => format_datetime(api_params[:scheduling][:start_before]),
      :end_time_limited => api_params[:scheduling][:start_before] ? true : false,
    }
  else
    {}
  end
end