Skip to content

Commit 4a21768

Browse files
committed
Provide stdlib::ensure_resource(s) functions
1 parent 329a43e commit 4a21768

File tree

10 files changed

+347
-243
lines changed

10 files changed

+347
-243
lines changed

REFERENCE.md

Lines changed: 186 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,8 @@ from an array or key from a hash.
4747
* [`dos2unix`](#dos2unix): Returns the Unix version of the given string.
4848
* [`enclose_ipv6`](#enclose_ipv6): Takes an array of ip addresses and encloses the ipv6 addresses with square brackets.
4949
* [`ensure_packages`](#ensure_packages): DEPRECATED. Use the namespaced function [`stdlib::ensure_packages`](#stdlibensure_packages) instead.
50-
* [`ensure_resource`](#ensure_resource): Takes a resource type, title, and a list of attributes that describe a
51-
resource.
52-
* [`ensure_resources`](#ensure_resources): Takes a resource type, title (only hash), and a list of attributes that describe a
53-
resource.
50+
* [`ensure_resource`](#ensure_resource): DEPRECATED. Use the namespaced function [`stdlib::ensure_resource`](#stdlibensure_resource) instead.
51+
* [`ensure_resources`](#ensure_resources): DEPRECATED. Use the namespaced function [`stdlib::ensure_resources`](#stdlibensure_resources) instead.
5452
* [`fact`](#fact): Digs into the facts hash using dot-notation
5553
* [`fqdn_rand_string`](#fqdn_rand_string): DEPRECATED. Use the namespaced function [`stdlib::fqdn_rand_string`](#stdlibfqdn_rand_string) instead.
5654
* [`fqdn_rotate`](#fqdn_rotate): DEPRECATED. Use the namespaced function [`stdlib::fqdn_rotate`](#stdlibfqdn_rotate) instead.
@@ -119,6 +117,8 @@ the provided regular expression.
119117
* [`stdlib::end_with`](#stdlib--end_with): Returns true if str ends with one of the prefixes given. Each of the prefixes should be a String.
120118
* [`stdlib::ensure`](#stdlib--ensure): function to cast ensure parameter to resource specific value
121119
* [`stdlib::ensure_packages`](#stdlib--ensure_packages): Takes a list of packages and only installs them if they don't already exist.
120+
* [`stdlib::ensure_resource`](#stdlib--ensure_resource): @summary Takes a resource type, title, and a list of attributes that describe a resource. user { 'dan': ensure => present, } @r
121+
* [`stdlib::ensure_resources`](#stdlib--ensure_resources): @summary Takes a resource type, title (only hash), and a list of attributes that describe a resource. @return created resources wi
122122
* [`stdlib::extname`](#stdlib--extname): Returns the Extension (the Portion of Filename in Path starting from the
123123
last Period).
124124
* [`stdlib::fqdn_rand_string`](#stdlib--fqdn_rand_string): Generates a random alphanumeric string. Combining the `$fqdn` fact and an
@@ -1606,125 +1606,39 @@ Data type: `Any`
16061606

16071607
### <a name="ensure_resource"></a>`ensure_resource`
16081608

1609-
Type: Ruby 3.x API
1610-
1611-
user { 'dan':
1612-
ensure => present,
1613-
}
1614-
1615-
#### Examples
1616-
1617-
##### Example usage
1618-
1619-
```puppet
1620-
1621-
Creates the resource if it does not already exist:
1622-
1623-
ensure_resource('user', 'dan', {'ensure' => 'present' })
1624-
1625-
If the resource already exists but does not match the specified parameters,
1626-
this function will attempt to recreate the resource leading to a duplicate
1627-
resource definition error.
1628-
1629-
An array of resources can also be passed in and each will be created with
1630-
the type and parameters specified if it doesn't already exist.
1631-
1632-
ensure_resource('user', ['dan','alex'], {'ensure' => 'present'})
1633-
```
1634-
1635-
#### `ensure_resource()`
1609+
Type: Ruby 4.x API
16361610

1637-
user { 'dan':
1638-
ensure => present,
1639-
}
1611+
DEPRECATED. Use the namespaced function [`stdlib::ensure_resource`](#stdlibensure_resource) instead.
16401612

1641-
Returns: `Any` created or recreated the passed resource with the passed type and attributes
1613+
#### `ensure_resource(Any *$args)`
16421614

1643-
##### Examples
1615+
The ensure_resource function.
16441616

1645-
###### Example usage
1646-
1647-
```puppet
1648-
1649-
Creates the resource if it does not already exist:
1617+
Returns: `Any`
16501618

1651-
ensure_resource('user', 'dan', {'ensure' => 'present' })
1619+
##### `*args`
16521620

1653-
If the resource already exists but does not match the specified parameters,
1654-
this function will attempt to recreate the resource leading to a duplicate
1655-
resource definition error.
1621+
Data type: `Any`
16561622

1657-
An array of resources can also be passed in and each will be created with
1658-
the type and parameters specified if it doesn't already exist.
16591623

1660-
ensure_resource('user', ['dan','alex'], {'ensure' => 'present'})
1661-
```
16621624

16631625
### <a name="ensure_resources"></a>`ensure_resources`
16641626

1665-
Type: Ruby 3.x API
1666-
1667-
An hash of resources should be passed in and each will be created with
1668-
the type and parameters specified if it doesn't already exist.
1669-
1670-
ensure_resources('user', {'dan' => { gid => 'mygroup', uid => '600' }, 'alex' => { gid => 'mygroup' }}, {'ensure' => 'present'})
1671-
1672-
From Hiera Backend:
1673-
1674-
userlist:
1675-
dan:
1676-
gid: 'mygroup'
1677-
uid: '600'
1678-
alex:
1679-
gid: 'mygroup'
1680-
1681-
Call:
1682-
ensure_resources('user', hiera_hash('userlist'), {'ensure' => 'present'})
1683-
1684-
#### Examples
1685-
1686-
##### Example usage
1687-
1688-
```puppet
1689-
1690-
user { 'dan':
1691-
gid => 'mygroup',
1692-
ensure => present,
1693-
}
1694-
```
1695-
1696-
#### `ensure_resources()`
1697-
1698-
An hash of resources should be passed in and each will be created with
1699-
the type and parameters specified if it doesn't already exist.
1700-
1701-
ensure_resources('user', {'dan' => { gid => 'mygroup', uid => '600' }, 'alex' => { gid => 'mygroup' }}, {'ensure' => 'present'})
1627+
Type: Ruby 4.x API
17021628

1703-
From Hiera Backend:
1629+
DEPRECATED. Use the namespaced function [`stdlib::ensure_resources`](#stdlibensure_resources) instead.
17041630

1705-
userlist:
1706-
dan:
1707-
gid: 'mygroup'
1708-
uid: '600'
1709-
alex:
1710-
gid: 'mygroup'
1631+
#### `ensure_resources(Any *$args)`
17111632

1712-
Call:
1713-
ensure_resources('user', hiera_hash('userlist'), {'ensure' => 'present'})
1633+
The ensure_resources function.
17141634

1715-
Returns: `Any` created resources with the passed type and attributes
1635+
Returns: `Any`
17161636

1717-
##### Examples
1637+
##### `*args`
17181638

1719-
###### Example usage
1639+
Data type: `Any`
17201640

1721-
```puppet
17221641

1723-
user { 'dan':
1724-
gid => 'mygroup',
1725-
ensure => present,
1726-
}
1727-
```
17281642

17291643
### <a name="fact"></a>`fact`
17301644

@@ -3299,7 +3213,7 @@ Data type: `Optional[Enum['directory', 'link', 'mounted', 'service', 'file', 'pa
32993213
Type: Ruby 4.x API
33003214

33013215
It optionally takes a hash as a second parameter that will be passed as the
3302-
third argument to the ensure_resource() function.
3216+
third argument to the stdlib::ensure_resource() function.
33033217

33043218
#### `stdlib::ensure_packages(Variant[String[1], Array[String[1]]] $packages, Optional[Hash] $default_attributes)`
33053219

@@ -3317,7 +3231,7 @@ The packages to ensure are installed.
33173231

33183232
Data type: `Optional[Hash]`
33193233

3320-
Default attributes to be passed to the `ensure_resource()` function
3234+
Default attributes to be passed to the `stdlib::ensure_resource()` function
33213235

33223236
#### `stdlib::ensure_packages(Hash[String[1], Any] $packages, Optional[Hash] $default_attributes)`
33233237

@@ -3337,6 +3251,172 @@ Data type: `Optional[Hash]`
33373251

33383252
Default attributes. Package specific attributes from the `packages` parameter will take precedence.
33393253

3254+
### <a name="stdlib--ensure_resource"></a>`stdlib::ensure_resource`
3255+
3256+
Type: Ruby 4.x API
3257+
3258+
@summary
3259+
Takes a resource type, title, and a list of attributes that describe a
3260+
resource.
3261+
3262+
user { 'dan':
3263+
ensure => present,
3264+
}
3265+
3266+
@return
3267+
created or recreated the passed resource with the passed type and attributes
3268+
3269+
@example Example usage
3270+
3271+
Creates the resource if it does not already exist:
3272+
3273+
stdlib::ensure_resource('user', 'dan', {'ensure' => 'present' })
3274+
3275+
If the resource already exists but does not match the specified parameters,
3276+
this function will attempt to recreate the resource leading to a duplicate
3277+
resource definition error.
3278+
3279+
An array of resources can also be passed in and each will be created with
3280+
the type and parameters specified if it doesn't already exist.
3281+
3282+
ensure_resource('user', ['dan','alex'], {'ensure' => 'present'})
3283+
3284+
#### `stdlib::ensure_resource(String $type, Variant[String,Array[String]] $title, Hash $params)`
3285+
3286+
@summary
3287+
Takes a resource type, title, and a list of attributes that describe a
3288+
resource.
3289+
3290+
user { 'dan':
3291+
ensure => present,
3292+
}
3293+
3294+
@return
3295+
created or recreated the passed resource with the passed type and attributes
3296+
3297+
@example Example usage
3298+
3299+
Creates the resource if it does not already exist:
3300+
3301+
stdlib::ensure_resource('user', 'dan', {'ensure' => 'present' })
3302+
3303+
If the resource already exists but does not match the specified parameters,
3304+
this function will attempt to recreate the resource leading to a duplicate
3305+
resource definition error.
3306+
3307+
An array of resources can also be passed in and each will be created with
3308+
the type and parameters specified if it doesn't already exist.
3309+
3310+
ensure_resource('user', ['dan','alex'], {'ensure' => 'present'})
3311+
3312+
Returns: `Any`
3313+
3314+
##### `type`
3315+
3316+
Data type: `String`
3317+
3318+
The resource type to create
3319+
3320+
##### `title`
3321+
3322+
Data type: `Variant[String,Array[String]]`
3323+
3324+
The resource title or array of resource titles
3325+
3326+
##### `params`
3327+
3328+
Data type: `Hash`
3329+
3330+
The resource parameters
3331+
3332+
### <a name="stdlib--ensure_resources"></a>`stdlib::ensure_resources`
3333+
3334+
Type: Ruby 4.x API
3335+
3336+
@summary
3337+
Takes a resource type, title (only hash), and a list of attributes that describe a
3338+
resource.
3339+
3340+
@return
3341+
created resources with the passed type and attributes
3342+
3343+
@example Example usage
3344+
3345+
user { 'dan':
3346+
gid => 'mygroup',
3347+
ensure => present,
3348+
}
3349+
3350+
An hash of resources should be passed in and each will be created with
3351+
the type and parameters specified if it doesn't already exist.
3352+
3353+
stdlib::ensure_resources('user', {'dan' => { gid => 'mygroup', uid => '600' }, 'alex' => { gid => 'mygroup' }}, {'ensure' => 'present'})
3354+
3355+
From Hiera Backend:
3356+
3357+
userlist:
3358+
dan:
3359+
gid: 'mygroup'
3360+
uid: '600'
3361+
alex:
3362+
gid: 'mygroup'
3363+
3364+
Call:
3365+
ensure_resources('user', hiera_hash('userlist'), {'ensure' => 'present'})
3366+
3367+
#### `stdlib::ensure_resources(String $type, Hash[String,Hash] $titles, Optional[Hash] $params)`
3368+
3369+
@summary
3370+
Takes a resource type, title (only hash), and a list of attributes that describe a
3371+
resource.
3372+
3373+
@return
3374+
created resources with the passed type and attributes
3375+
3376+
@example Example usage
3377+
3378+
user { 'dan':
3379+
gid => 'mygroup',
3380+
ensure => present,
3381+
}
3382+
3383+
An hash of resources should be passed in and each will be created with
3384+
the type and parameters specified if it doesn't already exist.
3385+
3386+
stdlib::ensure_resources('user', {'dan' => { gid => 'mygroup', uid => '600' }, 'alex' => { gid => 'mygroup' }}, {'ensure' => 'present'})
3387+
3388+
From Hiera Backend:
3389+
3390+
userlist:
3391+
dan:
3392+
gid: 'mygroup'
3393+
uid: '600'
3394+
alex:
3395+
gid: 'mygroup'
3396+
3397+
Call:
3398+
ensure_resources('user', hiera_hash('userlist'), {'ensure' => 'present'})
3399+
3400+
Returns: `Any`
3401+
3402+
##### `type`
3403+
3404+
Data type: `String`
3405+
3406+
The resource type to create
3407+
3408+
##### `titles`
3409+
3410+
Data type: `Hash[String,Hash]`
3411+
3412+
A hash of resource titles mapping to resource parameters
3413+
3414+
##### `params`
3415+
3416+
Data type: `Optional[Hash]`
3417+
3418+
A hash of default parameters to be merged with individual resource parameters
3419+
33403420
### <a name="stdlib--extname"></a>`stdlib::extname`
33413421

33423422
Type: Ruby 4.x API
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# frozen_string_literal: true
2+
3+
# @summary DEPRECATED. Use the namespaced function [`stdlib::ensure_resource`](#stdlibensure_resource) instead.
4+
Puppet::Functions.create_function(:ensure_resource) do
5+
dispatch :deprecation_gen do
6+
repeated_param 'Any', :args
7+
end
8+
def deprecation_gen(*args)
9+
call_function('deprecation', 'ensure_resource', 'This function is deprecated, please use stdlib::ensure_resource instead.', false)
10+
call_function('stdlib::ensure_resource', *args)
11+
end
12+
end
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# frozen_string_literal: true
2+
3+
# @summary DEPRECATED. Use the namespaced function [`stdlib::ensure_resources`](#stdlibensure_resources) instead.
4+
Puppet::Functions.create_function(:ensure_resources) do
5+
dispatch :deprecation_gen do
6+
repeated_param 'Any', :args
7+
end
8+
def deprecation_gen(*args)
9+
call_function('deprecation', 'ensure_resources', 'This function is deprecated, please use stdlib::ensure_resources instead.', false)
10+
call_function('stdlib::ensure_resources', *args)
11+
end
12+
end

0 commit comments

Comments
 (0)