|
1 | 1 | ---
|
2 | 2 | layout: default
|
3 |
| -title: Complete Resource Example |
| 3 | +title: Complete resource example |
4 | 4 | ---
|
5 | 5 |
|
6 |
| -## Resource Creation |
| 6 | +You can write resource types and providers in the Puppet language. This example walks through creating resources in Puppet. Alternatively, you can use the [Puppet Resource API](./create_types_and_providers_resource_api.html) for a simpler and faster way to build types and providers. |
7 | 7 |
|
8 |
| -Nearly every resource needs to be able to be created and destroyed, and resources have to have names, so we'll start with those two features. Puppet's property support has a helper method called `ensurable` that handles modeling creation and destruction; it creates an `ensure` property and adds `absent` and `present` values for it, which in turn require three methods on the provider, `create`, `destroy`, and `exists?`. Here's the first start to the resource. We're going to create one called 'file' --- this is an example of how to create a resource for something Puppet already has. |
| 8 | +## Resource creation |
| 9 | + |
| 10 | +Nearly every resource needs to be able to be created and destroyed, and resources have to have names, so we'll start with those two features. Puppet's property support has a helper method called `ensurable` that handles modeling creation and destruction; it creates an `ensure` property and adds `absent` and `present` values for it, which in turn require three methods on the provider, `create`, `destroy`, and `exists?`. Here's the first start to the resource. We're going to create one called 'file' --- this is an example of how to create a resource for something Puppet already has. |
9 | 11 |
|
10 | 12 |
|
11 | 13 | Puppet::Type.newtype(:file) do
|
@@ -78,11 +80,11 @@ Add this code to the provider to understand modes:
|
78 | 80 | File.chmod(Integer("0" + value), @resource[:name])
|
79 | 81 | end
|
80 | 82 |
|
81 |
| -Note that the getter method returns the value, it doesn't attempt to modify the resource itself. Also, when the setter gets passed the value it is supposed to set; it doesn't attempt to figure out the appropriate value to use. This should always be true of how providers are implemented. |
| 83 | +The getter method returns the value, but it doesn't attempt to modify the resource itself. Also, when the setter gets passed the value it is supposed to set, it doesn't attempt to figure out the appropriate value to use. This should always be true of how providers are implemented. |
82 | 84 |
|
83 |
| -Also notice that the `ensure` property, when created by the `ensurable` method, behaves differently because it uses methods for creation and destruction of the file, whereas normal properties use getter and setter methods. When a resource is being created, Puppet expects the `create` method (or, actually, any changes done within ensure) to make any other necessary changes. This is because most often resources are created already configured correctly, so it doesn't make sense for Puppet to test it manually (for example, useradd support is set up to add all specified properties when useradd is run, so usermod doesn't need to be run afterward). |
| 85 | +Also notice that the `ensure` property, when created by the `ensurable` method, behaves differently because it uses methods for creation and destruction of the file, whereas normal properties use getter and setter methods. When a resource is being created, Puppet expects the `create` method (or, actually, any changes done within `ensure`) to make any other necessary changes. This is because most often resources are created already configured correctly, so it doesn't make sense for Puppet to test it manually (for example, `useradd` support is set up to add all specified properties when `useradd` is run, so `usermod` doesn't need to be run afterward). |
84 | 86 |
|
85 |
| -You can see how the `absent` and `present` values are defined by looking in the property.rb file; here's the most important snippet: |
| 87 | +You can see how the `absent` and `present` values are defined by looking in the `property.rb` file; here's the most important snippet: |
86 | 88 |
|
87 | 89 | newvalue(:present) do
|
88 | 90 | if @resource.provider and @resource.provider.respond_to?(:create)
|
|
0 commit comments