Skip to content

Commit 3ac4575

Browse files
author
Kevin Paulisse
committed
Create wrapper class around OctocatalogDiff::Catalog
1 parent 09fa9ea commit 3ac4575

4 files changed

Lines changed: 84 additions & 2 deletions

File tree

lib/octocatalog-diff/api/v1/catalog-compile.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# frozen_string_literal: true
22

3+
require_relative 'catalog'
34
require_relative 'common'
45
require_relative '../../util/catalogs'
56

@@ -31,7 +32,7 @@ def self.catalog(options = nil)
3132
to_catalog: nil, # Forces a compile
3233
)
3334
cat_obj = OctocatalogDiff::Util::Catalogs.new(catalog_opts, logger)
34-
cat_obj.catalogs[:to]
35+
OctocatalogDiff::API::V1::Catalog.new(cat_obj.catalogs[:to])
3536
end
3637
end
3738
end
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# frozen_string_literal: true
2+
3+
require_relative 'common'
4+
require_relative '../../catalog'
5+
6+
require 'ostruct'
7+
8+
module OctocatalogDiff
9+
module API
10+
module V1
11+
# This is a wrapper class around OctocatalogDiff::Catalog. This contains the methods we
12+
# are choosing to expose, and will be a compatibility layer should underlying methods
13+
# change in the future. The raw object will be available as `#raw` but this is not
14+
# guaranteed to be stable.
15+
class Catalog
16+
attr_reader :raw
17+
18+
# Constructor: Accepts a raw OctocatalogDiff::Catalog object and stores it.
19+
# @param raw [OctocatalogDiff::Catalog] Catalog object
20+
def initialize(raw)
21+
unless raw.is_a?(OctocatalogDiff::Catalog)
22+
raise ArgumentError, 'OctocatalogDiff::API::V1::Catalog#initialize expects OctocatalogDiff::Catalog argument'
23+
end
24+
@raw = raw
25+
end
26+
27+
# Public: Get the builder for the catalog
28+
# @return [String] Class of backend used
29+
def builder
30+
@raw.builder
31+
end
32+
33+
# Public: Get the JSON for the catalog
34+
# @return [String] Catalog JSON
35+
def catalog_json
36+
@raw.catalog_json
37+
end
38+
39+
# Public: Get the compilation directory
40+
# @return [String] Compilation directory
41+
def compilation_dir
42+
@raw.compilation_dir
43+
end
44+
45+
# Public: Get the error message
46+
# @return [String] Error message, or nil if no error
47+
def error_message
48+
@raw.error_message
49+
end
50+
51+
# Public: Get the Puppet version used to compile the catalog
52+
# @return [String] Puppet version
53+
def puppet_version
54+
@raw.puppet_version
55+
end
56+
57+
# Public: Get a specific resource identified by type and title.
58+
# This is intended for use when a O(1) lookup is required.
59+
# @param :type [String] Type of resource
60+
# @param :title [String] Title of resource
61+
# @return [Hash] Resource item
62+
def resource(opts = {})
63+
@raw.resource(opts)
64+
end
65+
66+
# Public: Get the resources in the catalog
67+
# @return [Array] Resource array
68+
def resources
69+
@raw.resources
70+
end
71+
72+
# Public: Determine if the catalog build was successful.
73+
# @return [Boolean] Whether the catalog is valid
74+
def valid?
75+
@raw.valid?
76+
end
77+
end
78+
end
79+
end
80+
end

lib/octocatalog-diff/cli.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ def self.setup_logger(logger, options, argv_save)
158158
# Compile the catalog only
159159
def self.catalog_only(logger, options)
160160
opts = options.merge(logger: logger)
161-
to_catalog = OctocatalogDiff::API::V1::CatalogCompile.catalog(opts)
161+
to_catalog = OctocatalogDiff::API::V1::CatalogCompile.catalog(opts).raw
162162

163163
# If the catalog compilation failed, an exception would have been thrown. So if
164164
# we get here, the catalog succeeded. Dump the catalog to the appropriate place

spec/octocatalog-diff/tests/api/v1/catalog-compile_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646

4747
it 'should call OctocatalogDiff::Util::Catalogs and return to-key' do
4848
obj = OpenStruct.new(catalogs: { to: 'to-catalog', from: 'from-catalog' })
49+
expect(OctocatalogDiff::API::V1::Catalog).to receive(:new).with('to-catalog').and_return('to-catalog')
4950
expect(OctocatalogDiff::Util::Catalogs).to receive(:new).and_return(obj)
5051
result = described_class.catalog({})
5152
expect(result).to eq('to-catalog')

0 commit comments

Comments
 (0)