Search code examples
mysqlrubychef-infracookbook

chef: understanding Chef::resource provides?


I am studying the chef cookbook about mysql at https://github.com/chef-cookbooks/mysql/blob/master/libraries/resource_mysql_client.rb.

class Chef
  class Resource
    class MysqlClient < Chef::Resource::LWRPBase
      provides :mysql_client

However, I did not understand the meaning of provides :mysql_client. Can somebody help me figure out the code? For example, what's the purpose? If without the line, is it fine?

Thanks


Solution

  • There's a great blog post talking about Chef Provider resolution. I highly recommend you read that first. Historically, Chef has had a number of different ways to figure out which provider should be loaded for a particular resource -- by using a map of resource to provider, by using a class name matching algorithm, and more recently, as you noted, using provides:.

    provides is intended to signal that the class provides a particular DSL object. The comments from that method are especially instructive:

    Resources have an automatic DSL based on their resource_name, equivalent to provides :resource_name (providing the resource on all OS's). If you declare a provides with the given resource_name, it replaces that provides (so that you can provide your resource DSL only on certain OS's).

    If you use provides on a single resource class and a single provider class, Chef (Chef 12.5.x) will map that resource to that provider each time. If you omit provides, you're going to rely on one of Chef's other matching mechanisms mentioned in that blog post above. Pretty much all of the other methods are deprecated or are on the way to being deprecated.

    I'd also recommend the Q&A from this blog post. It talks about the older ways, deprecation, and covers some other nuances of provider resolution for resources.