Class: ChefDK::Policyfile::Lister

Inherits:
Object
  • Object
show all
Defined in:
lib/chef-dk/policyfile/lister.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config: nil) ⇒ Lister

Returns a new instance of Lister.



114
115
116
117
118
119
120
# File 'lib/chef-dk/policyfile/lister.rb', line 114

def initialize(config: nil)
  @config = config
  @policies_by_name = nil
  @policies_by_group = nil
  @policy_lock_content = {}
  @active_revisions = nil
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



112
113
114
# File 'lib/chef-dk/policyfile/lister.rb', line 112

def config
  @config
end

#policy_lock_contentObject

Returns the value of attribute policy_lock_content.



110
111
112
# File 'lib/chef-dk/policyfile/lister.rb', line 110

def policy_lock_content
  @policy_lock_content
end

Instance Method Details

#active_revisionsObject



171
172
173
174
175
176
177
178
# File 'lib/chef-dk/policyfile/lister.rb', line 171

def active_revisions
  @active_revisions ||= policies_by_group.inject(Set.new) do |set, (_group, policy_name_rev_id_map)|
    policy_name_rev_id_map.each do |policy_name, rev_id|
      set << rev_id
    end
    set
  end
end

#empty?Boolean

Returns:

  • (Boolean)


180
181
182
# File 'lib/chef-dk/policyfile/lister.rb', line 180

def empty?
  policies_by_name.empty? && policies_by_group.empty?
end

#http_clientObject



184
185
186
187
188
# File 'lib/chef-dk/policyfile/lister.rb', line 184

def http_client
  @http_client ||= ChefDK::AuthenticatedHTTP.new(config.chef_server_url,
                                                 signing_key_filename: config.client_key,
                                                 client_name: config.node_name)
end

#orphaned_revisions(policy_name) ⇒ Object



163
164
165
166
167
168
169
# File 'lib/chef-dk/policyfile/lister.rb', line 163

def orphaned_revisions(policy_name)
  orphans = []
  policies_by_name[policy_name].each do |rev_id, _data|
    orphans << rev_id unless active_revisions.include?(rev_id)
  end
  orphans
end

#policies_by_groupObject

A Hash with the following format:

"dev" => {
  "appserver" => "1111111111111111111111111111111111111111111111111111111111111111",
  "load-balancer" => "5555555555555555555555555555555555555555555555555555555555555555",
  "db" => "9999999999999999999999999999999999999999999999999999999999999999"
}


139
140
141
142
# File 'lib/chef-dk/policyfile/lister.rb', line 139

def policies_by_group
  @policies_by_group || fetch_policy_lists
  @policies_by_group
end

#policies_by_nameObject

A Hash with the following format

{
  "appserver" => {
    "1111111111111111111111111111111111111111111111111111111111111111" => {},
    "2222222222222222222222222222222222222222222222222222222222222222" => {}
  },


128
129
130
131
# File 'lib/chef-dk/policyfile/lister.rb', line 128

def policies_by_name
  @policies_by_name || fetch_policy_lists
  @policies_by_name
end

#revision_ids_by_group_for(policy_name) ⇒ Object



155
156
157
158
159
160
161
# File 'lib/chef-dk/policyfile/lister.rb', line 155

def revision_ids_by_group_for(policy_name)
  map = policies_by_group.inject({}) do |rev_id_map, (group_name, rev_id_map_for_group)|
    rev_id_map[group_name] = rev_id_map_for_group[policy_name]
    rev_id_map
  end
  PolicyGroupRevIDMap.new(policy_name, map)
end

#revision_ids_by_group_for_each_policyObject



148
149
150
151
152
153
# File 'lib/chef-dk/policyfile/lister.rb', line 148

def revision_ids_by_group_for_each_policy
  policies_by_name.each do |policy_name, _policies|
    rev_id_by_group = revision_ids_by_group_for(policy_name)
    yield policy_name, rev_id_by_group
  end
end

#revision_info_for(policy_name, _revision_id_list) ⇒ Object



144
145
146
# File 'lib/chef-dk/policyfile/lister.rb', line 144

def revision_info_for(policy_name, _revision_id_list)
  RevIDLockDataMap.new(policy_name, policy_lock_content[policy_name])
end

#set!(policies_by_name, policies_by_group) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Sets internal copy of policyfile data to policies_by_name and policies_by_group. Used for internal testing.



193
194
195
196
197
# File 'lib/chef-dk/policyfile/lister.rb', line 193

def set!(policies_by_name, policies_by_group)
  @policies_by_name = policies_by_name
  @policies_by_group = policies_by_group
  @active_revisions = nil
end