2011-09-07 10:00:25 -04:00
module Fog
module Compute
class Cloudstack
class Real
# Creates and automatically starts a virtual machine based on a service offering, disk offering, and template.
#
# {CloudStack API Reference}[http://download.cloud.com/releases/2.2.0/api_2.2.4/global_admin/deployVirtualMachine.html]
def deploy_virtual_machine ( options = { } )
options . merge! (
'command' = > 'deployVirtualMachine'
)
2012-05-16 14:12:08 -04:00
if security_group_ids = options . delete ( 'securitygroupids' )
2012-05-08 13:01:13 -04:00
options . merge! ( 'securitygroupids' = > Array ( security_group_ids ) . join ( ',' ) )
2011-09-07 10:00:25 -04:00
end
2012-05-08 13:01:13 -04:00
2012-05-16 14:12:08 -04:00
if security_group_names = options . delete ( 'securitygroupnames' )
2012-05-08 13:01:13 -04:00
options . merge! ( 'securitygroupnames' = > Array ( security_group_names ) . join ( ',' ) )
2011-09-07 10:00:25 -04:00
end
2012-05-08 13:01:13 -04:00
2012-05-16 14:12:08 -04:00
if network_ids = options . delete ( 'networkids' )
2012-05-08 13:01:13 -04:00
options . merge! ( 'networkids' = > Array ( network_ids ) . join ( ',' ) )
2012-01-25 11:11:22 -05:00
end
2012-05-08 13:01:13 -04:00
2011-09-07 10:00:25 -04:00
request ( options )
end
2012-05-08 13:01:13 -04:00
end # Real
class Mock
def deploy_virtual_machine ( options = { } )
2012-05-16 14:12:08 -04:00
zone_id = options [ 'zoneid' ]
2012-05-08 13:01:13 -04:00
unless zone_id
raise Fog :: Compute :: Cloudstack :: BadRequest . new ( 'Unable to execute API command deployvirtualmachine due to missing parameter zoneid' )
end
unless zone = self . data [ :zones ] [ zone_id ]
2012-05-16 14:12:08 -04:00
raise Fog :: Compute :: Cloudstack :: BadRequest . new ( " Unable to execute API command deployvirtualmachine due to invalid value. Object zone(uuid: #{ zone_id } ) does not exist. " )
2012-05-08 13:01:13 -04:00
end
zone_name = zone [ :name ]
2012-05-16 14:12:08 -04:00
template_id = options [ 'templateid' ]
unless template = self . data [ :images ] [ template_id ]
2012-05-08 13:01:13 -04:00
raise Fog :: Compute :: Cloudstack :: BadRequest . new ( 'Unable to execute API command deployvirtualmachine due to missing parameter templateid' )
end
template_name = template [ :name ]
template_display_text = template [ :display_text ]
2011-09-07 10:00:25 -04:00
2012-05-16 14:12:08 -04:00
service_offering_id = options [ 'serviceofferingid' ]
unless service_offering = self . data [ :flavors ] [ service_offering_id ]
2012-05-08 13:01:13 -04:00
raise Fog :: Compute :: Cloudstack :: BadRequest . new ( 'Unable to execute API command deployvirtualmachine due to missing parameter serviceofferingid' )
end
service_offering_name = service_offering [ :name ]
service_offering_cpu_number = service_offering [ :cpunumber ]
service_offering_cpu_speed = service_offering [ :cpuspeed ]
service_offering_memory = service_offering [ :cpumemory ]
identity = Fog :: Cloudstack . uuid
2012-05-16 14:12:08 -04:00
name = options [ 'name' ] || Fog :: Cloudstack . uuid
display_name = options [ 'displayname' ] || name
account_name = options [ 'account' ] || self . data [ :accounts ] . first [ 1 ] [ " name " ]
2012-05-08 13:01:13 -04:00
2012-05-16 14:12:08 -04:00
domain = options [ 'domainid' ] ? self . data [ :domains ] [ options [ 'domainid' ] ] : self . data [ :domains ] . first [ 1 ]
2012-05-08 13:01:13 -04:00
domain_id = domain [ :id ]
domain_name = domain [ :name ]
# how is this setup
password = nil
password_enabled = false
guest_os_id = Fog :: Cloudstack . uuid
2012-05-16 14:12:08 -04:00
security_group_ids = options [ 'securitygroupids' ] || [ ] # TODO: for now
2012-05-08 13:01:13 -04:00
2012-05-16 14:12:08 -04:00
network_ids = Array ( options [ 'networkids' ] ) || [ self . data [ :networks ] . first [ 1 ] [ " id " ] ]
2012-05-08 13:01:13 -04:00
networks = network_ids . map { | nid | self . data [ :networks ] [ nid ] }
nic = networks . map do | network |
{
" id " = > Fog :: Cloudstack . uuid ,
" networkid " = > network [ " id " ] ,
" netmask " = > Fog :: Cloudstack . ip_address ,
" gateway " = > network [ " gateway " ] ,
" ipaddress " = > Fog :: Cloudstack . ip_address ,
" traffictype " = > " Guest " , # TODO: ?
" type " = > network [ " type " ] ,
" isdefault " = > true , # TODO: ?
" macaddress " = > Fog :: Cloudstack . mac_address
}
end
virtual_machine = {
" id " = > identity ,
" name " = > name ,
" displayname " = > display_name ,
" account " = > account_name ,
" domainid " = > domain_id ,
" domain " = > domain_name ,
" created " = > Time . now . to_s ,
" state " = > " Running " ,
" haenable " = > false ,
" zoneid " = > zone_id ,
" zonename " = > zone_name ,
" templateid " = > template_id ,
" templatename " = > template_name ,
" templatedisplaytext " = > template_display_text ,
" passwordenabled " = > false ,
" serviceofferingid " = > service_offering_id ,
" serviceofferingname " = > service_offering_name ,
" cpunumber " = > service_offering_cpu_number ,
" cpuspeed " = > service_offering_cpu_speed ,
" memory " = > service_offering_memory ,
" cpuused " = > " 0% " ,
" networkkbsread " = > 0 ,
" networkkbswrite " = > 0 ,
" guestosid " = > guest_os_id ,
" rootdeviceid " = > 0 ,
" rootdevicetype " = > " NetworkFilesystem " ,
" securitygroup " = > security_group_ids , # TODO: mayhaps?
" nic " = > nic
}
2012-05-16 14:12:08 -04:00
2012-05-08 13:01:13 -04:00
self . data [ :servers ] [ identity ] = virtual_machine
{ 'deployvirtualmachineresponse' = > virtual_machine }
end
end # Mock
end # Cloudstack
end # Compute
end # Fog