Skip to content

Commit b4cfb75

Browse files
authored
Merge pull request #51 from github/kpaulisse-catalog-object-api
Wrapper classes around catalog and diff objects
2 parents b104b30 + d4c97ca commit b4cfb75

18 files changed

Lines changed: 1119 additions & 37 deletions

File tree

doc/dev/api/v1.md

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,35 @@
11
# octocatalog-diff v1 API documentation
22

3-
## catalog
3+
## API calls
4+
5+
#### catalog
46

57
`catalog` allows you to build a catalog using the octocatalog-diff compiler or to obtain a catalog from a Puppet server.
68

7-
[Read more about `catalog`](/doc/dev/api/v1/catalog.md)
9+
[Read more about the `catalog` call](/doc/dev/api/v1/calls/catalog.md)
810

9-
## catalog-diff
11+
#### catalog-diff
1012

1113
`catalog-diff` allows you compare two catalogs and obtain the differences between them. Catalogs can be built if necessary.
1214

13-
[Read more about `catalog-diff`](/doc/dev/api/v1/catalog-diff.md)
15+
[Read more about the `catalog-diff` call](/doc/dev/api/v1/calls/catalog-diff.md)
1416

15-
## config
17+
#### config
1618

1719
`config` allows you read and parse an [octocatalog-diff configuration file](/doc/configuration.md).
1820

