chef 12.9.41 → 12.10.24

Sign up to get free protection for your applications and to get access to all the features.
Files changed (132) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +52 -13
  3. data/README.md +145 -0
  4. data/Rakefile +4 -14
  5. data/VERSION +1 -1
  6. data/acceptance/.gitignore +0 -1
  7. data/acceptance/Gemfile +2 -2
  8. data/acceptance/Gemfile.lock +235 -0
  9. data/acceptance/fips/.kitchen.yml +5 -1
  10. data/acceptance/fips/test/integration/{fips → fips-integration}/serverspec/Gemfile +0 -0
  11. data/acceptance/fips/test/integration/fips-integration/serverspec/fips-integration_spec.rb +51 -0
  12. data/acceptance/fips/test/integration/fips-unit-functional/serverspec/Gemfile +3 -0
  13. data/acceptance/fips/test/integration/fips-unit-functional/serverspec/fips-unit-functional_spec.rb +56 -0
  14. data/{chef-windows.gemspec → chef-universal-mingw32.gemspec} +0 -0
  15. data/chef.gemspec +0 -6
  16. data/{lib → lib-backcompat}/chef/chef_fs/file_system/already_exists_error.rb +0 -0
  17. data/{lib → lib-backcompat}/chef/chef_fs/file_system/cookbook_frozen_error.rb +0 -0
  18. data/{lib → lib-backcompat}/chef/chef_fs/file_system/default_environment_cannot_be_modified_error.rb +0 -0
  19. data/{lib → lib-backcompat}/chef/chef_fs/file_system/file_system_error.rb +0 -0
  20. data/{lib → lib-backcompat}/chef/chef_fs/file_system/must_delete_recursively_error.rb +0 -0
  21. data/{lib → lib-backcompat}/chef/chef_fs/file_system/not_found_error.rb +0 -0
  22. data/{lib → lib-backcompat}/chef/chef_fs/file_system/operation_failed_error.rb +0 -0
  23. data/{lib → lib-backcompat}/chef/chef_fs/file_system/operation_not_allowed_error.rb +0 -0
  24. data/lib-backcompat/chef/chef_fs/file_system/repository/chef_repository_file_system_acls_dir.rb +5 -0
  25. data/lib-backcompat/chef/chef_fs/file_system/repository/chef_repository_file_system_client_keys_dir.rb +5 -0
  26. data/lib-backcompat/chef/chef_fs/file_system/repository/chef_repository_file_system_entry.rb +6 -0
  27. data/lib-backcompat/chef/chef_fs/file_system/repository/chef_repository_file_system_policies_dir.rb +5 -0
  28. data/{lib → lib-backcompat}/chef/chef_fs/file_system/repository/file_system_root_dir.rb +1 -0
  29. data/lib/chef/chef_fs/file_system/repository/acl.rb +38 -0
  30. data/lib/chef/chef_fs/file_system/repository/{chef_repository_file_system_acls_dir.rb → acls_dir.rb} +14 -5
  31. data/lib/chef/chef_fs/file_system/repository/acls_sub_dir.rb +42 -0
  32. data/lib/chef/chef_fs/file_system/repository/base_file.rb +120 -0
  33. data/lib/chef/chef_fs/file_system/repository/chef_repository_file_system_cookbook_entry.rb +1 -1
  34. data/lib/chef/chef_fs/file_system/repository/chef_repository_file_system_root_dir.rb +36 -32
  35. data/lib/chef/chef_fs/file_system/repository/client.rb +38 -0
  36. data/lib/chef/chef_fs/file_system/repository/client_key.rb +38 -0
  37. data/lib/chef/chef_fs/file_system/repository/{chef_repository_file_system_client_keys_dir.rb → client_keys_dir.rb} +9 -5
  38. data/lib/chef/chef_fs/file_system/repository/client_keys_sub_dir.rb +42 -0
  39. data/lib/chef/chef_fs/file_system/repository/clients_dir.rb +40 -0
  40. data/lib/chef/chef_fs/file_system/repository/container.rb +38 -0
  41. data/lib/chef/chef_fs/file_system/repository/containers_dir.rb +41 -0
  42. data/lib/chef/chef_fs/file_system/repository/cookbooks_dir.rb +0 -1
  43. data/lib/chef/chef_fs/file_system/repository/data_bag_item.rb +3 -79
  44. data/lib/chef/chef_fs/file_system/repository/directory.rb +15 -2
  45. data/lib/chef/chef_fs/file_system/repository/environment.rb +38 -0
  46. data/lib/chef/chef_fs/file_system/repository/environments_dir.rb +41 -0
  47. data/lib/chef/chef_fs/file_system/repository/file_system_entry.rb +66 -30
  48. data/lib/chef/chef_fs/file_system/repository/group.rb +38 -0
  49. data/lib/chef/chef_fs/file_system/repository/groups_dir.rb +41 -0
  50. data/lib/chef/chef_fs/file_system/repository/node.rb +38 -0
  51. data/lib/chef/chef_fs/file_system/repository/nodes_dir.rb +41 -0
  52. data/lib/chef/chef_fs/file_system/repository/{chef_repository_file_system_policies_dir.rb → policies_dir.rb} +9 -5
  53. data/lib/chef/chef_fs/file_system/repository/policy.rb +38 -0
  54. data/lib/chef/chef_fs/file_system/repository/policy_group.rb +38 -0
  55. data/lib/chef/chef_fs/file_system/repository/policy_groups_dir.rb +41 -0
  56. data/lib/chef/chef_fs/file_system/repository/role.rb +38 -0
  57. data/lib/chef/chef_fs/file_system/repository/roles_dir.rb +41 -0
  58. data/lib/chef/chef_fs/file_system/repository/user.rb +38 -0
  59. data/lib/chef/chef_fs/file_system/repository/users_dir.rb +41 -0
  60. data/lib/chef/dsl/declare_resource.rb +182 -7
  61. data/lib/chef/http/json_input.rb +2 -2
  62. data/lib/chef/knife.rb +1 -1
  63. data/lib/chef/mixin/shell_out.rb +10 -21
  64. data/lib/chef/property.rb +9 -2
  65. data/lib/chef/provider.rb +8 -7
  66. data/lib/chef/provider/apt_repository.rb +8 -6
  67. data/lib/chef/provider/directory.rb +15 -1
  68. data/lib/chef/provider/env/windows.rb +1 -1
  69. data/lib/chef/provider/mdadm.rb +1 -0
  70. data/lib/chef/provider/package/easy_install.rb +2 -0
  71. data/lib/chef/provider/package/rubygems.rb +4 -1
  72. data/lib/chef/provider/package/windows.rb +1 -1
  73. data/lib/chef/recipe.rb +1 -2
  74. data/lib/chef/resource/apt_repository.rb +6 -6
  75. data/lib/chef/resource/mdadm.rb +9 -0
  76. data/lib/chef/resource_collection.rb +5 -0
  77. data/lib/chef/resource_collection/resource_list.rb +10 -0
  78. data/lib/chef/resource_collection/resource_set.rb +14 -11
  79. data/lib/chef/version.rb +1 -1
  80. data/spec/functional/resource/git_spec.rb +1 -3
  81. data/spec/functional/resource/group_spec.rb +5 -5
  82. data/spec/functional/tiny_server_spec.rb +1 -1
  83. data/spec/functional/util/powershell/cmdlet_spec.rb +1 -1
  84. data/spec/functional/win32/registry_spec.rb +3 -3
  85. data/spec/integration/solo/solo_spec.rb +2 -2
  86. data/spec/support/shared/functional/securable_resource.rb +1 -1
  87. data/spec/support/shared/unit/provider/file.rb +3 -3
  88. data/spec/support/shared/unit/windows_script_resource.rb +1 -1
  89. data/spec/unit/application/apply_spec.rb +1 -0
  90. data/spec/unit/chef_fs/file_system/repository/base_file_spec.rb +128 -0
  91. data/spec/unit/chef_fs/file_system/repository/directory_spec.rb +174 -0
  92. data/spec/unit/cookbook/metadata_spec.rb +1 -1
  93. data/spec/unit/dsl/declare_resource_spec.rb +335 -0
  94. data/spec/unit/knife/bootstrap_spec.rb +2 -2
  95. data/spec/unit/mixin/shell_out_spec.rb +4 -0
  96. data/spec/unit/node/attribute_spec.rb +1 -1
  97. data/spec/unit/node/immutable_collections_spec.rb +2 -2
  98. data/spec/unit/node_map_spec.rb +1 -1
  99. data/spec/unit/property/validation_spec.rb +23 -7
  100. data/spec/unit/provider/apt_repository_spec.rb +5 -0
  101. data/spec/unit/provider/apt_update_spec.rb +1 -0
  102. data/spec/unit/provider/directory_spec.rb +0 -7
  103. data/spec/unit/provider/file/content_spec.rb +1 -1
  104. data/spec/unit/provider/mdadm_spec.rb +9 -0
  105. data/spec/unit/provider/package/easy_install_spec.rb +6 -0
  106. data/spec/unit/provider/package/rubygems_spec.rb +8 -4
  107. data/spec/unit/provider/package/yum_spec.rb +1 -1
  108. data/spec/unit/provider/powershell_script_spec.rb +1 -1
  109. data/spec/unit/provider/user/dscl_spec.rb +6 -6
  110. data/spec/unit/recipe_spec.rb +1 -0
  111. data/spec/unit/resource/apt_repository_spec.rb +4 -0
  112. data/spec/unit/resource/file/verification_spec.rb +1 -1
  113. data/spec/unit/resource/file_spec.rb +2 -2
  114. data/spec/unit/resource/mdadm_spec.rb +7 -2
  115. data/spec/unit/resource_collection_spec.rb +30 -0
  116. data/spec/unit/resource_spec.rb +1 -1
  117. data/tasks/bin/bundle-platform +15 -0
  118. data/tasks/bin/bundle-platform.bat +2 -0
  119. data/tasks/bin/create-override-gemfile +110 -0
  120. data/tasks/bin/run_chef_tests +17 -0
  121. data/tasks/bin/run_external_test +47 -0
  122. data/tasks/bundle.rb +97 -0
  123. data/tasks/bundle_util.rb +94 -0
  124. data/tasks/changelog.rb +12 -0
  125. data/tasks/dependencies.rb +147 -0
  126. data/tasks/gemfile_util.rb +390 -0
  127. data/tasks/maintainers.rb +2 -1
  128. data/tasks/rspec.rb +2 -1
  129. metadata +61 -83
  130. data/acceptance/fips/test/integration/fips/serverspec/fips_spec.rb +0 -39
  131. data/lib/chef/chef_fs/file_system/repository/chef_repository_file_system_entry.rb +0 -83
  132. data/tasks/external_tests.rb +0 -64
