forked from voxpupuli/puppet-redis
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert
redisget
to redis::get
API v4 function
Since we'll be dropping puppet 3 support in the next release, we should move away from using legacy functions that suffer from environment isolation issues. See https://puppet.com/docs/puppet/4.10/functions_legacy.html The newer function API also has built in support for validating function arguments, declaring some parameters as optional etc. This allows us to remove a lot of our own code that was performing this validation before. Fixes voxpupuli#291
- Loading branch information
1 parent
eaab8ab
commit 8b7104e
Showing
7 changed files
with
117 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# Reference | ||
<!-- DO NOT EDIT: This document was generated by Puppet Strings --> | ||
|
||
## Table of Contents | ||
|
||
**Functions** | ||
|
||
* [`redis::get`](#redisget): Returns the value of the key being looked up or `undef` if the key does not exist. Takes two arguments with an optional third. The first bein | ||
|
||
## Functions | ||
|
||
### redis::get | ||
|
||
Type: Ruby 4.x API | ||
|
||
Returns the value of the key being looked up or `undef` if the key does not | ||
exist. Takes two arguments with an optional third. The first being a string | ||
value of the key to be looked up, the second is the URL to the Redis service | ||
and the third optional argument is a default value to be used if the lookup | ||
fails. | ||
|
||
example usage | ||
``` | ||
$version = redis::get('version.myapp', 'redis://redis.example.com:6379') | ||
$version_with_default = redis::get('version.myapp', 'redis://redis.example.com:6379', $::myapp_version) | ||
``` | ||
|
||
#### `redis::get(String[1] $key, Redis::RedisUrl $url, Optional[String] $default)` | ||
|
||
Returns the value of the key being looked up or `undef` if the key does not | ||
exist. Takes two arguments with an optional third. The first being a string | ||
value of the key to be looked up, the second is the URL to the Redis service | ||
and the third optional argument is a default value to be used if the lookup | ||
fails. | ||
|
||
example usage | ||
``` | ||
$version = redis::get('version.myapp', 'redis://redis.example.com:6379') | ||
$version_with_default = redis::get('version.myapp', 'redis://redis.example.com:6379', $::myapp_version) | ||
``` | ||
|
||
Returns: `Optional[String]` Returns the value of the key from Redis | ||
|
||
##### `key` | ||
|
||
Data type: `String[1]` | ||
|
||
The key to look up in redis | ||
|
||
##### `url` | ||
|
||
Data type: `Redis::RedisUrl` | ||
|
||
The endpoint of the Redis instance | ||
|
||
##### `default` | ||
|
||
Data type: `Optional[String]` | ||
|
||
The value to return if the key is not found or the connection to Redis fails | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
require 'redis' | ||
# Returns the value of the key being looked up or `undef` if the key does not | ||
# exist. Takes two arguments with an optional third. The first being a string | ||
# value of the key to be looked up, the second is the URL to the Redis service | ||
# and the third optional argument is a default value to be used if the lookup | ||
# fails. | ||
# | ||
# example usage | ||
# ``` | ||
# $version = redis::get('version.myapp', 'redis://redis.example.com:6379') | ||
# $version_with_default = redis::get('version.myapp', 'redis://redis.example.com:6379', $::myapp_version) | ||
# ``` | ||
Puppet::Functions.create_function(:'redis::get') do | ||
# @param key The key to look up in redis | ||
# @param url The endpoint of the Redis instance | ||
# @param default The value to return if the key is not found or the connection to Redis fails | ||
# @return Returns the value of the key from Redis | ||
dispatch :get do | ||
param 'String[1]', :key | ||
param 'Redis::RedisUrl', :url | ||
optional_param 'String', :default | ||
return_type 'Optional[String]' | ||
end | ||
|
||
def get(key, url, default = nil) | ||
Redis.new(url: url).get(key) || default | ||
rescue Redis::CannotConnectError, SocketError => e | ||
raise Puppet::Error, "connection to redis server failed - #{e}" unless default | ||
Puppet.debug "Connection to redis failed with #{e} - Returning default value of #{default}" | ||
default | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
type Redis::RedisUrl = Pattern[/(^redis:\/\/)/] |