def create_instance(credentials, image_id, opts={})
server_ram = nil
if opts[:hwp_memory]
mem = opts[:hwp_memory].to_i
server_ram = (mem == 512) ? "512MB" : "#{mem / 1024}GB"
else
server_ram = "512MB"
end
name = opts[:name]
if not name
name = "Server #{Time.now.to_i.to_s.reverse[0..3]}#{rand(9)}"
end
if name.length > USER_NAME_MAX
raise "Parameter name must be #{USER_NAME_MAX} characters or less"
end
client = new_client(credentials)
params = {
'name' => name,
'image' => image_id,
'server.ram' => server_ram,
'ip' => get_free_ip_from_realm(credentials, opts[:realm_id] || '1')
}
params.merge!('isSandbox' => 'true') if opts[:sandbox]
safely do
instance = client.request('grid/server/add', params)['list'].first
if instance
login_data = get_login_data(client, instance[:id])
if login_data['username'] and login_data['password']
instance['username'] = login_data['username']
instance['password'] = login_data['password']
inst = convert_instance(instance, credentials.user)
else
inst = convert_instance(instance, credentials.user)
inst.authn_error = "Unable to fetch password"
end
return inst
else
return nil
end
end
end