Class: ChefZero::Endpoints::SandboxesEndpoint

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

Overview

/sandboxes

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, #json_only, #json_response, #list_data, #populate_defaults, #set_data, #text_response

Constructor Details

#initialize(server) ⇒ SandboxesEndpoint

Returns a new instance of SandboxesEndpoint


8
9
10
11
# File 'lib/chef_zero/endpoints/sandboxes_endpoint.rb', line 8

def initialize(server)
  super(server)
  @next_id = 1
end

Instance Method Details

#post(request) ⇒ Object


13
14
15
16
17
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
# File 'lib/chef_zero/endpoints/sandboxes_endpoint.rb', line 13

def post(request)
  sandbox_checksums = []

  needed_checksums = FFI_Yajl::Parser.parse(request.body, :create_additions => false)['checksums']
  result_checksums = {}
  needed_checksums.keys.each do |needed_checksum|
    if list_data(request, request.rest_path[0..1] + ['file_store', 'checksums']).include?(needed_checksum)
      result_checksums[needed_checksum] = { :needs_upload => false }
    else
      result_checksums[needed_checksum] = {
        :needs_upload => true,
        :url => build_uri(request.base_uri, request.rest_path[0..1] + ['file_store', 'checksums', needed_checksum])
      }
      sandbox_checksums << needed_checksum
    end
  end

  # There is an obvious race condition here.
  id = @next_id.to_s
  @next_id+=1

  time_str = Time.now.utc.strftime('%Y-%m-%dT%H:%M:%S%z')
  time_str = "#{time_str[0..21]}:#{time_str[22..23]}"

  create_data(request, request.rest_path, id, FFI_Yajl::Encoder.encode({
    :create_time => time_str,
    :checksums => sandbox_checksums
  }, :pretty => true))

  json_response(201, {
    :uri => build_uri(request.base_uri, request.rest_path + [id]),
    :checksums => result_checksums,
    :sandbox_id => id
  })
end