Class: Fog::Compute::OpenNebula::Mock

Inherits:
Object
  • Object
show all
Includes:
Collections
Defined in:
lib/fog/opennebula/requests/compute/template_pool.rb,
lib/fog/opennebula/compute.rb,
lib/fog/opennebula/requests/compute/list_vms.rb,
lib/fog/opennebula/requests/compute/vm_resume.rb,
lib/fog/opennebula/requests/compute/vm_destroy.rb,
lib/fog/opennebula/requests/compute/vm_suspend.rb,
lib/fog/opennebula/requests/compute/list_groups.rb,
lib/fog/opennebula/requests/compute/vm_allocate.rb,
lib/fog/opennebula/requests/compute/list_networks.rb,
lib/fog/opennebula/requests/compute/get_vnc_console.rb,
lib/fog/opennebula/requests/compute/vm_disk_snapshot.rb

Overview

class Real

Defined Under Namespace

Classes: Mock_nic

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Mock

Returns a new instance of Mock.



97
98
99
100
101
102
103
104
105
106
107
# File 'lib/fog/opennebula/compute.rb', line 97

def initialize(options={})
  @opennebula_endpoint = options[:opennebula_endpoint]
  @opennebula_username = options[:opennebula_username]
  @opennebula_password = options[:opennebula_password]

  begin
    require "opennebula"
  rescue LoadError
    raise Fog::Errors::LoadError, "To use OpenNebula provider, you must load 'opennebula' gem"
  end
end

Class Method Details

.dataObject



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
# File 'lib/fog/opennebula/compute.rb', line 37

def self.data
  @data ||= Hash.new do |hash, key|
    hash[key] = {
      "vms" => [
        {
          "onevm_object" => "",
          "status" => "RUNNING",
          "state" => "3",
          "id" => 4,
          "uuid" => "5",
          "gid" => "5",
          "name" => "MockVM",
          "user" => "MockUser",
          "group" => "MockGroup",
          "cpu" => "2",
          "memory" => "1024",
          "mac" => "00:01:02:03:04:05",
          "ip" => "1.1.1.1"
        }
      ],
      "image_pool" => [
        {}
      ],
      "template_pool" => [
        {
          "content" => %Q{
            NAME = mock-vm
            MEMORY = 512
            VCPU = 1
            CPU = 1
          },
          "id" => 1,
          "name" => 'mock',
          "cpu" => 1,
          "vcpu" => 1,
          "memory" => 512,
          "sched_requirements" => 'CPUSPEED > 1000',
          "sched_rank" => 'FREECPU',
          "sched_ds_requirements" => "NAME=mock",
          "sched_ds_rank" => "FREE_MB",
          "disk" => {},
          "nic" => {},
          "os" => {
            'ARCH' => 'x86_64'
          },
          "graphics" => {},
          "raw" => %|["DATA"=>"<cpu match='exact'><model fallback='allow'>core2duo</model></cpu>", "TYPE"=>"kvm"]|,
          "context" => {},
          "user_variables" => {}
        }
      ]
    }
  end
end

.resetObject



92
93
94
# File 'lib/fog/opennebula/compute.rb', line 92

def self.reset
  @data = nil
end

Instance Method Details

#clientObject



109
110
111
# File 'lib/fog/opennebula/compute.rb', line 109

def client
  return @client
end

#dataObject



113
114
115
# File 'lib/fog/opennebula/compute.rb', line 113

def data
  self.class.data[@opennebula_endpoint]
end

#get_vnc_console(server_id, console_type) ⇒ Object

Get a vnc console for an instance.

Parameters

  • server_id <~String> - The ID of the server.

  • console_type <~String> - Type of vnc console to get (‘novnc’ or ‘xvpvnc’).

Returns

  • response <~Excon::Response>:

    • body <~Hash>:

      • url <~String>

      • type <~String>



16
17
18
19
20
21
22
23
24
# File 'lib/fog/opennebula/requests/compute/get_vnc_console.rb', line 16

def get_vnc_console(server_id, console_type)
  body = {
      :type => "novnc",
      :proxy_port => "29876",
      :password => "null",
      :token => "3n32dtwpsdj5jkug3b4w",
      :proxy_host => "example.com"
  }
end

#list_groups(filter = {}) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/fog/opennebula/requests/compute/list_groups.rb', line 43

