Parent

Files

Class/Module Index [+]

Quicksearch

Chef::Environment

Public Class Methods

chef_server_rest() click to toggle source
# File lib/chef/environment.rb, line 49
def self.chef_server_rest
  Chef::REST.new(Chef::Config[:chef_server_url])
end
json_create(o) click to toggle source
# File lib/chef/environment.rb, line 216
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
list(inflate=false) click to toggle source
# File lib/chef/environment.rb, line 226
def self.list(inflate=false)
  if inflate
    response = Hash.new
    Chef::Search::Query.new.search(:environment) do |e|
      response[e.name] = e unless e.nil?
    end
    response
  else
    chef_server_rest.get_rest("environments")
  end
end
load(name) click to toggle source
# File lib/chef/environment.rb, line 238
def self.load(name)
  chef_server_rest.get_rest("environments/#{name}")
end
load_filtered_recipe_list(environment) click to toggle source
# File lib/chef/environment.rb, line 261
def self.load_filtered_recipe_list(environment)
  chef_server_rest.get_rest("environments/#{environment}/recipes")
end
new() click to toggle source
# File lib/chef/environment.rb, line 37
def initialize
  @name = ''
  @description = ''
  @default_attributes = Mash.new
  @override_attributes = Mash.new
  @cookbook_versions = Hash.new
end
validate_cookbook_version(version) click to toggle source
# File lib/chef/environment.rb, line 277
def self.validate_cookbook_version(version)
  begin
    Chef::VersionConstraint.new version
    true
  rescue ArgumentError
    false
  end
end
validate_cookbook_versions(cv) click to toggle source
# File lib/chef/environment.rb, line 269
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

Public Instance Methods

add_cookbook_constraint_error(index, cookbook_constraint_spec) click to toggle source
# File lib/chef/environment.rb, line 197
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
chef_server_rest() click to toggle source
# File lib/chef/environment.rb, line 45
def chef_server_rest
  Chef::REST.new(Chef::Config[:chef_server_url])
end
cookbook(cookbook, version) click to toggle source
# File lib/chef/environment.rb, line 106
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) click to toggle source
# File lib/chef/environment.rb, line 93
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
create() click to toggle source
# File lib/chef/environment.rb, line 256
def create
  chef_server_rest.post_rest("environments", self)
  self
end
default_attributes(arg=nil) click to toggle source
# File lib/chef/environment.rb, line 69
def default_attributes(arg=nil)
  set_or_return(
    :default_attributes,
    arg,
    :kind_of => Hash
  )
end
default_attributes=(attrs) click to toggle source
# File lib/chef/environment.rb, line 77
def default_attributes=(attrs)
  default_attributes(attrs)
end
description(arg=nil) click to toggle source
# File lib/chef/environment.rb, line 61
def description(arg=nil)
  set_or_return(
    :description,
    arg,
    :kind_of => String
  )
end
destroy() click to toggle source
# File lib/chef/environment.rb, line 242
def destroy
  chef_server_rest.delete_rest("environments/#{@name}")
end
invalid_fields() click to toggle source
# File lib/chef/environment.rb, line 202
def invalid_fields
  @invalid_fields ||= {}
end
name(arg=nil) click to toggle source
# File lib/chef/environment.rb, line 53
def name(arg=nil)
  set_or_return(
    :name,
    arg,
    { :regex => /^[\-[:alnum:]_]+$/, :kind_of => String }
  )
end
override_attributes(arg=nil) click to toggle source
# File lib/chef/environment.rb, line 81
def override_attributes(arg=nil)
  set_or_return(
    :override_attributes,
    arg,
    :kind_of => Hash
  )
end
override_attributes=(attrs) click to toggle source
# File lib/chef/environment.rb, line 89
def override_attributes=(attrs)
  override_attributes(attrs)
end
save() click to toggle source
# File lib/chef/environment.rb, line 246
def save
  begin
    chef_server_rest.put_rest("environments/#{@name}", self)
  rescue Net::HTTPServerException => e
    raise e unless e.response.code == "404"
    chef_server_rest.post_rest("environments", self)
  end
  self
end
to_hash() click to toggle source
# File lib/chef/environment.rb, line 117
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) click to toggle source
# File lib/chef/environment.rb, line 130
def to_json(*a)
  to_hash.to_json(*a)
end
to_s() click to toggle source
# File lib/chef/environment.rb, line 265
def to_s
  @name
end
update_attributes_from_params(params) click to toggle source
# File lib/chef/environment.rb, line 143
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) click to toggle source
# File lib/chef/environment.rb, line 182
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) click to toggle source
# File lib/chef/environment.rb, line 134
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) click to toggle source
# File lib/chef/environment.rb, line 152
def update_from_params(params)
  # reset because everything we need will be in the params, this is necessary because certain constraints
  # may have been removed in the params and need to be removed from cookbook_versions as well.
  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 # restore the old cookbook_versions if valid is false
  valid
end
validate_required_attrs_present() click to toggle source
# File lib/chef/environment.rb, line 206
def validate_required_attrs_present
  if name.nil? || name.size == 0
    invalid_fields[:name] ||= "name cannot be empty"
    false
  else
    true
  end
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.