Class: PuppetX::Puppetlabs::Migration::OverviewModel::Factory

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet_x/puppetlabs/migration/overview_model/factory.rb

Overview

Builds Overview instances by merging instances of CatalogDeltaModel::CatalogDelta

Instance Method Summary collapse

Constructor Details

#initialize(origin = nil) ⇒ Factory

Creates and optionally initializes a new instance.

Parameters:

  • origin (Factory) (defaults to: nil)


11
12
13
14
15
16
17
18
19
# File 'lib/puppet_x/puppetlabs/migration/overview_model/factory.rb', line 11

def initialize(origin = nil)
  @maps_per_class = {}
  if origin.nil?
    @entities = {}
  else
    @entities = origin.entities.clone
    @entities.each { |entity| add_entity(entity) }
  end
end

Instance Method Details

#[](id) ⇒ Entity

Returns the entity instance that corresponds to the given id

Parameters:

  • id (Integer)

    The id to lookup

Returns:

  • (Entity)

    entity with the given id or nil if no such entity exists



27
28
29
# File 'lib/puppet_x/puppetlabs/migration/overview_model/factory.rb', line 27

def [](id)
  @entities[id]
end

#add_log_entries(compilation_id, log) ⇒ Object

Adds log entries from the given log to the Compilation identified by _compilation_id

Parameters:

  • compilation_id (Integer)

    id of the Compliation

  • log (Array<Hash<String,Object>>)

    log for the given compilation



111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/puppet_x/puppetlabs/migration/overview_model/factory.rb', line 111

def add_log_entries(compilation_id, log)
  log.each do |entry|
    level_id = single_key_entity(LogLevel, entry['level'])
    issue_id = complex_key_entity(LogIssue, entry['issue_code'], level_id) { |li| li.level_id == level_id }
    message = entry['message']
    message_id = complex_key_entity(LogMessage, issue_id, message) { |lm| lm.message == message }
    location_id = location(entry['file'], entry['line'], entry['pos'])
    complex_key_entity(LogEntry, compilation_id, entry['time'], message_id, location_id)  do |le|
      le.message_id == message_id && le.location_id == location_id
    end
  end
end

#attribute(name) ⇒ Integer

Returns the id of the Attribute entity that corresponds to the given name. A new entity will be created if needed.

Parameters:

  • name (String)

    name of attribute

Returns:

  • (Integer)

    id of found or created entity



153
154
155
# File 'lib/puppet_x/puppetlabs/migration/overview_model/factory.rb', line 153

def attribute(name)
  single_key_entity(Attribute, name)
end

#attribute_conflict_issue(resource_conflict_id, attribute_conflict) ⇒ Integer

Returns the id of the AttributeConflict entity that corresponds to the given arguments. A new entity will be created if needed.

Parameters:

Returns:

  • (Integer)

    id of found or created entity



289
290
291
292
293
294
295
296
# File 'lib/puppet_x/puppetlabs/migration/overview_model/factory.rb', line 289

def attribute_conflict_issue(resource_conflict_id, attribute_conflict)
  attribute_id = attribute(attribute_conflict.name)
  baseline_value = attribute_conflict.baseline_value
  preview_value = attribute_conflict.preview_value
  complex_key_entity(AttributeConflict, resource_conflict_id, attribute_id, baseline_value, preview_value, attribute_conflict.compliant?) do |ac|
    ac.attribute_id == attribute_id && ac.value == baseline_value && ac.preview_value == preview_value
  end
end

#attribute_issue(resource_conflict_id, attribute, issue_class) ⇒ Integer

Returns the id of the AttributeIssue entity that corresponds to the given arguments. A new entity will be created if needed.

Parameters:

  • resource_conflict_id (Integer)

    id of the resource conflict that contains this attribute

  • attribute (CatalogDeltaModel::Attribute)
  • issue_class (Class)

Returns:

  • (Integer)

    id of found or created entity



275
276
277
278
279
# File 'lib/puppet_x/puppetlabs/migration/overview_model/factory.rb', line 275

def attribute_issue(resource_conflict_id, attribute, issue_class)
  attribute_id = attribute(attribute.name)
  value = attribute.value
  complex_key_entity(issue_class, resource_conflict_id, attribute_id, value) { |ac| ac.attribute_id == attribute_id && ac.value == value }
end

#cloneFactory

Creates a factory with a snapshot of this factory’s current entity content

Returns:

  • (Factory)

    the created factory



43
44
45
# File 'lib/puppet_x/puppetlabs/migration/overview_model/factory.rb', line 43

def clone
  Factory.new(self)
end

#create_overviewOverview

Creates an overview of the factory’s current entity content

Returns:



34
35
36
# File 'lib/puppet_x/puppetlabs/migration/overview_model/factory.rb', line 34

def create_overview
  Overview.new(@entities.clone)
end

#edge_issue(edge, issue_class) ⇒ Integer

Returns id of found or created entity.

Parameters:

Returns:

  • (Integer)

    id of found or created entity



333
334
335
336
337
# File 'lib/puppet_x/puppetlabs/migration/overview_model/factory.rb', line 333

