Class: Fog::Compute::CloudSigma::Mock

Inherits:
Object
  • Object
show all
Includes:
Collections, Fog::CloudSigma::CloudSigmaConnection::Mock, CommonMockAndReal
Defined in:
lib/fog/cloudsigma/compute.rb,
lib/fog/cloudsigma/mock_data.rb,
lib/fog/cloudsigma/requests/get_ip.rb,
lib/fog/cloudsigma/requests/get_vlan.rb,
lib/fog/cloudsigma/requests/list_ips.rb,
lib/fog/cloudsigma/requests/open_vnc.rb,
lib/fog/cloudsigma/requests/close_vnc.rb,
lib/fog/cloudsigma/requests/get_server.rb,
lib/fog/cloudsigma/requests/get_volume.rb,
lib/fog/cloudsigma/requests/list_vlans.rb,
lib/fog/cloudsigma/requests/get_balance.rb,
lib/fog/cloudsigma/requests/get_pricing.rb,
lib/fog/cloudsigma/requests/get_profile.rb,
lib/fog/cloudsigma/requests/stop_server.rb,
lib/fog/cloudsigma/requests/update_vlan.rb,
lib/fog/cloudsigma/requests/clone_server.rb,
lib/fog/cloudsigma/requests/clone_volume.rb,
lib/fog/cloudsigma/requests/list_servers.rb,
lib/fog/cloudsigma/requests/list_volumes.rb,
lib/fog/cloudsigma/requests/start_server.rb,
lib/fog/cloudsigma/requests/create_server.rb,
lib/fog/cloudsigma/requests/create_volume.rb,
lib/fog/cloudsigma/requests/delete_server.rb,
lib/fog/cloudsigma/requests/delete_volume.rb,
lib/fog/cloudsigma/requests/update_server.rb,
lib/fog/cloudsigma/requests/update_volume.rb,
lib/fog/cloudsigma/requests/get_lib_volume.rb,
lib/fog/cloudsigma/requests/update_profile.rb,
lib/fog/cloudsigma/requests/clone_libvolume.rb,
lib/fog/cloudsigma/requests/list_fwpolicies.rb,
lib/fog/cloudsigma/requests/get_subscription.rb,
lib/fog/cloudsigma/requests/list_lib_volumes.rb,
lib/fog/cloudsigma/requests/get_current_usage.rb,
lib/fog/cloudsigma/requests/list_subscriptions.rb,
lib/fog/cloudsigma/requests/create_subscription.rb,
lib/fog/cloudsigma/requests/extend_subscription.rb,
lib/fog/cloudsigma/requests/calculate_subscription_price.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Fog::CloudSigma::CloudSigmaConnection::Mock

#mock_create, #mock_delete, #mock_get, #mock_list, #mock_update, #setup_connection

Methods included from CommonMockAndReal

#balance, #currency, #current_pricing, #current_pricing_levels, #current_usage, #initialize, #next_pricing, #next_pricing_levels, #pricing, #profile, #subscription_pricing

Class Method Details

.dataObject



190
191
192
193
194
# File 'lib/fog/cloudsigma/compute.rb', line 190

def self.data
  @data ||= Hash.new do |hash, key|
    hash[key] = mock_data
  end
end

.mock_dataObject



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
# File 'lib/fog/cloudsigma/mock_data.rb', line 5

def self.mock_data
  {
      :volumes => {},
      :servers => {},
      :vlans => {},
      :ips => {},
      :profile => {:login_sms=>false,
                   :town=>"",
                   :postcode=>"",
                   :reseller=>"",
                   :has_autotopup=>false,
                   :currency=>"CHF",
                   :state=>"REGULAR",
                   :uuid=>"6c2203a1-a2e6-433f-aeab-b976b8cd3d18",
                   :company=>"",
                   :api_https_only=>false,
                   :my_notes=>"",
                   :key_auth=>false,
                   :email=>"[email protected]",
                   :bank_reference=>"mmlastname278",
                   :first_name=>"MyFirstName",
                   :meta =>"",
                   :phone=>"",
                   :language=>"EN",
                   :vat=>"",
                   :last_name=>"MyLasttName",
                   :title=>"",
                   :mailing_list=>true,
                   :autotopup_amount=>0.0,
                   :country=>"",
                   :address=>""},
      :subscriptions => {},
      :current_usage => {},
      :balance => {:balance => 100, :currency => 'CHF'},

  }
