1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
|
require File.join(File.dirname(__FILE__), '..','..','..', 'puppet/provider/nova')
Puppet::Type.type(:nova_flavor).provide(
:openstack,
:parent => Puppet::Provider::Nova
) do
desc <<-EOT
Manage Nova flavor
EOT
@credentials = Puppet::Provider::Openstack::CredentialsV3.new
def initialize(value={})
super(value)
@property_flush = {}
@project_flush = {}
end
def create
opts = [@resource[:name]]
opts << (@resource[:is_public] == :true ? '--public' : '--private')
(opts << '--id' << @resource[:id]) if @resource[:id]
(opts << '--ram' << @resource[:ram]) if @resource[:ram]
(opts << '--disk' << @resource[:disk]) if @resource[:disk]
(opts << '--ephemeral' << @resource[:ephemeral]) if @resource[:ephemeral]
(opts << '--vcpus' << @resource[:vcpus]) if @resource[:vcpus]
(opts << '--swap' << @resource[:swap]) if @resource[:swap]
(opts << '--rxtx-factor' << @resource[:rxtx_factor]) if @resource[:rxtx_factor]
@property_hash = self.class.request('flavor', 'create', opts)
if @resource[:properties] and !(@resource[:properties].empty?)
prop_opts = [@resource[:name]]
prop_opts << props_to_s(@resource[:properties])
self.class.request('flavor', 'set', prop_opts)
end
if @resource[:project] and @resource[:project] != ''
proj_opts = [@resource[:name]]
proj_opts << '--project' << @resource[:project]
self.class.request('flavor', 'set', proj_opts)
project = self.class.request('project', 'show', @resource[:project])
@property_hash[:project_name] = project[:name]
@property_hash[:project] = project[:id]
elsif @resource[:project_name] and @resource[:project_name] != ''
proj_opts = [@resource[:name]]
proj_opts << '--project' << @resource[:project_name]
self.class.request('flavor', 'set', proj_opts)
project = self.class.request('project', 'show', @resource[:project_name])
@property_hash[:project_name] = project[:name]
@property_hash[:project] = project[:id]
end
@property_hash[:ensure] = :present
end
def exists?
@property_hash[:ensure] == :present
end
def destroy
self.class.request('flavor', 'delete', @property_hash[:id])
@property_hash.clear
end
mk_resource_methods
[
:is_public,
:id,
:ram,
:disk,
:vcpus,
:swap,
:rtxt_factor,
].each do |attr|
define_method(attr.to_s + "=") do |value|
fail("{#attr.to_s} is read only")
end
end
def properties=(value)
@property_flush[:properties] = value
end
def project=(value)
@project_flush[:project] = value
end
def project_name=(value)
@project_flush[:project_name] = value
end
def self.instances
request('flavor', 'list', ['--long', '--all']).collect do |attrs|
project = request('flavor', 'show', [attrs[:id], '-c', 'access_project_ids'])
access_project_ids = project[:access_project_ids]
# Client can return None and this should be considered as ''
if access_project_ids.downcase.chomp == 'none'
project_id = nil
project_name = nil
else
# TODO(tkajinam): We'd need to consider multiple projects can be returned
project_value = parse_python_list(access_project_ids)[0]
project = request('project', 'show', project_value)
project_id = project[:id]
project_name = project[:name]
end
properties = parse_python_dict(attrs[:properties])
new(
:ensure => :present,
:name => attrs[:name],
:id => attrs[:id],
:ram => attrs[:ram],
:disk => attrs[:disk],
:ephemeral => attrs[:ephemeral],
:vcpus => attrs[:vcpus],
:is_public => attrs[:is_public].downcase.chomp == 'true'? :true : :false,
:swap => attrs[:swap],
:rxtx_factor => attrs[:rxtx_factor],
:properties => properties,
:project => project_id,
:project_name => project_name,
)
end
end
def self.prefetch(resources)
flavors = instances
resources.keys.each do |name|
if provider = flavors.find{ |flavor| flavor.name == name }
resources[name].provider = provider
end
end
end
def flush
unless @property_flush.empty?
opts = [@resource[:name]]
opts << props_to_s(@property_flush[:properties])
self.class.request('flavor', 'set', opts)
@property_flush.clear
end
unless @project_flush.empty?
if @project_flush[:project]
if @property_hash[:project] and @property_hash[:project] != ''
opts = [@resource[:name], '--project', @property_hash[:project]]
self.class.request('flavor', 'unset', opts)
end
if @project_flush[:project] != ''
opts = [@resource[:name], '--project', @project_flush[:project]]
self.class.request('flavor', 'set', opts)
end
elsif @project_flush[:project_name]
if @property_hash[:project_name] and @property_hash[:project_name] != ''
opts = [@resource[:name], '--project', @property_hash[:project_name]]
self.class.request('flavor', 'unset', opts)
end
if @project_flush[:project_name] != ''
opts = [@resource[:name], '--project', @project_flush[:project_name]]
self.class.request('flavor', 'set', opts)
end
end
@project_flush.clear
end
end
private
def props_to_s(props)
props.flat_map{ |k, v| ['--property', "#{k}=#{v}"] }
end
end
|