Skip to content

Commit b104b30

Browse files
authored
Merge pull request #44 from github/kpaulisse-better-api
Create API classes for compiled catalog and catalog-diff
2 parents 7262c47 + 0f5e49f commit b104b30

64 files changed

Lines changed: 1288 additions & 248 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ The example above reflects the changes in the Puppet catalog from switching an u
5151
- [Requirements](/doc/requirements.md)
5252
- [Limitations](/doc/limitations.md)
5353
- [List of all command line options](/doc/optionsref.md)
54+
- [API](/doc/dev/api.md)
5455

5556
### Project
5657

bin/octocatalog-diff

Lines changed: 6 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -20,54 +20,13 @@ end
2020

2121
config_test = ARGV.include?('--config-test')
2222

23-
paths = [
24-
ENV['OCTOCATALOG_DIFF_CONFIG_FILE'],
25-
File.join(Dir.pwd, '.octocatalog-diff.cfg.rb'),
26-
File.join(ENV['HOME'], '.octocatalog-diff.cfg.rb'),
27-
'/opt/puppetlabs/octocatalog-diff/octocatalog-diff.cfg.rb',
28-
'/usr/local/etc/octocatalog-diff.cfg.rb',
29-
'/etc/octocatalog-diff.cfg.rb'
30-
]
23+
logger = Logger.new(STDERR)
24+
logger.level = Logger::DEBUG if config_test
3125

32-
cfgfile = nil
33-
34-
paths.each do |path|
35-
next if path.nil?
36-
puts "Looking for config in: #{path}" if config_test
37-
next unless File.file?(path)
38-
begin
39-
cfgfile = path
40-
puts "Loading config: #{path}" if config_test
41-
require path
42-
rescue => exc
43-
STDERR.puts "#{exc.class} error with #{path}: #{exc.message}\n#{exc.backtrace}"
44-
exit 1
45-
end
46-
break
47-
end
48-
49-
options = {}
50-
51-
if cfgfile
52-
begin
53-
options = OctocatalogDiff::Config.config
54-
rescue => exc
55-
STDERR.puts "#{exc.class} error with #{cfgfile}: #{exc.message}\n#{exc.backtrace}"
56-
exit 1
57-
end
58-
unless options.is_a?(Hash)
59-
STDERR.puts "Configuration must be Hash not #{options.class}!"
60-
exit 1
61-
end
62-
if config_test
63-
options.each do |key, val|
64-
puts ":#{key} => (#{val.class}) #{val.inspect}"
65-
end
66-
exit 0
67-
end
68-
elsif config_test
69-
STDERR.puts 'No configuration files found'
70-
exit 1
26+
options = OctocatalogDiff::API::V1.config(logger: logger, test: config_test)
27+
if config_test
28+
logger.info 'Exiting now because --config-test was specified'
29+
exit(0)
7130
end
7231

7332
argv = ARGV.dup

doc/dev/api.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# octocatalog-diff API documentation
2+
3+
The octocatalog-diff API allows developers to construct and work with certain octocatalog-diff internals in their own projects.
4+
5+
The current API version is [API v1](/doc/dev/api/v1.md), which is available as of octocatalog-diff version 0.7.

doc/dev/api/v1.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# octocatalog-diff v1 API documentation
2+
3+
## catalog
4+
5+
`catalog` allows you to build a catalog using the octocatalog-diff compiler or to obtain a catalog from a Puppet server.
6+
7+
[Read more about `catalog`](/doc/dev/api/v1/catalog.md)
8+
9+
## catalog-diff
10+
11+
`catalog-diff` allows you compare two catalogs and obtain the differences between them. Catalogs can be built if necessary.
12+
13+
[Read more about `catalog-diff`](/doc/dev/api/v1/catalog-diff.md)
14+
15+
## config
16+
17+
`config` allows you read and parse an [octocatalog-diff configuration file](/doc/configuration.md).
18+
19+
[Read more about `config`](/doc/dev/api/v1/config)

doc/dev/api/v1/catalog-diff.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# octocatalog-diff v1 API documentation: catalog-diff
2+
3+
## Overview
4+
5+
`catalog-diff` allows you compare two catalogs and obtain the differences between them. Catalogs can be built if necessary.
6+
7+
This is analogous to using the default arguments with the octocatalog-diff command-line script.
8+
9+
```
10+
catalog_diff_result = OctocatalogDiff::API::V1.catalog_diff(
11+
12+
)
13+
```
14+
15+
## Options
16+
17+
**NOTE**: Additional options as described in the [options reference](/doc/optionsref.md) may also have an effect on catalog difference generation.
18+
19+
## Return value
20+
21+
## Exceptions

