Class: Chef::Resource::ChefDataBagResource

Inherits:
LWRPBase
  • Object
show all
Defined in:
lib/chef/resource/chef_data_bag_resource.rb

Overview

A resource that is backed by a data bag in a Chef server somewhere

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, run_context = nil) ⇒ ChefDataBagResource

Returns a new instance of ChefDataBagResource.



15
16
17
18
19
# File 'lib/chef/resource/chef_data_bag_resource.rb', line 15

def initialize(name, run_context=nil)
  super
  Chef::Log.debug("Re-hydrating #{name} from #{self.class.databag_name}...")
  self.hydrate
end

Class Attribute Details

.databag_nameObject

The name of the databag to store the item in.



12
13
14
# File 'lib/chef/resource/chef_data_bag_resource.rb', line 12

def databag_name
  @databag_name
end

Instance Attribute Details

#nameObject (readonly)

The key to store this thing under (/data/bag/<<name>>).



8
9
10
# File 'lib/chef/resource/chef_data_bag_resource.rb', line 8

def name
  @name
end

Class Method Details

.stored_attribute(attr_name, *args) ⇒ Void

Mark an attribute as stored by adding it to the internal tracking list stored_attributes and then delegating to LWRPBase#attribute

Parameters:

  • attr_name (Symbol)

    Name of the attribute as a symbol

Returns:

  • (Void)


38
39
40
41
42
# File 'lib/chef/resource/chef_data_bag_resource.rb', line 38

def self.stored_attribute(attr_name, *args)
  @stored_attributes ||= []
  @stored_attributes << attr_name
  self.attribute attr_name, *args
end

.stored_attributesArray

A list of attributes to be persisted into the databag.

Returns:

  • (Array)

    List of attributes that are stored in the databag



23
24
25
# File 'lib/chef/resource/chef_data_bag_resource.rb', line 23

def self.stored_attributes
  @stored_attributes || []
end

Instance Method Details

#deleteVoid

Delete this entity from the server

Returns:

  • (Void)


118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/chef/resource/chef_data_bag_resource.rb', line 118

def delete
  # Clone for inline_resource
  _name = self.name
  _databag_name = self.class.databag_name

  Cheffish.inline_resource(self, @action) do
    chef_data_bag_item _name do
      data_bag _databag_name
      action :delete
    end
  end
end

#hydrate(chef_server = Cheffish.default_chef_server) ⇒ Object

Load persisted data from the server’s databag. If the databag does not exist on the server, returns nil.

databag.

Parameters:

  • chef_server (Hash) (defaults to: Cheffish.default_chef_server)

    A hash representing which Chef server to talk to

  • options (Hash)

    a customizable set of options

Options Hash (chef_server):

  • :chef_server_url (String)

    URL to the Chef server

  • :options (Hash)

    Options for when talking to the Chef server

Returns:

  • (Object)

    an instance of this class re-hydrated from the data hash stored in the



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/chef/resource/chef_data_bag_resource.rb', line 54

def hydrate(chef_server = Cheffish.default_chef_server)
  chef_api = Cheffish.chef_server_api(chef_server)
  begin
    data = chef_api.get("/data/#{self.class.databag_name}/#{name}")
    load_from_hash(data)
    Chef::Log.debug("Rehydrating resource from #{self.class.databag_name}/#{name}: #{data}")
  rescue Net::HTTPServerException => e
    if e.response.code == '404'
      nil
    else
      raise
    end
  end
end

#load_from_hash(hash) ⇒ Object

Load instance variable data from a hash. For each key,value pair, set @<key> to value

Parameters:

  • hash (Hash)

    Hash containing the instance variable data

Returns:

  • (Object)

    self after having been populated with data.



72
73
74
75
76
77
# File 'lib/chef/resource/chef_data_bag_resource.rb', line 72

def load_from_hash hash
  hash.each do |k,v|
    self.instance_variable_set("@#{k}", v)
  end
  self
end

#new_resourceObject



131
132
133
# File 'lib/chef/resource/chef_data_bag_resource.rb', line 131

def new_resource
  self
end

#saveVoid

Save this entity to the server. If you have significant information that could be lost, you should do this as quickly as possible.

Returns:

  • (Void)


98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/chef/resource/chef_data_bag_resource.rb', line 98

def save

  create_databag_if_needed self.class.databag_name

  # Clone for inline_resource
  _databag_name = self.class.databag_name
  _hash = self.storage_hash
  _name = self.name

  Cheffish.inline_resource(self, @action) do
    chef_data_bag_item _name do
      data_bag _databag_name
      raw_data _hash
      action :create
    end
  end
end

#storage_hashHash

Convert the values in stored_attributes to a hash for storing in a databag

Returns:



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/chef/resource/chef_data_bag_resource.rb', line 81

def storage_hash
  ignored = []

  hash = {}
  (self.class.stored_attributes - ignored).each do |attr_name|
    varname = "@#{attr_name.to_s.gsub('@', '')}"
    key = varname.gsub('@', '')
    hash[key] = self.instance_variable_get varname
  end

  hash
end