Skip to main content

NamePropertyIsRequired

The department is: NamePropertyIsRequired

The full name of the cop is: Chef/RedundantCode/NamePropertyIsRequired

Enabled by defaultSupports autocorrectionTarget Chef Version
EnabledNoAll Versions

When using properties in a custom resource you shouldn’t set a property to be both required and a name_property. Name properties are a way to optionally override the name given to the resource block in cookbook code. In your resource code you use the name_property and if the user doesn’t pass in anything to that property its value will be populated with resource block’s name. This allows users to provide more friendly resource names for logging that give additional context on the change being made.

How about a nice example! Here we have a resource called ntp_config that has a name_property of config_file. All throughout the code of this resource we’d use new_resource.config_file when referencing the path to the config.

We can use a friendly name for the block and specific a value to config_file ntp_config ‘Configure the main config file’ do config_file ‘/etc/ntp/ntp.conf’ action :create end

We can also just set the config path as the resource block and Chef will make sure to pass this in as new_resource.config_file as well. ntp_config ‘/etc/ntp/ntp.conf’ do action :create end

The core tenant of the name property feature is that these properties are optional and making them required effectively turns off the functionality provided by name properties. If the goal is to always require the user to pass the config_file property then it should just be made a required property and not a name_property.

Examples

incorrect

property :config_file, String, required: true, name_property: true
attribute :config_file, String, required: true, name_attribute: true

correct

property :config_file, String, required: true

Configurable attributes

NameDefault valueConfigurable values
Version Added5.1.0String
Include
  • **/resources/*.rb
  • **/libraries/*.rb
Array

Was this page helpful?

×









Search Results