1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
|
require 'chef_zero/data_store/interface_v2'
module ChefZero
module DataStore
class V1ToV2Adapter < ChefZero::DataStore::InterfaceV2
def initialize(real_store, single_org, options = {})
@real_store = real_store
@single_org = single_org
org_defaults = options[:org_defaults] || {}
@defaults = { 'organizations' => { single_org => org_defaults }}
end
ORG_DEFAULTS = {
'clients' => {
'chef-validator' => '{ "validator": true }',
'chef-webui' => '{ "admin": true }'
},
'environments' => {
'_default' => '{ "description": "The default Chef environment" }'
},
'users' => {
'admin' => '{ "admin": "true" }'
}
}
attr_reader :real_store
attr_reader :single_org
def clear
real_store.clear
end
def create_dir(path, name, *options)
return nil if skip_organizations?(path, name)
if using_default?(path, name)
raise DataAlreadyExistsError.new(path + [name])
end
fix_exceptions do
real_store.create_dir(path[2..-1], name, *options)
end
end
def create(path, name, data, *options)
return nil if skip_organizations?(path, name)
if using_default?(path, name)
raise DataAlreadyExistsError.new(path + [name])
end
remove_default(path, name)
fix_exceptions do
real_store.create(path[2..-1], name, data, *options)
end
end
def get(path, request=nil)
return nil if skip_organizations?(path)
if using_default?(path)
get_default(path)
else
fix_exceptions do
real_store.get(path[2..-1], request)
end
end
end
def set(path, data, *options)
return nil if skip_organizations?(path)
remove_default(path)
fix_exceptions do
real_store.set(path[2..-1], data, *options)
end
end
def delete(path)
return nil if skip_organizations?(path)
remove_default(path)
fix_exceptions do
real_store.delete(path[2..-1])
end
end
def delete_dir(path, *options)
return nil if skip_organizations?(path)
fix_exceptions do
real_store.delete_dir(path[2..-1], *options)
end
end
def list(path)
return nil if skip_organizations?(path)
fix_exceptions do
result = real_store.list(path[2..-1])
if using_default?(path)
result ||= []
get_default(path).keys.each do |value|
result << value if !result.include?(value)
end
end
result
end
end
def exists?(path)
return nil if skip_organizations?(path)
if using_default?(path)
true
else
fix_exceptions do
real_store.exists?(path[2..-1])
end
end
end
def exists_dir?(path)
return nil if skip_organizations?(path)
if using_default?(path)
true
else
fix_exceptions do
real_store.exists_dir?(path[2..-1])
end
end
end
private
def using_default?(path, name = nil)
path = path + [name] if name
result = @defaults
path.each do |part|
return false if !result.has_key?(part)
result = result[part]
end
!result.nil?
end
def get_default(path, name = nil)
path = path + [name] if name
result = @defaults
path.each do |part|
return nil if !result.has_key?(part)
result = result[part]
end
result
end
def remove_default(path, name = nil)
dir = name ? path[0..-2] : path
default = @defaults
dir.each do |part|
return if !default.has_key?(part)
default = default[part]
end
name = name || path.last
if name
default.delete(name)
end
end
def fix_exceptions
begin
yield
rescue DataAlreadyExistsError => e
raise DataAlreadyExistsError.new([ 'organizations', single_org ] + e.path, e)
rescue DataNotFoundError => e
raise DataNotFoundError.new([ 'organizations', single_org ] + e.path, e)
end
end
def skip_organizations?(path, name = nil)
if path == []
raise "" if name == nil || name != 'organizations'
true
elsif path == ['organizations']
raise "" if name == nil || name != single_org
true
else
raise "Path #{path} must start with /organizations/#{single_org}" if path[0..1] != [ 'organizations', single_org ]
if !name
raise "Path #{path} must start with /organizations/#{single_org}/<something>" if path.size <= 2
end
false
end
end
end
end
end
|