Skip to content

Commit 7e3fa25

Browse files
committed
Provide stdlib::ensure_resource(s) functions
1 parent a4c01e0 commit 7e3fa25

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.
@@ -120,6 +118,8 @@ the provided regular expression.
120118
* [`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.
121119
* [`stdlib::ensure`](#stdlib--ensure): function to cast ensure parameter to resource specific value
122120
* [`stdlib::ensure_packages`](#stdlib--ensure_packages): Takes a list of packages and only installs them if they don't already exist.
121+
* [`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
122+
* [`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
123123
* [`stdlib::extname`](#stdlib--extname): Returns the Extension (the Portion of Filename in Path starting from the
124124
last Period).
125125
* [`stdlib::fqdn_rand_string`](#stdlib--fqdn_rand_string): Generates a random alphanumeric string. Combining the `$fqdn` fact and an
@@ -1605,125 +1605,39 @@ Data type: `Any`
16051605

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

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

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

1640-
Returns: `Any` created or recreated the passed resource with the passed type and attributes
1612+
#### `ensure_resource(Any *$args)`
16411613

1642-
##### Examples
1614+
The ensure_resource function.
16431615

1644-
###### Example usage
1645-
1646-
```puppet
1647-
1648-
Creates the resource if it does not already exist:
1616+
Returns: `Any`
16491617

1650-
ensure_resource('user', 'dan', {'ensure' => 'present' })
1618+
##### `*args`
16511619

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

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

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

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

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

1702-
From Hiera Backend:
1628+
DEPRECATED. Use the namespaced function [`stdlib::ensure_resources`](#stdlibensure_resources) instead.
17031629

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

1711-
Call:
1712-
ensure_resources('user', hiera_hash('userlist'), {'ensure' => 'present'})
1632+
The ensure_resources function.
17131633

1714-
Returns: `Any` created resources with the passed type and attributes
1634+
Returns: `Any`
17151635

1716-
##### Examples
1636+
##### `*args`
17171637

1718-
###### Example usage
1638+
Data type: `Any`
17191639

1720-
```puppet
17211640

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

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

@@ -3332,7 +3246,7 @@ Data type: `Optional[Enum['directory', 'link', 'mounted', 'service', 'file', 'pa
33323246
Type: Ruby 4.x API
33333247

33343248
It optionally takes a hash as a second parameter that will be passed as the
3335-
third argument to the ensure_resource() function.
3249+
third argument to the stdlib::ensure_resource() function.
33363250

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

@@ -3350,7 +3264,7 @@ The packages to ensure are installed.
33503264

33513265
Data type: `Optional[Hash]`
33523266

3353-
Default attributes to be passed to the `ensure_resource()` function
3267+
Default attributes to be passed to the `stdlib::ensure_resource()` function
33543268

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

@@ -3370,6 +3284,172 @@ Data type: `Optional[Hash]`
33703284

33713285
Default attributes. Package specific attributes from the `packages` parameter will take precedence.
33723286

3287+
### <a name="stdlib--ensure_resource"></a>`stdlib::ensure_resource`
3288+
3289+
Type: Ruby 4.x API
3290+
3291+
@summary
3292+
Takes a resource type, title, and a list of attributes that describe a
3293+
resource.
3294+
3295+
user { 'dan':
3296+
ensure => present,
3297+
}
3298+
3299+
@return
3300+
created or recreated the passed resource with the passed type and attributes
3301+
3302+
@example Example usage
3303+
3304+
Creates the resource if it does not already exist:
3305+
3306+
stdlib::ensure_resource('user', 'dan', {'ensure' => 'present' })
3307+
3308+
If the resource already exists but does not match the specified parameters,
3309+
this function will attempt to recreate the resource leading to a duplicate
3310+
resource definition error.
3311+
3312+
An array of resources can also be passed in and each will be created with
3313+
the type and parameters specified if it doesn't already exist.
3314+
3315+
ensure_resource('user', ['dan','alex'], {'ensure' => 'present'})
3316+
3317+
#### `stdlib::ensure_resource(String $type, Variant[String,Array[String]] $title, Hash $params)`
3318+
3319+
@summary
3320+
Takes a resource type, title, and a list of attributes that describe a
3321+
resource.
3322+
3323+
user { 'dan':
3324+
ensure => present,
3325+
}
3326+
3327+
@return
3328+
created or recreated the passed resource with the passed type and attributes
3329+
3330+
@example Example usage
3331+
3332+
Creates the resource if it does not already exist:
3333+
3334+
stdlib::ensure_resource('user', 'dan', {'ensure' => 'present' })
3335+
3336+
If the resource already exists but does not match the specified parameters,
3337+
this function will attempt to recreate the resource leading to a duplicate
3338+
resource definition error.
3339+
3340+
An array of resources can also be passed in and each will be created with
3341+
the type and parameters specified if it doesn't already exist.
3342+
3343+
ensure_resource('user', ['dan','alex'], {'ensure' => 'present'})
3344+
3345+
Returns: `Any`
3346+
3347+
##### `type`
3348+
3349+
Data type: `String`
3350+
3351+
The resource type to create
3352+
3353+
##### `title`
3354+
3355+
Data type: `Variant[String,Array[String]]`
3356+
3357+
The resource title or array of resource titles
3358+
3359+
##### `params`
3360+
3361+
Data type: `Hash`
3362+
3363+
The resource parameters
3364+
3365+
### <a name="stdlib--ensure_resources"></a>`stdlib::ensure_resources`
3366+
3367+
Type: Ruby 4.x API
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+
#### `stdlib::ensure_resources(String $type, Hash[String,Hash] $titles, Optional[Hash] $params)`
3401+
3402+
@summary
3403+
Takes a resource type, title (only hash), and a list of attributes that describe a
3404+
resource.
3405+
3406+
@return
3407+
created resources with the passed type and attributes
3408+
3409+
@example Example usage
3410+
3411+
user { 'dan':
3412+
gid => 'mygroup',
3413+
ensure => present,
3414+
}
3415+
3416+
An hash of resources should be passed in and each will be created with
3417+
the type and parameters specified if it doesn't already exist.
3418+
3419+
stdlib::ensure_resources('user', {'dan' => { gid => 'mygroup', uid => '600' }, 'alex' => { gid => 'mygroup' }}, {'ensure' => 'present'})
3420+
3421+
From Hiera Backend:
3422+
3423+
userlist:
3424+
dan:
3425+
gid: 'mygroup'
3426+
uid: '600'
3427+
alex:
3428+
gid: 'mygroup'
3429+
3430+
Call:
3431+
ensure_resources('user', hiera_hash('userlist'), {'ensure' => 'present'})
3432+
3433+
Returns: `Any`
3434+
3435+
##### `type`
3436+
3437+
Data type: `String`
3438+
3439+
The resource type to create
3440+
3441+
##### `titles`
3442+
3443+
Data type: `Hash[String,Hash]`
3444+
3445+
A hash of resource titles mapping to resource parameters
3446+
3447+
##### `params`
3448+
3449+
Data type: `Optional[Hash]`
3450+
3451+
A hash of default parameters to be merged with individual resource parameters
3452+
33733453
### <a name="stdlib--extname"></a>`stdlib::extname`
33743454

33753455
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)