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.



73
74
75
# File 'app/models/job_invocation_composer.rb', line 73

def initialize(api_params)
  @api_params = api_params
end

Instance Attribute Details

#api_paramsObject (readonly)

Returns the value of attribute api_params.



71
72
73
# File 'app/models/job_invocation_composer.rb', line 71

def api_params
  @api_params
end

Instance Method Details

#concurrency_control_paramsObject



114
115
116
117
118
119
# File 'app/models/job_invocation_composer.rb', line 114

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

#paramsObject



77
78
79
80
81
82
83
84
# File 'app/models/job_invocation_composer.rb', line 77

def params
  { :job_category => template.job_category,
    :targeting => targeting_params,
    :triggering => triggering_params,
    :description_format => api_params[:description_format],
    :concurrency_control => concurrency_control_params,
    :template_invocations => template_invocations_params }.with_indifferent_access
end

#targeting_paramsObject

Raises:

  • (::Foreman::Exception)


86
87
88
89
# File 'app/models/job_invocation_composer.rb', line 86

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).merge(:user_id => User.current.id)
end

#templateObject



134
135
136
# File 'app/models/job_invocation_composer.rb', line 134

def template
  @template ||= JobTemplate.authorized(:view_job_templates).find(api_params[:job_template_id])
end

#template_invocations_paramsObject



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

def template_invocations_params
  template_invocation_params = { :template_id => template.id, :effective_user => api_params[:effective_user] }
  template_invocation_params[:input_values] = api_params.fetch(:inputs, {}).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

Raises:

  • (::Foreman::Exception)


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

def triggering_params
  raise ::Foreman::Exception, _('Cannot specify both recurrence and scheduling') if api_params[:recurrence].present? && api_params[:scheduling].present?

  if api_params[:recurrence].present?
    {
      :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]
    }
  elsif 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