Skip to content

Commit b553c88

Browse files
author
Kevin Paulisse
committed
Add documentation and test for structure
1 parent 8482345 commit b553c88

2 files changed

Lines changed: 101 additions & 1 deletion

File tree

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

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,86 @@ Returns the change type of the diff, which is one of the following characters:
2424
- `-` for removal (resource exists in old catalog but not new catalog)
2525
- `~` or `!` for change (resource exists in both catalogs but is different between them)
2626

27+
See also: `#addition?`, `#change?`, `#removal?`
28+
2729
Note: Internally `~` and `!` represent different types of changes, but when presenting the output, these can generally be considered equivalent.
2830

29-
#### `removal?` (Boolean)
31+
#### `#removal?` (Boolean)
3032

3133
Returns true if this diff is a removal (resource exists in old catalog but not new catalog).
34+
35+
#### `#structure` (Array)
36+
37+
Returns the structure that has been changed, as an array.
38+
39+
When a resource was added or removed, the result is an empty array. That's because all of the parameters and other metadata from the resource exist entirely in one catalog but not the other.
40+
41+
When a resource has changed, one diff is created for each parameter that changed. For example, both the old and new catalogs contain the file resource `/etc/foo` but just the content has changed:
42+
43+
```
44+
# Old
45+
file { '/etc/foo':
46+
owner => 'root',
47+
content => 'This is the old file',
48+
}
49+
50+
# New
51+
file { '/etc/foo':
52+
owner => 'root',
53+
content => 'This is the NEW FILE!!!!!',
54+
}
55+
```
56+
57+
Internally, the Puppet catalog for this resource will look like this in the catalogs (this has been abbreviated a bit for clarity):
58+
59+
```
60+
# Old
61+
{
62+
"type": "File",
63+
"title": "/etc/foo",
64+
"exported": false,
65+
"parameters": {
66+
"owner": "root",
67+
"content": "This is the old file"
68+
}
69+
}
70+
71+
# New
72+
{
73+
"type": "File",
74+
"title": "/etc/foo",
75+
"exported": false,
76+
"parameters": {
77+
"owner": "root",
78+
"content": "This is the NEW FILE!!!!!"
79+
}
80+
}
81+
```
82+
83+
One diff will be generated to represent the change to the content of the file (which in the catalog is nested in the 'parameters' hash). The diff will be structured as follows:
84+
85+
```
86+
diff.type #=> 'File'
87+
diff.title #=> '/etc/foo'
88+
diff.structure #=> ['parameters', 'content']
89+
```
90+
91+
#### `#title` (String)
92+
93+
Returns the title of the resource from the Puppet catalog.
94+
95+
For example, a diff involving `File['/etc/passwd']` would have:
96+
97+
- `diff.title #=> '/etc/passwd'`
98+
- `diff.type #=> 'File`
99+
100+
#### `#type` (String)
101+
102+
Returns the type of the resource from the Puppet catalog.
103+
104+
For example, a diff involving `File['/etc/passwd']` would have:
105+
106+
- `diff.title #=> '/etc/passwd'`
107+
- `diff.type #=> 'File`
108+
109+
Note that the type will be capitalized because Puppet capitalizes this in catalogs.

spec/octocatalog-diff/tests/api/v1/diff_spec.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,34 @@
101101
end
102102

103103
describe '#type' do
104+
it 'should return the type' do
105+
testobj = described_class.new(chg_2)
106+
expect(testobj.type).to eq('File')
107+
end
104108
end
105109

106110
describe '#title' do
111+
it 'should return the title when there is no structure' do
112+
testobj = described_class.new(add_2)
113+
expect(testobj.title).to eq('/etc/foo')
114+
end
115+
116+
it 'should return the title when there is structure' do
117+
testobj = described_class.new(chg_2)
118+
expect(testobj.title).to eq('/etc/foo')
119+
end
107120
end
108121

109122
describe '#structure' do
123+
it 'should return an empty array for an addition' do
124+
testobj = described_class.new(add_2)
125+
expect(testobj.structure).to eq([])
126+
end
127+
128+
it 'should return the proper array for a change' do
129+
testobj = described_class.new(chg_2)
130+
expect(testobj.structure).to eq(%w(parameters content))
131+
end
110132
end
111133

112134
describe '#old_value' do

0 commit comments

Comments
 (0)