@@ -0,0 +1,38 @@
1
+ #
2
+ # Author:: Thom May (<thom@chef.io>)
3
+ # Copyright:: Copyright 2013-2016, Chef Software Inc.
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+
19
+ require "chef/chef_fs/data_handler/policy_data_handler"
20
+ require "chef/chef_fs/file_system/repository/base_file"
21
+
22
+ class Chef
23
+ module ChefFS
24
+ module FileSystem
25
+ module Repository
26
+
27
+ class Policy < BaseFile
28
+
29
+ def initialize(name, parent)
30
+ @data_handler = Chef::ChefFS::DataHandler::PolicyDataHandler.new
31
+ super
32
+ end
33
+
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,38 @@
1
+ #
2
+ # Author:: Thom May (<thom@chef.io>)
3
+ # Copyright:: Copyright 2013-2016, Chef Software Inc.
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+
19
+ require "chef/chef_fs/data_handler/policy_group_data_handler"
20
+ require "chef/chef_fs/file_system/repository/base_file"
21
+
22
+ class Chef
23
+ module ChefFS
24
+ module FileSystem
25
+ module Repository
26
+
27
+ class PolicyGroup < BaseFile
28
+
29
+ def initialize(name, parent)
30
+ @data_handler = Chef::ChefFS::DataHandler::PolicyGroupDataHandler.new
31
+ super
32
+ end
33
+
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,41 @@
1
+ #
2
+ # Author:: John Keiser (<jkeiser@chef.io>)
3
+ # Author:: Ho-Sheng Hsiao (<hosh@chef.io>)
4
+ # Copyright:: Copyright 2012-2016, Chef Software Inc.
5
+ # License:: Apache License, Version 2.0
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+ #
19
+
20
+ require "chef/chef_fs/file_system/repository/policy_group"
21
+ require "chef/chef_fs/file_system/repository/directory"
22
+ require "chef/chef_fs/file_system/exceptions"
23
+
24
+ class Chef
25
+ module ChefFS
26
+ module FileSystem
27
+ module Repository
28
+ class PolicyGroupsDir < Repository::Directory
29
+
30
+ def can_have_child?(name, is_dir)
31
+ !is_dir && File.extname(name) == ".json"
32
+ end
33
+
34
+ def make_child_entry(child_name)
35
+ PolicyGroup.new(child_name, self)
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,38 @@
1
+ #
2
+ # Author:: Thom May (<thom@chef.io>)
3
+ # Copyright:: Copyright 2013-2016, Chef Software Inc.
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+
19
+ require "chef/chef_fs/data_handler/role_data_handler"
20
+ require "chef/chef_fs/file_system/repository/base_file"
21
+
22
+ class Chef
23
+ module ChefFS
24
+ module FileSystem
25
+ module Repository
26
+
27
+ class Role < BaseFile
28
+
29
+ def initialize(name, parent)
30
+ @data_handler = Chef::ChefFS::DataHandler::RoleDataHandler.new
31
+ super
32
+ end
33
+
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,41 @@
1
+ #
2
+ # Author:: John Keiser (<jkeiser@chef.io>)
3
+ # Author:: Ho-Sheng Hsiao (<hosh@chef.io>)
4
+ # Copyright:: Copyright 2012-2016, Chef Software Inc.
5
+ # License:: Apache License, Version 2.0
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+ #
19
+
20
+ require "chef/chef_fs/file_system/repository/role"
21
+ require "chef/chef_fs/file_system/repository/directory"
22
+ require "chef/chef_fs/file_system/exceptions"
23
+
24
+ class Chef
25
+ module ChefFS
26
+ module FileSystem
27
+ module Repository
28
+ class RolesDir < Repository::Directory
29
+
30
+ def can_have_child?(name, is_dir)
31
+ !is_dir && File.extname(name) == ".json"
32
+ end
33
+
34
+ def make_child_entry(child_name)
35
+ Role.new(child_name, self)
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,38 @@
1
+ #
2
+ # Author:: Thom May (<thom@chef.io>)
3
+ # Copyright:: Copyright 2013-2016, Chef Software Inc.
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+
19
+ require "chef/chef_fs/data_handler/user_data_handler"
20
+ require "chef/chef_fs/file_system/repository/base_file"
21
+
22
+ class Chef
23
+ module ChefFS
24
+ module FileSystem
25
+ module Repository
26
+
27
+ class User < BaseFile
28
+
29
+ def initialize(name, parent)
30
+ @data_handler = Chef::ChefFS::DataHandler::UserDataHandler.new
31
+ super
32
+ end
33
+
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,41 @@
1
+ #
2
+ # Author:: John Keiser (<jkeiser@chef.io>)
3
+ # Author:: Ho-Sheng Hsiao (<hosh@chef.io>)
4
+ # Copyright:: Copyright 2012-2016, Chef Software Inc.
5
+ # License:: Apache License, Version 2.0
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+ #
19
+
20
+ require "chef/chef_fs/file_system/repository/user"
21
+ require "chef/chef_fs/file_system/repository/directory"
22
+ require "chef/chef_fs/file_system/exceptions"
23
+
24
+ class Chef
25
+ module ChefFS
26
+ module FileSystem
27
+ module Repository
28
+ class UsersDir < Repository::Directory
29
+
30
+ def can_have_child?(name, is_dir)
31
+ !is_dir && File.extname(name) == ".json"
32
+ end
33
+
34
+ def make_child_entry(child_name)
35
+ User.new(child_name, self)
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -1,6 +1,6 @@
1
1
  #--
