chef-zero 4.3.0 → 4.3.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (100) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE +201 -201
  3. data/README.md +155 -155
  4. data/Rakefile +31 -31
  5. data/bin/chef-zero +100 -100
  6. data/lib/chef_zero.rb +10 -10
  7. data/lib/chef_zero/chef_data/acl_path.rb +139 -139
  8. data/lib/chef_zero/chef_data/cookbook_data.rb +240 -240
  9. data/lib/chef_zero/chef_data/data_normalizer.rb +208 -207
  10. data/lib/chef_zero/chef_data/default_creator.rb +446 -446
  11. data/lib/chef_zero/data_store/data_already_exists_error.rb +29 -29
  12. data/lib/chef_zero/data_store/data_error.rb +31 -31
  13. data/lib/chef_zero/data_store/data_not_found_error.rb +28 -28
  14. data/lib/chef_zero/data_store/default_facade.rb +149 -149
  15. data/lib/chef_zero/data_store/interface_v1.rb +67 -67
  16. data/lib/chef_zero/data_store/interface_v2.rb +18 -18
  17. data/lib/chef_zero/data_store/memory_store.rb +33 -33
  18. data/lib/chef_zero/data_store/memory_store_v2.rb +155 -155
  19. data/lib/chef_zero/data_store/raw_file_store.rb +147 -147
  20. data/lib/chef_zero/data_store/v1_to_v2_adapter.rb +142 -142
  21. data/lib/chef_zero/data_store/v2_to_v1_adapter.rb +107 -107
  22. data/lib/chef_zero/endpoints/acl_endpoint.rb +38 -38
  23. data/lib/chef_zero/endpoints/acls_endpoint.rb +29 -29
  24. data/lib/chef_zero/endpoints/actor_endpoint.rb +94 -94
  25. data/lib/chef_zero/endpoints/actors_endpoint.rb +64 -64
  26. data/lib/chef_zero/endpoints/authenticate_user_endpoint.rb +31 -31
  27. data/lib/chef_zero/endpoints/container_endpoint.rb +22 -22
  28. data/lib/chef_zero/endpoints/containers_endpoint.rb +13 -13
  29. data/lib/chef_zero/endpoints/cookbook_endpoint.rb +39 -39
  30. data/lib/chef_zero/endpoints/cookbook_version_endpoint.rb +119 -119
  31. data/lib/chef_zero/endpoints/cookbooks_base.rb +65 -65
  32. data/lib/chef_zero/endpoints/cookbooks_endpoint.rb +19 -19
  33. data/lib/chef_zero/endpoints/data_bag_endpoint.rb +45 -45
  34. data/lib/chef_zero/endpoints/data_bag_item_endpoint.rb +25 -25
  35. data/lib/chef_zero/endpoints/data_bags_endpoint.rb +23 -23
  36. data/lib/chef_zero/endpoints/environment_cookbook_endpoint.rb +24 -24
  37. data/lib/chef_zero/endpoints/environment_cookbook_versions_endpoint.rb +123 -123
  38. data/lib/chef_zero/endpoints/environment_cookbooks_endpoint.rb +22 -22
  39. data/lib/chef_zero/endpoints/environment_endpoint.rb +33 -33
  40. data/lib/chef_zero/endpoints/environment_nodes_endpoint.rb +23 -23
  41. data/lib/chef_zero/endpoints/environment_recipes_endpoint.rb +22 -22
  42. data/lib/chef_zero/endpoints/environment_role_endpoint.rb +36 -36
  43. data/lib/chef_zero/endpoints/file_store_file_endpoint.rb +22 -22
  44. data/lib/chef_zero/endpoints/group_endpoint.rb +20 -20
  45. data/lib/chef_zero/endpoints/groups_endpoint.rb +13 -13
  46. data/lib/chef_zero/endpoints/license_endpoint.rb +25 -25
  47. data/lib/chef_zero/endpoints/node_endpoint.rb +17 -17
  48. data/lib/chef_zero/endpoints/node_identifiers_endpoint.rb +22 -22
  49. data/lib/chef_zero/endpoints/not_found_endpoint.rb +11 -11
  50. data/lib/chef_zero/endpoints/organization_association_request_endpoint.rb +22 -22
  51. data/lib/chef_zero/endpoints/organization_association_requests_endpoint.rb +30 -30
  52. data/lib/chef_zero/endpoints/organization_authenticate_user_endpoint.rb +26 -26
  53. data/lib/chef_zero/endpoints/organization_endpoint.rb +46 -46
  54. data/lib/chef_zero/endpoints/organization_user_base.rb +15 -15
  55. data/lib/chef_zero/endpoints/organization_user_endpoint.rb +26 -26
  56. data/lib/chef_zero/endpoints/organization_users_endpoint.rb +43 -43
  57. data/lib/chef_zero/endpoints/organization_validator_key_endpoint.rb +20 -20
  58. data/lib/chef_zero/endpoints/organizations_endpoint.rb +62 -62
  59. data/lib/chef_zero/endpoints/policies_endpoint.rb +151 -151
  60. data/lib/chef_zero/endpoints/principal_endpoint.rb +42 -42
  61. data/lib/chef_zero/endpoints/rest_list_endpoint.rb +42 -42
  62. data/lib/chef_zero/endpoints/rest_object_endpoint.rb +63 -63
  63. data/lib/chef_zero/endpoints/role_endpoint.rb +16 -16
  64. data/lib/chef_zero/endpoints/role_environments_endpoint.rb +14 -14
  65. data/lib/chef_zero/endpoints/sandbox_endpoint.rb +27 -27
  66. data/lib/chef_zero/endpoints/sandboxes_endpoint.rb +50 -50
  67. data/lib/chef_zero/endpoints/search_endpoint.rb +194 -194
  68. data/lib/chef_zero/endpoints/searches_endpoint.rb +18 -18
  69. data/lib/chef_zero/endpoints/server_api_version_endpoint.rb +14 -14
  70. data/lib/chef_zero/endpoints/system_recovery_endpoint.rb +30 -30
  71. data/lib/chef_zero/endpoints/user_association_request_endpoint.rb +40 -40
  72. data/lib/chef_zero/endpoints/user_association_requests_count_endpoint.rb +19 -19
  73. data/lib/chef_zero/endpoints/user_association_requests_endpoint.rb +19 -19
  74. data/lib/chef_zero/endpoints/user_organizations_endpoint.rb +22 -22
  75. data/lib/chef_zero/endpoints/version_endpoint.rb +12 -12
  76. data/lib/chef_zero/log.rb +7 -7
  77. data/lib/chef_zero/rest_base.rb +242 -242
  78. data/lib/chef_zero/rest_error_response.rb +11 -11
  79. data/lib/chef_zero/rest_request.rb +69 -69
  80. data/lib/chef_zero/rest_router.rb +45 -45
  81. data/lib/chef_zero/rspec.rb +308 -308
  82. data/lib/chef_zero/server.rb +642 -642
  83. data/lib/chef_zero/socketless_server_map.rb +92 -92
  84. data/lib/chef_zero/solr/query/binary_operator.rb +52 -52
  85. data/lib/chef_zero/solr/query/phrase.rb +23 -23
  86. data/lib/chef_zero/solr/query/range_query.rb +46 -46
  87. data/lib/chef_zero/solr/query/regexpable_query.rb +29 -29
  88. data/lib/chef_zero/solr/query/subquery.rb +37 -37
  89. data/lib/chef_zero/solr/query/term.rb +45 -45
  90. data/lib/chef_zero/solr/query/unary_operator.rb +43 -43
  91. data/lib/chef_zero/solr/solr_doc.rb +53 -53
  92. data/lib/chef_zero/solr/solr_parser.rb +203 -203
  93. data/lib/chef_zero/version.rb +3 -3
  94. data/spec/run_oc_pedant.rb +63 -63
  95. data/spec/search_spec.rb +32 -32
  96. data/spec/server_spec.rb +92 -92
  97. data/spec/socketless_server_map_spec.rb +76 -76
  98. data/spec/support/oc_pedant.rb +132 -132
  99. data/spec/support/stickywicket.pem +27 -27
  100. metadata +3 -3
