Class: Chef::Environment
Constant Summary
collapse
- DEFAULT =
"default"
- COMBINED_COOKBOOK_CONSTRAINT =
/(.+)(?:[\s]+)((?:#{Chef::VersionConstraint::OPS.join('|')})(?:[\s]+).+)$/.freeze
Class Method Summary
collapse
Instance Method Summary
collapse
#class_from_file, #from_file
#set_or_return, #validate
Constructor Details
Returns a new instance of Environment.
37
38
39
40
41
42
43
|
# File 'lib/chef/environment.rb', line 37
def initialize()
@name = ''
@description = ''
@default_attributes = Mash.new
@override_attributes = Mash.new
@cookbook_versions = Hash.new
end
|
Class Method Details
.json_create(o) ⇒ Object
200
201
202
203
204
205
206
207
208
|
# File 'lib/chef/environment.rb', line 200
def self.json_create(o)
environment = new
environment.name(o["name"])
environment.description(o["description"])
environment.cookbook_versions(o["cookbook_versions"])
environment.default_attributes(o["default_attributes"])
environment.override_attributes(o["override_attributes"])
environment
end
|
.validate_cookbook_version(version) ⇒ Object
222
223
224
225
226
227
228
229
|
# File 'lib/chef/environment.rb', line 222
def self.validate_cookbook_version(version)
begin
Chef::VersionConstraint.new version
true
rescue ArgumentError
false
end
end
|
.validate_cookbook_versions(cv) ⇒ Object
214
215
216
217
218
219
220
|
# File 'lib/chef/environment.rb', line 214
def self.validate_cookbook_versions(cv)
return false unless cv.kind_of?(Hash)
cv.each do |cookbook, version|
return false unless Chef::Environment.validate_cookbook_version(version)
end
true
end
|
Instance Method Details
#add_cookbook_constraint_error(index, cookbook_constraint_spec) ⇒ Object
181
182
183
184
|
# File 'lib/chef/environment.rb', line 181
def add_cookbook_constraint_error(index, cookbook_constraint_spec)
invalid_fields[:cookbook_version] ||= {}
invalid_fields[:cookbook_version][index] = "#{cookbook_constraint_spec} is not a valid cookbook constraint"
end
|
#cookbook(cookbook, version) ⇒ Object
90
91
92
93
94
95
96
97
98
99
|
# File 'lib/chef/environment.rb', line 90
def cookbook(cookbook, version)
validate({
:version => version
},{
:version => {
:callbacks => { "should be a valid version requirement" => lambda { |v| Chef::Environment.validate_cookbook_version(v) } }
}
})
@cookbook_versions[cookbook] = version
end
|
#cookbook_versions(arg = nil) ⇒ Object
77
78
79
80
81
82
83
84
85
86
87
88
|
# File 'lib/chef/environment.rb', line 77
def cookbook_versions(arg=nil)
set_or_return(
:cookbook_versions,
arg,
{
:kind_of => Hash,
:callbacks => {
"should be a valid set of cookbook version requirements" => lambda { |cv| Chef::Environment.validate_cookbook_versions(cv) }
}
}
)
end
|
#default_attributes(arg = nil) ⇒ Object
61
62
63
64
65
66
67
|
# File 'lib/chef/environment.rb', line 61
def default_attributes(arg=nil)
set_or_return(
:default_attributes,
arg,
:kind_of => Hash
)
end
|
#description(arg = nil) ⇒ Object
53
54
55
56
57
58
59
|
# File 'lib/chef/environment.rb', line 53
def description(arg=nil)
set_or_return(
:description,
arg,
:kind_of => String
)
end
|
#invalid_fields ⇒ Object
186
187
188
|
# File 'lib/chef/environment.rb', line 186
def invalid_fields
@invalid_fields ||= {}
end
|
#name(arg = nil) ⇒ Object
45
46
47
48
49
50
51
|
# File 'lib/chef/environment.rb', line 45
def name(arg=nil)
set_or_return(
:name,
arg,
{ :regex => /^[\-[:alnum:]_]+$/, :kind_of => String }
)
end
|
#override_attributes(arg = nil) ⇒ Object
69
70
71
72
73
74
75
|
# File 'lib/chef/environment.rb', line 69
def override_attributes(arg=nil)
set_or_return(
:override_attributes,
arg,
:kind_of => Hash
)
end
|
101
102
103
104
105
106
107
108
109
110
111
112
|
# File 'lib/chef/environment.rb', line 101
def to_hash
result = {
"name" => @name,
"description" => @description,
"cookbook_versions" => @cookbook_versions,
"json_class" => self.class.name,
"chef_type" => "environment",
"default_attributes" => @default_attributes,
"override_attributes" => @override_attributes
}
result
end
|
#to_json(*a) ⇒ Object
114
115
116
|
# File 'lib/chef/environment.rb', line 114
def to_json(*a)
to_hash.to_json(*a)
end
|
210
211
212
|
# File 'lib/chef/environment.rb', line 210
def to_s
@name
end
|
#update_attributes_from_params(params) ⇒ Object
127
128
129
130
131
132
133
134
|
# File 'lib/chef/environment.rb', line 127
def update_attributes_from_params(params)
unless params[:default_attributes].nil? || params[:default_attributes].size == 0
default_attributes(Chef::JSONCompat.from_json(params[:default_attributes]))
end
unless params[:override_attributes].nil? || params[:override_attributes].size == 0
override_attributes(Chef::JSONCompat.from_json(params[:override_attributes]))
end
end
|
#update_cookbook_constraint_from_param(index, cookbook_constraint_spec) ⇒ Object
166
167
168
169
170
171
172
173
174
175
176
177
178
179
|
# File 'lib/chef/environment.rb', line 166
def update_cookbook_constraint_from_param(index, cookbook_constraint_spec)
valid = true
md = cookbook_constraint_spec.match(COMBINED_COOKBOOK_CONSTRAINT)
if md.nil? || md[2].nil?
valid = false
add_cookbook_constraint_error(index, cookbook_constraint_spec)
elsif self.class.validate_cookbook_version(md[2])
cookbook_versions[md[1]] = md[2]
else
valid = false
add_cookbook_constraint_error(index, cookbook_constraint_spec)
end
valid
end
|
#update_from!(o) ⇒ Object
118
119
120
121
122
123
124
|
# File 'lib/chef/environment.rb', line 118
def update_from!(o)
description(o.description)
cookbook_versions(o.cookbook_versions)
default_attributes(o.default_attributes)
override_attributes(o.override_attributes)
self
end
|
#update_from_params(params) ⇒ Object
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
|
# File 'lib/chef/environment.rb', line 136
def update_from_params(params)
bkup_cb_versions = cookbook_versions
cookbook_versions(Hash.new)
valid = true
begin
name(params[:name])
rescue Chef::Exceptions::ValidationFailed => e
invalid_fields[:name] = e.message
valid = false
end
description(params[:description])
unless params[:cookbook_version].nil?
params[:cookbook_version].each do |index, cookbook_constraint_spec|
unless (cookbook_constraint_spec.nil? || cookbook_constraint_spec.size == 0)
valid = valid && update_cookbook_constraint_from_param(index, cookbook_constraint_spec)
end
end
end
update_attributes_from_params(params)
valid = validate_required_attrs_present && valid
cookbook_versions(bkup_cb_versions) unless valid
valid
end
|
#validate_required_attrs_present ⇒ Object
190
191
192
193
194
195
196
197
|
# File 'lib/chef/environment.rb', line 190
def validate_required_attrs_present
if name.nil? || name.size == 0
invalid_fields[:name] ||= "name cannot be empty"
false
else
true
end
end
|