2
2
  # Author:: Adam Jacob (<adam@chef.io>)
3
- # Author:: Christopher Walters (<cw@chef.io>)
3
+ # Author:: Christopher Walters
4
4
  # Copyright:: Copyright 2008-2016, 2009-2015 Chef Software, Inc.
5
5
  # License:: Apache License, Version 2.0
6
6
  #
@@ -23,7 +23,180 @@ class Chef
23
23
  module DSL
24
24
  module DeclareResource
25
25
 
26
+ # Helper for switching run_contexts. Allows for using :parent or :root in place of
27
+ # passing the run_context. Executes the block in the run_context. Returns the return
28
+ # value of the passed block.
26
29
  #
30
+ # @param rc [Chef::RunContext,Symbol] Either :root, :parent or a Chef::RunContext
31
+ #
32
+ # @return return value of the block
33
+ #
34
+ # @example
35
+ # # creates/returns a 'service[foo]' resource in the root run_context
36
+ # resource = with_run_context(:root)
37
+ # edit_resource(:service, "foo") do
38
+ # action :nothing
39
+ # end
40
+ # end
41
+ #
42
+ def with_run_context(rc)
43
+ raise ArgumentError, "with_run_context is useless without a block" unless block_given?
44
+ old_run_context = @run_context
45
+ @run_context =
46
+ case rc
47
+ when Chef::RunContext
48
+ rc
49
+ when :root
50
+ Chef.run_context
51
+ when :parent
52
+ run_context.parent_run_context
53
+ else
54
+ raise ArgumentError, "bad argument to run_context helper, must be :root, :parent, or a Chef::RunContext"
55
+ end
56
+ yield
57
+ ensure
58
+ @run_context = old_run_context
59
+ end
60
+
61
+ # Lookup a resource in the resource collection by name and delete it. This
62
+ # will raise Chef::Exceptions::ResourceNotFound if the resource is not found.
63
+ #
64
+ # @param type [Symbol] The type of resource (e.g. `:file` or `:package`)
65
+ # @param name [String] The name of the resource (e.g. '/x/y.txt' or 'apache2')
66
+ # @param run_context [Chef::RunContext] the run_context of the resource collection to operate on
67
+ #
68
+ # @return [Chef::Resource] The resource
69
+ #
70
+ # @example
71
+ # delete_resource!(:template, '/x/y.txy')
72
+ #
73
+ def delete_resource!(type, name, run_context: self.run_context)
74
+ run_context.resource_collection.delete("#{type}[#{name}]")
75
+ end
76
+
77
+ # Lookup a resource in the resource collection by name and delete it. Returns
78
+ # nil if the resource is not found and should not fail.
79
+ #
80
+ # @param type [Symbol] The type of resource (e.g. `:file` or `:package`)
81
+ # @param name [String] The name of the resource (e.g. '/x/y.txt' or 'apache2')
82
+ # @param run_context [Chef::RunContext] the run_context of the resource collection to operate on
83
+ #
84
+ # @return [Chef::Resource] The resource
85
+ #
86
+ # @example
87
+ # delete_resource(:template, '/x/y.txy')
88
+ #
89
+ def delete_resource(type, name, run_context: self.run_context)
90
+ delete_resource!(type, name, run_context: run_context)
91
+ rescue Chef::Exceptions::ResourceNotFound
92
+ nil
93
+ end
94
+
95
+ # Lookup a resource in the resource collection by name and edit the resource. If the resource is not
96
+ # found this will raise Chef::Exceptions::ResourceNotFound. This is the correct API to use for
97
+ # "chef_rewind" functionality.
98
+ #
99
+ # @param type [Symbol] The type of resource (e.g. `:file` or `:package`)
100
+ # @param name [String] The name of the resource (e.g. '/x/y.txt' or 'apache2')
101
+ # @param run_context [Chef::RunContext] the run_context of the resource collection to operate on
102
+ # @param resource_attrs_block A block that lets you set attributes of the
103
+ # resource (it is instance_eval'd on the resource instance).
104
+ #
105
+ # @return [Chef::Resource] The updated resource
106
+ #
107
+ # @example
108
+ # edit_resource!(:template, '/x/y.txy') do
109
+ # cookbook_name: cookbook_name
110
+ # end
111
+ #
112
+ def edit_resource!(type, name, created_at = nil, run_context: self.run_context, &resource_attrs_block)
113
+ resource = find_resource!(type, name, run_context: run_context)
114
+ resource.instance_eval(&resource_attrs_block) if block_given?
115
+ resource
116
+ end
117
+
118
+ # Lookup a resource in the resource collection by name. If it exists,
119
+ # return it. If it does not exist, create it. This is a useful function
120
+ # for accumulator patterns. In CRUD terminology this is an "upsert" operation and is
121
+ # used to assert that the resource must exist with the specified properties.
122
+ #
123
+ # @param type [Symbol] The type of resource (e.g. `:file` or `:package`)
124
+ # @param name [String] The name of the resource (e.g. '/x/y.txt' or 'apache2')
125
+ # @param created_at [String] The caller of the resource. Use `caller[0]`
126
+ # to get the caller of your function. Defaults to the caller of this
127
+ # function.
128
+ # @param run_context [Chef::RunContext] the run_context of the resource collection to operate on
129
+ # @param resource_attrs_block A block that lets you set attributes of the
130
+ # resource (it is instance_eval'd on the resource instance).
131
+ #
132
+ # @return [Chef::Resource] The updated or created resource
133
+ #
134
+ # @example
135
+ # resource = edit_resource(:template, '/x/y.txy') do
136
+ # source "y.txy.erb"
137
+ # variables {}
138
+ # end
139
+ # resource.variables.merge!({ home: "/home/klowns" })
140
+ #
141
+ def edit_resource(type, name, created_at = nil, run_context: self.run_context, &resource_attrs_block)
142
+ edit_resource!(type, name, created_at, run_context: run_context, &resource_attrs_block)
143
+ rescue Chef::Exceptions::ResourceNotFound
144
+ declare_resource(type, name, created_at, run_context: run_context, &resource_attrs_block)
145
+ end
146
+
147
+ # Lookup a resource in the resource collection by name. If the resource is not
148
+ # found this will raise Chef::Exceptions::ResourceNotFound. This API is identical to the
149
+ # resources() call and while it is a synonym it is not intended to deprecate that call.
150
+ #
151
+ # @param type [Symbol] The type of resource (e.g. `:file` or `:package`)
152
+ # @param name [String] The name of the resource (e.g. '/x/y.txt' or 'apache2')
153
+ # @param run_context [Chef::RunContext] the run_context of the resource collection to operate on
154
+ #
155
+ # @return [Chef::Resource] The updated resource
156
+ #
157
+ # @example
158
+ # resource = find_resource!(:template, '/x/y.txy')
159
+ #
160
+ def find_resource!(type, name, run_context: self.run_context)
161
+ raise ArgumentError, "find_resource! does not take a block" if block_given?
162
+ run_context.resource_collection.find(type => name)
163
+ end
164
+
165
+ # Lookup a resource in the resource collection by name. If the resource is not found
166
+ # the will be no exception raised and the call will return nil. If a block is given and
167
+ # no resource is found it will create the resource using the block, if the resource is
168
+ # found then the block will not be applied. The block version is similar to create_if_missing
169
+ #
170
+ # @param type [Symbol] The type of resource (e.g. `:file` or `:package`)
171
+ # @param name [String] The name of the resource (e.g. '/x/y.txt' or 'apache2')
172
+ # @param run_context [Chef::RunContext] the run_context of the resource collection to operate on
173
+ #
174
+ # @return [Chef::Resource] The updated resource
175
+ #
176
+ # @example
177
+ # if ( find_resource(:template, '/x/y.txy') )
178
+ # # do something
179
+ # else
180
+ # # don't worry about the error
181
+ # end
182
+ #
183
+ # @example
184
+ # # this API can be used to return a resource from an outer run context, and will only create
185
+ # # an action :nothing service if one does not already exist.
186
+ # resource = with_run_context(:root) do
187
+ # find_resource(:service, 'whatever') do
188
+ # action :nothing
189
+ # end
190
+ # end
191
+ #
192
+ def find_resource(type, name, created_at: nil, run_context: self.run_context, &resource_attrs_block)
193
+ find_resource!(type, name, run_context: run_context)
194
+ rescue Chef::Exceptions::ResourceNotFound
195
+ if block_given?
196
+ declare_resource(type, name, created_at, run_context: run_context, &resource_attrs_block)
197
+ end # returns nil otherwise
198
+ end
199
+
27
200
  # Instantiates a resource (via #build_resource), then adds it to the
