Class: ForemanTemplates::ParseResult

Inherits:
Object
  • Object
show all
Defined in:
app/services/foreman_templates/parse_result.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template_file) ⇒ ParseResult

Returns a new instance of ParseResult.



6
7
8
# File 'app/services/foreman_templates/parse_result.rb', line 6

def initialize(template_file)
  @template_file = template_file.split('/').last
end

Instance Attribute Details

#additional_errorsObject (readonly)

Returns the value of attribute additional_errors.



4
5
6
# File 'app/services/foreman_templates/parse_result.rb', line 4

def additional_errors
  @additional_errors
end

#additional_infoObject (readonly)

Returns the value of attribute additional_info.



4
5
6
# File 'app/services/foreman_templates/parse_result.rb', line 4

def additional_info
  @additional_info
end

#diffObject (readonly)

Returns the value of attribute diff.



4
5
6
# File 'app/services/foreman_templates/parse_result.rb', line 4

def diff
  @diff
end

#exceptionObject (readonly)

Returns the value of attribute exception.



4
5
6
# File 'app/services/foreman_templates/parse_result.rb', line 4

def exception
  @exception
end

#importedObject (readonly)

Returns the value of attribute imported.



4
5
6
# File 'app/services/foreman_templates/parse_result.rb', line 4

def imported
  @imported
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'app/services/foreman_templates/parse_result.rb', line 3

def name
  @name
end

#templateObject

Returns the value of attribute template.



3
4
5
# File 'app/services/foreman_templates/parse_result.rb', line 3

def template
  @template
end

#template_fileObject (readonly)

Returns the value of attribute template_file.



4
5
6
# File 'app/services/foreman_templates/parse_result.rb', line 4

def template_file
  @template_file
end

Instance Method Details

#add_exception(exception) ⇒ Object



61
62
63
64
65
# File 'app/services/foreman_templates/parse_result.rb', line 61

def add_exception(exception)
  @imported = false
  @exception = exception
  self
end

#calculate_diff(old, new) ⇒ Object



93
94
95
96
97
98
99
# File 'app/services/foreman_templates/parse_result.rb', line 93

def calculate_diff(old, new)
  if old.present? && new.present? && old != new
    Diffy::Diff.new(old, new, :include_diff_info => true).to_s(:color)
  else
    nil
  end
end

#changed?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'app/services/foreman_templates/parse_result.rb', line 89

def changed?
  @diff.present?
end

#check_for_errorsObject



56
57
58
59
# File 'app/services/foreman_templates/parse_result.rb', line 56

def check_for_errors
  @imported = @template.errors.blank?
  self
end

#corrupted_metadataObject



33
34
35
# File 'app/services/foreman_templates/parse_result.rb', line 33

def 
  generic_error "Failed to parse metadata"
end

#determine_result_diffObject



81
82
83
84
85
86
87
# File 'app/services/foreman_templates/parse_result.rb', line 81

def determine_result_diff
  return if @template.nil? || !@template.template_changed?

  template_was = @template.template_was
  template_is = @template.template
  @diff = calculate_diff(template_was, template_is)
end

#errorsObject



29
30
31
# File 'app/services/foreman_templates/parse_result.rb', line 29

def errors
  @template&.errors
end

#generic_error(additional_msg) ⇒ Object



49
50
51
52
53
54
# File 'app/services/foreman_templates/parse_result.rb', line 49

def generic_error(additional_msg)
  @imported = false
  @additional_errors = additional_msg
  Logging.logger('app').debug "Error in '#{@template_file}': #{additional_msg}"
  self
end

#generic_info(additional_msg) ⇒ Object



67
68
69
70
71
72
# File 'app/services/foreman_templates/parse_result.rb', line 67

def generic_info(additional_msg)
  @imported = false
  @additional_info = additional_msg
  Logging.logger('app').debug "Not importing #{@template_file}: #{additional_msg}"
  self
end

#matching_filterObject



37
38
39
# File 'app/services/foreman_templates/parse_result.rb', line 37

def matching_filter
  generic_info "Skipping, 'name' filtered out based on 'filter' and 'negate' settings"
end

#missing_modelObject



45
46
47
# File 'app/services/foreman_templates/parse_result.rb', line 45

def missing_model
  generic_error "No 'model' found in metadata"
end

#name_error(exception, template_type) ⇒ Object



74
75
76
77
78
79
# File 'app/services/foreman_templates/parse_result.rb', line 74

def name_error(exception, template_type)
  @imported = false
  @exception = exception
  @additional_errors = "Template type #{template_type} was not found, are you missing a plugin?"
  self
end

#no_metadata_nameObject



41
42
43
# File 'app/services/foreman_templates/parse_result.rb', line 41

def 
  generic_error "No 'name' found in metadata"
end

#to_h(verbose = false) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/services/foreman_templates/parse_result.rb', line 10

def to_h(verbose = false)
  res = {
    :name => @name,
    :id => @template.present? ? @template.id : nil,
    :changed => changed?,
    :imported => @imported,
    :additional_errors => @additional_errors,
    :additional_info => @additional_info,
    :exception => @exception&.message,
    :validation_errors => errors.to_h,
    :file => @template_file,
    :type => @template.present? ? @template.class.name.underscore : nil
  }

  # false comes as a string when using rake tasks
  res[:diff] = @diff if verbose && verbose != 'false'
  res
end