Module: ForemanTemplates::ProvisioningTemplateImport::ClassMethods

Defined in:
app/models/concerns/foreman_templates/provisioning_template_import.rb

Instance Method Summary collapse

Instance Method Details

#associate_metadata(data, template, metadata, oses, organizations, locations) ⇒ Object



52
53
54
55
56
57
58
59
# File 'app/models/concerns/foreman_templates/provisioning_template_import.rb', line 52

def (data, template, , oses, organizations, locations)
  if (['associate'] == 'new' && template.new_record?) || (['associate'] == 'always')
    data[:operatingsystem_ids] = oses.map(&:id)
    data[:location_ids]        = locations.map(&:id)
    data[:organization_ids]    = organizations.map(&:id)
  end
  data
end

#associations_changed?(data) ⇒ Boolean

Returns:

  • (Boolean)


85
86
87
# File 'app/models/concerns/foreman_templates/provisioning_template_import.rb', line 85

def associations_changed?(data)
  !(data[:operatingsystem_ids] || data[:location_ids] || data[:organization_ids]).nil?
end

#build_associations_result(c_or_u, id_string, name, oses, organizations, locations) ⇒ Object



61
62
63
64
65
66
67
# File 'app/models/concerns/foreman_templates/provisioning_template_import.rb', line 61

def build_associations_result(c_or_u, id_string, name, oses, organizations, locations)
  res  = "  #{c_or_u} Template #{id_string}:#{name}"
  res += "\n    Operatingsystem Associations:\n    - #{oses.map(&:fullname).join("\n    - ")}" unless oses.empty?
  res += "\n    Organizations Associations:\n    - #{organizations.map(&:name).join("\n    - ")}" unless organizations.empty?
  res += "\n    Location Associations:\n    - #{locations.map(&:name).join("\n    - ")}" unless locations.empty?
  res
end

#create_diff(data, template) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
# File 'app/models/concerns/foreman_templates/provisioning_template_import.rb', line 69

def create_diff(data, template)
  if template_content_changed?(template.template, data[:template])
    Diffy::Diff.new(
      template.template,
      data[:template],
      :include_diff_info => true
    ).to_s(:color)
  else
    nil
  end
end

#import!(name, text, metadata, force = false) ⇒ Object

Raises:



6
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/models/concerns/foreman_templates/provisioning_template_import.rb', line 6

def import!(name, text, , force = false)
  # Check for snippet type
  return import_snippet!(name, text, force) if ['snippet'] || ['kind'] == 'snippet'

  # Get template type
  kind = TemplateKind.find_by(name: ['kind'])
  raise NoKindError unless kind

  # Data
  template = ProvisioningTemplate.where(:name => name).first_or_initialize
  data = {
    :template         => text,
    :snippet          => false,
    :template_kind_id => kind.id
  }
  oses          = (, 'oses')
  locations     = (, 'locations')
  organizations = (, 'organizations')

  # Printout helpers
  c_or_u = template.new_record? ? 'Creating' : 'Updating'
  id_string = template.new_record? ? '' : "id #{template.id}"
  if template.locked? && !template.new_record? && !force
    return { :diff => nil,
             :status => false,
             :result => "Skipping Template #{id_string}:#{name} - template is locked" }
  end

   data, template, , oses, organizations, locations

  diff = nil
  status = nil
  if template_changed?(data, template)
    diff = create_diff(data, template)
    template.ignore_locking do
      status = template.update_attributes(data)
    end
    result = build_associations_result c_or_u, id_string, name, oses, organizations, locations
  else
    status  = true
    result  = "  No change to Template #{id_string}:#{name}"
  end

  { :diff => diff, :status => status, :result => result, :errors => template.errors }
end

#template_changed?(data, template) ⇒ Boolean

Returns:

  • (Boolean)


89
90
91
# File 'app/models/concerns/foreman_templates/provisioning_template_import.rb', line 89

def template_changed?(data, template)
  template_content_changed?(template.template, data[:template]) || associations_changed?(data)
end

#template_content_changed?(template_template, data_template) ⇒ Boolean

Returns:

  • (Boolean)


81
82
83
# File 'app/models/concerns/foreman_templates/provisioning_template_import.rb', line 81

def template_content_changed?(template_template, data_template)
  template_template != data_template
end