Class: Chef::Provisioning::ChefManagedEntryStore

Inherits:
ManagedEntryStore show all
Defined in:
lib/chef/provisioning/chef_managed_entry_store.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ManagedEntryStore

#delete, #get, #get!, #get_or_new, #new_entry

Constructor Details

#initialize(chef_server = Cheffish.default_chef_server) ⇒ ChefManagedEntryStore

Returns a new instance of ChefManagedEntryStore.



7
8
9
# File 'lib/chef/provisioning/chef_managed_entry_store.rb', line 7

def initialize(chef_server = Cheffish.default_chef_server)
  @chef_server = chef_server
end

Instance Attribute Details

#chef_serverObject (readonly)

Returns the value of attribute chef_server.



11
12
13
# File 'lib/chef/provisioning/chef_managed_entry_store.rb', line 11

def chef_server
  @chef_server
end

Class Method Details

.type_names_for_backcompatObject

A list of the name that we used to use to store a given type before we standardized on “just use the resource name so we don’t create collisions.” Will be used to look up the old data.



123
124
125
# File 'lib/chef/provisioning/chef_managed_entry_store.rb', line 123

def self.type_names_for_backcompat
  @@type_names_for_backcompat ||= {}
end

Instance Method Details

#chef_apiObject



13
14
15
# File 'lib/chef/provisioning/chef_managed_entry_store.rb', line 13

def chef_api
  @chef_api ||= Cheffish.chef_server_api(chef_server)
end

#delete_data(resource_type, name, action_handler) ⇒ Boolean

Delete the given data

Parameters:

  • resource_type (Symbol)

    The type of thing to delete (:machine, :machine_image, :load_balancer, :aws_vpc, :aws_subnet, …)

  • name (String)

    The unique identifier of the thing to delete

Returns:

  • (Boolean)

    Whether anything was deleted or not.



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/chef/provisioning/chef_managed_entry_store.rb', line 87

def delete_data(resource_type, name, action_handler)
  _chef_server = self.chef_server
  Chef::Provisioning.inline_resource(action_handler) do
    if resource_type == :machine
      chef_node name do
        chef_server _chef_server
        action :delete
      end
    else
      chef_data_bag_item name do
        chef_server _chef_server
        data_bag resource_type.to_s
        action :delete
      end
    end
  end

  backcompat_type = ChefManagedEntryStore.type_names_for_backcompat[resource_type]
  if backcompat_type && backcompat_type != resource_type
    delete_data(backcompat_type, name, action_handler)
  end
end

#get_data(resource_type, name) ⇒ Hash, Array

Get the given data

Parameters:

  • resource_type (Symbol)

    The type of thing to retrieve (:machine, :machine_image, :load_balancer, :aws_vpc, :aws_subnet, …)

  • name (String)

    The unique identifier of the thing to retrieve

Returns:

  • (Hash, Array)

    The data, or ‘nil` if the data does not exist. Will be JSON- and YAML-compatible (Hash, Array, String, Integer, Boolean, Nil)



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/chef/provisioning/chef_managed_entry_store.rb', line 25

def get_data(resource_type, name)
  begin
    if resource_type == :machine
      chef_api.get("nodes/#{name}")
    else
      chef_api.get("data/#{resource_type}/#{name}")
    end
  rescue Net::HTTPServerException => e
    if e.response.code == '404'
      backcompat_type = ChefManagedEntryStore.type_names_for_backcompat[resource_type]
      if backcompat_type && backcompat_type != resource_type
        get_data(backcompat_type, name)
      else
        nil
      end
    else
      raise
    end
  end
end

#identifier(resource_type, name) ⇒ Object



110
111
112
113
114
115
116
# File 'lib/chef/provisioning/chef_managed_entry_store.rb', line 110

def identifier(resource_type, name)
  if resource_type == :machine
    File.join(chef_server[:chef_server_url], "nodes", name)
  else
    File.join(chef_server[:chef_server_url], "data", resource_type.to_s, name)
  end
end

#save_data(resource_type, name, data, action_handler) ⇒ Object

Save the given data

Parameters:

  • resource_type (Symbol)

    The type of thing to save (:machine, :machine_image, :load_balancer, :aws_vpc, :aws_subnet …)

  • name (String)

    The unique identifier of the thing to save

  • data (Hash, Array)

    The data to save. Must be JSON- and YAML-compatible (Hash, Array, String, Integer, Boolean, Nil)



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
# File 'lib/chef/provisioning/chef_managed_entry_store.rb', line 53

def save_data(resource_type, name, data, action_handler)
  _chef_server = self.chef_server
  Chef::Provisioning.inline_resource(action_handler) do
    if resource_type == :machine
      chef_node name do
        chef_server _chef_server
        raw_json data
      end
    else
      chef_data_bag resource_type.to_s do
        chef_server _chef_server
      end
      chef_data_bag_item name do
        chef_server _chef_server
        data_bag resource_type.to_s
        raw_data data
      end
    end
  end

  backcompat_type = ChefManagedEntryStore.type_names_for_backcompat[resource_type]
  if backcompat_type && backcompat_type != resource_type
    delete_data(backcompat_type, name, action_handler)
  end
end