summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Keiser <john@johnkeiser.com>2015-06-01 13:21:50 -0700
committerJohn Keiser <john@johnkeiser.com>2015-06-01 13:21:50 -0700
commit22da6b42a02fcfd7e9d03f5f09828b7ddc569bb9 (patch)
tree486e4d616f2f4b2d1d605ffeb96e0eec6c97f7b5
parent02e8874637dd661b5aaf656a5c2d6fbbb5795619 (diff)
parent33df30d0f8c75b1e600da2f169fee0f8e58f694a (diff)
downloadchef-22da6b42a02fcfd7e9d03f5f09828b7ddc569bb9.tar.gz
Merge branch 'jk/resource_action_attributes'
-rw-r--r--CHANGELOG.md2
-rw-r--r--DOC_CHANGES.md17
-rw-r--r--lib/chef/resource.rb88
-rw-r--r--lib/chef/resource/breakpoint.rb5
-rw-r--r--lib/chef/resource/cookbook_file.rb3
-rw-r--r--lib/chef/resource/cron.rb5
-rw-r--r--lib/chef/resource/deploy.rb5
-rw-r--r--lib/chef/resource/directory.rb5
-rw-r--r--lib/chef/resource/dsc_resource.rb4
-rw-r--r--lib/chef/resource/dsc_script.rb4
-rw-r--r--lib/chef/resource/env.rb5
-rw-r--r--lib/chef/resource/erl_call.rb5
-rw-r--r--lib/chef/resource/execute.rb4
-rw-r--r--lib/chef/resource/file.rb4
-rw-r--r--lib/chef/resource/group.rb5
-rw-r--r--lib/chef/resource/http_request.rb5
-rw-r--r--lib/chef/resource/ifconfig.rb5
-rw-r--r--lib/chef/resource/ips_package.rb3
-rw-r--r--lib/chef/resource/link.rb5
-rw-r--r--lib/chef/resource/log.rb4
-rw-r--r--lib/chef/resource/lwrp_base.rb44
-rw-r--r--lib/chef/resource/mdadm.rb6
-rw-r--r--lib/chef/resource/mount.rb5
-rw-r--r--lib/chef/resource/ohai.rb4
-rw-r--r--lib/chef/resource/package.rb5
-rw-r--r--lib/chef/resource/paludis_package.rb3
-rw-r--r--lib/chef/resource/reboot.rb3
-rw-r--r--lib/chef/resource/registry_key.rb5
-rw-r--r--lib/chef/resource/remote_directory.rb5
-rw-r--r--lib/chef/resource/remote_file.rb1
-rw-r--r--lib/chef/resource/route.rb5
-rw-r--r--lib/chef/resource/ruby_block.rb4
-rw-r--r--lib/chef/resource/scm.rb5
-rw-r--r--lib/chef/resource/service.rb5
-rw-r--r--lib/chef/resource/subversion.rb2
-rw-r--r--lib/chef/resource/template.rb1
-rw-r--r--lib/chef/resource/user.rb5
-rw-r--r--lib/chef/resource/windows_package.rb3
-rw-r--r--lib/chef/resource/windows_service.rb3
-rw-r--r--spec/integration/recipes/provider_choice.rb6
-rw-r--r--spec/support/lib/chef/resource/zen_master.rb5
-rw-r--r--spec/unit/lwrp_spec.rb8
-rw-r--r--spec/unit/provider/deploy_spec.rb2
-rw-r--r--spec/unit/recipe_spec.rb4
-rw-r--r--spec/unit/resource/breakpoint_spec.rb2
-rw-r--r--spec/unit/resource/erl_call_spec.rb2
-rw-r--r--spec/unit/resource/file_spec.rb2
-rw-r--r--spec/unit/resource/route_spec.rb2
-rw-r--r--spec/unit/resource/ruby_block_spec.rb4
-rw-r--r--spec/unit/resource/windows_service_spec.rb2
50 files changed, 200 insertions, 136 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 29abb09ca5..cdaff78a22 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -15,6 +15,8 @@
* [pr#3397](https://github.com/chef/chef/pull/3397): Validate owner exists in directory resources
* [pr#3418](https://github.com/chef/chef/pull/3418): Add `shell_out` mixin to Chef::Resource class for use in `not_if`/`only_if` conditionals, etc.
* [pr#3406](https://github.com/chef/chef/pull/3406): Add wide-char 'Environment' to `broadcast_env_change` mixin for setting windows environment variables
+* [pr#3442](https://github.com/chef/chef/pull/3442): Add resource_name to top-level Resource class to make defining resources easier. `use_automatic_resource_name` also added, to support convention-based resource naming.
+* [pr#3447](https://github.com/chef/chef/pull/3447): Add allowed_actions and default_action to top-level Resource class.
## 12.4.0
diff --git a/DOC_CHANGES.md b/DOC_CHANGES.md
index b5eb124f26..cba06a26e6 100644
--- a/DOC_CHANGES.md
+++ b/DOC_CHANGES.md
@@ -6,7 +6,7 @@ Example Doc Change:
Description of the required change.
-->
-### Resources must now use `resource_name` (or `provides`) to declare recipe DSL
+### Resources must now use `resource_name` to declare recipe DSL
Resources declared in `Chef::Resource` namespace will no longer get recipe DSL
automatically. Instead, `resource_name` is required in order to have DSL:
@@ -21,13 +21,26 @@ end
`resource_name :my_resource` may be used to explicitly set the resource name.
-`provides :my_resource`, still works, but at least one resource_name *must* be
+`provides :my_resource` still works, but at least one resource_name *must* be
set for the resource to work.
Authors of HWRPs need to be aware that in the future all resources and providers will be required to include a provides line. Starting with Chef 12.4.0 any HWRPs in the `Chef::Resource` or `Chef::Provider` namespaces that do not have provides lines will trigger deprecation warning messages when called. The LWRPBase code does `provides` automatically so LWRP authors and users who write classes that inherit from LWRPBase do not need to explicitly include provides lines.
Users are encouraged to declare resources in their own namespaces instead of putting them in the special `Chef::Resource` namespace.
+### Resources may now use `allowed_actions` and `default_action`
+
+Instead of overriding `Chef::Resource.initialize` and setting `@allowed_actions` and `@action` in the constructor, you may now use the `allowed_actions` and `default_action` DSL to declare them:
+
+```ruby
+class MyResource < Chef::Resource
+ use_automatic_resource_name
+
+ allowed_actions :create, :delete
+ default_action :create
+end
+```
+
### LWRPs are no longer automatically placed in the `Chef::Resource` namespace
Starting with Chef 12.4.0, accessing an LWRP class by name from the `Chef::Resource` namespace will trigger a deprecation warning message. This means that if your cookbook includes the LWRP `mycookbook/resources/myresource.rb`, you will no longer be able to extend or reference `Chef::Resource::MycookbookMyresource` in Ruby code. LWRP recipe DSL does not change: the LWRP will still be available to recipes as `mycookbook_myresource`.
diff --git a/lib/chef/resource.rb b/lib/chef/resource.rb
index ac98df5513..070793a7a2 100644
--- a/lib/chef/resource.rb
+++ b/lib/chef/resource.rb
@@ -34,6 +34,7 @@ require 'chef/platform'
require 'chef/resource/resource_notification'
require 'chef/provider_resolver'
require 'chef/resource_resolver'
+require 'set'
require 'chef/mixin/deprecation'
require 'chef/mixin/provides'
@@ -108,8 +109,8 @@ class Chef
@before = nil
@params = Hash.new
@provider = nil
- @allowed_actions = [ :nothing ]
- @action = :nothing
+ @allowed_actions = self.class.allowed_actions.to_a
+ @action = self.class.default_action
@updated = false
@updated_by_last_action = false
@supports = {}
@@ -170,19 +171,24 @@ class Chef
# @param arg [Array[Symbol], Symbol] A list of actions (e.g. `:create`)
# @return [Array[Symbol]] the list of actions.
#
+ attr_accessor :action
def action(arg=nil)
if arg
- action_list = arg.kind_of?(Array) ? arg : [ arg ]
- action_list = action_list.collect { |a| a.to_sym }
- action_list.each do |action|
+ if arg.is_a?(Array)
+ arg = arg.map { |a| a.to_sym }
+ else
+ arg = arg.to_sym
+ end
+ Array(arg).each do |action|
validate(
{ action: action },
- { action: { kind_of: Symbol, equal_to: @allowed_actions } }
+ { action: { kind_of: Symbol, equal_to: allowed_actions } }
)
end
- @action = action_list
+ self.action = arg
else
- @action
+ # Pull the action from the class if it's not set
+ @action || self.class.default_action
end
end
@@ -190,8 +196,7 @@ class Chef
# Sets up a notification that will run a particular action on another resource
# if and when *this* resource is updated by an action.
#
- # If the action does nothing--does not update this resource, the
- # notification never triggers.)
+ # If the action does not update this resource, the notification never triggers.
#
# Only one resource may be specified per notification.
#
@@ -768,6 +773,12 @@ class Chef
# have.
#
attr_accessor :allowed_actions
+ def allowed_actions(value=NULL_ARG)
+ if value != NULL_ARG
+ self.allowed_actions = value
+ end
+ @allowed_actions
+ end
#
# Whether or not this resource was updated during an action. If multiple
@@ -945,8 +956,63 @@ class Chef
end
@provider_base ||= arg || Chef::Provider
end
- end
+ #
+ # The list of allowed actions for the resource.
+ #
+ # @param actions [Array<Symbol>] The list of actions to add to allowed_actions.
+ #
+ # @return [Arrau<Symbol>] The list of actions, as symbols.
+ #
+ def allowed_actions(*actions)
+ @allowed_actions ||=
+ if superclass.respond_to?(:allowed_actions)
+ superclass.allowed_actions.dup
+ else
+ [ :nothing ]
+ end
+ @allowed_actions |= actions
+ end
+ def allowed_actions=(value)
+ @allowed_actions = value
+ end
+
+ #
+ # The action that will be run if no other action is specified.
+ #
+ # Setting default_action will automatially add the action to
+ # allowed_actions, if it isn't already there.
+ #
+ # Defaults to :nothing.
+ #
+ # @param action_name [Symbol,Array<Symbol>] The default action (or series
+ # of actions) to use.
+ #
+ # @return [Symbol,Array<Symbol>] The default actions for the resource.
+ #
+ def default_action(action_name=NULL_ARG)
+ unless action_name.equal?(NULL_ARG)
+ if action_name.is_a?(Array)
+ @default_action = action_name.map { |arg| arg.to_sym }
+ else
+ @default_action = action_name.to_sym
+ end
+
+ self.allowed_actions |= Array(@default_action)
+ end
+
+ if @default_action
+ @default_action
+ elsif superclass.respond_to?(:default_action)
+ superclass.default_action
+ else
+ :nothing
+ end
+ end
+ def default_action=(action_name)
+ default_action action_name
+ end
+ end
#
# Internal Resource Interface (for Chef)
diff --git a/lib/chef/resource/breakpoint.rb b/lib/chef/resource/breakpoint.rb
index dca4fd04a6..0107b8968a 100644
--- a/lib/chef/resource/breakpoint.rb
+++ b/lib/chef/resource/breakpoint.rb
@@ -24,11 +24,12 @@ class Chef
class Breakpoint < Chef::Resource
use_automatic_resource_name
+ default_action :break
+
def initialize(action="break", *args)
super(caller.first, *args)
- @action = "break"
- @allowed_actions << :break
end
+
end
end
end
diff --git a/lib/chef/resource/cookbook_file.rb b/lib/chef/resource/cookbook_file.rb
index ade9c0e3e3..08f4733497 100644
--- a/lib/chef/resource/cookbook_file.rb
+++ b/lib/chef/resource/cookbook_file.rb
@@ -29,10 +29,11 @@ class Chef
use_automatic_resource_name
+ default_action :create
+
def initialize(name, run_context=nil)
super
@provider = Chef::Provider::CookbookFile
- @action = "create"
@source = ::File.basename(name)
@cookbook = nil
end
diff --git a/lib/chef/resource/cron.rb b/lib/chef/resource/cron.rb
index daf462866e..a399caebe9 100644
--- a/lib/chef/resource/cron.rb
+++ b/lib/chef/resource/cron.rb
@@ -29,10 +29,11 @@ class Chef
use_automatic_resource_name
+ default_action :create
+ allowed_actions :create, :delete
+
def initialize(name, run_context=nil)
super
- @action = :create
- @allowed_actions.push(:create, :delete)
@minute = "*"
@hour = "*"
@day = "*"
diff --git a/lib/chef/resource/deploy.rb b/lib/chef/resource/deploy.rb
index 8d007df348..f88133119a 100644
--- a/lib/chef/resource/deploy.rb
+++ b/lib/chef/resource/deploy.rb
@@ -56,6 +56,9 @@ class Chef
state_attrs :deploy_to, :revision
+ default_action :deploy
+ allowed_actions :force_deploy, :deploy, :rollback
+
def initialize(name, run_context=nil)
super
@deploy_to = name
@@ -67,7 +70,6 @@ class Chef
@symlink_before_migrate = {"config/database.yml" => "config/database.yml"}
@symlinks = {"system" => "public/system", "pids" => "tmp/pids", "log" => "log"}
@revision = 'HEAD'
- @action = :deploy
@migrate = false
@rollback_on_error = false
@remote = "origin"
@@ -75,7 +77,6 @@ class Chef
@shallow_clone = false
@scm_provider = Chef::Provider::Git
@svn_force_export = false
- @allowed_actions.push(:force_deploy, :deploy, :rollback)
@additional_remotes = Hash[]
@keep_releases = 5
@enable_checkout = true
diff --git a/lib/chef/resource/directory.rb b/lib/chef/resource/directory.rb
index ea1805353d..304317e07a 100644
--- a/lib/chef/resource/directory.rb
+++ b/lib/chef/resource/directory.rb
@@ -34,12 +34,13 @@ class Chef
use_automatic_resource_name
+ default_action :create
+ allowed_actions :create, :delete
+
def initialize(name, run_context=nil)
super
@path = name
- @action = :create
@recursive = false
- @allowed_actions.push(:create, :delete)
end
def recursive(arg=nil)
diff --git a/lib/chef/resource/dsc_resource.rb b/lib/chef/resource/dsc_resource.rb
index cdfdff54f0..7c822604e2 100644
--- a/lib/chef/resource/dsc_resource.rb
+++ b/lib/chef/resource/dsc_resource.rb
@@ -26,12 +26,12 @@ class Chef
include Chef::DSL::Powershell
+ default_action :run
+
def initialize(name, run_context)
super
@properties = {}
@resource = nil
- @allowed_actions.push(:run)
- @action = :run
end
def resource(value=nil)
diff --git a/lib/chef/resource/dsc_script.rb b/lib/chef/resource/dsc_script.rb
index 4eabb92438..0ff7988d7d 100644
--- a/lib/chef/resource/dsc_script.rb
+++ b/lib/chef/resource/dsc_script.rb
@@ -25,10 +25,10 @@ class Chef
use_automatic_resource_name
provides :dsc_script, platform: "windows"
+ default_action :run
+
def initialize(name, run_context=nil)
super
- @allowed_actions.push(:run)
- @action = :run
@imports = {}
end
diff --git a/lib/chef/resource/env.rb b/lib/chef/resource/env.rb
index d74b4feee0..da7d48f062 100644
--- a/lib/chef/resource/env.rb
+++ b/lib/chef/resource/env.rb
@@ -28,13 +28,14 @@ class Chef
use_automatic_resource_name
provides :env, os: "windows"
+ default_action :create
+ allowed_actions :create, :delete, :modify
+
def initialize(name, run_context=nil)
super
@key_name = name
@value = nil
- @action = :create
@delim = nil
- @allowed_actions.push(:create, :delete, :modify)
end
def key_name(arg=nil)
diff --git a/lib/chef/resource/erl_call.rb b/lib/chef/resource/erl_call.rb
index c6d45c7aca..b025259b4c 100644
--- a/lib/chef/resource/erl_call.rb
+++ b/lib/chef/resource/erl_call.rb
@@ -29,6 +29,8 @@ class Chef
identity_attr :code
+ default_action :run
+
def initialize(name, run_context=nil)
super
@@ -37,9 +39,6 @@ class Chef
@distributed = false # if you want to have a distributed erlang node
@name_type = "sname" # type of erlang hostname name or sname
@node_name = "chef@localhost" # the erlang node hostname
-
- @action = "run"
- @allowed_actions.push(:run)
end
def code(arg=nil)
diff --git a/lib/chef/resource/execute.rb b/lib/chef/resource/execute.rb
index c0263501c5..34ed6b1bd9 100644
--- a/lib/chef/resource/execute.rb
+++ b/lib/chef/resource/execute.rb
@@ -33,11 +33,12 @@ class Chef
# Only execute resources (and subclasses) can be guard interpreters.
attr_accessor :is_guard_interpreter
+ default_action :run
+
def initialize(name, run_context=nil)
super
@command = name
@backup = 5
- @action = "run"
@creates = nil
@cwd = nil
@environment = nil
@@ -46,7 +47,6 @@ class Chef
@returns = 0
@timeout = nil
@user = nil
- @allowed_actions.push(:run)
@umask = nil
@default_guard_interpreter = :execute
@is_guard_interpreter = false
diff --git a/lib/chef/resource/file.rb b/lib/chef/resource/file.rb
index 32fd984f8c..51969f65a0 100644
--- a/lib/chef/resource/file.rb
+++ b/lib/chef/resource/file.rb
@@ -48,13 +48,13 @@ class Chef
attr_accessor :final_checksum
use_automatic_resource_name
+ default_action :create
+ allowed_actions :create, :delete, :touch, :create_if_missing
def initialize(name, run_context=nil)
super
@path = name
@backup = 5
- @action = "create"
- @allowed_actions.push(:create, :delete, :touch, :create_if_missing)
@atomic_update = Chef::Config[:file_atomic_update]
@force_unlink = false
@manage_symlink_source = nil
diff --git a/lib/chef/resource/group.rb b/lib/chef/resource/group.rb
index 64062dbe8f..49cf92a282 100644
--- a/lib/chef/resource/group.rb
+++ b/lib/chef/resource/group.rb
@@ -27,16 +27,17 @@ class Chef
use_automatic_resource_name
+ allowed_actions :create, :remove, :modify, :manage
+ default_action :create
+
def initialize(name, run_context=nil)
super
@group_name = name
@gid = nil
@members = []
@excluded_members = []
- @action = :create
@append = false
@non_unique = false
- @allowed_actions.push(:create, :remove, :modify, :manage)
end
def group_name(arg=nil)
diff --git a/lib/chef/resource/http_request.rb b/lib/chef/resource/http_request.rb
index ea22a26456..192484f557 100644
--- a/lib/chef/resource/http_request.rb
+++ b/lib/chef/resource/http_request.rb
@@ -27,13 +27,14 @@ class Chef
identity_attr :url
+ default_action :get
+ allowed_actions :get, :put, :post, :delete, :head, :options
+
def initialize(name, run_context=nil)
super
@message = name
@url = nil
- @action = :get
@headers = {}
- @allowed_actions.push(:get, :put, :post, :delete, :head, :options)
end
def url(args=nil)
diff --git a/lib/chef/resource/ifconfig.rb b/lib/chef/resource/ifconfig.rb
index 18a688c2a5..47adc52425 100644
--- a/lib/chef/resource/ifconfig.rb
+++ b/lib/chef/resource/ifconfig.rb
@@ -28,11 +28,12 @@ class Chef
state_attrs :inet_addr, :mask
+ default_action :add
+ allowed_actions :add, :delete, :enable, :disable
+
def initialize(name, run_context=nil)
super
@target = name
- @action = :add
- @allowed_actions.push(:add, :delete, :enable, :disable)
@hwaddr = nil
@mask = nil
@inet_addr = nil
diff --git a/lib/chef/resource/ips_package.rb b/lib/chef/resource/ips_package.rb
index 93b554eb6c..6b40fe138c 100644
--- a/lib/chef/resource/ips_package.rb
+++ b/lib/chef/resource/ips_package.rb
@@ -26,9 +26,10 @@ class Chef
use_automatic_resource_name
provides :ips_package, os: "solaris2"
+ allowed_actions :install, :remove, :upgrade
+
def initialize(name, run_context = nil)
super(name, run_context)
- @allowed_actions.push(:install, :remove, :upgrade)
@accept_license = false
end
diff --git a/lib/chef/resource/link.rb b/lib/chef/resource/link.rb
index 84a2b94ca1..2fe4da718c 100644
--- a/lib/chef/resource/link.rb
+++ b/lib/chef/resource/link.rb
@@ -31,14 +31,15 @@ class Chef
state_attrs :to, :owner, :group
+ default_action :create
+ allowed_actions :create, :delete
+
def initialize(name, run_context=nil)
verify_links_supported!
super
@to = nil
- @action = :create
@link_type = :symbolic
@target_file = name
- @allowed_actions.push(:create, :delete)
end
def to(arg=nil)
diff --git a/lib/chef/resource/log.rb b/lib/chef/resource/log.rb
index 5b4081b0fe..5c119df4d8 100644
--- a/lib/chef/resource/log.rb
+++ b/lib/chef/resource/log.rb
@@ -27,6 +27,8 @@ class Chef
identity_attr :message
+ default_action :write
+
# Sends a string from a recipe to a log provider
#
# log "some string to log" do
@@ -50,8 +52,6 @@ class Chef
def initialize(name, run_context=nil)
super
@level = :info
- @action = :write
- @allowed_actions.push(:write)
@message = name
end
diff --git a/lib/chef/resource/lwrp_base.rb b/lib/chef/resource/lwrp_base.rb
index 129fc38d6f..20d236a161 100644
--- a/lib/chef/resource/lwrp_base.rb
+++ b/lib/chef/resource/lwrp_base.rb
@@ -82,42 +82,21 @@ class Chef
end
end
- # Sets the default action
- def default_action(action_name=NULL_ARG)
- unless action_name.equal?(NULL_ARG)
- @actions ||= []
- if action_name.is_a?(Array)
- action = action_name.map { |arg| arg.to_sym }
- @actions = actions | action
- @default_action = action
- else
- action = action_name.to_sym
- @actions.push(action) unless @actions.include?(action)
- @default_action = [action]
- end
- end
-
- @default_action ||= from_superclass(:default_action)
- end
-
# Adds +action_names+ to the list of valid actions for this resource.
+ # Does not include superclass's action list when appending.
def actions(*action_names)
- if action_names.empty?
- defined?(@actions) ? @actions : from_superclass(:actions, []).dup
+ if !action_names.empty? && !@allowed_actions
+ self.allowed_actions = action_names
else
- # BC-compat way for checking if actions have already been defined
- if defined?(@actions)
- @actions.push(*action_names)
- else
- @actions = action_names
- end
+ allowed_actions(*action_names)
end
end
+ alias :actions= :allowed_actions=
# @deprecated
def valid_actions(*args)
- Chef::Log.warn("`valid_actions' is deprecated, please use actions `instead'!")
- actions(*args)
+ Chef::Log.warn("`valid_actions' is deprecated, please use allowed_actions `instead'!")
+ allowed_actions(*args)
end
# Set the run context on the class. Used to provide access to the node
@@ -149,15 +128,6 @@ class Chef
superclass.respond_to?(m) ? superclass.send(m) : default
end
end
-
- private
-
- # Default initializer. Sets the default action and allowed actions.
- def initialize(*args, &block)
- super
- @action = self.class.default_action
- allowed_actions.push(self.class.actions).flatten!
- end
end
end
end
diff --git a/lib/chef/resource/mdadm.rb b/lib/chef/resource/mdadm.rb
index 8f19410b9c..2927cc8321 100644
--- a/lib/chef/resource/mdadm.rb
+++ b/lib/chef/resource/mdadm.rb
@@ -29,6 +29,9 @@ class Chef
use_automatic_resource_name
+ default_action :create
+ allowed_actions :create, :assemble, :stop
+
def initialize(name, run_context=nil)
super
@@ -39,9 +42,6 @@ class Chef
@metadata = "0.90"
@bitmap = nil
@raid_device = name
-
- @action = :create
- @allowed_actions.push(:create, :assemble, :stop)
end
def chunk(arg=nil)
diff --git a/lib/chef/resource/mount.rb b/lib/chef/resource/mount.rb
index f8a0cd157b..ce6d2cc232 100644
--- a/lib/chef/resource/mount.rb
+++ b/lib/chef/resource/mount.rb
@@ -29,6 +29,9 @@ class Chef
use_automatic_resource_name
+ default_action :mount
+ allowed_actions :mount, :umount, :remount, :enable, :disable
+
def initialize(name, run_context=nil)
super
@mount_point = name
@@ -41,9 +44,7 @@ class Chef
@pass = 2
@mounted = false
@enabled = false
- @action = :mount
@supports = { :remount => false }
- @allowed_actions.push(:mount, :umount, :remount, :enable, :disable)
@username = nil
@password = nil
@domain = nil
diff --git a/lib/chef/resource/ohai.rb b/lib/chef/resource/ohai.rb
index be24e97bde..005b149442 100644
--- a/lib/chef/resource/ohai.rb
+++ b/lib/chef/resource/ohai.rb
@@ -26,11 +26,11 @@ class Chef
state_attrs :plugin
+ default_action :reload
+
def initialize(name, run_context=nil)
super
@name = name
- @allowed_actions.push(:reload)
- @action = :reload
@plugin = nil
end
diff --git a/lib/chef/resource/package.rb b/lib/chef/resource/package.rb
index 0f4a5359ae..4148144816 100644
--- a/lib/chef/resource/package.rb
+++ b/lib/chef/resource/package.rb
@@ -28,10 +28,11 @@ class Chef
state_attrs :version, :options
+ default_action :install
+ allowed_actions :install, :upgrade, :remove, :purge, :reconfig
+
def initialize(name, run_context=nil)
super
- @action = :install
- @allowed_actions.push(:install, :upgrade, :remove, :purge, :reconfig)
@candidate_version = nil
@options = nil
@package_name = name
diff --git a/lib/chef/resource/paludis_package.rb b/lib/chef/resource/paludis_package.rb
index 0907ba71a9..f0ddc5927a 100644
--- a/lib/chef/resource/paludis_package.rb
+++ b/lib/chef/resource/paludis_package.rb
@@ -26,9 +26,10 @@ class Chef
use_automatic_resource_name
provides :paludis_package, os: "linux"
+ allowed_actions :install, :remove, :upgrade
+
def initialize(name, run_context=nil)
super(name, run_context)
- @allowed_actions.push(:install, :remove, :upgrade)
@timeout = 3600
end
end
diff --git a/lib/chef/resource/reboot.rb b/lib/chef/resource/reboot.rb
index c2a4ff29e2..216367692f 100644
--- a/lib/chef/resource/reboot.rb
+++ b/lib/chef/resource/reboot.rb
@@ -26,10 +26,11 @@ class Chef
class Reboot < Chef::Resource
use_automatic_resource_name
+ allowed_actions :request_reboot, :reboot_now, :cancel
+
def initialize(name, run_context=nil)
super
@provider = Chef::Provider::Reboot
- @allowed_actions.push(:request_reboot, :reboot_now, :cancel)
@reason = "Reboot by Chef"
@delay_mins = 0
diff --git a/lib/chef/resource/registry_key.rb b/lib/chef/resource/registry_key.rb
index 5a700e5365..9ee031b751 100644
--- a/lib/chef/resource/registry_key.rb
+++ b/lib/chef/resource/registry_key.rb
@@ -27,6 +27,9 @@ class Chef
identity_attr :key
state_attrs :values
+ default_action :create
+ allowed_actions :create, :create_if_missing, :delete, :delete_key
+
# Some registry key data types may not be safely reported as json.
# Example (CHEF-5323):
#
@@ -60,12 +63,10 @@ class Chef
def initialize(name, run_context=nil)
super
- @action = :create
@architecture = :machine
@recursive = false
@key = name
@values, @unscrubbed_values = [], []
- @allowed_actions.push(:create, :create_if_missing, :delete, :delete_key)
end
def key(arg=nil)
diff --git a/lib/chef/resource/remote_directory.rb b/lib/chef/resource/remote_directory.rb
index a35e0995b0..bb052d3bd4 100644
--- a/lib/chef/resource/remote_directory.rb
+++ b/lib/chef/resource/remote_directory.rb
@@ -32,12 +32,14 @@ class Chef
state_attrs :files_owner, :files_group, :files_mode
+ default_action :create
+ allowed_actions :create, :create_if_missing, :delete
+
def initialize(name, run_context=nil)
super
@path = name
@source = ::File.basename(name)
@delete = false
- @action = :create
@recursive = true
@purge = false
@files_backup = 5
@@ -45,7 +47,6 @@ class Chef
@files_group = nil
@files_mode = 0644 unless Chef::Platform.windows?
@overwrite = true
- @allowed_actions.push(:create, :create_if_missing, :delete)
@cookbook = nil
end
diff --git a/lib/chef/resource/remote_file.rb b/lib/chef/resource/remote_file.rb
index 603151357c..99c21cae52 100644
--- a/lib/chef/resource/remote_file.rb
+++ b/lib/chef/resource/remote_file.rb
@@ -32,7 +32,6 @@ class Chef
def initialize(name, run_context=nil)
super
- @action = "create"
@source = []
@use_etag = true
@use_last_modified = true
diff --git a/lib/chef/resource/route.rb b/lib/chef/resource/route.rb
index aca2a52e87..3df767a128 100644
--- a/lib/chef/resource/route.rb
+++ b/lib/chef/resource/route.rb
@@ -28,11 +28,12 @@ class Chef
state_attrs :netmask, :gateway
+ default_action :add
+ allowed_actions :add, :delete
+
def initialize(name, run_context=nil)
super
@target = name
- @action = [:add]
- @allowed_actions.push(:add, :delete)
@netmask = nil
@gateway = nil
@metric = nil
diff --git a/lib/chef/resource/ruby_block.rb b/lib/chef/resource/ruby_block.rb
index e4667533f2..4ce7b2cee1 100644
--- a/lib/chef/resource/ruby_block.rb
+++ b/lib/chef/resource/ruby_block.rb
@@ -24,13 +24,13 @@ class Chef
class Resource
class RubyBlock < Chef::Resource
use_automatic_resource_name
+ default_action :run
+ allowed_actions :create, :run
identity_attr :block_name
def initialize(name, run_context=nil)
super
- @action = "run"
- @allowed_actions << :create << :run
@block_name = name
end
diff --git a/lib/chef/resource/scm.rb b/lib/chef/resource/scm.rb
index c97090d0a5..8379a3d8a9 100644
--- a/lib/chef/resource/scm.rb
+++ b/lib/chef/resource/scm.rb
@@ -28,6 +28,9 @@ class Chef
state_attrs :revision
+ default_action :sync
+ allowed_actions :checkout, :export, :sync, :diff, :log
+
def initialize(name, run_context=nil)
super
@destination = name
@@ -37,8 +40,6 @@ class Chef
@remote = "origin"
@ssh_wrapper = nil
@depth = nil
- @allowed_actions.push(:checkout, :export, :sync, :diff, :log)
- @action = [:sync]
@checkout_branch = "deploy"
@environment = nil
end
diff --git a/lib/chef/resource/service.rb b/lib/chef/resource/service.rb
index 6ef203db3b..9995eccb3f 100644
--- a/lib/chef/resource/service.rb
+++ b/lib/chef/resource/service.rb
@@ -28,6 +28,9 @@ class Chef
state_attrs :enabled, :running
+ default_action :nothing
+ allowed_actions :enable, :disable, :start, :stop, :restart, :reload
+
def initialize(name, run_context=nil)
super
@service_name = name
@@ -43,9 +46,7 @@ class Chef
@init_command = nil
@priority = nil
@timeout = nil
- @action = "nothing"
@supports = { :restart => false, :reload => false, :status => false }
- @allowed_actions.push(:enable, :disable, :start, :stop, :restart, :reload)
end
def service_name(arg=nil)
diff --git a/lib/chef/resource/subversion.rb b/lib/chef/resource/subversion.rb
index f1b0391a8c..e12d939ca6 100644
--- a/lib/chef/resource/subversion.rb
+++ b/lib/chef/resource/subversion.rb
@@ -23,12 +23,12 @@ class Chef
class Resource
class Subversion < Chef::Resource::Scm
use_automatic_resource_name
+ allowed_actions :force_export
def initialize(name, run_context=nil)
super
@svn_arguments = '--no-auth-cache'
@svn_info_args = '--no-auth-cache'
- allowed_actions << :force_export
end
# Override exception to strip password if any, so it won't appear in logs and different Chef notifications
diff --git a/lib/chef/resource/template.rb b/lib/chef/resource/template.rb
index 2c7ade6c29..a8e38aa5fb 100644
--- a/lib/chef/resource/template.rb
+++ b/lib/chef/resource/template.rb
@@ -34,7 +34,6 @@ class Chef
def initialize(name, run_context=nil)
super
- @action = "create"
@source = "#{::File.basename(name)}.erb"
@cookbook = nil
@local = false
diff --git a/lib/chef/resource/user.rb b/lib/chef/resource/user.rb
index b762001d4b..66c183ad7e 100644
--- a/lib/chef/resource/user.rb
+++ b/lib/chef/resource/user.rb
@@ -28,6 +28,9 @@ class Chef
use_automatic_resource_name
+ default_action :create
+ allowed_actions :create, :remove, :modify, :manage, :lock, :unlock
+
def initialize(name, run_context=nil)
super
@username = name
@@ -41,14 +44,12 @@ class Chef
@manage_home = false
@force = false
@non_unique = false
- @action = :create
@supports = {
:manage_home => false,
:non_unique => false
}
@iterations = 27855
@salt = nil
- @allowed_actions.push(:create, :remove, :modify, :manage, :lock, :unlock)
end
def username(arg=nil)
diff --git a/lib/chef/resource/windows_package.rb b/lib/chef/resource/windows_package.rb
index 8f9944a5af..bcf288e7b2 100644
--- a/lib/chef/resource/windows_package.rb
+++ b/lib/chef/resource/windows_package.rb
@@ -30,9 +30,10 @@ class Chef
provides :windows_package, os: "windows"
provides :package, os: "windows"
+ allowed_actions :install, :remove
+
def initialize(name, run_context=nil)
super
- @allowed_actions.push(:install, :remove)
@source ||= source(@package_name)
# Unique to this resource
diff --git a/lib/chef/resource/windows_service.rb b/lib/chef/resource/windows_service.rb
index 3f62a67a82..099042840c 100644
--- a/lib/chef/resource/windows_service.rb
+++ b/lib/chef/resource/windows_service.rb
@@ -29,13 +29,14 @@ class Chef
provides :windows_service, os: "windows"
provides :service, os: "windows"
+ allowed_actions :configure_startup
+
identity_attr :service_name
state_attrs :enabled, :running
def initialize(name, run_context=nil)
super
- @allowed_actions.push(:configure_startup)
@startup_type = :automatic
@run_as_user = ""
@run_as_password = ""
diff --git a/spec/integration/recipes/provider_choice.rb b/spec/integration/recipes/provider_choice.rb
index 5aa5dcb9b3..01537b2c05 100644
--- a/spec/integration/recipes/provider_choice.rb
+++ b/spec/integration/recipes/provider_choice.rb
@@ -6,12 +6,8 @@ describe "Recipe DSL methods" do
context "With resource class providing 'provider_thingy'" do
before :context do
class Chef::Resource::ProviderThingy < Chef::Resource
- def initialize(*args, &block)
- super
- @action = :create
- @allowed_actions = [ :create ]
- end
resource_name :provider_thingy
+ default_action :create
def to_s
"provider_thingy resource class"
end
diff --git a/spec/support/lib/chef/resource/zen_master.rb b/spec/support/lib/chef/resource/zen_master.rb
index ed5b1b3add..82cef0bb50 100644
--- a/spec/support/lib/chef/resource/zen_master.rb
+++ b/spec/support/lib/chef/resource/zen_master.rb
@@ -23,13 +23,10 @@ class Chef
class Resource
class ZenMaster < Chef::Resource
use_automatic_resource_name
+ allowed_actions :win, :score
attr_reader :peace
- def initialize(name, run_context=nil)
- super
- allowed_actions << :win << :score
- end
def peace(tf)
@peace = tf
diff --git a/spec/unit/lwrp_spec.rb b/spec/unit/lwrp_spec.rb
index 8cac70e800..f83678308a 100644
--- a/spec/unit/lwrp_spec.rb
+++ b/spec/unit/lwrp_spec.rb
@@ -146,7 +146,7 @@ describe "LWRP" do
it "Should load the old content, and not the new" do
resource = Chef::Resource.resource_for_node(:lwrp_foo, Chef::Node.new)
expect(resource).to eq @original_resource
- expect(resource.default_action).to eq([:pass_buck])
+ expect(resource.default_action).to eq(:pass_buck)
expect(Chef.method_defined?(:method_created_by_override_lwrp_foo)).to be_falsey
end
end
@@ -185,7 +185,7 @@ describe "LWRP" do
end
it "should set the specified action as the default action" do
- expect(get_lwrp(:lwrp_foo).new("blah").action).to eq([:pass_buck])
+ expect(get_lwrp(:lwrp_foo).new("blah").action).to eq(:pass_buck)
end
it "should create a method for each attribute" do
@@ -281,7 +281,7 @@ describe "LWRP" do
end
it "delegates #default_action to the parent" do
- expect(child.default_action).to eq([:eat])
+ expect(child.default_action).to eq(:eat)
end
end
@@ -298,7 +298,7 @@ describe "LWRP" do
end
it "does not delegate #default_action to the parent" do
- expect(child.default_action).to eq([:dont_eat])
+ expect(child.default_action).to eq(:dont_eat)
end
end
diff --git a/spec/unit/provider/deploy_spec.rb b/spec/unit/provider/deploy_spec.rb
index a2997d8399..63658ac601 100644
--- a/spec/unit/provider/deploy_spec.rb
+++ b/spec/unit/provider/deploy_spec.rb
@@ -622,7 +622,7 @@ describe Chef::Provider::Deploy do
gems = @provider.send(:gem_packages)
- expect(gems.map { |g| g.action }).to eq([[:install]])
+ expect(gems.map { |g| g.action }).to eq([:install])
expect(gems.map { |g| g.name }).to eq(%w{eventmachine})
expect(gems.map { |g| g.version }).to eq(%w{0.12.9})
end
diff --git a/spec/unit/recipe_spec.rb b/spec/unit/recipe_spec.rb
index 59d05298c3..86f9a2d1d7 100644
--- a/spec/unit/recipe_spec.rb
+++ b/spec/unit/recipe_spec.rb
@@ -412,7 +412,7 @@ describe Chef::Recipe do
end
it "does not copy the action from the first resource" do
- expect(original_resource.action).to eq([:score])
+ expect(original_resource.action).to eq(:score)
expect(duplicated_resource.action).to eq(:nothing)
end
@@ -508,7 +508,7 @@ describe Chef::Recipe do
recipe.from_file(File.join(CHEF_SPEC_DATA, "recipes", "test.rb"))
res = recipe.resources(:file => "/etc/nsswitch.conf")
expect(res.name).to eql("/etc/nsswitch.conf")
- expect(res.action).to eql([:create])
+ expect(res.action).to eql(:create)
expect(res.owner).to eql("root")
expect(res.group).to eql("root")
expect(res.mode).to eql(0644)
diff --git a/spec/unit/resource/breakpoint_spec.rb b/spec/unit/resource/breakpoint_spec.rb
index ed1f3ebcd5..9c867ebcc7 100644
--- a/spec/unit/resource/breakpoint_spec.rb
+++ b/spec/unit/resource/breakpoint_spec.rb
@@ -37,7 +37,7 @@ describe Chef::Resource::Breakpoint do
end
it "defaults to the break action" do
- expect(@breakpoint.action).to eq("break")
+ expect(@breakpoint.action).to eq(:break)
end
it "names itself after the line number of the file where it's created" do
diff --git a/spec/unit/resource/erl_call_spec.rb b/spec/unit/resource/erl_call_spec.rb
index 8ec182665f..008d27372a 100644
--- a/spec/unit/resource/erl_call_spec.rb
+++ b/spec/unit/resource/erl_call_spec.rb
@@ -35,7 +35,7 @@ describe Chef::Resource::ErlCall do
end
it "should have a default action of run" do
- expect(@resource.action).to eql("run")
+ expect(@resource.action).to eql(:run)
end
it "should accept run as an action" do
diff --git a/spec/unit/resource/file_spec.rb b/spec/unit/resource/file_spec.rb
index db52e35004..dd20f5f03a 100644
--- a/spec/unit/resource/file_spec.rb
+++ b/spec/unit/resource/file_spec.rb
@@ -29,7 +29,7 @@ describe Chef::Resource::File do
end
it "should have a default action of 'create'" do
- expect(@resource.action).to eql("create")
+ expect(@resource.action).to eql(:create)
end
it "should have a default content of nil" do
diff --git a/spec/unit/resource/route_spec.rb b/spec/unit/resource/route_spec.rb
index ec1d369932..ffb9304511 100644
--- a/spec/unit/resource/route_spec.rb
+++ b/spec/unit/resource/route_spec.rb
@@ -35,7 +35,7 @@ describe Chef::Resource::Route do
end
it "should have a default action of 'add'" do
- expect(@resource.action).to eql([:add])
+ expect(@resource.action).to eql(:add)
end
it "should accept add or delete for action" do
diff --git a/spec/unit/resource/ruby_block_spec.rb b/spec/unit/resource/ruby_block_spec.rb
index 9f19fecd4f..5d83f7e367 100644
--- a/spec/unit/resource/ruby_block_spec.rb
+++ b/spec/unit/resource/ruby_block_spec.rb
@@ -31,7 +31,7 @@ describe Chef::Resource::RubyBlock do
end
it "should have a default action of 'create'" do
- expect(@resource.action).to eql("run")
+ expect(@resource.action).to eql(:run)
end
it "should have a resource name of :ruby_block" do
@@ -46,7 +46,7 @@ describe Chef::Resource::RubyBlock do
it "allows the action to be 'create'" do
@resource.action :create
- expect(@resource.action).to eq([:create])
+ expect(@resource.action).to eq(:create)
end
describe "when it has been initialized with block code" do
diff --git a/spec/unit/resource/windows_service_spec.rb b/spec/unit/resource/windows_service_spec.rb
index 45a295c24e..8866cad1bf 100644
--- a/spec/unit/resource/windows_service_spec.rb
+++ b/spec/unit/resource/windows_service_spec.rb
@@ -44,6 +44,6 @@ describe Chef::Resource::WindowsService, "initialize" do
it "allows the action to be 'configure_startup'" do
resource.action :configure_startup
- expect(resource.action).to eq([:configure_startup])
+ expect(resource.action).to eq(:configure_startup)
end
end