doc/dev/api/v1/catalog.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# octocatalog-diff v1 API documentation: catalog
2+
3+
## Overview
4+
5+
`catalog` returns an `OctocatalogDiff::Catalog` object built with the octocatalog-diff compiler, obtained from a Puppet server, or read in from a file. This is analogous to using the `--catalog-only` option with the octocatalog-diff command-line script.
6+
7+
```
8+
catalog_obj = OctocatalogDiff::API::V1.catalog(
9+
10+
)
11+
```
12+
13+
## Options
14+
15+
16+
**NOTE**: Additional options as described in the [options reference](/doc/optionsref.md) may also have an effect on catalog generation.
17+
18+
## Return value
19+
20+
The return value is an [`OctocatalogDiff::Catalog`](/doc/dev/api/v1/octocatalogdiff-catalog.md) object.
21+
22+
## Exceptions

doc/dev/api/v1/config.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# octocatalog-diff v1 API documentation: config
2+
3+
## Overview
4+
5+
`config` reads and parses an [octocatalog-diff configuration file](/doc/configuration.md).
6+
7+
```
8+
options = OctocatalogDiff::API::V1.config(
9+
filename: "String",
10+
logger: Logger,
11+
test: <true|false>
12+
)
13+
```
14+
15+
## Options
16+
17+
- **`:filename`** (String, optional): Full path to configuration file to read. If not provided, the configuration file will be searched as described in [Configuration](/doc/configuration.md).
18+
19+
- **`:logger`** (Logger, optional): Logger object. If provided, debug messages and fatal errors will be logged to this object.
20+
21+
- **`:test`** (Boolean, optional): Test mode, defaults to false. If true, the value of the configuration settings will be logged to the logger (with priority DEBUG) and an exception will be raised if the configuration file cannot be located.
22+
23+
## Return value
24+
25+
If the configuration file is located and valid, the return value is a Hash consisting of the options defined in the configuration file.
26+
27+
If the configuration file cannot be found, the return value is an empty Hash (`{}`). Except, with `:test => true`, an exception will be raised.
28+
29+
## Exceptions
30+
31+
- `OctocatalogDiff::Errors::ConfigurationFileContentError`
32+
33+
Raised if the configuration file could not be evaluated. A more specific error message will help identify the cause. Possible causes include the file not being valid ruby, the file not containing the expected structure or methods, or the method returning something other than a Hash.
34+
35+
- `OctocatalogDiff::Errors::ConfigurationFileNotFoundError`
36+
37+
Raised if the configuration file could not be found, *and* `:test => true` was supplied. (Note, if no configuration file is found, but `:test => false`, no error is raised, and `{}` is returned.)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# octocatalog-diff v1 API documentation: OctocatalogDiff::Catalog
2+
3+
## Overview
4+
5+
`OctocatalogDiff::Catalog` is a class that represents a compiled catalog.
6+
7+
This object is the return value from the [`catalog`](/doc/dev/api/v1/catalog.md) API call.
8+
9+
## Methods
10+
11+
## Exceptions

lib/octocatalog-diff.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# These are all the classes we believe people might want to call directly, so load
22
# them in response to a 'require octocatalog-diff'.
33

4-
loads = %w(bootstrap catalog cli facts puppetdb version)
4+
loads = %w(api/v1 bootstrap catalog cli errors facts puppetdb version)
55
loads.each { |f| require_relative "octocatalog-diff/#{f}" }

lib/octocatalog-diff/api/v1.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# frozen_string_literal: true
2+
3+
require_relative 'v1/catalog-compile'
4+
require_relative 'v1/catalog-diff'
5+
require_relative 'v1/config'
6+
7+
module OctocatalogDiff
8+
module API
9+
# Call available methods for this version of the API
10+
module V1
11+
def self.catalog(options = nil)
12+
OctocatalogDiff::API::V1::CatalogCompile.catalog(options)
13+
end
14+
15+
def self.catalog_diff(options = nil)
16+
OctocatalogDiff::API::V1::CatalogDiff.catalog_diff(options)
17+
end
18+
19+
def self.config(options = nil)
20+
OctocatalogDiff::API::V1::Config.config(options)
21+
end
22+
end
23+
end
24+
end

0 commit comments

Comments
 (0)