module Fog::CloudSigma::CloudSigmaConnection::Mock

Public Instance Methods

mock_create(collection, status, data, key, defaults={}, &clean_before_store) click to toggle source
# File lib/fog/cloudsigma/connection.rb, line 166
def mock_create(collection, status, data, key, defaults={}, &clean_before_store)
  data_with_defaults = data.merge(defaults) {|k, oldval, newval| oldval == nil ? newval: oldval}

  if clean_before_store
    cleaned_data = clean_before_store.call(data_with_defaults)
  else
    cleaned_data = data_with_defaults
  end

  # Encode and decode into JSON so that the result is the same as the one returned and parsed from the API
  final_data =  Fog::JSON.decode(Fog::JSON.encode(cleaned_data))

  self.data[collection][key] = final_data

  # dup so that stored data is different instance from response data
  response_data = final_data.dup

  response = Excon::Response.new
  response.body = {'objects' => [response_data]}
  response.status = status

  response
end
mock_delete(collection, status, key) click to toggle source
# File lib/fog/cloudsigma/connection.rb, line 160
def mock_delete(collection, status, key)
  self.data[collection].delete(key)

  Excon::Response.new(:body => '', :status => status)
end
mock_get(obj_or_collection, status, key=nil) click to toggle source
# File lib/fog/cloudsigma/connection.rb, line 116
def mock_get(obj_or_collection, status, key=nil)
  data = self.data[obj_or_collection]
  if key
    data = data[key]
    unless data
      raise Fog::CloudSigma::Errors::NotFound.new("Object with uuid #{key} does not exist", 'notexist')
    end
  end

  Excon::Response.new(:body => Fog::JSON.decode(Fog::JSON.encode(data)), :status => status)
end
mock_list(collection, status) click to toggle source
# File lib/fog/cloudsigma/connection.rb, line 128
def mock_list(collection, status)
  data_array = self.data[collection].values

  Excon::Response.new(:body => {'objects' => data_array}, :status => status)
end
mock_update(data, obj_or_collection, status, key, &clean_before_update) click to toggle source
# File lib/fog/cloudsigma/connection.rb, line 134
def mock_update(data, obj_or_collection, status, key, &clean_before_update)
  data = Fog::JSON.decode(Fog::JSON.encode(data))
  if key
    unless self.data[obj_or_collection][key]
      raise Fog::CloudSigma::Errors::NotFound.new("Object with uuid #{key} does not exist", 'notexist')
    end
    if clean_before_update
      new_data = clean_before_update.call(self.data[obj_or_collection][key], data)
    else
      new_data = self.data[obj_or_collection][key].merge(data)
    end

    self.data[obj_or_collection][key] = new_data
  else
    if clean_before_update
      new_data = clean_before_update.call(self.data[obj_or_collection], data)
    else
      new_data = self.data[obj_or_collection].merge(data)
    end

    self.data[obj_or_collection] = new_data
  end

  Excon::Response.new(:body =>  Fog::JSON.decode(Fog::JSON.encode(new_data)), :status => status)
end
setup_connection(options) click to toggle source
# File lib/fog/cloudsigma/connection.rb, line 111
def setup_connection(options)
  @username = options[:cloudsigma_username]
  @password = options[:cloudsigma_password]
end