@@ -1,42 +1,42 @@
1
- require 'ffi_yajl'
2
- require 'chef_zero'
3
- require 'chef_zero/rest_base'
4
-
5
- module ChefZero
6
- module Endpoints
7
- # /principals/NAME
8
- class PrincipalEndpoint < RestBase
9
- def get(request)
10
- name = request.rest_path[-1]
11
- json = get_data(request, request.rest_path[0..1] + [ 'users', name ], :nil)
12
- if json
13
- type = 'user'
14
- org_member = true
15
- else
16
- json = get_data(request, request.rest_path[0..1] + [ 'clients', name ], :nil)
17
- if json
18
- type = 'client'
19
- org_member = true
20
- else
21
- json = get_data(request, [ 'users', name ], :nil)
22
- if json
23
- type = 'user'
24
- org_member = false
25
- end
26
- end
27
- end
28
- if json
29
- json_response(200, {
30
- 'name' => name,
31
- 'type' => type,
32
- 'public_key' => FFI_Yajl::Parser.parse(json)['public_key'] || PUBLIC_KEY,
33
- 'authz_id' => '0'*32,
34
- 'org_member' => org_member
35
- })
36
- else
37
- error(404, 'Principal not found')
38
- end
39
- end
40
- end
41
- end
42
- end
1
+ require 'ffi_yajl'
2
+ require 'chef_zero'
3
+ require 'chef_zero/rest_base'
4
+
5
+ module ChefZero
6
+ module Endpoints
7
+ # /principals/NAME
8
+ class PrincipalEndpoint < RestBase
9
+ def get(request)
10
+ name = request.rest_path[-1]
11
+ json = get_data(request, request.rest_path[0..1] + [ 'users', name ], :nil)
12
+ if json
13
+ type = 'user'
14
+ org_member = true
15
+ else
16
+ json = get_data(request, request.rest_path[0..1] + [ 'clients', name ], :nil)
17
+ if json
18
+ type = 'client'
19
+ org_member = true
20
+ else
21
+ json = get_data(request, [ 'users', name ], :nil)
22
+ if json
23
+ type = 'user'
24
+ org_member = false
25
+ end
26
+ end
27
+ end
28
+ if json
29
+ json_response(200, {
30
+ 'name' => name,
31
+ 'type' => type,
32
+ 'public_key' => FFI_Yajl::Parser.parse(json)['public_key'] || PUBLIC_KEY,
33
+ 'authz_id' => '0'*32,
34
+ 'org_member' => org_member
35
+ })
36
+ else
37
+ error(404, 'Principal not found')
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -1,42 +1,42 @@
1
- require 'ffi_yajl'
2
- require 'chef_zero/rest_base'
3
-
4
- module ChefZero
5
- module Endpoints
6
- # Typical REST list endpoint (/roles or /data/BAG)
7
- class RestListEndpoint < RestBase
8
- def initialize(server, identity_keys = [ 'name' ])
9
- super(server)
10
- identity_keys = [ identity_keys ] if identity_keys.is_a?(String)
11
- @identity_keys = identity_keys
12
- end
13
-
14
- attr_reader :identity_keys
15
-
16
- def get(request)
17
- # Get the result
18
- result_hash = {}
19
- list_data(request).sort.each do |name|
20
- result_hash[name] = "#{build_uri(request.base_uri, request.rest_path + [name])}"
21
- end
22
- json_response(200, result_hash)
23
- end
24
-
25
- def post(request)
26
- contents = request.body
27
- key = get_key(contents)
28
- if key.nil?
29
- error(400, "Must specify #{identity_keys.map { |k| k.inspect }.join(' or ')} in JSON")
30
- else
31
- create_data(request, request.rest_path, key, contents)
32
- json_response(201, {'uri' => "#{build_uri(request.base_uri, request.rest_path + [key])}"})
33
- end
34
- end
35
-
36
- def get_key(contents)
37
- json = FFI_Yajl::Parser.parse(contents, :create_additions => false)
38
- identity_keys.map { |k| json[k] }.select { |v| v }.first
39
- end
40
- end
41
- end
42
- end
1
+ require 'ffi_yajl'
2
+ require 'chef_zero/rest_base'
3
+
4
+ module ChefZero
5
+ module Endpoints
6
+ # Typical REST list endpoint (/roles or /data/BAG)
7
+ class RestListEndpoint < RestBase
8
+ def initialize(server, identity_keys = [ 'name' ])
9
+ super(server)
10
+ identity_keys = [ identity_keys ] if identity_keys.is_a?(String)
11
+ @identity_keys = identity_keys
12
+ end
13
+
14
+ attr_reader :identity_keys
15
+
16
+ def get(request)
17
+ # Get the result
18
+ result_hash = {}
19
+ list_data(request).sort.each do |name|
20
+ result_hash[name] = "#{build_uri(request.base_uri, request.rest_path + [name])}"
21
+ end
22
+ json_response(200, result_hash)
23
+ end
24
+
25
+ def post(request)
26
+ contents = request.body
27
+ key = get_key(contents)
28
+ if key.nil?
29
+ error(400, "Must specify #{identity_keys.map { |k| k.inspect }.join(' or ')} in JSON")
30
+ else
31
+ create_data(request, request.rest_path, key, contents)
32
+ json_response(201, {'uri' => "#{build_uri(request.base_uri, request.rest_path + [key])}"})
33
+ end
34
+ end
35
+
36
+ def get_key(contents)
37
+ json = FFI_Yajl::Parser.parse(contents, :create_additions => false)
38
+ identity_keys.map { |k| json[k] }.select { |v| v }.first
39
+ end
40
+ end
41
+ end
42
+ end
@@ -1,63 +1,63 @@
1
- require 'ffi_yajl'
2
- require 'chef_zero/rest_base'
3
- require 'chef_zero/rest_error_response'
4
-
5
- module ChefZero
6
- module Endpoints
7
- # Typical REST leaf endpoint (/roles/NAME or /data/BAG/NAME)
8
- class RestObjectEndpoint < RestBase
9
- def initialize(server, identity_keys = [ 'name' ])
10
- super(server)
11
- identity_keys = [ identity_keys ] if identity_keys.is_a?(String)
12
- @identity_keys = identity_keys
13
- end
14
-
15
- attr_reader :identity_keys
16
-
17
- def get(request)
18
- already_json_response(200, populate_defaults(request, get_data(request)))
19
- end
20
-
21
- def put(request)
22
- # We grab the old body to trigger a 404 if it doesn't exist
23
- old_body = get_data(request)
24
- request_json = FFI_Yajl::Parser.parse(request.body, :create_additions => false)
25
- key = identity_keys.map { |k| request_json[k] }.select { |v| v }.first
26
- key ||= request.rest_path[-1]
27
- # If it's a rename, check for conflict and delete the old value
28
- rename = key != request.rest_path[-1]
29
- if rename
30
- begin
31
- create_data(request, request.rest_path[0..-2], key, request.body, :data_store_exceptions)
32
- rescue DataStore::DataAlreadyExistsError
33
- return error(409, "Cannot rename '#{request.rest_path[-1]}' to '#{key}': '#{key}' already exists")
34
- end
35
- delete_data(request)
36
- already_json_response(201, populate_defaults(request, request.body))
37
- else
38
- set_data(request, request.rest_path, request.body)
39
- already_json_response(200, populate_defaults(request, request.body))
40
- end
41
- end
42
-
43
- def delete(request)
44
- result = get_data(request)
45
- delete_data(request)
46
- already_json_response(200, populate_defaults(request, result))
47
- end
48
-
49
- def patch_request_body(request)
50
- existing_value = get_data(request, nil, :nil)
51
- if existing_value
52
- request_json = FFI_Yajl::Parser.parse(request.body, :create_additions => false)
53
- existing_json = FFI_Yajl::Parser.parse(existing_value, :create_additions => false)
54
- merged_json = existing_json.merge(request_json)
55
- if merged_json.size > request_json.size
56
- return FFI_Yajl::Encoder.encode(merged_json, :pretty => true)
57
- end
58
- end
59
- request.body
60
- end
61
- end
62
- end
63
- end
1
+ require 'ffi_yajl'
2
+ require 'chef_zero/rest_base'
3
+ require 'chef_zero/rest_error_response'
4
+
5
+ module ChefZero
6
+ module Endpoints
7
+ # Typical REST leaf endpoint (/roles/NAME or /data/BAG/NAME)
8
+ class RestObjectEndpoint < RestBase
9
+ def initialize(server, identity_keys = [ 'name' ])
10
+ super(server)
11
+ identity_keys = [ identity_keys ] if identity_keys.is_a?(String)
12
+ @identity_keys = identity_keys
13
+ end
14
+
15
+ attr_reader :identity_keys
16
+
17
+ def get(request)
18
+ already_json_response(200, populate_defaults(request, get_data(request)))
19
+ end
20
+
21
+ def put(request)
22
+ # We grab the old body to trigger a 404 if it doesn't exist
23
+ old_body = get_data(request)
24
+ request_json = FFI_Yajl::Parser.parse(request.body, :create_additions => false)
25
+ key = identity_keys.map { |k| request_json[k] }.select { |v| v }.first
26
+ key ||= request.rest_path[-1]
27
+ # If it's a rename, check for conflict and delete the old value
28
+ rename = key != request.rest_path[-1]
29
+ if rename
30
+ begin
31
+ create_data(request, request.rest_path[0..-2], key, request.body, :data_store_exceptions)
32
+ rescue DataStore::DataAlreadyExistsError
33
+ return error(409, "Cannot rename '#{request.rest_path[-1]}' to '#{key}': '#{key}' already exists")
34
+ end
35
+ delete_data(request)
36
+ already_json_response(201, populate_defaults(request, request.body))
37
+ else
38
+ set_data(request, request.rest_path, request.body)
39
+ already_json_response(200, populate_defaults(request, request.body))
40
+ end
41
+ end
42
+
43
+ def delete(request)
44
+ result = get_data(request)
45
+ delete_data(request)
46
+ already_json_response(200, populate_defaults(request, result))
47
+ end
48
+
49
+ def patch_request_body(request)
50
+ existing_value = get_data(request, nil, :nil)
51
+ if existing_value
52
+ request_json = FFI_Yajl::Parser.parse(request.body, :create_additions => false)
53
+ existing_json = FFI_Yajl::Parser.parse(existing_value, :create_additions => false)
54
+ merged_json = existing_json.merge(request_json)
55
+ if merged_json.size > request_json.size
56
+ return FFI_Yajl::Encoder.encode(merged_json, :pretty => true)
57
+ end
58
+ end
59
+ request.body
60
+ end
61
+ end
62
+ end
63
+ end
@@ -1,16 +1,16 @@
1
- require 'ffi_yajl'
2
- require 'chef_zero/endpoints/rest_object_endpoint'
3
- require 'chef_zero/chef_data/data_normalizer'
4
-
5
- module ChefZero
6
- module Endpoints
7
- # /roles/NAME
8
- class RoleEndpoint < RestObjectEndpoint
9
- def populate_defaults(request, response_json)
10
- role = FFI_Yajl::Parser.parse(response_json, :create_additions => false)
11
- role = ChefData::DataNormalizer.normalize_role(role, request.rest_path[3])
12
- FFI_Yajl::Encoder.encode(role, :pretty => true)
13
- end
14
- end
15
- end
16
- end
1
+ require 'ffi_yajl'
2
+ require 'chef_zero/endpoints/rest_object_endpoint'
3
+ require 'chef_zero/chef_data/data_normalizer'
4
+
5
+ module ChefZero
6
+ module Endpoints
7
+ # /roles/NAME
8
+ class RoleEndpoint < RestObjectEndpoint
9
+ def populate_defaults(request, response_json)
10
+ role = FFI_Yajl::Parser.parse(response_json, :create_additions => false)
11
+ role = ChefData::DataNormalizer.normalize_role(role, request.rest_path[3])
12
+ FFI_Yajl::Encoder.encode(role, :pretty => true)
13
+ end
14
+ end
15
+ end
16
+ end
@@ -1,14 +1,14 @@
1
- require 'ffi_yajl'
2
- require 'chef_zero/rest_base'
3
-
4
- module ChefZero
5
- module Endpoints
6
- # /roles/NAME/environments
7
- class RoleEnvironmentsEndpoint < RestBase
8
- def get(request)
9
- role = FFI_Yajl::Parser.parse(get_data(request, request.rest_path[0..3]), :create_additions => false)
10
- json_response(200, [ '_default' ] + (role['env_run_lists'].keys || []))
11
- end
12
- end
13
- end
14
- end
1
+ require 'ffi_yajl'
2
+ require 'chef_zero/rest_base'
3
+
4
+ module ChefZero
5
+ module Endpoints
6
+ # /roles/NAME/environments
7
+ class RoleEnvironmentsEndpoint < RestBase
8
+ def get(request)
9
+ role = FFI_Yajl::Parser.parse(get_data(request, request.rest_path[0..3]), :create_additions => false)
10
+ json_response(200, [ '_default' ] + (role['env_run_lists'].keys || []))
11
+ end
12
+ end
13
+ end
14
+ end
@@ -1,27 +1,27 @@
1
- require 'chef_zero/rest_base'
2
- require 'chef_zero/rest_error_response'
3
- require 'ffi_yajl'
4
-
5
- module ChefZero
6
- module Endpoints
7
- # /sandboxes/ID
8
- class SandboxEndpoint < RestBase
9
- def put(request)
10
- existing_sandbox = FFI_Yajl::Parser.parse(get_data(request), :create_additions => false)
11
- existing_sandbox['checksums'].each do |checksum|
12
- if !exists_data?(request, request.rest_path[0..1] + ['file_store', 'checksums', checksum])
13
- raise RestErrorResponse.new(503, "Checksum not uploaded: #{checksum}")
14
- end
15
- end
16
- delete_data(request)
17
- json_response(200, {
18
- :guid => request.rest_path[3],
19
- :name => request.rest_path[3],
20
- :checksums => existing_sandbox['checksums'],
21
- :create_time => existing_sandbox['create_time'],
22
- :is_completed => true
23
- })
24
- end
25
- end
26
- end
27
- end
1
+ require 'chef_zero/rest_base'
2
+ require 'chef_zero/rest_error_response'
3
+ require 'ffi_yajl'
4
+
5
+ module ChefZero
6
+ module Endpoints
7
+ # /sandboxes/ID
8
+ class SandboxEndpoint < RestBase
9
+ def put(request)
10
+ existing_sandbox = FFI_Yajl::Parser.parse(get_data(request), :create_additions => false)
11
+ existing_sandbox['checksums'].each do |checksum|
12
+ if !exists_data?(request, request.rest_path[0..1] + ['file_store', 'checksums', checksum])
13
+ raise RestErrorResponse.new(503, "Checksum not uploaded: #{checksum}")
14
+ end
15
+ end
16
+ delete_data(request)
17
+ json_response(200, {
18
+ :guid => request.rest_path[3],
19
+ :name => request.rest_path[3],
20
+ :checksums => existing_sandbox['checksums'],
21
+ :create_time => existing_sandbox['create_time'],
22
+ :is_completed => true
23
+ })
24
+ end
25
+ end
26
+ end
27
+ end
@@ -1,50 +1,50 @@
1
- require 'ffi_yajl'
2
- require 'chef_zero/rest_base'
3
-
4
- module ChefZero
5
- module Endpoints
6
- # /sandboxes
7
- class SandboxesEndpoint < RestBase
8
- def initialize(server)
9
- super(server)
10
- @next_id = 1
11
- end
12
-
13
- def post(request)
14
- sandbox_checksums = []
15
-
16
- needed_checksums = FFI_Yajl::Parser.parse(request.body, :create_additions => false)['checksums']
17
- result_checksums = {}
18
- needed_checksums.keys.each do |needed_checksum|
19
- if list_data(request, request.rest_path[0..1] + ['file_store', 'checksums']).include?(needed_checksum)
20
- result_checksums[needed_checksum] = { :needs_upload => false }
21
- else
22
- result_checksums[needed_checksum] = {
23
- :needs_upload => true,
24
- :url => build_uri(request.base_uri, request.rest_path[0..1] + ['file_store', 'checksums', needed_checksum])
25
- }
26
- sandbox_checksums << needed_checksum
27
- end
28
- end
29
-
30
- # There is an obvious race condition here.
31
- id = @next_id.to_s
32
- @next_id+=1
33
-
34
- time_str = Time.now.utc.strftime('%Y-%m-%dT%H:%M:%S%z')
35
- time_str = "#{time_str[0..21]}:#{time_str[22..23]}"
36
-
37
- create_data(request, request.rest_path, id, FFI_Yajl::Encoder.encode({
38
- :create_time => time_str,
39
- :checksums => sandbox_checksums
40
- }, :pretty => true))
41
-
42
- json_response(201, {
43
- :uri => build_uri(request.base_uri, request.rest_path + [id]),
44
- :checksums => result_checksums,
45
- :sandbox_id => id
46
- })
47
- end
48
- end
49
- end
50
- end
1
+ require 'ffi_yajl'
2
+ require 'chef_zero/rest_base'
3
+
4
+ module ChefZero
5
+ module Endpoints
6
+ # /sandboxes
7
+ class SandboxesEndpoint < RestBase
8
+ def initialize(server)
9
+ super(server)
10
+ @next_id = 1
11
+ end
12
+
13
+ def post(request)
14
+ sandbox_checksums = []
15
+
16
+ needed_checksums = FFI_Yajl::Parser.parse(request.body, :create_additions => false)['checksums']
17
+ result_checksums = {}
18
+ needed_checksums.keys.each do |needed_checksum|
19
+ if list_data(request, request.rest_path[0..1] + ['file_store', 'checksums']).include?(needed_checksum)
20
+ result_checksums[needed_checksum] = { :needs_upload => false }
21
+ else
22
+ result_checksums[needed_checksum] = {
23
+ :needs_upload => true,
24
+ :url => build_uri(request.base_uri, request.rest_path[0..1] + ['file_store', 'checksums', needed_checksum])
25
+ }
26
+ sandbox_checksums << needed_checksum
27
+ end
28
+ end
29
+
30
+ # There is an obvious race condition here.
31
+ id = @next_id.to_s
32
+ @next_id+=1
33
+
34
+ time_str = Time.now.utc.strftime('%Y-%m-%dT%H:%M:%S%z')
35
+ time_str = "#{time_str[0..21]}:#{time_str[22..23]}"
36
+
37
+ create_data(request, request.rest_path, id, FFI_Yajl::Encoder.encode({
38
+ :create_time => time_str,
39
+ :checksums => sandbox_checksums
40
+ }, :pretty => true))
41
+
42
+ json_response(201, {
43
+ :uri => build_uri(request.base_uri, request.rest_path + [id]),
44
+ :checksums => result_checksums,
45
+ :sandbox_id => id
46
+ })
47
+ end
48
+ end
49
+ end
50
+ end