end

.random_macObject



203
204
205
# File 'lib/fog/cloudsigma/compute.rb', line 203

def self.random_mac
  (0..5).map{Fog::Mock.random_hex(2)}.join(':')
end

.random_uuidObject



196
197
198
199
200
201
# File 'lib/fog/cloudsigma/compute.rb', line 196

def self.random_uuid
  # Insert '4' at 13th position and 'a' at 17th as per uuid4 spec
  hex = Fog::Mock.random_hex(30).insert(12,'4').insert(16, 'a')
  # Add dashes
  "#{hex[0...8]}-#{hex[8...12]}-#{hex[12...16]}-#{hex[16...20]}-#{hex[20..32]}"
end

Instance Method Details

#calculate_subscription_price(data) ⇒ Object



11
12
# File 'lib/fog/cloudsigma/requests/calculate_subscription_price.rb', line 11

def calculate_subscription_price(data)
end

#clone_libvolume(vol_id, clone_params = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/fog/cloudsigma/requests/clone_libvolume.rb', line 15

def clone_libvolume(vol_id, clone_params={})
  volume = self.data[:libvolumes][vol_id].dup
  uuid = self.class.random_uuid
  volume['uuid'] = uuid

  self.data[:volumes][uuid] = volume

  response = Excon::Response.new
  response.status = 200
  response.body = volume

  response
end

#clone_server(server_id, clone_params = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/fog/cloudsigma/requests/clone_server.rb', line 15

def clone_server(server_id, clone_params={})
  server = self.data[:servers][server_id].dup
  uuid = self.class.random_uuid
  server['uuid'] = uuid

  self.data[:servers][uuid] = server

  response = Excon::Response.new
  response.status = 200
  response.body = server

  response
end

#clone_volume(vol_id, clone_params = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/fog/cloudsigma/requests/clone_volume.rb', line 15

def clone_volume(vol_id, clone_params={})
  volume = self.data[:volumes][vol_id].dup
  uuid = self.class.random_uuid
  volume['uuid'] = uuid

  self.data[:volumes][uuid] = volume

  response = Excon::Response.new
  response.status = 200
  response.body = volume

  response
end

#close_vnc(server_id) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/fog/cloudsigma/requests/close_vnc.rb', line 14

def close_vnc(server_id)
  response = Excon::Response.new
  response.status = 200

  response.body = {
      'action' => 'close_vnc',
      'result' => 'success',
      'uuid' => server_id,
  }

  response
end

#create_server(data) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/fog/cloudsigma/requests/create_server.rb', line 11

def create_server(data)
  uuid = self.class.random_uuid

  defaults = {'uuid' => uuid,
              'status' => 'stopped',
              'smp' => 1,
              'hv_relaxed' => false,
              'hv_tsc' => false,
              'enable_numa' => false,
              'cpus_instead_of_cores' => false,
              'drives' => [],
              'nics' => [],
              'tags' => []
  }

  mock_create(:servers, 202, data, uuid, defaults)
end

#create_subscription(data) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/fog/cloudsigma/requests/create_subscription.rb', line 11

def create_subscription(data)
  if data[:period] != '1 month' || data[:start_time] || data[:end_time]
    raise Fog::Errors::MockNotImplemented.new('Currently only mocks for subscriptions with period 1 month from now are implemented as mock')
  end

  id = Fog::Mock.random_numbers(3).to_i
  defaults = {'id' => id,
              'start_time' => DateTime.now,
              'end_time' => DateTime.now + 30 * 24 * 60 *60,
              'auto_renew' => false,
              'amount' => 1.0}

  if data[:resource] == 'vlan'
    vlan_uuid = self.class.random_uuid
    self.data[:vlans][vlan_uuid] = {'uuid' => vlan_uuid,
                                    'subscription' => {'id' => id},
                                    'servers' => [],
                                    'meta' => {},
                                    'tags' => []}
    defaults['subscribed_object'] = vlan_uuid
  end

  mock_create(:subscriptions, 200, data, id, defaults)
