Class: ChefZero::Endpoints::CookbookVersionEndpoint

Inherits:
RestObjectEndpoint show all
Defined in:
lib/chef_zero/endpoints/cookbook_version_endpoint.rb

Overview

/organizations/ORG/cookbooks/NAME/VERSION

Instance Attribute Summary

Attributes inherited from RestObjectEndpoint

#identity_keys

Attributes inherited from RestBase

#server

Instance Method Summary collapse

Methods inherited from RestObjectEndpoint

#initialize, #patch_request_body

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, #set_data, #text_response

Constructor Details

This class inherits a constructor from ChefZero::Endpoints::RestObjectEndpoint

Instance Method Details

#delete(request) ⇒ Object


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/chef_zero/endpoints/cookbook_version_endpoint.rb', line 53

def delete(request)
  if request.rest_path[4] == "_latest" || request.rest_path[4] == "latest"
    request.rest_path[4] = latest_version(list_data(request, request.rest_path[0..3]))
  end

  deleted_cookbook = get_data(request)

  response = super(request)
  cookbook_name = request.rest_path[3]
  cookbook_path = request.rest_path[0..1] + ['cookbooks', cookbook_name]
  if exists_data_dir?(request, cookbook_path) && list_data(request, cookbook_path).size == 0
    delete_data_dir(request, cookbook_path)
  end

  # Hoover deleted files, if they exist
  hoover_unused_checksums(get_checksums(deleted_cookbook), request)
  response
end

#get(request) ⇒ Object


11
12
13
14
15
16
# File 'lib/chef_zero/endpoints/cookbook_version_endpoint.rb', line 11

def get(request)
  if request.rest_path[4] == "_latest" || request.rest_path[4] == "latest"
    request.rest_path[4] = latest_version(list_data(request, request.rest_path[0..3]))
  end
  super(request)
end

#get_checksums(cookbook) ⇒ Object


72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/chef_zero/endpoints/cookbook_version_endpoint.rb', line 72

def get_checksums(cookbook)
  result = []
  FFI_Yajl::Parser.parse(cookbook, :create_additions => false).each_pair do |key, value|
    if value.is_a?(Array)
      value.each do |file|
        if file.is_a?(Hash) && file.has_key?('checksum')
          result << file['checksum']
        end
      end
    end
  end
  result.uniq
end

#put(request) ⇒ Object


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/chef_zero/endpoints/cookbook_version_endpoint.rb', line 18

def put(request)
  name = request.rest_path[3]
  version = request.rest_path[4]
  existing_cookbook = get_data(request, request.rest_path, :nil)

  # Honor frozen
  if existing_cookbook
    existing_cookbook_json = FFI_Yajl::Parser.parse(existing_cookbook, :create_additions => false)
    if existing_cookbook_json['frozen?']
      if request.query_params['force'] != "true"
        raise RestErrorResponse.new(409, "The cookbook #{name} at version #{version} is frozen. Use the 'force' option to override.")
      end
      # For some reason, you are forever unable to modify "frozen?" on a frozen cookbook.
      request_body = FFI_Yajl::Parser.parse(request.body, :create_additions => false)
      if !request_body['frozen?']
        request_body['frozen?'] = true
        request.body = FFI_Yajl::Encoder.encode(request_body, :pretty => true)
      end
    end
  end

  # Set the cookbook
  set_data(request, request.rest_path, request.body, :create_dir, :create)

  # If the cookbook was updated, check for deleted files and clean them up
  if existing_cookbook
    missing_checksums = get_checksums(existing_cookbook) - get_checksums(request.body)
    if missing_checksums.size > 0
      hoover_unused_checksums(missing_checksums, request)
    end
  end

  already_json_response(existing_cookbook ? 200 : 201, populate_defaults(request, request.body))
end