def edge_issue(edge, issue_class)
  source_id = resource_from_string(edge.source)
  target_id = resource_from_string(edge.target)
  complex_key_entity(issue_class, source_id, target_id) { |i| i.target_id == target_id }
end

#environment(name) ⇒ Integer

Returns the id of the Environment entity that corresponds to the given name. A new entity will be created if needed.

Parameters:

  • name (String)

Returns:

  • (Integer)

    id of found or created entity



131
132
133
# File 'lib/puppet_x/puppetlabs/migration/overview_model/factory.rb', line 131

def environment(name)
  single_key_entity(Environment, name)
end

#file(path) ⇒ Integer

Returns the id of the SourceFile entity that corresponds to the given path. A new entity will be created if needed.

Parameters:

  • path (String)

    path of the file

Returns:

  • (Integer)

    id of found or created entity



164
165
166
# File 'lib/puppet_x/puppetlabs/migration/overview_model/factory.rb', line 164

def file(path)
  single_key_entity(SourceFile, path)
end

#location(file, line, pos) ⇒ Integer

Returns the id of the Location entity that corresponds to the given parameters. A new entity will be created if needed.

Parameters:

  • file (String)

    name of file

  • line (Integer)

    line in file

  • pos (Integer)

    position on line

Returns:

  • (Integer)

    id of found or created entity



234
235
236
# File 'lib/puppet_x/puppetlabs/migration/overview_model/factory.rb', line 234

def location(file, line, pos)
  complex_key_entity(Location, file(file), line, pos) { |l| l.line == line && l.pos == pos }
end

#location_from_delta(loc) ⇒ Integer

Returns the id of the Location entity that corresponds to the given loc. A new entity will be created if needed.

Parameters:

Returns:

  • (Integer)

    id of found or created entity



221
222
223
# File 'lib/puppet_x/puppetlabs/migration/overview_model/factory.rb', line 221

def location_from_delta(loc)
  loc.nil? ? nil : location(loc.file, loc.line, nil)
end

#merge(catalog_delta, baseline_log = nil, preview_log = nil) ⇒ Factory

Adds all issues from the given catalog_delta to the overview

Parameters:

  • catalog_delta (CatalogDeltaModel::CatalogDelta)

    the delta to add

  • baseline_log (Array<Hash<String,Object>>) (defaults to: nil)

    log from the baseline compilation

  • preview_log (Array<Hash<String,Object>>) (defaults to: nil)

    log from the preview compilation

Returns:



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/puppet_x/puppetlabs/migration/overview_model/factory.rb', line 55

def merge(catalog_delta, baseline_log = nil, preview_log = nil)
  node_id = node(catalog_delta, baseline_log, preview_log)
  catalog_delta.added_resources.each do |ar|
    node_issue(node_id, resource_issue(ar, ResourceAdded))
  end

  catalog_delta.missing_resources.each do |mr|
    node_issue(node_id, resource_issue(mr, ResourceMissing))
  end

  catalog_delta.conflicting_resources.each do |cr|
    resource_conflict_id = resource_conflict_issue(cr)
    node_issue(node_id, resource_conflict_id)
    cr.added_attributes.each do |aa|
      attribute_issue(resource_conflict_id, aa, AttributeAdded)
    end
    cr.missing_attributes.each do |ma|
      attribute_issue(resource_conflict_id, ma, AttributeMissing)
    end
    cr.conflicting_attributes.each do |ca|
      attribute_conflict_issue(resource_conflict_id, ca)
    end
  end

  catalog_delta.added_edges.each do |ae|
    node_issue(node_id, edge_issue(ae, EdgeAdded))
  end

  catalog_delta.missing_edges.each do |me|
    node_issue(node_id, edge_issue(me, EdgeMissing))
  end
  self
end

#merge_failure(node_name, env, timestamp, exit_code, log = nil) ⇒ Factory

Adds a failing node described by the given arguments to to the overview

