diff options
author | John Keiser <john@johnkeiser.com> | 2015-06-01 08:11:42 -0700 |
---|---|---|
committer | John Keiser <john@johnkeiser.com> | 2015-06-01 08:11:42 -0700 |
commit | e3c455bd083ec7f87b8935a54cfc53692fdb1311 (patch) | |
tree | a3908bd1d6e95aafe5ed337a1aa88cd29f4cc4ec | |
parent | 9f8d3fbb943206c27364593b49b875f7254b77be (diff) | |
parent | 305052f3dc2c4f9f705706ec478d84ee830e6fad (diff) | |
download | chef-e3c455bd083ec7f87b8935a54cfc53692fdb1311.tar.gz |
Merge branch 'jk/resource_name'
82 files changed, 321 insertions, 288 deletions
diff --git a/DOC_CHANGES.md b/DOC_CHANGES.md index b0a8e0aaaf..b5eb124f26 100644 --- a/DOC_CHANGES.md +++ b/DOC_CHANGES.md @@ -6,19 +6,24 @@ Example Doc Change: Description of the required change. --> -### Resources must now use `provides` to declare recipe DSL +### Resources must now use `resource_name` (or `provides`) to declare recipe DSL Resources declared in `Chef::Resource` namespace will no longer get recipe DSL -automatically. Instead, explicit `provides` is required in order to have DSL: +automatically. Instead, `resource_name` is required in order to have DSL: ```ruby module MyModule class MyResource < Chef::Resource - provides :my_resource + use_automatic_resource_name # Names the resource "my_resource" end 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 +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. diff --git a/lib/chef/provider/service/macosx.rb b/lib/chef/provider/service/macosx.rb index 7cfe57a92a..7324822eff 100644 --- a/lib/chef/provider/service/macosx.rb +++ b/lib/chef/provider/service/macosx.rb @@ -28,8 +28,8 @@ class Chef class Service class Macosx < Chef::Provider::Service::Simple - provides :service, os: "darwin" provides :macosx_service, os: "darwin" + provides :service, os: "darwin" def self.gather_plist_dirs locations = %w{/Library/LaunchAgents diff --git a/lib/chef/resource.rb b/lib/chef/resource.rb index 18759b29f7..ac98df5513 100644 --- a/lib/chef/resource.rb +++ b/lib/chef/resource.rb @@ -57,6 +57,8 @@ class Chef include Chef::Mixin::ShellOut include Chef::Mixin::PowershellOut + NULL_ARG = Object.new + # # The node the current Chef run is using. # @@ -88,7 +90,6 @@ class Chef run_context.resource_collection.find(*args) end - # # Resource User Interface (for users) # @@ -606,7 +607,7 @@ class Chef # def to_s - "#{@resource_name}[#{@name}]" + "#{resource_name}[#{name}]" end def to_text @@ -825,23 +826,15 @@ class Chef end # - # The DSL name of this resource (e.g. `package` or `yum_package`) - # - # @return [String] The DSL name of this resource. - def self.dsl_name - Chef::Log.deprecation "Resource.dsl_name is deprecated and will be removed in Chef 11. Use resource.resource_name instead." - if name - name = self.name.split('::')[-1] - convert_to_snake_case(name) - end - end - + # The display name of this resource type, for printing purposes. # - # The name of this resource (e.g. `file`) + # Will be used to print out the resource in messages, e.g. resource_name[name] # - # @return [String] The name of this resource. + # @return [Symbol] The name of this resource type (e.g. `:execute`). # - attr_reader :resource_name + def resource_name + @resource_name || self.class.resource_name + end # # Sets a list of capabilities of the real resource. For example, `:remount` @@ -873,23 +866,85 @@ class Chef nil end - # - # The module where Chef should look for providers for this resource. - # The provider for `MyResource` will be looked up using - # `provider_base::MyResource`. Defaults to `Chef::Provider`. - # - # @param arg [Module] The module containing providers for this resource - # @return [Module] The module containing providers for this resource - # - # @example - # class MyResource < Chef::Resource - # provider_base Chef::Provider::Deploy - # # ...other stuff - # end - # - def self.provider_base(arg=nil) - @provider_base ||= arg - @provider_base ||= Chef::Provider + # Provider lookup and naming + class<<self + # + # The DSL name of this resource (e.g. `package` or `yum_package`) + # + # @return [String] The DSL name of this resource. + # + # @deprecated Use resource_name instead. + # + def dsl_name + Chef::Log.deprecation "Resource.dsl_name is deprecated and will be removed in Chef 13. Use resource_name instead." + if name + name = self.name.split('::')[-1] + convert_to_snake_case(name) + end + end + + # + # The display name of this resource type, for printing purposes. + # + # This also automatically calls "provides" to provide DSL with the given + # name. + # + # @param value [Symbol] The desired name of this resource type (e.g. + # `execute`). + # + # @return [Symbol] The name of this resource type (e.g. `:execute`). + # + def resource_name(value=NULL_ARG) + if value != NULL_ARG + @resource_name = value.to_sym + provides self.resource_name + end + # Backcompat: set resource name for classes in Chef::Resource automatically + if !@resource_name && self.name + chef, resource, class_name, *extra = self.name.split('::') + if chef == 'Chef' && resource == 'Resource' && extra.size == 0 + @resource_name = convert_to_snake_case(self.name.split('::')[-1]) + end + end + @resource_name + end + alias :resource_name= :resource_name + + # + # Use the class name as the resource name. + # + # Munges the last part of the class name from camel case to snake case, + # and sets the resource_name to that: + # + # A::B::BlahDBlah -> blah_d_blah + # + def use_automatic_resource_name + automatic_name = convert_to_snake_case(self.name.split('::')[-1]) + resource_name automatic_name + end + + # + # The module where Chef should look for providers for this resource. + # The provider for `MyResource` will be looked up using + # `provider_base::MyResource`. Defaults to `Chef::Provider`. + # + # @param arg [Module] The module containing providers for this resource + # @return [Module] The module containing providers for this resource + # + # @example + # class MyResource < Chef::Resource + # provider_base Chef::Provider::Deploy + # # ...other stuff + # end + # + # @deprecated Use `provides` on the provider, or `provider` on the resource, instead. + # + def provider_base(arg=nil) + if arg + Chef::Log.deprecation("Resource.provider_base is deprecated and will be removed in Chef 13. Use provides on the provider, or provider on the resource, instead.") + end + @provider_base ||= arg || Chef::Provider + end end @@ -1162,7 +1217,7 @@ class Chef def const_missing(class_name) if deprecated_constants[class_name.to_sym] - Chef::Log.deprecation("Using an LWRP by its name (#{class_name}) directly is no longer supported in Chef 12 and will be removed. Use Chef::Resource.resource_for_node(node, name) instead.") + Chef::Log.deprecation("Using an LWRP by its name (#{class_name}) directly is no longer supported in Chef 13 and will be removed. Use Chef::Resource.resource_for_node(node, name) instead.") deprecated_constants[class_name.to_sym] else raise NameError, "uninitialized constant Chef::Resource::#{class_name}" diff --git a/lib/chef/resource/apt_package.rb b/lib/chef/resource/apt_package.rb index f944825ac3..83bb6906d4 100644 --- a/lib/chef/resource/apt_package.rb +++ b/lib/chef/resource/apt_package.rb @@ -23,12 +23,11 @@ class Chef class Resource class AptPackage < Chef::Resource::Package - provides :apt_package + use_automatic_resource_name provides :package, os: "linux", platform_family: [ "debian" ] def initialize(name, run_context=nil) super - @resource_name = :apt_package @default_release = nil end diff --git a/lib/chef/resource/bash.rb b/lib/chef/resource/bash.rb index 366d8c7bd6..554d2de924 100644 --- a/lib/chef/resource/bash.rb +++ b/lib/chef/resource/bash.rb @@ -22,11 +22,10 @@ require 'chef/provider/script' class Chef class Resource class Bash < Chef::Resource::Script - provides :bash + use_automatic_resource_name def initialize(name, run_context=nil) super - @resource_name = :bash @interpreter = "bash" end diff --git a/lib/chef/resource/batch.rb b/lib/chef/resource/batch.rb index c091ec56b6..8a19e04174 100644 --- a/lib/chef/resource/batch.rb +++ b/lib/chef/resource/batch.rb @@ -22,10 +22,11 @@ class Chef class Resource class Batch < Chef::Resource::WindowsScript + use_automatic_resource_name provides :batch, os: "windows" def initialize(name, run_context=nil) - super(name, run_context, :batch, "cmd.exe") + super(name, run_context, nil, "cmd.exe") end end diff --git a/lib/chef/resource/bff_package.rb b/lib/chef/resource/bff_package.rb index d4139e7ffe..f31fe6a0d8 100644 --- a/lib/chef/resource/bff_package.rb +++ b/lib/chef/resource/bff_package.rb @@ -22,13 +22,7 @@ require 'chef/provider/package/aix' class Chef class Resource class BffPackage < Chef::Resource::Package - provides :bff_package - - def initialize(name, run_context=nil) - super - @resource_name = :bff_package - end - + use_automatic_resource_name end end end diff --git a/lib/chef/resource/breakpoint.rb b/lib/chef/resource/breakpoint.rb index 5a55858f71..dca4fd04a6 100644 --- a/lib/chef/resource/breakpoint.rb +++ b/lib/chef/resource/breakpoint.rb @@ -22,14 +22,12 @@ require 'chef/resource' class Chef class Resource class Breakpoint < Chef::Resource - provides :breakpoint + use_automatic_resource_name def initialize(action="break", *args) - @name = caller.first - super(@name, *args) + super(caller.first, *args) @action = "break" @allowed_actions << :break - @resource_name = :breakpoint end end end diff --git a/lib/chef/resource/chef_gem.rb b/lib/chef/resource/chef_gem.rb index 59f575a524..e4e3ccfbab 100644 --- a/lib/chef/resource/chef_gem.rb +++ b/lib/chef/resource/chef_gem.rb @@ -23,11 +23,10 @@ class Chef class Resource class ChefGem < Chef::Resource::Package::GemPackage - provides :chef_gem + use_automatic_resource_name def initialize(name, run_context=nil) super - @resource_name = :chef_gem @compile_time = Chef::Config[:chef_gem_compile_time] @gem_binary = RbConfig::CONFIG['bindir'] + "/gem" end diff --git a/lib/chef/resource/cookbook_file.rb b/lib/chef/resource/cookbook_file.rb index 7be353b648..ade9c0e3e3 100644 --- a/lib/chef/resource/cookbook_file.rb +++ b/lib/chef/resource/cookbook_file.rb @@ -27,12 +27,11 @@ class Chef class CookbookFile < Chef::Resource::File include Chef::Mixin::Securable - provides :cookbook_file + use_automatic_resource_name def initialize(name, run_context=nil) super @provider = Chef::Provider::CookbookFile - @resource_name = :cookbook_file @action = "create" @source = ::File.basename(name) @cookbook = nil diff --git a/lib/chef/resource/cron.rb b/lib/chef/resource/cron.rb index cb16506012..daf462866e 100644 --- a/lib/chef/resource/cron.rb +++ b/lib/chef/resource/cron.rb @@ -27,11 +27,10 @@ class Chef state_attrs :minute, :hour, :day, :month, :weekday, :user - provides :cron + use_automatic_resource_name def initialize(name, run_context=nil) super - @resource_name = :cron @action = :create @allowed_actions.push(:create, :delete) @minute = "*" diff --git a/lib/chef/resource/csh.rb b/lib/chef/resource/csh.rb index d37f1a8e0c..1c89f2aca6 100644 --- a/lib/chef/resource/csh.rb +++ b/lib/chef/resource/csh.rb @@ -22,11 +22,10 @@ require 'chef/provider/script' class Chef class Resource class Csh < Chef::Resource::Script - provides :csh + use_automatic_resource_name def initialize(name, run_context=nil) super - @resource_name = :csh @interpreter = "csh" end diff --git a/lib/chef/resource/deploy.rb b/lib/chef/resource/deploy.rb index 55e3547b25..8d007df348 100644 --- a/lib/chef/resource/deploy.rb +++ b/lib/chef/resource/deploy.rb @@ -50,9 +50,7 @@ class Chef # release directory. Callback files can contain chef code (resources, etc.) # class Deploy < Chef::Resource - provides :deploy - - provider_base Chef::Provider::Deploy + use_automatic_resource_name identity_attr :repository @@ -60,7 +58,6 @@ class Chef def initialize(name, run_context=nil) super - @resource_name = :deploy @deploy_to = name @environment = nil @repository_cache = 'cached-copy' @@ -282,6 +279,12 @@ class Chef ) end + # This is to support "provider :revision" without deprecation warnings. + # Do NOT copy this. + def self.provider_base + Chef::Provider::Deploy + end + def svn_force_export(arg=nil) set_or_return( :svn_force_export, diff --git a/lib/chef/resource/deploy_revision.rb b/lib/chef/resource/deploy_revision.rb index e144ce2162..86a8631ba7 100644 --- a/lib/chef/resource/deploy_revision.rb +++ b/lib/chef/resource/deploy_revision.rb @@ -23,22 +23,14 @@ class Chef # deployment strategy (provider) class DeployRevision < Chef::Resource::Deploy - provides :deploy_revision + use_automatic_resource_name - def initialize(*args, &block) - super - @resource_name = :deploy_revision - end end class DeployBranch < Chef::Resource::DeployRevision - provides :deploy_branch + use_automatic_resource_name - def initialize(*args, &block) - super - @resource_name = :deploy_branch - end end end diff --git a/lib/chef/resource/directory.rb b/lib/chef/resource/directory.rb index 1ab7f0d16d..ea1805353d 100644 --- a/lib/chef/resource/directory.rb +++ b/lib/chef/resource/directory.rb @@ -32,11 +32,10 @@ class Chef include Chef::Mixin::Securable - provides :directory + use_automatic_resource_name def initialize(name, run_context=nil) super - @resource_name = :directory @path = name @action = :create @recursive = false diff --git a/lib/chef/resource/dpkg_package.rb b/lib/chef/resource/dpkg_package.rb index 35a47e8a82..e0b86947f1 100644 --- a/lib/chef/resource/dpkg_package.rb +++ b/lib/chef/resource/dpkg_package.rb @@ -23,13 +23,9 @@ class Chef class Resource class DpkgPackage < Chef::Resource::Package + use_automatic_resource_name provides :dpkg_package, os: "linux" - def initialize(name, run_context=nil) - super - @resource_name = :dpkg_package - end - end end end diff --git a/lib/chef/resource/dsc_resource.rb b/lib/chef/resource/dsc_resource.rb index 912b683434..cdfdff54f0 100644 --- a/lib/chef/resource/dsc_resource.rb +++ b/lib/chef/resource/dsc_resource.rb @@ -21,6 +21,7 @@ class Chef class Resource
class DscResource < Chef::Resource
+ use_automatic_resource_name
provides :dsc_resource, os: "windows"
include Chef::DSL::Powershell
@@ -28,7 +29,6 @@ class Chef def initialize(name, run_context)
super
@properties = {}
- @resource_name = :dsc_resource
@resource = nil
@allowed_actions.push(:run)
@action = :run
diff --git a/lib/chef/resource/dsc_script.rb b/lib/chef/resource/dsc_script.rb index cf96ef6b7f..4eabb92438 100644 --- a/lib/chef/resource/dsc_script.rb +++ b/lib/chef/resource/dsc_script.rb @@ -22,13 +22,13 @@ class Chef class Resource class DscScript < Chef::Resource + use_automatic_resource_name provides :dsc_script, platform: "windows" def initialize(name, run_context=nil) super @allowed_actions.push(:run) @action = :run - @resource_name = :dsc_script @imports = {} end diff --git a/lib/chef/resource/easy_install_package.rb b/lib/chef/resource/easy_install_package.rb index 5286e9a289..2483b0a8b7 100644 --- a/lib/chef/resource/easy_install_package.rb +++ b/lib/chef/resource/easy_install_package.rb @@ -22,12 +22,7 @@ class Chef class Resource class EasyInstallPackage < Chef::Resource::Package - provides :easy_install_package - - def initialize(name, run_context=nil) - super - @resource_name = :easy_install_package - end + use_automatic_resource_name def easy_install_binary(arg=nil) set_or_return( diff --git a/lib/chef/resource/env.rb b/lib/chef/resource/env.rb index 2072ae5d80..d74b4feee0 100644 --- a/lib/chef/resource/env.rb +++ b/lib/chef/resource/env.rb @@ -25,11 +25,11 @@ class Chef state_attrs :value + use_automatic_resource_name provides :env, os: "windows" def initialize(name, run_context=nil) super - @resource_name = :env @key_name = name @value = nil @action = :create diff --git a/lib/chef/resource/erl_call.rb b/lib/chef/resource/erl_call.rb index 75422c55a1..c6d45c7aca 100644 --- a/lib/chef/resource/erl_call.rb +++ b/lib/chef/resource/erl_call.rb @@ -23,7 +23,7 @@ require 'chef/provider/erl_call' class Chef class Resource class ErlCall < Chef::Resource - provides :erl_call + use_automatic_resource_name # erl_call : http://erlang.org/doc/man/erl_call.html @@ -31,7 +31,6 @@ class Chef def initialize(name, run_context=nil) super - @resource_name = :erl_call @code = "q()." # your erlang code goes here @cookie = nil # cookie of the erlang node diff --git a/lib/chef/resource/execute.rb b/lib/chef/resource/execute.rb index 8fc97d748f..c0263501c5 100644 --- a/lib/chef/resource/execute.rb +++ b/lib/chef/resource/execute.rb @@ -23,7 +23,7 @@ require 'chef/provider/execute' class Chef class Resource class Execute < Chef::Resource - provides :execute + use_automatic_resource_name identity_attr :command @@ -35,7 +35,6 @@ class Chef def initialize(name, run_context=nil) super - @resource_name = :execute @command = name @backup = 5 @action = "run" diff --git a/lib/chef/resource/file.rb b/lib/chef/resource/file.rb index b36fcc2135..32fd984f8c 100644 --- a/lib/chef/resource/file.rb +++ b/lib/chef/resource/file.rb @@ -47,11 +47,10 @@ class Chef # @returns [String] Checksum of the file we actually rendered attr_accessor :final_checksum - provides :file + use_automatic_resource_name def initialize(name, run_context=nil) super - @resource_name = :file @path = name @backup = 5 @action = "create" diff --git a/lib/chef/resource/freebsd_package.rb b/lib/chef/resource/freebsd_package.rb index 9c8db506f8..f89e1813b9 100644 --- a/lib/chef/resource/freebsd_package.rb +++ b/lib/chef/resource/freebsd_package.rb @@ -29,13 +29,9 @@ class Chef class FreebsdPackage < Chef::Resource::Package include Chef::Mixin::ShellOut + use_automatic_resource_name provides :package, platform: "freebsd" - def initialize(name, run_context=nil) - super - @resource_name = :freebsd_package - end - def after_created assign_provider end diff --git a/lib/chef/resource/gem_package.rb b/lib/chef/resource/gem_package.rb index 0e838ca040..5bd3a89100 100644 --- a/lib/chef/resource/gem_package.rb +++ b/lib/chef/resource/gem_package.rb @@ -22,11 +22,10 @@ class Chef class Resource class GemPackage < Chef::Resource::Package - provides :gem_package + use_automatic_resource_name def initialize(name, run_context=nil) super - @resource_name = :gem_package @clear_sources = false end diff --git a/lib/chef/resource/git.rb b/lib/chef/resource/git.rb index 7156873315..1229914766 100644 --- a/lib/chef/resource/git.rb +++ b/lib/chef/resource/git.rb @@ -22,11 +22,10 @@ class Chef class Resource class Git < Chef::Resource::Scm - provides :git + use_automatic_resource_name def initialize(name, run_context=nil) super - @resource_name = :git @additional_remotes = Hash[] end diff --git a/lib/chef/resource/group.rb b/lib/chef/resource/group.rb index 9e8f1309b0..64062dbe8f 100644 --- a/lib/chef/resource/group.rb +++ b/lib/chef/resource/group.rb @@ -25,11 +25,10 @@ class Chef state_attrs :members - provides :group + use_automatic_resource_name def initialize(name, run_context=nil) super - @resource_name = :group @group_name = name @gid = nil @members = [] diff --git a/lib/chef/resource/homebrew_package.rb b/lib/chef/resource/homebrew_package.rb index 73409b13ac..fe0bd89ced 100644 --- a/lib/chef/resource/homebrew_package.rb +++ b/lib/chef/resource/homebrew_package.rb @@ -25,12 +25,11 @@ class Chef class Resource class HomebrewPackage < Chef::Resource::Package - provides :homebrew_package + use_automatic_resource_name provides :package, os: "darwin" def initialize(name, run_context=nil) super - @resource_name = :homebrew_package @homebrew_user = nil end diff --git a/lib/chef/resource/http_request.rb b/lib/chef/resource/http_request.rb index 5986ebd4a0..ea22a26456 100644 --- a/lib/chef/resource/http_request.rb +++ b/lib/chef/resource/http_request.rb @@ -23,13 +23,12 @@ require 'chef/provider/http_request' class Chef class Resource class HttpRequest < Chef::Resource - provides :http_request + use_automatic_resource_name identity_attr :url def initialize(name, run_context=nil) super - @resource_name = :http_request @message = name @url = nil @action = :get diff --git a/lib/chef/resource/ifconfig.rb b/lib/chef/resource/ifconfig.rb index 60feba1704..18a688c2a5 100644 --- a/lib/chef/resource/ifconfig.rb +++ b/lib/chef/resource/ifconfig.rb @@ -22,7 +22,7 @@ require 'chef/resource' class Chef class Resource class Ifconfig < Chef::Resource - provides :ifconfig + use_automatic_resource_name identity_attr :device @@ -30,7 +30,6 @@ class Chef def initialize(name, run_context=nil) super - @resource_name = :ifconfig @target = name @action = :add @allowed_actions.push(:add, :delete, :enable, :disable) diff --git a/lib/chef/resource/ips_package.rb b/lib/chef/resource/ips_package.rb index c0e699e31a..93b554eb6c 100644 --- a/lib/chef/resource/ips_package.rb +++ b/lib/chef/resource/ips_package.rb @@ -23,11 +23,11 @@ class Chef class Resource class IpsPackage < ::Chef::Resource::Package + use_automatic_resource_name provides :ips_package, os: "solaris2" def initialize(name, run_context = nil) super(name, run_context) - @resource_name = :ips_package @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 30f8ec86d1..84a2b94ca1 100644 --- a/lib/chef/resource/link.rb +++ b/lib/chef/resource/link.rb @@ -25,7 +25,7 @@ class Chef class Link < Chef::Resource include Chef::Mixin::Securable - provides :link + use_automatic_resource_name identity_attr :target_file @@ -34,7 +34,6 @@ class Chef def initialize(name, run_context=nil) verify_links_supported! super - @resource_name = :link @to = nil @action = :create @link_type = :symbolic diff --git a/lib/chef/resource/log.rb b/lib/chef/resource/log.rb index 87be01aaa9..5b4081b0fe 100644 --- a/lib/chef/resource/log.rb +++ b/lib/chef/resource/log.rb @@ -23,7 +23,7 @@ require 'chef/provider/log' class Chef class Resource class Log < Chef::Resource - provides :log + use_automatic_resource_name identity_attr :message @@ -49,7 +49,6 @@ class Chef # node<Chef::Node>:: Node where resource will be used def initialize(name, run_context=nil) super - @resource_name = :log @level = :info @action = :write @allowed_actions.push(:write) diff --git a/lib/chef/resource/lwrp_base.rb b/lib/chef/resource/lwrp_base.rb index a42261cfc4..129fc38d6f 100644 --- a/lib/chef/resource/lwrp_base.rb +++ b/lib/chef/resource/lwrp_base.rb @@ -35,8 +35,6 @@ class Chef # so attributes, default action, etc. can be defined with pleasing syntax. class LWRPBase < Resource - NULL_ARG = Object.new - # Class methods class <<self @@ -55,12 +53,11 @@ class Chef # We load the class first to give it a chance to set its own name resource_class = Class.new(self) - resource_class.resource_name = resource_name + resource_class.resource_name resource_name.to_sym resource_class.run_context = run_context - resource_class.provides resource_name.to_sym resource_class.class_from_file(filename) - # Respect resource_name set inside the LWRP + # Make a useful string for the class (rather than <Class:312894723894>) resource_class.instance_eval do define_singleton_method(:to_s) do "LWRP resource #{resource_name} from cookbook #{cookbook_name}" @@ -77,16 +74,6 @@ class Chef resource_class end - def resource_name(arg = NULL_ARG) - if arg.equal?(NULL_ARG) - @resource_name - else - @resource_name = arg - end - end - - alias_method :resource_name=, :resource_name - # Define an attribute on this resource, including optional validation # parameters. def attribute(attr_name, validation_opts={}) @@ -166,16 +153,8 @@ class Chef private # Default initializer. Sets the default action and allowed actions. - def initialize(name, run_context=nil) - super(name, run_context) - - # Raise an exception if the resource_name was not defined - if self.class.resource_name.nil? - raise Chef::Exceptions::InvalidResourceSpecification, - "You must specify `resource_name'!" - end - - @resource_name = self.class.resource_name.to_sym + def initialize(*args, &block) + super @action = self.class.default_action allowed_actions.push(self.class.actions).flatten! end diff --git a/lib/chef/resource/macosx_service.rb b/lib/chef/resource/macosx_service.rb index 879ea99cf8..29da2e6309 100644 --- a/lib/chef/resource/macosx_service.rb +++ b/lib/chef/resource/macosx_service.rb @@ -22,8 +22,9 @@ class Chef class Resource class MacosxService < Chef::Resource::Service - provides :service, os: "darwin" + use_automatic_resource_name provides :macosx_service, os: "darwin" + provides :service, os: "darwin" identity_attr :service_name @@ -31,7 +32,6 @@ class Chef def initialize(name, run_context=nil) super - @resource_name = :macosx_service @plist = nil @session_type = nil end diff --git a/lib/chef/resource/macports_package.rb b/lib/chef/resource/macports_package.rb index 0d4e5dec65..3ccf831cf2 100644 --- a/lib/chef/resource/macports_package.rb +++ b/lib/chef/resource/macports_package.rb @@ -20,13 +20,8 @@ class Chef class Resource class MacportsPackage < Chef::Resource::Package - provides :macports_package + use_automatic_resource_name provides :package, os: "darwin" - - def initialize(name, run_context=nil) - super - @resource_name = :macports_package - end end end end diff --git a/lib/chef/resource/mdadm.rb b/lib/chef/resource/mdadm.rb index 971b6c51b4..8f19410b9c 100644 --- a/lib/chef/resource/mdadm.rb +++ b/lib/chef/resource/mdadm.rb @@ -27,11 +27,10 @@ class Chef state_attrs :devices, :level, :chunk - provides :mdadm + use_automatic_resource_name def initialize(name, run_context=nil) super - @resource_name = :mdadm @chunk = 16 @devices = [] diff --git a/lib/chef/resource/mount.rb b/lib/chef/resource/mount.rb index 142dec87f7..f8a0cd157b 100644 --- a/lib/chef/resource/mount.rb +++ b/lib/chef/resource/mount.rb @@ -27,11 +27,10 @@ class Chef state_attrs :mount_point, :device_type, :fstype, :username, :password, :domain - provides :mount + use_automatic_resource_name def initialize(name, run_context=nil) super - @resource_name = :mount @mount_point = name @device = nil @device_type = :device diff --git a/lib/chef/resource/ohai.rb b/lib/chef/resource/ohai.rb index e2d12ce395..be24e97bde 100644 --- a/lib/chef/resource/ohai.rb +++ b/lib/chef/resource/ohai.rb @@ -20,7 +20,7 @@ class Chef class Resource class Ohai < Chef::Resource - provides :ohai + use_automatic_resource_name identity_attr :name @@ -28,7 +28,6 @@ class Chef def initialize(name, run_context=nil) super - @resource_name = :ohai @name = name @allowed_actions.push(:reload) @action = :reload diff --git a/lib/chef/resource/openbsd_package.rb b/lib/chef/resource/openbsd_package.rb index 20a2523e3a..1071958cd2 100644 --- a/lib/chef/resource/openbsd_package.rb +++ b/lib/chef/resource/openbsd_package.rb @@ -28,13 +28,9 @@ class Chef class OpenbsdPackage < Chef::Resource::Package include Chef::Mixin::ShellOut + use_automatic_resource_name provides :package, os: "openbsd" - def initialize(name, run_context=nil) - super - @resource_name = :openbsd_package - end - def after_created assign_provider end @@ -48,4 +44,3 @@ class Chef end end end - diff --git a/lib/chef/resource/package.rb b/lib/chef/resource/package.rb index 5bea894a02..0f4a5359ae 100644 --- a/lib/chef/resource/package.rb +++ b/lib/chef/resource/package.rb @@ -22,7 +22,7 @@ require 'chef/resource' class Chef class Resource class Package < Chef::Resource - provides :package + use_automatic_resource_name identity_attr :package_name @@ -35,7 +35,6 @@ class Chef @candidate_version = nil @options = nil @package_name = name - @resource_name = :package @response_file = nil @response_file_variables = Hash.new @source = nil diff --git a/lib/chef/resource/pacman_package.rb b/lib/chef/resource/pacman_package.rb index 4c45dd004f..222fb3c78e 100644 --- a/lib/chef/resource/pacman_package.rb +++ b/lib/chef/resource/pacman_package.rb @@ -22,13 +22,9 @@ class Chef class Resource class PacmanPackage < Chef::Resource::Package + use_automatic_resource_name provides :pacman_package, os: "linux" - def initialize(name, run_context=nil) - super - @resource_name = :pacman_package - end - end end end diff --git a/lib/chef/resource/paludis_package.rb b/lib/chef/resource/paludis_package.rb index 552c96857a..0907ba71a9 100644 --- a/lib/chef/resource/paludis_package.rb +++ b/lib/chef/resource/paludis_package.rb @@ -23,11 +23,11 @@ class Chef class Resource class PaludisPackage < Chef::Resource::Package + use_automatic_resource_name provides :paludis_package, os: "linux" def initialize(name, run_context=nil) super(name, run_context) - @resource_name = :paludis_package @allowed_actions.push(:install, :remove, :upgrade) @timeout = 3600 end diff --git a/lib/chef/resource/perl.rb b/lib/chef/resource/perl.rb index cb741d145a..6870f487eb 100644 --- a/lib/chef/resource/perl.rb +++ b/lib/chef/resource/perl.rb @@ -22,11 +22,10 @@ require 'chef/provider/script' class Chef class Resource class Perl < Chef::Resource::Script - provides :perl + use_automatic_resource_name def initialize(name, run_context=nil) super - @resource_name = :perl @interpreter = "perl" end diff --git a/lib/chef/resource/portage_package.rb b/lib/chef/resource/portage_package.rb index b03b69796a..009a525d22 100644 --- a/lib/chef/resource/portage_package.rb +++ b/lib/chef/resource/portage_package.rb @@ -21,11 +21,10 @@ require 'chef/resource/package' class Chef class Resource class PortagePackage < Chef::Resource::Package - provides :portage_package + use_automatic_resource_name def initialize(name, run_context=nil) super - @resource_name = :portage_package @provider = Chef::Provider::Package::Portage end diff --git a/lib/chef/resource/powershell_script.rb b/lib/chef/resource/powershell_script.rb index 43aafe4df2..a3a24fce7f 100644 --- a/lib/chef/resource/powershell_script.rb +++ b/lib/chef/resource/powershell_script.rb @@ -21,10 +21,11 @@ class Chef class Resource class PowershellScript < Chef::Resource::WindowsScript + use_automatic_resource_name provides :powershell_script, os: "windows" def initialize(name, run_context=nil) - super(name, run_context, :powershell_script, "powershell.exe") + super(name, run_context, nil, "powershell.exe") @convert_boolean_return = false end diff --git a/lib/chef/resource/python.rb b/lib/chef/resource/python.rb index fffd4d75f6..5c120d7d27 100644 --- a/lib/chef/resource/python.rb +++ b/lib/chef/resource/python.rb @@ -21,11 +21,10 @@ require 'chef/provider/script' class Chef class Resource class Python < Chef::Resource::Script - provides :python + use_automatic_resource_name def initialize(name, run_context=nil) super - @resource_name = :python @interpreter = "python" end diff --git a/lib/chef/resource/reboot.rb b/lib/chef/resource/reboot.rb index 7cd53450ed..c2a4ff29e2 100644 --- a/lib/chef/resource/reboot.rb +++ b/lib/chef/resource/reboot.rb @@ -24,11 +24,10 @@ require 'chef/resource' class Chef class Resource class Reboot < Chef::Resource - provides :reboot + use_automatic_resource_name def initialize(name, run_context=nil) super - @resource_name = :reboot @provider = Chef::Provider::Reboot @allowed_actions.push(:request_reboot, :reboot_now, :cancel) diff --git a/lib/chef/resource/registry_key.rb b/lib/chef/resource/registry_key.rb index cc8d05dd53..5a700e5365 100644 --- a/lib/chef/resource/registry_key.rb +++ b/lib/chef/resource/registry_key.rb @@ -22,7 +22,7 @@ require 'chef/digester' class Chef class Resource class RegistryKey < Chef::Resource - provides :registry_key + use_automatic_resource_name identity_attr :key state_attrs :values @@ -60,7 +60,6 @@ class Chef def initialize(name, run_context=nil) super - @resource_name = :registry_key @action = :create @architecture = :machine @recursive = false diff --git a/lib/chef/resource/remote_directory.rb b/lib/chef/resource/remote_directory.rb index d4108da47a..a35e0995b0 100644 --- a/lib/chef/resource/remote_directory.rb +++ b/lib/chef/resource/remote_directory.rb @@ -26,7 +26,7 @@ class Chef class RemoteDirectory < Chef::Resource::Directory include Chef::Mixin::Securable - provides :remote_directory + use_automatic_resource_name identity_attr :path @@ -34,7 +34,6 @@ class Chef def initialize(name, run_context=nil) super - @resource_name = :remote_directory @path = name @source = ::File.basename(name) @delete = false diff --git a/lib/chef/resource/remote_file.rb b/lib/chef/resource/remote_file.rb index df9fb7ad76..603151357c 100644 --- a/lib/chef/resource/remote_file.rb +++ b/lib/chef/resource/remote_file.rb @@ -28,11 +28,10 @@ class Chef class RemoteFile < Chef::Resource::File include Chef::Mixin::Securable - provides :remote_file + use_automatic_resource_name def initialize(name, run_context=nil) super - @resource_name = :remote_file @action = "create" @source = [] @use_etag = true diff --git a/lib/chef/resource/route.rb b/lib/chef/resource/route.rb index 8f9172060b..aca2a52e87 100644 --- a/lib/chef/resource/route.rb +++ b/lib/chef/resource/route.rb @@ -22,7 +22,7 @@ require 'chef/resource' class Chef class Resource class Route < Chef::Resource - provides :route + use_automatic_resource_name identity_attr :target @@ -30,7 +30,6 @@ class Chef def initialize(name, run_context=nil) super - @resource_name = :route @target = name @action = [:add] @allowed_actions.push(:add, :delete) diff --git a/lib/chef/resource/rpm_package.rb b/lib/chef/resource/rpm_package.rb index f00121dd69..67a6c156d8 100644 --- a/lib/chef/resource/rpm_package.rb +++ b/lib/chef/resource/rpm_package.rb @@ -23,11 +23,11 @@ class Chef class Resource class RpmPackage < Chef::Resource::Package + use_automatic_resource_name provides :rpm_package, os: [ "linux", "aix" ] def initialize(name, run_context=nil) super - @resource_name = :rpm_package @allow_downgrade = false end diff --git a/lib/chef/resource/ruby.rb b/lib/chef/resource/ruby.rb index 2b7644562a..759955da42 100644 --- a/lib/chef/resource/ruby.rb +++ b/lib/chef/resource/ruby.rb @@ -22,11 +22,10 @@ require 'chef/provider/script' class Chef class Resource class Ruby < Chef::Resource::Script - provides :ruby + use_automatic_resource_name def initialize(name, run_context=nil) super - @resource_name = :ruby @interpreter = "ruby" end diff --git a/lib/chef/resource/ruby_block.rb b/lib/chef/resource/ruby_block.rb index 07eec5599d..e4667533f2 100644 --- a/lib/chef/resource/ruby_block.rb +++ b/lib/chef/resource/ruby_block.rb @@ -23,13 +23,12 @@ require 'chef/provider/ruby_block' class Chef class Resource class RubyBlock < Chef::Resource - provides :ruby_block + use_automatic_resource_name identity_attr :block_name def initialize(name, run_context=nil) super - @resource_name = :ruby_block @action = "run" @allowed_actions << :create << :run @block_name = name diff --git a/lib/chef/resource/scm.rb b/lib/chef/resource/scm.rb index d41764d595..c97090d0a5 100644 --- a/lib/chef/resource/scm.rb +++ b/lib/chef/resource/scm.rb @@ -22,7 +22,7 @@ require 'chef/resource' class Chef class Resource class Scm < Chef::Resource - provides :scm + use_automatic_resource_name identity_attr :destination @@ -31,7 +31,6 @@ class Chef def initialize(name, run_context=nil) super @destination = name - @resource_name = :scm @enable_submodules = false @enable_checkout = true @revision = "HEAD" diff --git a/lib/chef/resource/script.rb b/lib/chef/resource/script.rb index e2fbb29d0f..f3d3ef01f4 100644 --- a/lib/chef/resource/script.rb +++ b/lib/chef/resource/script.rb @@ -23,14 +23,13 @@ require 'chef/provider/script' class Chef class Resource class Script < Chef::Resource::Execute - provides :script + use_automatic_resource_name # Chef-13: go back to using :name as the identity attr identity_attr :command def initialize(name, run_context=nil) super - @resource_name = :script # Chef-13: the command variable should be initialized to nil @command = name @code = nil diff --git a/lib/chef/resource/service.rb b/lib/chef/resource/service.rb index 47d2ab9e12..6ef203db3b 100644 --- a/lib/chef/resource/service.rb +++ b/lib/chef/resource/service.rb @@ -22,7 +22,7 @@ require 'chef/resource' class Chef class Resource class Service < Chef::Resource - provides :service + use_automatic_resource_name identity_attr :service_name @@ -30,7 +30,6 @@ class Chef def initialize(name, run_context=nil) super - @resource_name = :service @service_name = name @enabled = nil @running = nil diff --git a/lib/chef/resource/smartos_package.rb b/lib/chef/resource/smartos_package.rb index 99b3b953b0..7460f0f687 100644 --- a/lib/chef/resource/smartos_package.rb +++ b/lib/chef/resource/smartos_package.rb @@ -23,15 +23,9 @@ class Chef class Resource class SmartosPackage < Chef::Resource::Package - provides :smartos_package + use_automatic_resource_name provides :package, os: "solaris2", platform_family: "smartos" - def initialize(name, run_context=nil) - super - @resource_name = :smartos_package - end - end end end - diff --git a/lib/chef/resource/solaris_package.rb b/lib/chef/resource/solaris_package.rb index 94be4693b6..545e783b75 100644 --- a/lib/chef/resource/solaris_package.rb +++ b/lib/chef/resource/solaris_package.rb @@ -24,20 +24,13 @@ class Chef class Resource class SolarisPackage < Chef::Resource::Package - provides :solaris_package + use_automatic_resource_name provides :package, os: "solaris2", platform_family: "nexentacore" provides :package, os: "solaris2", platform_family: "solaris2" do |node| # on >= Solaris 11 we default to IPS packages instead node[:platform_version].to_f <= 5.10 end - def initialize(name, run_context=nil) - super - @resource_name = :solaris_package - end - end end end - - diff --git a/lib/chef/resource/subversion.rb b/lib/chef/resource/subversion.rb index e5a2e9d1a5..f1b0391a8c 100644 --- a/lib/chef/resource/subversion.rb +++ b/lib/chef/resource/subversion.rb @@ -22,13 +22,12 @@ require "chef/resource/scm" class Chef class Resource class Subversion < Chef::Resource::Scm - provides :subversion + use_automatic_resource_name def initialize(name, run_context=nil) super @svn_arguments = '--no-auth-cache' @svn_info_args = '--no-auth-cache' - @resource_name = :subversion allowed_actions << :force_export end diff --git a/lib/chef/resource/template.rb b/lib/chef/resource/template.rb index 67a9e6a418..2c7ade6c29 100644 --- a/lib/chef/resource/template.rb +++ b/lib/chef/resource/template.rb @@ -27,14 +27,13 @@ class Chef class Template < Chef::Resource::File include Chef::Mixin::Securable - provides :template + use_automatic_resource_name attr_reader :inline_helper_blocks attr_reader :inline_helper_modules def initialize(name, run_context=nil) super - @resource_name = :template @action = "create" @source = "#{::File.basename(name)}.erb" @cookbook = nil diff --git a/lib/chef/resource/timestamped_deploy.rb b/lib/chef/resource/timestamped_deploy.rb index b2109db85c..15ac296f78 100644 --- a/lib/chef/resource/timestamped_deploy.rb +++ b/lib/chef/resource/timestamped_deploy.rb @@ -21,10 +21,7 @@ class Chef # Convenience class for using the deploy resource with the timestamped # deployment strategy (provider) class TimestampedDeploy < Chef::Resource::Deploy - provides :timestamped_deploy - def initialize(*args, &block) - super(*args, &block) - end + use_automatic_resource_name end end end diff --git a/lib/chef/resource/user.rb b/lib/chef/resource/user.rb index 7d2ec25596..b762001d4b 100644 --- a/lib/chef/resource/user.rb +++ b/lib/chef/resource/user.rb @@ -26,11 +26,10 @@ class Chef state_attrs :uid, :gid, :home - provides :user + use_automatic_resource_name def initialize(name, run_context=nil) super - @resource_name = :user @username = name @comment = nil @uid = nil diff --git a/lib/chef/resource/whyrun_safe_ruby_block.rb b/lib/chef/resource/whyrun_safe_ruby_block.rb index f512dc67fc..0ade9c981f 100644 --- a/lib/chef/resource/whyrun_safe_ruby_block.rb +++ b/lib/chef/resource/whyrun_safe_ruby_block.rb @@ -19,12 +19,8 @@ class Chef class Resource class WhyrunSafeRubyBlock < Chef::Resource::RubyBlock - provides :whyrun_safe_ruby_block - def initialize(name, run_context=nil) - super - @resource_name = :whyrun_safe_ruby_block - end + use_automatic_resource_name end end diff --git a/lib/chef/resource/windows_package.rb b/lib/chef/resource/windows_package.rb index d4f8ae0603..8f9944a5af 100644 --- a/lib/chef/resource/windows_package.rb +++ b/lib/chef/resource/windows_package.rb @@ -26,13 +26,13 @@ class Chef class WindowsPackage < Chef::Resource::Package include Chef::Mixin::Uris - provides :package, os: "windows" + use_automatic_resource_name provides :windows_package, os: "windows" + provides :package, os: "windows" def initialize(name, run_context=nil) super @allowed_actions.push(:install, :remove) - @resource_name = :windows_package @source ||= source(@package_name) # Unique to this resource diff --git a/lib/chef/resource/windows_script.rb b/lib/chef/resource/windows_script.rb index 1af7a48fe4..48e2b535a8 100644 --- a/lib/chef/resource/windows_script.rb +++ b/lib/chef/resource/windows_script.rb @@ -31,8 +31,8 @@ class Chef def initialize(name, run_context, resource_name, interpreter_command) super(name, run_context) @interpreter = interpreter_command - @resource_name = resource_name - @default_guard_interpreter = resource_name + @resource_name = resource_name if resource_name + @default_guard_interpreter = self.resource_name end include Chef::Mixin::WindowsArchitectureHelper diff --git a/lib/chef/resource/windows_service.rb b/lib/chef/resource/windows_service.rb index 8090adceb0..3f62a67a82 100644 --- a/lib/chef/resource/windows_service.rb +++ b/lib/chef/resource/windows_service.rb @@ -25,8 +25,9 @@ class Chef # Until #1773 is resolved, you need to manually specify the windows_service resource # to use action :configure_startup and attribute startup_type - provides :service, os: "windows" + use_automatic_resource_name provides :windows_service, os: "windows" + provides :service, os: "windows" identity_attr :service_name @@ -34,7 +35,6 @@ class Chef def initialize(name, run_context=nil) super - @resource_name = :windows_service @allowed_actions.push(:configure_startup) @startup_type = :automatic @run_as_user = "" diff --git a/lib/chef/resource/yum_package.rb b/lib/chef/resource/yum_package.rb index d8be8c9748..ae9c840582 100644 --- a/lib/chef/resource/yum_package.rb +++ b/lib/chef/resource/yum_package.rb @@ -23,12 +23,11 @@ class Chef class Resource class YumPackage < Chef::Resource::Package - provides :yum_package + use_automatic_resource_name provides :package, os: "linux", platform_family: [ "rhel", "fedora" ] def initialize(name, run_context=nil) super - @resource_name = :yum_package @flush_cache = { :before => false, :after => false } @allow_downgrade = false end diff --git a/lib/chef/resource_builder.rb b/lib/chef/resource_builder.rb index bb0962d128..9e9f7047a4 100644 --- a/lib/chef/resource_builder.rb +++ b/lib/chef/resource_builder.rb @@ -18,6 +18,10 @@ # NOTE: this was extracted from the Recipe DSL mixin, relevant specs are in spec/unit/recipe_spec.rb +require 'chef/exceptions' +require 'chef/resource' +require 'chef/log' + class Chef class ResourceBuilder attr_reader :type @@ -46,6 +50,9 @@ class Chef raise ArgumentError, "You must supply a name when declaring a #{type} resource" if name.nil? @resource = resource_class.new(name, run_context) + if resource.resource_name.nil? + raise Chef::Exceptions::InvalidResourceSpecification, "#{resource}.resource_name is `nil`! Did you forget to put `provides :blah` or `resource_name :blah` in your resource class?" + end resource.source_line = created_at resource.declared_type = type diff --git a/spec/integration/recipes/provider_choice.rb b/spec/integration/recipes/provider_choice.rb index 999765a1de..5aa5dcb9b3 100644 --- a/spec/integration/recipes/provider_choice.rb +++ b/spec/integration/recipes/provider_choice.rb @@ -10,9 +10,8 @@ describe "Recipe DSL methods" do super @action = :create @allowed_actions = [ :create ] - @resource_name = 'provider_thingy' end - provides :provider_thingy + resource_name :provider_thingy def to_s "provider_thingy resource class" end diff --git a/spec/integration/recipes/recipe_dsl_spec.rb b/spec/integration/recipes/recipe_dsl_spec.rb index 079baaa162..c79c20cd7d 100644 --- a/spec/integration/recipes/recipe_dsl_spec.rb +++ b/spec/integration/recipes/recipe_dsl_spec.rb @@ -9,11 +9,12 @@ describe "Recipe DSL methods" do class BaseThingy < Chef::Resource def initialize(*args, &block) super - @resource_name = 'base_thingy' @allowed_actions = [ :create ] @action = :create end + resource_name 'base_thingy' + class<<self attr_accessor :created_resource attr_accessor :created_provider @@ -54,7 +55,6 @@ describe "Recipe DSL methods" do class Chef::Resource::BackcompatThingy < Chef::Resource def initialize(*args, &block) super - @resource_name = 'backcompat_thingy' @allowed_actions = [ :create ] @action = :create end @@ -84,6 +84,7 @@ describe "Recipe DSL methods" do class RecipeDSLSpecNamespace::BackcompatThingy < BaseThingy provides :backcompat_thingy + resource_name :backcompat_thingy end } @@ -118,7 +119,7 @@ describe "Recipe DSL methods" do before(:context) { class RecipeDSLSpecNamespace::MySupplier < BaseThingy - provides :hemlock + resource_name :hemlock end } @@ -141,7 +142,7 @@ describe "Recipe DSL methods" do before(:context) { class RecipeDSLSpecNamespace::Thingy3 < BaseThingy - provides :thingy3 + resource_name :thingy3 end } @@ -157,7 +158,7 @@ describe "Recipe DSL methods" do before(:context) { class RecipeDSLSpecNamespace::Thingy4 < Chef::Resource - provides :thingy3 + resource_name :thingy3 end } @@ -182,7 +183,7 @@ describe "Recipe DSL methods" do before(:context) { class RecipeDSLSpecNamespace::Thingy5 < BaseThingy - provides :thingy5 + resource_name :thingy5 provides :twizzle provides :twizzle2 end @@ -214,10 +215,12 @@ describe "Recipe DSL methods" do context "With platform-specific resources 'my_super_thingy_foo' and 'my_super_thingy_bar'" do before(:context) { class MySuperThingyFoo < BaseThingy + resource_name :my_super_thingy_foo provides :my_super_thingy, platform: 'foo' end class MySuperThingyBar < BaseThingy + resource_name :my_super_thingy_bar provides :my_super_thingy, platform: 'bar' end } diff --git a/spec/integration/recipes/resource_definition_spec.rb b/spec/integration/recipes/resource_definition_spec.rb new file mode 100644 index 0000000000..4e5bc53428 --- /dev/null +++ b/spec/integration/recipes/resource_definition_spec.rb @@ -0,0 +1,18 @@ +require 'support/shared/integration/integration_helper' + +describe "Resource definition" do + include IntegrationSupport + + context "With a resource with only provides lines and no resource_name" do + before(:context) { + class ResourceDefinitionNoNameTest < Chef::Resource + provides :resource_definition_no_name_test + end + } + it "Creating said resource with the resource builder fails with an exception" do + expect_converge { + resource_definition_no_name_test 'blah' + }.to raise_error(Chef::Exceptions::InvalidResourceSpecification) + end + end +end diff --git a/spec/support/lib/chef/resource/cat.rb b/spec/support/lib/chef/resource/cat.rb index 641ce28795..9bfaebf07b 100644 --- a/spec/support/lib/chef/resource/cat.rb +++ b/spec/support/lib/chef/resource/cat.rb @@ -19,12 +19,11 @@ class Chef class Resource class Cat < Chef::Resource - provides :cat + use_automatic_resource_name attr_accessor :action def initialize(name, run_context=nil) - @resource_name = :cat super @action = "sell" end diff --git a/spec/support/lib/chef/resource/one_two_three_four.rb b/spec/support/lib/chef/resource/one_two_three_four.rb index d7e5ea095e..4543744a28 100644 --- a/spec/support/lib/chef/resource/one_two_three_four.rb +++ b/spec/support/lib/chef/resource/one_two_three_four.rb @@ -19,15 +19,10 @@ class Chef class Resource class OneTwoThreeFour < Chef::Resource - provides :one_two_three_four + use_automatic_resource_name attr_reader :i_can_count - def initialize(name, run_context) - @resource_name = :one_two_three_four - super - end - def i_can_count(tf) @i_can_count = tf end diff --git a/spec/support/lib/chef/resource/with_state.rb b/spec/support/lib/chef/resource/with_state.rb index 226de0a6d2..efff2e7c12 100644 --- a/spec/support/lib/chef/resource/with_state.rb +++ b/spec/support/lib/chef/resource/with_state.rb @@ -22,16 +22,9 @@ require 'chef/json_compat' class Chef class Resource class WithState < Chef::Resource - attr_accessor :state - - def initialize(name, run_context=nil) - @resource_name = :with_state - super - end + use_automatic_resource_name - def state - @state - end + attr_accessor :state end end end diff --git a/spec/support/lib/chef/resource/zen_follower.rb b/spec/support/lib/chef/resource/zen_follower.rb index 590aa0827b..90d33e6039 100644 --- a/spec/support/lib/chef/resource/zen_follower.rb +++ b/spec/support/lib/chef/resource/zen_follower.rb @@ -21,15 +21,10 @@ require 'chef/json_compat' class Chef class Resource class ZenFollower < Chef::Resource - provides :zen_follower + use_automatic_resource_name provides :follower, platform: "zen" - def initialize(name, run_context=nil) - @resource_name = :zen_follower - super - end - def master(arg=nil) if !arg.nil? @master = arg diff --git a/spec/support/lib/chef/resource/zen_master.rb b/spec/support/lib/chef/resource/zen_master.rb index 145dd70e64..ed5b1b3add 100644 --- a/spec/support/lib/chef/resource/zen_master.rb +++ b/spec/support/lib/chef/resource/zen_master.rb @@ -22,12 +22,11 @@ require 'chef/json_compat' class Chef class Resource class ZenMaster < Chef::Resource - provides :zen_master + use_automatic_resource_name attr_reader :peace def initialize(name, run_context=nil) - @resource_name = :zen_master super allowed_actions << :win << :score end diff --git a/spec/unit/lwrp_spec.rb b/spec/unit/lwrp_spec.rb index 659696fd28..8cac70e800 100644 --- a/spec/unit/lwrp_spec.rb +++ b/spec/unit/lwrp_spec.rb @@ -229,14 +229,6 @@ describe "LWRP" do expect(klass.resource_name).to eq(:foo) end - context "when creating a new instance" do - it "raises an exception if resource_name is nil" do - expect { - klass.new('blah') - }.to raise_error(Chef::Exceptions::InvalidResourceSpecification) - end - end - context "lazy default values" do let(:klass) do Class.new(Chef::Resource::LWRPBase) do diff --git a/spec/unit/recipe_spec.rb b/spec/unit/recipe_spec.rb index 9c4fb49497..59d05298c3 100644 --- a/spec/unit/recipe_spec.rb +++ b/spec/unit/recipe_spec.rb @@ -121,6 +121,7 @@ describe Chef::Recipe do it "locate resource for particular platform" do ShaunTheSheep = Class.new(Chef::Resource) + ShaunTheSheep.use_automatic_resource_name ShaunTheSheep.provides :laughter, :on_platforms => ["television"] node.automatic[:platform] = "television" node.automatic[:platform_version] = "123" @@ -131,6 +132,7 @@ describe Chef::Recipe do it "locate a resource for all platforms" do YourMom = Class.new(Chef::Resource) + YourMom.use_automatic_resource_name YourMom.provides :love_and_caring res = recipe.love_and_caring "mommy" expect(res.name).to eql("mommy") @@ -141,8 +143,10 @@ describe Chef::Recipe do before do node.automatic[:platform] = "nbc_sports" Sounders = Class.new(Chef::Resource) + Sounders.use_automatic_resource_name Sounders.provides :football, platform: "nbc_sports" TottenhamHotspur = Class.new(Chef::Resource) + TottenhamHotspur.use_automatic_resource_name TottenhamHotspur.provides :football, platform: "nbc_sports" end diff --git a/spec/unit/resource/timestamped_deploy_spec.rb b/spec/unit/resource/timestamped_deploy_spec.rb index eca6c570d4..4ebfdaf059 100644 --- a/spec/unit/resource/timestamped_deploy_spec.rb +++ b/spec/unit/resource/timestamped_deploy_spec.rb @@ -23,11 +23,10 @@ describe Chef::Resource::TimestampedDeploy, "initialize" do static_provider_resolution( resource: Chef::Resource::TimestampedDeploy, provider: Chef::Provider::Deploy::Timestamped, - name: :deploy, + name: :timestamped_deploy, action: :deploy, os: 'linux', platform_family: 'rhel', ) end - diff --git a/spec/unit/resource_spec.rb b/spec/unit/resource_spec.rb index 3bfd63f5ab..fefe78fbda 100644 --- a/spec/unit/resource_spec.rb +++ b/spec/unit/resource_spec.rb @@ -21,10 +21,6 @@ require 'spec_helper' -class ResourceTestHarness < Chef::Resource - provider_base Chef::Provider::Package -end - describe Chef::Resource do before(:each) do @cookbook_repo_path = File.join(CHEF_SPEC_DATA, 'cookbooks') @@ -336,6 +332,85 @@ describe Chef::Resource do end end + describe "self.resource_name" do + context "When resource_name is not set" do + it "and there are no provides lines, resource_name is nil" do + c = Class.new(Chef::Resource) do + end + + r = c.new('hi') + r.declared_type = :d + expect(c.resource_name).to be_nil + expect(r.resource_name).to be_nil + expect(r.declared_type).to eq :d + end + it "and there are no provides lines, @resource_name is used" do + c = Class.new(Chef::Resource) do + def initialize(*args, &block) + @resource_name = :blah + super + end + end + + r = c.new('hi') + r.declared_type = :d + expect(c.resource_name).to be_nil + expect(r.resource_name).to eq :blah + expect(r.declared_type).to eq :d + end + end + + it "resource_name without provides is honored" do + c = Class.new(Chef::Resource) do + resource_name 'blah' + end + + r = c.new('hi') + r.declared_type = :d + expect(c.resource_name).to eq :blah + expect(r.resource_name).to eq :blah + expect(r.declared_type).to eq :d + end + it "setting class.resource_name with 'resource_name = blah' overrides declared_type" do + c = Class.new(Chef::Resource) do + provides :self_resource_name_test_2 + end + c.resource_name = :blah + + r = c.new('hi') + r.declared_type = :d + expect(c.resource_name).to eq :blah + expect(r.resource_name).to eq :blah + expect(r.declared_type).to eq :d + end + it "setting class.resource_name with 'resource_name blah' overrides declared_type" do + c = Class.new(Chef::Resource) do + resource_name :blah + provides :self_resource_name_test_3 + end + + r = c.new('hi') + r.declared_type = :d + expect(c.resource_name).to eq :blah + expect(r.resource_name).to eq :blah + expect(r.declared_type).to eq :d + end + it "use_automatic_resource_name yields a resource name from the class name" do + class SelfResourceNameTestBlahDBlah4 < Chef::Resource + use_automatic_resource_name + end + + c = SelfResourceNameTestBlahDBlah4 + + r = c.new('hi') + r.declared_type = :d + expect(c.resource_name).to eq :self_resource_name_test_blah_d_blah4 + expect(r.resource_name).to eq :self_resource_name_test_blah_d_blah4 + expect(r.declared_type).to eq :d + + end + end + describe "is" do it "should return the arguments passed with 'is'" do zm = Chef::Resource::ZenMaster.new("coffee") @@ -459,8 +534,21 @@ describe Chef::Resource do expect(Chef::Resource.provider_base).to eq(Chef::Provider) end - it "allows the base provider to be overriden by a " do - expect(ResourceTestHarness.provider_base).to eq(Chef::Provider::Package) + it "allows the base provider to be overridden" do + Chef::Config.treat_deprecation_warnings_as_errors(false) + class OverrideProviderBaseTest < Chef::Resource + provider_base Chef::Provider::Package + end + + expect(OverrideProviderBaseTest.provider_base).to eq(Chef::Provider::Package) + end + + it "warns when setting provider_base" do + expect { + class OverrideProviderBaseTest2 < Chef::Resource + provider_base Chef::Provider::Package + end + }.to raise_error(Chef::Exceptions::DeprecatedFeatureError) end end |