map.pp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. # define: nginx::resource::map
  2. #
  3. # This definition creates a new mapping entry for NGINX
  4. #
  5. # Parameters:
  6. # [*ensure*] - Enables or disables the specified location (present|absent)
  7. # [*default*] - Sets the resulting value if the source values fails to
  8. # match any of the variants.
  9. # [*string*] - Source string or variable to provide mapping for
  10. # [*mappings*] - Hash of map lookup keys and resultant values
  11. # [*hostnames*] - Indicates that source values can be hostnames with a
  12. # prefix or suffix mask.
  13. # Actions:
  14. #
  15. # Requires:
  16. #
  17. # Sample Usage:
  18. #
  19. # nginx::resource::map { 'backend_pool':
  20. # ensure => present,
  21. # hostnames => true,
  22. # default => 'ny-pool-1,
  23. # string => '$http_host',
  24. # mappings => {
  25. # '*.nyc.example.com' => 'ny-pool-1',
  26. # '*.sf.example.com' => 'sf-pool-1',
  27. # }
  28. # }
  29. #
  30. # Sample Hiera usage:
  31. #
  32. # nginx::string_mappings:
  33. # client_network:
  34. # ensure: present
  35. # hostnames: true
  36. # default: 'ny-pool-1'
  37. # string: $http_host
  38. # mappings:
  39. # '*.nyc.example.com': 'ny-pool-1'
  40. # '*.sf.example.com': 'sf-pool-1'
  41. define nginx::resource::map (
  42. $string,
  43. $mappings,
  44. $default = undef,
  45. $ensure = 'present',
  46. $hostnames = false
  47. ) {
  48. validate_string($string)
  49. validate_re($string, '^.{2,}$',
  50. "Invalid string value [${string}]. Expected a minimum of 2 characters.")
  51. validate_hash($mappings)
  52. validate_bool($hostnames)
  53. validate_re($ensure, '^(present|absent)$',
  54. "Invalid ensure value '${ensure}'. Expected 'present' or 'absent'")
  55. if ($default != undef) { validate_string($default) }
  56. $root_group = $::nginx::config::root_group
  57. $ensure_real = $ensure ? {
  58. 'absent' => absent,
  59. default => 'file',
  60. }
  61. File {
  62. owner => 'root',
  63. group => $root_group,
  64. mode => '0644',
  65. }
  66. file { "${::nginx::config::conf_dir}/conf.d/${name}-map.conf":
  67. ensure => $ensure_real,
  68. content => template('nginx/conf.d/map.erb'),
  69. notify => Class['::nginx::service'],
  70. }
  71. }