Parameters:

  • node_name (String)

    name of node

  • env (String)

    name of environment where compilation failed

  • timestamp (String)

    timestamp of the failure (produced with DateTime#iso8601(9))

  • exit_code (Integer)

    the exit code

  • log (Array<Hash<String,Object>>) (defaults to: nil)

    log from the failing compilation

Returns:



99
100
101
102
103
104
105
# File 'lib/puppet_x/puppetlabs/migration/overview_model/factory.rb', line 99

def merge_failure(node_name, env, timestamp, exit_code, log = nil)
  node_id = complex_key_entity(Node, node_name, timestamp, :error, exit_code) { |node| node.timestamp == timestamp }
  is_baseline = exit_code == BASELINE_FAILED
  compilation_id = complex_key_entity(Compilation, node_id, environment(env), is_baseline)  { |comp| comp.baseline? == is_baseline }
  add_log_entries(compilation_id, log) unless log.nil?
  self
end

#node(catalog_delta, baseline_log, preview_log) ⇒ Integer

Returns the id of the Node entity that corresponds to the given catalog_delta. A new entity will be created if needed.

Parameters:

  • catalog_delta (CatalogDeltaModel::CatalogDelta)

    the delta to add

  • baseline_log (Array<Hash<Symbol,Object>>)

    log from the baseline compilation

  • preview_log (Array<Hash<Symbol,Object>>)

    log from the preview compilation

Returns:

  • (Integer)

    id of found or created entity



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/puppet_x/puppetlabs/migration/overview_model/factory.rb', line 189

def node(catalog_delta, baseline_log, preview_log)
  baseline_env_id = environment(catalog_delta.baseline_env)
  preview_env_id = environment(catalog_delta.preview_env)
  severity =
      if catalog_delta.preview_equal?
        :equal
      elsif catalog_delta.preview_compliant?
        :compliant
      else
        :conflicting
      end
  timestamp = catalog_delta.timestamp

  node_id = complex_key_entity(Node, catalog_delta.node_name,timestamp, severity, 0) do |node|
    node.timestamp == timestamp
  end

  baseline_compilation_id = complex_key_entity(Compilation, node_id, baseline_env_id, true) { |comp| comp.baseline? }
  add_log_entries(baseline_compilation_id, baseline_log) unless baseline_log.nil?
  preview_compilation_id = complex_key_entity(Compilation, node_id, preview_env_id, false) { |comp| !comp.baseline? }
  add_log_entries(preview_compilation_id, preview_log) unless preview_log.nil?

  node_id
end

#node_issue(node_id, node_issue_id) ⇒ Integer

Returns the id of the IssueOnNode entity that corresponds to the given arguments. A new entity will be created if needed.

Parameters:

  • node_id (Integer)

    id of a Node

  • node_issue_id (Integer)

    id of NodeIssue.

Returns:

  • (Integer)

    id of found or created entity



176
177
178
# File 'lib/puppet_x/puppetlabs/migration/overview_model/factory.rb', line 176

def node_issue(node_id, node_issue_id)
  complex_key_entity(IssueOnNode, node_id, node_issue_id) { |n| n.node_issue_id == node_issue_id }
end

#resource(title, type) ⇒ Integer

Returns the id of the Resource entity that corresponds to the given arguments. A new entity will be created if needed.

Parameters:

  • title (String)

    the resource title

  • type (String)

    the resource type

Returns:

  • (Integer)

    id of found or created entity



261
262
263
264
# File 'lib/puppet_x/puppetlabs/migration/overview_model/factory.rb', line 261

def resource(title, type)
  resource_type_id = resource_type(type)
  complex_key_entity(Resource, title, resource_type_id) { |r| r.resource_type_id == resource_type_id }
end

#resource_conflict_issue(resource_conflict) ⇒ Integer

Returns the id of the ResourceIssue entity that corresponds to the given arguments. A new entity will be created if needed.

Parameters:

Returns:

  • (Integer)

    id of found or created entity



319
320
321
322
323
324
325
326
# File 'lib/puppet_x/puppetlabs/migration/overview_model/factory.rb', line 319

def resource_conflict_issue(resource_conflict)
  resource_id = resource(resource_conflict.title, resource_conflict.type)
  baseline_location_id = location_from_delta(resource_conflict.baseline_location)
  preview_location_id = location_from_delta(resource_conflict.preview_location)
  complex_key_entity(ResourceConflict, resource_id, baseline_location_id, preview_location_id, resource_conflict.compliant?) do |i|
    i.location_id == baseline_location_id && i.preview_location_id == preview_location_id
  end
end

#resource_from_string(resource_string) ⇒ Integer

Returns the id of the Resource entity that corresponds to the given arguments. A new entity will be created if needed.

Parameters:

  • resource_string (String)

    a resource reference in the format ‘type

Returns:

  • (Integer)

    id of found or created entity



245
246
247
248
249
250
251
# File 'lib/puppet_x/puppetlabs/migration/overview_model/factory.rb', line 245

def resource_from_string(resource_string)
  if resource_string =~ /^([^\[\]]+)\[(.+)\]$/m
    resource($2, $1)
  else
    raise ArgumentError, "Bad resource reference '#{resource_string}'"
  end
end

#resource_issue(resource, issue_class) ⇒ Integer

Returns the id of the ResourceIssue entity that corresponds to the given arguments. A new entity will be created if needed.

Parameters:

Returns:

  • (Integer)

    id of found or created entity



306
307
308
309
310
# File 'lib/puppet_x/puppetlabs/migration/overview_model/factory.rb', line 306

def resource_issue(resource, issue_class)
  resource_id = resource(resource.title, resource.type)
  location_id = location_from_delta(resource.location)
  complex_key_entity(issue_class, resource_id, location_id) { |i| i.location_id == location_id }
end

#resource_type(type_name) ⇒ Integer

Returns the id of the Resource entity that corresponds to the given resource. A new entity will be created if needed.

Parameters:

  • type_name (String)

Returns:

  • (Integer)

    id of found or created entity



142
143
144
# File 'lib/puppet_x/puppetlabs/migration/overview_model/factory.rb', line 142

def resource_type(type_name)
  single_key_entity(ResourceType, type_name)
end