end

#create_volume(data) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/fog/cloudsigma/requests/create_volume.rb', line 11

def create_volume(data)
  uuid = self.class.random_uuid

  defaults = {'uuid' => uuid,
              'status' => 'unmounted',
              'tags' => [],
              'mounted_on' => [],
              'affinities' => [],
              'licenses' => [],
              'jobs' => [],
              'allow_multimount' => false,
  }

  mock_create(:volumes, 202, data, uuid, defaults)
end

#dataObject



207
208
209
# File 'lib/fog/cloudsigma/compute.rb', line 207

def data
  self.class.data[:test]
end

#delete_server(server_id) ⇒ Object



11
12
13
# File 'lib/fog/cloudsigma/requests/delete_server.rb', line 11

def delete_server(server_id)
  mock_delete(:servers, 204, server_id)
end

#delete_volume(vol_id) ⇒ Object



11
12
13
# File 'lib/fog/cloudsigma/requests/delete_volume.rb', line 11

def delete_volume(vol_id)
  mock_delete(:volumes, 204, vol_id)
end

#extend_subscription(sub_id, data) ⇒ Object



15
16
# File 'lib/fog/cloudsigma/requests/extend_subscription.rb', line 15

def extend_subscription(sub_id, data)
end

#get_balanceObject



11
12
13
# File 'lib/fog/cloudsigma/requests/get_balance.rb', line 11

def get_balance
  mock_get(:balance,  200)
end

#get_current_usageObject



11
12
# File 'lib/fog/cloudsigma/requests/get_current_usage.rb', line 11

def get_current_usage
end

#get_ip(ip) ⇒ Object



11
12
13
# File 'lib/fog/cloudsigma/requests/get_ip.rb', line 11

def get_ip(ip)
  mock_get(:ips, 200, ip)
end

#get_lib_volume(vol_id) ⇒ Object



11
12
13
# File 'lib/fog/cloudsigma/requests/get_lib_volume.rb', line 11

def get_lib_volume(vol_id)
  mock_get(:libvolumes, 200, vol_id)
end

#get_pricing(currency = nil, subscription = false) ⇒ Object



21
22
23
# File 'lib/fog/cloudsigma/requests/get_pricing.rb', line 21

def get_pricing(currency=nil, subscription=false)
  mock_get(:pricing,  200)
end

#get_profileObject



11
12
13
# File 'lib/fog/cloudsigma/requests/get_profile.rb', line 11

def get_profile
  mock_get(:profile,  200)
end

#get_server(server_id) ⇒ Object



11
12
13
# File 'lib/fog/cloudsigma/requests/get_server.rb', line 11

def get_server(server_id)
  mock_get(:servers, 200, server_id)
end

#get_subscription(sub_id) ⇒ Object



11
12
13
# File 'lib/fog/cloudsigma/requests/get_subscription.rb', line 11

def get_subscription(sub_id)
  mock_get(:subscriptions, 200, sub_id)
end

#get_vlan(vlan) ⇒ Object



11
12
13
# File 'lib/fog/cloudsigma/requests/get_vlan.rb', line 11

def get_vlan(vlan)
  mock_get(:vlans, 200, vlan)
end

#get_volume(vol_id) ⇒ Object



11
12
13
# File 'lib/fog/cloudsigma/requests/get_volume.rb', line 11

def get_volume(vol_id)
  mock_get(:volumes, 200, vol_id)
end

#list_fwpoliciesObject



11
12
13
# File 'lib/fog/cloudsigma/requests/list_fwpolicies.rb', line 11

def list_fwpolicies
  Fog::Mock.not_implemented
end

#list_ipsObject



11
12
13
# File 'lib/fog/cloudsigma/requests/list_ips.rb', line 11

