Class: ChefZero::Endpoints::CookbooksBase

Inherits:
RestBase
  • Object
show all
Defined in:
lib/chef_zero/endpoints/cookbooks_base.rb

Overview

Common code for endpoints that return cookbook lists

Instance Attribute Summary

Attributes inherited from RestBase

#server

Instance Method Summary collapse

Methods inherited from RestBase

#accepts?, #already_json_response, #build_uri, build_uri, #call, #create_data, #create_data_dir, #data_store, #delete_data, #delete_data_dir, #error, #exists_data?, #exists_data_dir?, #get_data, #initialize, #json_only, #json_response, #list_data, #populate_defaults, #set_data, #text_response

Constructor Details

This class inherits a constructor from ChefZero::RestBase

Instance Method Details

#all_cookbooks_list(request) ⇒ Object


26
27
28
29
30
31
32
33
# File 'lib/chef_zero/endpoints/cookbooks_base.rb', line 26

def all_cookbooks_list(request)
  result = {}
  # Race conditions exist here (if someone deletes while listing).  I don't care.
  data_store.list(request.rest_path[0..1] + ['cookbooks']).each do |name|
    result[name] = data_store.list(request.rest_path[0..1] + ['cookbooks', name])
  end
  result
end

#filter_cookbooks(cookbooks_list, constraints = {}, num_versions = nil) ⇒ Object


35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/chef_zero/endpoints/cookbooks_base.rb', line 35

def filter_cookbooks(cookbooks_list, constraints = {}, num_versions = nil)
  cookbooks_list.keys.sort.each do |name|
    constraint = Gem::Requirement.new(constraints[name])
    versions = []
    cookbooks_list[name].sort_by { |version| Gem::Version.new(version.dup) }.reverse.each do |version|
      break if num_versions && versions.size >= num_versions
      if constraint.satisfied_by?(Gem::Version.new(version.dup))
        versions << version
      end
    end
    yield [name, versions]
  end
end

#format_cookbooks_list(request, cookbooks_list, constraints = {}, num_versions = nil) ⇒ Object


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/chef_zero/endpoints/cookbooks_base.rb', line 9

def format_cookbooks_list(request, cookbooks_list, constraints = {}, num_versions = nil)
  results = {}
  filter_cookbooks(cookbooks_list, constraints, num_versions) do |name, versions|
    versions_list = versions.map do |version|
      {
        'url' => build_uri(request.base_uri, request.rest_path[0..1] + ['cookbooks', name, version]),
        'version' => version
      }
    end
    results[name] = {
      'url' => build_uri(request.base_uri, request.rest_path[0..1] + ['cookbooks', name]),
      'versions' => versions_list
    }
  end
  results
end

#recipe_names(cookbook_name, cookbook) ⇒ Object


49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/chef_zero/endpoints/cookbooks_base.rb', line 49

def recipe_names(cookbook_name, cookbook)
  result = []
  if cookbook['recipes']
    cookbook['recipes'].each do |recipe|
      if recipe['path'] == "recipes/#{recipe['name']}" && recipe['name'][-3..-1] == '.rb'
        if recipe['name'] == 'default.rb'
          result << cookbook_name
        end
        result << "#{cookbook_name}::#{recipe['name'][0..-4]}"
      end
    end
  end
  result
end