def list_groups(filter={})
  groups = []
  net1 = mock_group "1", 'net1'
  net2 = mock_group "2", 'fogtest'

  grouppool = [net1, net2]
  grouppool.each do |group| 
    filter_missmatch = false

    unless (filter.empty?)
      filter.each do |k,v|
        if group["#{k.to_s.upcase}"] && group["#{k.to_s.upcase}"] != v.to_s
          filter_missmatch = true
          break
        end
      end 
      next if filter_missmatch
    end 
    groups << {:id => group["ID"], :name => group["NAME"]}
  end
  groups
end

#list_networks(filters = {}) ⇒ Object



52
53
54
55
56
# File 'lib/fog/opennebula/requests/compute/list_networks.rb', line 52

def list_networks(filters={})
  net1 = mock_network 'fogtest'
  net2 = mock_network 'net2'
  [net1, net2]
end

#list_vms(filter = {}) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/fog/opennebula/requests/compute/list_vms.rb', line 55

def list_vms(filter = {})
  vms = []
  self.data['vms'].each do |vm|
    if filter[:id].nil?
      vms << vm
    elsif filter[:id] == vm['id']
      vms << vm
    end
  end
  vms
end

#mock_group(id, name) ⇒ Object



66
67
68
69
70
71
72
73
74
75
# File 'lib/fog/opennebula/requests/compute/list_groups.rb', line 66

def mock_group id, name
  {
    "ID"    	 => id,
    "NAME"  	 => name,
    "UID"   	 => "5",
    "GID"   	 => "5",
    "DESCRIPTION" => "netDescription",
    "VLAN"	 => "5"
  }
end

#mock_network(name) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/fog/opennebula/requests/compute/list_networks.rb', line 58

def mock_network name
  {
    :id    	     => "5",
    :name  	     => name,
    :uid   	     => "5",
    :uname       => "mock",
    :gid   	     => "5",
    :description => "netDescription",
    :vlan	       => "5"
  }
end

#reset_dataObject



117
118
119
# File 'lib/fog/opennebula/compute.rb', line 117

def reset_data
  self.class.data.delete(@opennebula_endpoint)
end

#template_pool(filter = { }) ⇒ Object



109
110
111
112
113
114
115
116
117
118
# File 'lib/fog/opennebula/requests/compute/template_pool.rb', line 109

def template_pool(filter = { })
  nic1 = Mock_nic.new
  nic1.vnet = networks.first

  self.data['template_pool']
  self.data['template_pool'].each do |tmpl|
    tmpl['nic'][0] = nic1
  end
  self.data['template_pool']
end

#vm_allocate(attr = { }) ⇒ Object



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
# File 'lib/fog/opennebula/requests/compute/vm_allocate.rb', line 70

def vm_allocate(attr={ })
  response = Excon::Response.new
  response.status = 200

  id = rand(1000)
  ids = []

  self.data['vms'].each do |vm|
    ids << vm['id']
    if vm['id'] == id
      while ids.include?(id)
        id = rand(1000)
      end
      break
    end
  end

  data = {}
  data['id'] = id
					data['flavor'] = attr[:flavor]
					data['name'] = attr[:name]
  data['state'] = 'RUNNING'
  data['status'] = 3
  self.data['vms'] << data
					data
end

#vm_destroy(id) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/fog/opennebula/requests/compute/vm_destroy.rb', line 17

def vm_destroy(id)
  response = Excon::Response.new
  response.status = 200

  self.data['vms'].each do |vm|
    if vm['id'] == id
      self.data['vms'].delete(vm)
    end
  end
  true
end

#vm_disk_snapshot(id, disk_id, image_name) ⇒ Object



23
24
# File 'lib/fog/opennebula/requests/compute/vm_disk_snapshot.rb', line 23

def vm_disk_snapshot(id, disk_id, image_name)
end

#vm_resume(id) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/fog/opennebula/requests/compute/vm_resume.rb', line 16

def vm_resume(id)
  self.data['vms'].each do |vm|
    if id == vm['id']
      vm['state'] = 'RUNNING'
      vm['status'] = 3
    end
  end
end

#vm_suspend(id) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/fog/opennebula/requests/compute/vm_suspend.rb', line 16

def vm_suspend(id)
  response = Excon::Response.new
  response.status = 200

  self.data['vms'].each do |vm|
    if id == vm['id']
      vm['state'] = 'LCM_INIT'
      vm['status'] = 5
    end
  end

end