Module: Fog::CloudSigma::CloudSigmaConnection::Mock

Included in:
Fog::Compute::CloudSigma::Mock
Defined in:
lib/fog/cloudsigma/connection.rb

Instance Method Summary collapse

Instance Method Details

#mock_create(collection, status, data, key, defaults = {}, &clean_before_store) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/fog/cloudsigma/connection.rb', line 169

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) ⇒ Object



163
164
165
166
167
# File 'lib/fog/cloudsigma/connection.rb', line 163

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) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
# File 'lib/fog/cloudsigma/connection.rb', line 119

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) ⇒ Object



131
132
133
134
135
# File 'lib/fog/cloudsigma/connection.rb', line 131

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) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/fog/cloudsigma/connection.rb', line 137

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) ⇒ Object



113
114
115
116
117
# File 'lib/fog/cloudsigma/connection.rb', line 113

def setup_connection(options)
  @username = options[:cloudsigma_username]
  @password = options[:cloudsigma_password]

end