1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00
fog--fog/lib/fog/openstack/requests/compute/create_flavor.rb
Alfonso Juan Dillera 1db7605986 [openstack|compute] Add flavor CRUD
- Added requests.
- Added definitions to model.
- Added tests but having a problem with some items being of float type.
- Must revisit test.
2012-04-30 10:35:00 +08:00

82 lines
2.6 KiB
Ruby

module Fog
module Compute
class OpenStack
class Real
# PARAMETERS #
# name = Name of flavor
# ram = Memory in MB
# vcpus = Number of VCPUs
# disk = Size of local disk in GB
# swap = Swap space in MB
# rxtx_factor = RX/TX factor
def create_flavor(attributes)
# Get last flavor id
flavor_ids = Array.new
flavors = list_flavors_detail.body['flavors']
flavors.each do |flavor|
flavor_ids << flavor['id'].to_i
end
# Set flavor id
attributes[:flavor_id] = attributes[:flavor_id] || flavor_ids.sort.last + 1
data = {
'flavor' => {
'name' => attributes[:name],
'ram' => attributes[:ram],
'vcpus' => attributes[:vcpus],
'disk' => attributes[:disk],
'id' => attributes[:flavor_id],
'swap' => attributes[:swap],
'OS-FLV-EXT-DATA:ephemeral' => attributes[:ephemeral],
'rxtx_factor' => attributes[:rxtx_factor]
}
}
request(
:body => MultiJson.encode(data),
:expects => 200,
:method => 'POST',
:path => 'flavors'
)
end
end
class Mock
def create_flavor(attributes)
response = Excon::Response.new
response.status = 200
response.headers = {
"X-Compute-Request-Id" => "req-fdc6f99e-55a2-4ab1-8904-0892753828cf",
"Content-Type" => "application/json",
"Content-Length" => "356",
"Date" => Date.new
}
response.body = {
"flavor" => {
"vcpus" => attributes[:vcpus],
"disk" => attributes[:disc],
"name" => attributes[:name],
"links" => [
{
"href" => "http://192.168.27.100:8774/v1.1/6733e93c5f5c4eb1bcabc6902ba208d6/flavors/11",
"rel" => "self"
},
{
"href" => "http://192.168.27.100:8774/6733e93c5f5c4eb1bcabc6902ba208d6/flavors/11",
"rel" => "bookmark"
}
],
"rxtx_factor" => attributes[:rxtx_factor] || 1,
"OS-FLV-EXT-DATA:ephemeral" => attributes[:ephemeral] || 0,
"ram" => attributes[:ram],
"id" => 11,
"swap" => attributes[:swap] || ""
}
}
response
end
end # mock
end # openstack
end # compute
end # fog