28
201
  # resource collection. Note that resource classes are looked up directly,
29
202
  # so this will create the resource you intended even if the method name
@@ -34,6 +207,7 @@ class Chef
34
207
  # @param created_at [String] The caller of the resource. Use `caller[0]`
35
208
  # to get the caller of your function. Defaults to the caller of this
36
209
  # function.
210
+ # @param run_context [Chef::RunContext] the run_context of the resource collection to operate on
37
211
  # @param resource_attrs_block A block that lets you set attributes of the
38
212
  # resource (it is instance_eval'd on the resource instance).
39
213
  #
@@ -52,11 +226,9 @@ class Chef
52
226
  created_at ||= caller[0]
53
227
 
54
228
  if create_if_missing
55
- begin
56
- resource = run_context.resource_collection.find(type => name)
57
- return resource
58
- rescue Chef::Exceptions::ResourceNotFound
59
- end
229
+ Chef::Log.deprecation "build_resource with a create_if_missing flag is deprecated, use edit_resource instead"
230
+ # midly goofy since we call edit_resource only to re-call ourselves, but that's why its deprecated...
231
+ return edit_resource(type, name, created_at, run_context: run_context, &resource_attrs_block)
60
232
  end
61
233
 
62
234
  resource = build_resource(type, name, created_at, &resource_attrs_block)
@@ -65,7 +237,6 @@ class Chef
65
237
  resource
66
238
  end
67
239
 
68
- #
69
240
  # Instantiate a resource of the given +type+ with the given +name+ and
70
241
  # attributes as given in the +resource_attrs_block+.
71
242
  #
@@ -76,6 +247,7 @@ class Chef
76
247
  # @param created_at [String] The caller of the resource. Use `caller[0]`
77
248
  # to get the caller of your function. Defaults to the caller of this
78
249
  # function.
250
+ # @param run_context [Chef::RunContext] the run_context of the resource collection to operate on
79
251
  # @param resource_attrs_block A block that lets you set attributes of the
80
252
  # resource (it is instance_eval'd on the resource instance).
81
253
  #
@@ -88,6 +260,9 @@ class Chef
88
260
  #
89
261
  def build_resource(type, name, created_at = nil, run_context: self.run_context, &resource_attrs_block)
90
262
  created_at ||= caller[0]
263
+
264
+ # this needs to be lazy in order to avoid circular dependencies since ResourceBuilder
265
+ # will requires the entire provider+resolver universe
91
266
  require "chef/resource_builder" unless defined?(Chef::ResourceBuilder)
92
267
 
93
268
  Chef::ResourceBuilder.new(