openstack.rb 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. require File.join(File.dirname(__FILE__), '..','..','..', 'puppet/provider/nova')
  2. Puppet::Type.type(:nova_aggregate).provide(
  3. :openstack,
  4. :parent => Puppet::Provider::Nova
  5. ) do
  6. desc <<-EOT
  7. Provider to manage nova aggregations
  8. EOT
  9. @credentials = Puppet::Provider::Openstack::CredentialsV3.new
  10. mk_resource_methods
  11. def self.instances
  12. request('aggregate', 'list').collect do |el|
  13. attrs = request('aggregate', 'show', el[:name])
  14. new(
  15. :ensure => :present,
  16. :name => attrs[:name],
  17. :id => attrs[:id],
  18. :availability_zone => attrs[:availability_zone],
  19. :metadata => str2hash(attrs[:properties]),
  20. :hosts => string2list(attrs[:hosts]).sort
  21. )
  22. end
  23. end
  24. def self.string2list(input)
  25. return input[1..-2].split(",").map { |x| x.match(/'(.*?)'/)[1] }
  26. end
  27. def self.prefetch(resources)
  28. instances_ = instances
  29. resources.keys.each do |name|
  30. if provider = instances_.find{ |instance| instance.name == name }
  31. resources[name].provider = provider
  32. end
  33. end
  34. end
  35. def exists?
  36. @property_hash[:ensure] == :present
  37. end
  38. def destroy
  39. @property_hash[:hosts].each do |h|
  40. properties = [@property_hash[:name], h]
  41. self.class.request('aggregate', 'remove host', properties)
  42. end
  43. self.class.request('aggregate', 'delete', @property_hash[:name])
  44. end
  45. def create
  46. properties = [@resource[:name]]
  47. if not @resource[:availability_zone].nil? and not @resource[:availability_zone].empty?
  48. properties << "--zone" << @resource[:availability_zone]
  49. end
  50. if not @resource[:metadata].nil? and not @resource[:metadata].empty?
  51. @resource[:metadata].each do |key, value|
  52. properties << "--property" << "#{key}=#{value}"
  53. end
  54. end
  55. @property_hash = self.class.request('aggregate', 'create', properties)
  56. if not @resource[:hosts].nil? and not @resource[:hosts].empty?
  57. @resource[:hosts].each do |host|
  58. properties = [@property_hash[:name], host]
  59. self.class.request('aggregate', 'add host', properties)
  60. end
  61. end
  62. end
  63. def availability_zone=(value)
  64. self.class.request('aggregate', 'set', [ @resource[:name], '--zone', @resource[:availability_zone] ])
  65. end
  66. def metadata=(value)
  67. # clear obsolete keys
  68. # wip untill #1559866
  69. # if @property_hash[:metadata].keys.length > 0
  70. # properties = [@resource[:name] ]
  71. # (@property_hash[:metadata].keys - @resource[:metadata].keys).each do |key|
  72. # properties << "--property" << "#{key}"
  73. # end
  74. # self.class.request('aggregate', 'unset', properties)
  75. # end
  76. properties = [@resource[:name] ]
  77. @resource[:metadata].each do |key, value|
  78. properties << "--property" << "#{key}=#{value}"
  79. end
  80. self.class.request('aggregate', 'set', properties)
  81. end
  82. def hosts=(value)
  83. # remove hosts, which are not present in update
  84. (@property_hash[:hosts] - @resource[:hosts]).each do |host|
  85. properties = [@property_hash[:id], host]
  86. self.class.request('aggregate', 'remove host', properties)
  87. end
  88. # add new hosts
  89. (@resource[:hosts] - @property_hash[:hosts]).each do |host|
  90. properties = [@property_hash[:id], host]
  91. self.class.request('aggregate', 'add host', properties)
  92. end
  93. end
  94. end