19-
[Read more about `config`](/doc/dev/api/v1/config)
21+
[Read more about the `config` call](/doc/dev/api/v1/calls/config.md)
22+
23+
## Objects
24+
25+
#### OctocatalogDiff::API::V1::Catalog
26+
27+
The `OctocatalogDiff::API::V1::Catalog` object represents a compiled catalog and supports several methods to get information about the catalog.
28+
29+
[Read more about the `OctocatalogDiff::API::V1::Catalog` object](/doc/dev/api/v1/objects/catalog.md)
30+
31+
#### OctocatalogDiff::API::V1::Diff
32+
33+
The `OctocatalogDiff::API::V1::Diff` object represents a difference between two catalogs and supports several methods to get information about the difference.
34+
35+
[Read more about the `OctocatalogDiff::API::V1::Diff` object](/doc/dev/api/v1/objects/diff.md)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,20 @@ catalog_diff_result = OctocatalogDiff::API::V1.catalog_diff(
1818

1919
## Return value
2020

21+
The return value is a structure in the following format:
22+
23+
```
24+
{
25+
diffs: Array<OctocatalogDiff::API::V1::Diff>,
26+
from: OctocatalogDiff::API::V1::Catalog,
27+
to: OctocatalogDiff::API::V1::Catalog
28+
}
29+
```
30+
31+
It is possible to query this object as a hash (e.g. `result[:diffs]`) or using methods (e.g. `result.diffs`).
32+
33+
[Read more about the `OctocatalogDiff::API::V1::Catalog` object](/doc/dev/api/v1/objects/catalog.md)
34+
35+
[Read more about the `OctocatalogDiff::API::V1::Diff` object](/doc/dev/api/v1/objects/diff.md)
36+
2137
## Exceptions
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ catalog_obj = OctocatalogDiff::API::V1.catalog(
1717

1818
## Return value
1919

20-
The return value is an [`OctocatalogDiff::Catalog`](/doc/dev/api/v1/octocatalogdiff-catalog.md) object.
20+
The return value is an [`OctocatalogDiff::API::V1::Catalog`](/doc/dev/api/v1/objects/catalog.md) object.
2121

2222
## Exceptions
File renamed without changes.

doc/dev/api/v1/objects/catalog.md

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
# octocatalog-diff v1 API documentation: OctocatalogDiff::API::V1::Catalog
2+
3+
## Overview
4+
5+
`OctocatalogDiff::API::V1::Catalog` is an object that represents a compiled catalog.
6+
7+
It wraps the [`OctocatalogDiff::Catalog`](/lib/octocatalog-diff/catalog.rb) object.
8+
9+
This object is the return value from the [`catalog`](/doc/dev/api/v1/calls/catalog.md) API call, and the `to` and `from` catalogs computed by the [`catalog-diff`](/doc/dev/api/v1/calls/catalog-diff.md) API call.
10+
11+
## Methods
12+
13+
#### `#builder` (String)
14+
15+
Returns the name of the class the built the catalog.
16+
17+
```
18+
catalog.builder #=> 'OctocatalogDiff::Catalog::JSON'
19+
```
20+
21+
#### `#compilation_dir` (String)
22+
23+
Returns the temporary directory in which the catalog was compiled.
24+
25+
```
26+
catalog.compilation_dir #=> '/var/folders/dw/5ftmkqk972j_kw2fdjyzdqdw0000gn/T/d20170108-95774-1r4ohjd'
27+
```
28+
29+
This is only applicable for catalogs compiled by `OctocatalogDiff::Catalog::Computed`. This method will return `nil` for catalogs generated by other backends.
30+
31+
#### `#error_message` (String)
32+
33+
Returns the error message for a failed catalog. (If the catalog is valid, this returns `nil`.)
34+
35+
```
36+
good_catalog.valid? #=> true
37+
good_catalog.error_message #=> nil
38+
39+
bad_catalog.valid? #=> false
40+
bad_catalog.error_message #=> 'Failed to compile catalog for node ...'
41+
```
42+
43+
#### `#puppet_version` (String)
44+
45+
Returns the Puppet version used to compile the catalog.
46+
47+
```
48+
catalog.puppet_version #=> '3.8.7'
49+
```
50+
51+
This is only applicable for catalogs compiled by `OctocatalogDiff::Catalog::Computed`. This method will return `nil` for catalogs generated by other backends.
52+
53+
#### `#resource(<Hash>)` (Hash)
54+
55+
Returns the hash representation of the object identified by the type and title supplied.
56+
57+
This method requires 1 argument, which is a hash containing `:type` and `:title` keys.
58+
59+
```
60+
catalog.resource(type: 'File', title: '/etc/foo')
61+
#=> {"title"=>"/etc/foo", "type"=>"File",
62+
"parameters"=>{"content"=>"This is the file", "owner"=>"root"},
63+
"file"=>"/etc/puppetlabs/code/environments/production/manifests/site.pp", "line"=>25}
64+
```
65+
66+
Returns `nil` if the type+title resource was not present in the catalog.
67+
68+
##### Notes
69+
70+
1. The first call to this method will build a hash table (`O(N)` operation) from the resource array. Thereafter, each call to this method will look up the value in that hash table (`O(1)` operation). To perform lookups of known types and titles, it is faster to use this method than to use `#resources` with array operations when performing multiple lookups.
71+
72+
2. The structure of the returned hash is dependent on the representation of the resource in the Puppet catalog. It is therefore possible that different versions of Puppet could cause different data structures. It is the author's experience that Puppet 3.8.7 and Puppet 4.x produce similar (if not indistinguishable) resource representations.
73+
74+
3. This method will also locate a resource that was named using an alias.
75+
76+
```
77+
# Puppet code
78+
file { '/etc/foo':
79+
alias => 'foo file for the win',
80+
...
81+
}
82+
83+
# octocatalog-diff API
84+
catalog.resource(type: 'File', title: 'foo file for the win')
85+
#=> {"title"=>"/etc/foo", "type"=>"File", ...}
86+
```
87+
88+
#### `#resources` (Array&lt;Hash&gt;)
89+
90+
Returns an array of catalog resources in a successful catalog. (If the catalog is not valid, this returns `nil`.)
91+
92+
```
93+
bad_catalog.valid? #=> false
94+
bad_catalog.resources #=> nil
95+
96+
good_catalog.valid? #=> true
97+
good_catalog.resources
98+
#=> [
99+
{ "title"=>"/etc/foo", "type"=>"File", ... },
100+
{ "title"=>"/bin/true", "type"=>"Exec", ... }
101+
]
102+
```
103+
104+
The structure of the returned hash is dependent on the representation of the resource in the Puppet catalog. It is therefore possible that different versions of Puppet could cause different data structures. It is the author's experience that Puppet 3.8.7 and Puppet 4.x produce similar (if not indistinguishable) resource representations.
105+
106+
#### `#to_json` (String)
107+
108+
Returns the JSON representation of a successful catalog. (If the catalog is not valid, this returns `nil`.)
109+
110+
```
111+
bad_catalog.valid? #=> false
112+
bad_catalog.to_json #=> nil
113+
114+
good_catalog.valid? #=> true
115+
good_catalog.to_json #=> '{ "document_type": "Catalog", ... }'
116+
```
117+
118+
#### `#valid?` (Boolean)
119+
120+
Returns `true` if the catalog is valid, and `false` if the catalog is not valid.
121+
122+
## Other methods
123+
124+
These methods are available for debugging or development purposes but are not guaranteed to remain consistent between versions:
125+
126+
- `#to_h` (Hash): Returns hash representation of parsed JSON catalog
127+
- `#raw` (OctocatalogDiff::Catalog): Returns underlying internal catalog object

0 commit comments

Comments
 (0)