def list_ips
  mock_list(:ips, 200)
end

#list_lib_volumesObject



11
12
13
# File 'lib/fog/cloudsigma/requests/list_lib_volumes.rb', line 11

def list_lib_volumes
  mock_list(:libvolumes, 200)
end

#list_serversObject



11
12
13
# File 'lib/fog/cloudsigma/requests/list_servers.rb', line 11

def list_servers
  mock_list(:servers, 200)
end

#list_subscriptionsObject



11
12
13
# File 'lib/fog/cloudsigma/requests/list_subscriptions.rb', line 11

def list_subscriptions
  mock_list(:subscriptions, 200)
end

#list_vlansObject



11
12
13
# File 'lib/fog/cloudsigma/requests/list_vlans.rb', line 11

def list_vlans
  mock_list(:vlans, 200)
end

#list_volumesObject



11
12
13
# File 'lib/fog/cloudsigma/requests/list_volumes.rb', line 11

def list_volumes
  mock_list(:volumes, 200)
end

#open_vnc(server_id) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/fog/cloudsigma/requests/open_vnc.rb', line 14

def open_vnc(server_id)
  response = Excon::Response.new
  response.status = 200
  host = @init_options[:cloudsigma_host]
  port = Fog::Mock.random_number(65000)
  vnc_url = "vnc://#{host}:#{port}"

  response.body = {
      'action' => 'open_vnc',
      'result' => 'success',
      'uuid' => server_id,
      'vnc_url' => vnc_url
  }

  response
end

#start_server(server_id, start_params = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/fog/cloudsigma/requests/start_server.rb', line 14

def start_server(server_id, start_params={})
  server = self.data[:servers][server_id]
  server['status'] = 'running'

  response = Excon::Response.new
  response.status = 200
  response.body = {
      'action' => 'start',
      'result' => 'success',
      'uuid' => server_id
  }

  response
end

#stop_server(server_id) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/fog/cloudsigma/requests/stop_server.rb', line 14

def stop_server(server_id)
  server = self.data[:servers][server_id]
  server['status'] = 'stopped'

  response = Excon::Response.new
  response.status = 200
  response.body = {
      'action' => 'stop',
      'result' => 'success',
      'uuid' => server_id
  }

  response
end

#update_profile(data) ⇒ Object



11
12
13
# File 'lib/fog/cloudsigma/requests/update_profile.rb', line 11

def update_profile(data)
  mock_update(data, :profile, 200)
end

#update_server(server_id, data) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/fog/cloudsigma/requests/update_server.rb', line 13

def update_server(server_id, data)
  mock_update(data, :servers, 200, server_id) do |old_data, new_data|
    old_nics = old_data['nics']
    new_nics = new_data['nics']

    old_nics_macs = old_nics.map { |nic| nic['mac'] }.compact
    new_nics_macs = new_nics.map { |nic| nic['mac'] }.compact

    newly_created_macs = Set.new(new_nics_macs) - old_nics_macs
    unless newly_created_macs.empty?
      mac_err = <<-EOS
      MAC(s) #{newly_created_macs.to_a} not specified on guest #{server_id}. Nic MACs are automatically assigned at
      creation time and cannot be changed. Do not specify MAC to create a new NIC or specify existing MAC to
      update existing NIC.
      EOS
      raise Fog::CloudSigma::Errors::RequestError.new(mac_err, 'permission')
    end

    new_nics.each { |nic| nic['mac'] ||= Fog::Compute::CloudSigma::Mock.random_mac }

    old_data.merge(new_data)
  end
end

#update_vlan(vlan_id, data) ⇒ Object



13
14
15
# File 'lib/fog/cloudsigma/requests/update_vlan.rb', line 13

def update_vlan(vlan_id, data)
  mock_update(data, :vlans, 200, vlan_id)
end

#update_volume(vol_id, data) ⇒ Object



11
12
13
# File 'lib/fog/cloudsigma/requests/update_volume.rb', line 11

def update_volume(vol_id, data)
  mock_update(data, :volumes, 200,  vol_id)
end