openstack.rb 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. require File.join(File.dirname(__FILE__), '..','..','..', 'puppet/provider/nova')
  2. Puppet::Type.type(:nova_flavor).provide(
  3. :openstack,
  4. :parent => Puppet::Provider::Nova
  5. ) do
  6. desc <<-EOT
  7. Manage Nova flavor
  8. EOT
  9. @credentials = Puppet::Provider::Openstack::CredentialsV3.new
  10. def initialize(value={})
  11. super(value)
  12. @property_flush = {}
  13. end
  14. def create
  15. opts = [@resource[:name]]
  16. opts << (@resource[:is_public] == :true ? '--public' : '--private')
  17. (opts << '--id' << @resource[:id]) if @resource[:id]
  18. (opts << '--ram' << @resource[:ram]) if @resource[:ram]
  19. (opts << '--disk' << @resource[:disk]) if @resource[:disk]
  20. (opts << '--ephemeral' << @resource[:ephemeral]) if @resource[:ephemeral]
  21. (opts << '--vcpus' << @resource[:vcpus]) if @resource[:vcpus]
  22. (opts << '--swap' << @resource[:swap]) if @resource[:swap]
  23. (opts << '--rxtx-factor' << @resource[:rxtx_factor]) if @resource[:rxtx_factor]
  24. @property_hash = self.class.request('flavor', 'create', opts)
  25. if @resource[:properties]
  26. prop_opts = [@resource[:name]]
  27. prop_opts << props_to_s(@resource[:properties])
  28. self.class.request('flavor', 'set', prop_opts)
  29. end
  30. @property_hash[:ensure] = :present
  31. end
  32. def exists?
  33. @property_hash[:ensure] == :present
  34. end
  35. def destroy
  36. self.class.request('flavor', 'delete', @property_hash[:id])
  37. end
  38. mk_resource_methods
  39. def is_public=(value)
  40. fail('is_public is read only')
  41. end
  42. def id=(value)
  43. fail('id is read only')
  44. end
  45. def ram=(value)
  46. fail('ram is read only')
  47. end
  48. def disk=(value)
  49. fail('disk is read only')
  50. end
  51. def vcpus=(value)
  52. fail('vcpus is read only')
  53. end
  54. def swap=(value)
  55. fail('swap is read only')
  56. end
  57. def rxtx_factor=(value)
  58. fail('rxtx_factor is read only')
  59. end
  60. def properties=(value)
  61. @property_flush[:properties] = value
  62. end
  63. def self.instances
  64. request('flavor', 'list', ['--long', '--all']).collect do |attrs|
  65. properties = Hash[attrs[:properties].scan(/(\S+)='([^']*)'/)] rescue nil
  66. new(
  67. :ensure => :present,
  68. :name => attrs[:name],
  69. :id => attrs[:id],
  70. :ram => attrs[:ram],
  71. :disk => attrs[:disk],
  72. :ephemeral => attrs[:ephemeral],
  73. :vcpus => attrs[:vcpus],
  74. :is_public => attrs[:is_public].downcase.chomp == 'true'? true : false,
  75. :swap => attrs[:swap],
  76. :rxtx_factor => attrs[:rxtx_factor],
  77. :properties => properties
  78. )
  79. end
  80. end
  81. def self.prefetch(resources)
  82. flavors = instances
  83. resources.keys.each do |name|
  84. if provider = flavors.find{ |flavor| flavor.name == name }
  85. resources[name].provider = provider
  86. end
  87. end
  88. end
  89. def flush
  90. unless @property_flush.empty?
  91. opts = [@resource[:name]]
  92. opts << props_to_s(@property_flush[:properties])
  93. self.class.request('flavor', 'set', opts)
  94. @property_flush.clear
  95. end
  96. end
  97. private
  98. def props_to_s(props)
  99. props.flat_map{ |k, v| ['--property', "#{k}=#{v}"] }
  100. end
  101. end