PageRenderTime 25ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/chef/lib/chef/resource_platform_map.rb

https://github.com/btm/chef
Ruby | 151 lines | 118 code | 16 blank | 17 comment | 12 complexity | a8d8231d9fec9ee045a84a7d7d762e70 MD5 | raw file
Possible License(s): Apache-2.0
  1. #
  2. # Author:: Seth Chisamore (<schisamo@opscode.com>)
  3. # Copyright:: Copyright (c) 2011 Opscode, Inc.
  4. # License:: Apache License, Version 2.0
  5. #
  6. # Licensed under the Apache License, Version 2.0 (the "License");
  7. # you may not use this file except in compliance with the License.
  8. # You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. #
  18. require 'chef/mixin/params_validate'
  19. require 'chef/mixin/convert_to_class_name'
  20. class Chef
  21. class Resource
  22. class PlatformMap
  23. include Chef::Mixin::ParamsValidate
  24. include Chef::Mixin::ConvertToClassName
  25. attr_reader :map
  26. def initialize(map={:default => {}})
  27. @map = map
  28. end
  29. def filter(platform, version)
  30. resource_map = map[:default].clone
  31. platform_sym = platform
  32. if platform.kind_of?(String)
  33. platform.downcase!
  34. platform.gsub!(/\s/, "_")
  35. platform_sym = platform.to_sym
  36. end
  37. if map.has_key?(platform_sym)
  38. if map[platform_sym].has_key?(version)
  39. if map[platform_sym].has_key?(:default)
  40. resource_map.merge!(map[platform_sym][:default])
  41. end
  42. resource_map.merge!(map[platform_sym][version])
  43. elsif map[platform_sym].has_key?(:default)
  44. resource_map.merge!(map[platform_sym][:default])
  45. end
  46. end
  47. resource_map
  48. end
  49. def set(args)
  50. validate(
  51. args,
  52. {
  53. :platform => {
  54. :kind_of => Symbol,
  55. :required => false
  56. },
  57. :version => {
  58. :kind_of => String,
  59. :required => false
  60. },
  61. :short_name => {
  62. :kind_of => Symbol,
  63. :required => true
  64. },
  65. :resource => {
  66. :kind_of => [ String, Symbol, Class ],
  67. :required => true
  68. }
  69. }
  70. )
  71. if args.has_key?(:platform)
  72. if args.has_key?(:version)
  73. if map.has_key?(args[:platform])
  74. if map[args[:platform]].has_key?(args[:version])
  75. map[args[:platform]][args[:version]][args[:short_name].to_sym] = args[:resource]
  76. else
  77. map[args[:platform]][args[:version]] = {
  78. args[:short_name].to_sym => args[:resource]
  79. }
  80. end
  81. else
  82. map[args[:platform]] = {
  83. args[:version] => {
  84. args[:short_name].to_sym => args[:resource]
  85. }
  86. }
  87. end
  88. else
  89. if map.has_key?(args[:platform])
  90. if map[args[:platform]].has_key?(:default)
  91. map[args[:platform]][:default][args[:short_name].to_sym] = args[:resource]
  92. else
  93. map[args[:platform]] = { :default => { args[:short_name].to_sym => args[:resource] } }
  94. end
  95. else
  96. map[args[:platform]] = {
  97. :default => {
  98. args[:short_name].to_sym => args[:resource]
  99. }
  100. }
  101. end
  102. end
  103. else
  104. if map.has_key?(:default)
  105. map[:default][args[:short_name].to_sym] = args[:resource]
  106. else
  107. map[:default] = {
  108. args[:short_name].to_sym => args[:resource]
  109. }
  110. end
  111. end
  112. end
  113. def get(short_name, platform=nil, version=nil)
  114. resource_klass = platform_resource(short_name, platform, version) ||
  115. resource_matching_short_name(short_name)
  116. raise NameError, "Cannot find a resource for #{short_name} on #{platform} version #{version}" if resource_klass.nil?
  117. resource_klass
  118. end
  119. private
  120. def platform_resource(short_name, platform, version)
  121. pmap = filter(platform, version)
  122. rtkey = short_name.kind_of?(Chef::Resource) ? short_name.resource_name.to_sym : short_name
  123. pmap.has_key?(rtkey) ? pmap[rtkey] : nil
  124. end
  125. def resource_matching_short_name(short_name)
  126. begin
  127. rname = convert_to_class_name(short_name.to_s)
  128. Chef::Resource.const_get(rname)
  129. rescue NameError
  130. nil
  131. end
  132. end
  133. end
  134. end
  135. end