Class: OctocatalogDiff::API::V1::CatalogDiff

Inherits:
Object
  • Object
show all
Defined in:
lib/octocatalog-diff/api/v1/catalog-diff.rb

Overview

This class allows octocatalog-diff to be used to compile catalogs (if needed) and then compute the differences between them.

Class Method Summary collapse

Class Method Details

.catalog_diff(options = nil) ⇒ OpenStruct

Public: Run catalog-diff

Parameters are to be passed in a hash. Other catalog-diff parameters are required

Parameters:

  • :logger (Logger)

    Logger object (be sure to configure log level)

Returns:

  • (OpenStruct)

    { :diffs (Array); :from (OctocatalogDiff::Catalog), :to (OctocatalogDiff::Catalog) }



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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/octocatalog-diff/api/v1/catalog-diff.rb', line 23

def self.catalog_diff(options = nil)
  # Validate the required options.
  unless options.is_a?(Hash)
    raise ArgumentError, 'Usage: #catalog_diff(options_hash)'
  end

  pass_opts, logger = OctocatalogDiff::API::V1::Common.logger_from_options(options)

  # Compile catalogs
  logger.debug "Compiling catalogs for #{options[:node]}"
  catalogs_obj = OctocatalogDiff::Util::Catalogs.new(pass_opts, logger)
  catalogs = catalogs_obj.catalogs
  logger.info "Catalogs compiled for #{options[:node]}"

  # Cache catalogs if master caching is enabled. If a catalog is being read from the cached master
  # directory, set the compilation directory attribute, so that the "compilation directory dependent"
  # suppressor will still work.
  %w(from to).each do |x|
    next unless options["#{x}_env".to_sym] == options.fetch(:master_cache_branch, 'origin/master')
    next if options[:cached_master_dir].nil?
    catalogs[x.to_sym].compilation_dir = options["#{x}_catalog_compilation_dir".to_sym] || options[:cached_master_dir]
    rc = OctocatalogDiff::CatalogUtil::CachedMasterDirectory.save_catalog_in_cache_dir(
      options[:node],
      options[:cached_master_dir],
      catalogs[x.to_sym]
    )
    logger.debug "Cached master catalog for #{options[:node]}" if rc
  end

  # Compute diffs
  diffs_obj = OctocatalogDiff::Cli::Diffs.new(options, logger)
  diffs = diffs_obj.diffs(catalogs)
  logger.info "Diffs computed for #{options[:node]}"
  logger.info 'No differences' if diffs.empty?

  # Return diffs and catalogs in expected format
  OpenStruct.new(
    diffs: diffs.map { |x| OctocatalogDiff::API::V1::Diff.factory(x) },
    from: OctocatalogDiff::API::V1::Catalog.new(catalogs[:from]),
    to: OctocatalogDiff::API::V1::Catalog.new(catalogs[:to])
  )
end