Class: Fog::AWS::SimpleDB::Mock

Inherits:
Object
  • Object
show all
Defined in:
lib/fog/aws/simpledb.rb,
lib/fog/aws/requests/simpledb/list_domains.rb,
lib/fog/aws/requests/simpledb/create_domain.rb,
lib/fog/aws/requests/simpledb/delete_domain.rb,
lib/fog/aws/requests/simpledb/get_attributes.rb,
lib/fog/aws/requests/simpledb/put_attributes.rb,
lib/fog/aws/requests/simpledb/domain_metadata.rb,
lib/fog/aws/requests/simpledb/delete_attributes.rb,
lib/fog/aws/requests/simpledb/batch_put_attributes.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Mock

Returns a new instance of Mock.



35
36
37
# File 'lib/fog/aws/simpledb.rb', line 35

def initialize(options={})
  @aws_access_key_id = options[:aws_access_key_id]
end

Class Method Details

.dataObject



23
24
25
26
27
28
29
# File 'lib/fog/aws/simpledb.rb', line 23

def self.data
  @data ||= Hash.new do |hash, key|
    hash[key] = {
      :domains => {}
    }
  end
end

.resetObject



31
32
33
# File 'lib/fog/aws/simpledb.rb', line 31

def self.reset
  @data = nil
end

Instance Method Details

#batch_put_attributes(domain_name, items, replace_attributes = Hash.new([])) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/fog/aws/requests/simpledb/batch_put_attributes.rb', line 35

def batch_put_attributes(domain_name, items, replace_attributes = Hash.new([]))
  response = Excon::Response.new
  if self.data[:domains][domain_name]
    for item_name, attributes in items do
      for key, value in attributes do
        self.data[:domains][domain_name][item_name] ||= {}
        if replace_attributes[item_name] && replace_attributes[item_name].include?(key)
          self.data[:domains][domain_name][item_name][key.to_s] = []
        else
          self.data[:domains][domain_name][item_name][key.to_s] ||= []
        end
        self.data[:domains][domain_name][item_name][key.to_s] << value.to_s
      end
    end
    response.status = 200
    response.body = {
      'BoxUsage'  => Fog::AWS::Mock.box_usage,
      'RequestId' => Fog::AWS::Mock.request_id
    }
  else
    response.status = 400
    raise(Excon::Errors.status_error({:expects => 200}, response))
  end
  response
end

#create_domain(domain_name) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/fog/aws/requests/simpledb/create_domain.rb', line 30

def create_domain(domain_name)
  response = Excon::Response.new
  self.data[:domains][domain_name] = {}
  response.status = 200
  response.body = {
    'BoxUsage'  => Fog::AWS::Mock.box_usage,
    'RequestId' => Fog::AWS::Mock.request_id
  }
  response
end

#dataObject



39
40
41
# File 'lib/fog/aws/simpledb.rb', line 39

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

#delete_attributes(domain_name, item_name, attributes = nil) ⇒ Object



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
# File 'lib/fog/aws/requests/simpledb/delete_attributes.rb', line 38

def delete_attributes(domain_name, item_name, attributes = nil)
  response = Excon::Response.new
  if self.data[:domains][domain_name]
    if self.data[:domains][domain_name][item_name]
      if attributes
        for key, value in attributes
          if self.data[:domains][domain_name][item_name][key]
            if value.nil? || value.empty?
              self.data[:domains][domain_name][item_name].delete(key)
            else
              for v in value
                self.data[:domains][domain_name][item_name][key].delete(v)
              end
            end
          end
        end
      else
        self.data[:domains][domain_name][item_name].clear
      end
    end
    response.status = 200
    response.body = {
      'BoxUsage'  => Fog::AWS::Mock.box_usage,
      'RequestId' => Fog::AWS::Mock.request_id
    }
  else
    response.status = 400
    raise(Excon::Errors.status_error({:expects => 200}, response))
  end
  response
end

#delete_domain(domain_name) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/fog/aws/requests/simpledb/delete_domain.rb', line 30

def delete_domain(domain_name)
  response = Excon::Response.new
  if self.data[:domains].delete(domain_name)
    response.status = 200
    response.body = {
      'BoxUsage'  => Fog::AWS::Mock.box_usage,
      'RequestId' => Fog::AWS::Mock.request_id
    }
  end
  response
end

#domain_metadata(domain_name) ⇒ Object



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
# File 'lib/fog/aws/requests/simpledb/domain_metadata.rb', line 39

def (domain_name)
  response = Excon::Response.new
  if domain = self.data[:domains][domain_name]
    response.status = 200
  
    attribute_names = []
    attribute_values = []
    for item in domain.values
      for key, values in item
        attribute_names << key
        for value in values
          attribute_values << value
        end
      end
    end
  
    response.body = {
      'AttributeNameCount'        => attribute_names.length,
      'AttributeNamesSizeBytes'   => attribute_names.join('').length,
      'AttributeValueCount'       => attribute_values.length,
      'AttributeValuesSizeBytes'  => attribute_values.join('').length,
      'BoxUsage'                  => Fog::AWS::Mock.box_usage,
      'ItemCount'                 => domain.keys.length,
      'ItemNamesSizeBytes'        => domain.keys.join('').length,
      'RequestId'                 => Fog::AWS::Mock.request_id,
      'Timestamp'                 => Time.now
    }
  else
    response.status = 400
    raise(Excon::Errors.status_error({:expects => 200}, response))
  end
  response
end

#get_attributes(domain_name, item_name, options = {}) ⇒ Object



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
# File 'lib/fog/aws/requests/simpledb/get_attributes.rb', line 50

def get_attributes(domain_name, item_name, options = {})
  if options.is_a?(Array)
    Fog::Logger.deprecation("get_attributes with array attributes param is deprecated, use 'AttributeName' => attributes) instead [light_black](#{caller.first})[/]")
    options['AttributeName'] ||= options if options.is_a?(Array)
  end
  options['AttributeName'] ||= []
  response = Excon::Response.new
  if self.data[:domains][domain_name]
    object = {}
    if !options['AttributeName'].empty?
      for attribute in options['AttributeName']
        if self.data[:domains][domain_name].has_key?(item_name) && self.data[:domains][domain_name][item_name].has_key?(attribute)
          object[attribute] = self.data[:domains][domain_name][item_name][attribute]
        end
      end
    elsif self.data[:domains][domain_name][item_name]
      object = self.data[:domains][domain_name][item_name]
    end
    response.status = 200
    response.body = {
      'Attributes'  => object,
      'BoxUsage'    => Fog::AWS::Mock.box_usage,
      'RequestId'   => Fog::AWS::Mock.request_id
    }
  else
    response.status = 400
    raise(Excon::Errors.status_error({:expects => 200}, response))
  end
  response
end

#list_domains(options = {}) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/fog/aws/requests/simpledb/list_domains.rb', line 35

def list_domains(options = {})
  response = Excon::Response.new
  keys = self.data[:domains].keys
  max = options['MaxNumberOfDomains'] || keys.size
  offset = options['NextToken'] || 0
  domains = []
  for key, value in self.data[:domains].keys[offset...max]
    domains << key
  end
  response.status = 200
  response.body = {
    'BoxUsage'  => Fog::AWS::Mock.box_usage,
    'Domains'   => domains,
    'RequestId' => Fog::AWS::Mock.request_id
  }
  if max < keys.size
    response.body['NextToken'] = max + 1
  end
  response
end

#put_attributes(domain_name, item_name, attributes, options = {}) ⇒ Object



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
# File 'lib/fog/aws/requests/simpledb/put_attributes.rb', line 42

def put_attributes(domain_name, item_name, attributes, options = {})
  options[:expect] = {} unless options[:expect]
  options[:replace] = [] unless options[:replace]
  response = Excon::Response.new
  if self.data[:domains][domain_name]
    options[:expect].each do |ck, cv|
      if self.data[:domains][domain_name][item_name][ck] != [cv]
        response.status = 409
        raise(Excon::Errors.status_error({:expects => 200}, response))
      end
    end
    attributes.each do |key, value|
      self.data[:domains][domain_name][item_name] ||= {}
      self.data[:domains][domain_name][item_name][key.to_s] = [] unless self.data[:domains][domain_name][item_name][key.to_s]
      if options[:replace].include?(key.to_s)
        self.data[:domains][domain_name][item_name][key.to_s] = [*value].map {|x| x.to_s}
      else
        self.data[:domains][domain_name][item_name][key.to_s] += [*value].map {|x| x.to_s}
      end
    end
    response.status = 200
    response.body = {
      'BoxUsage'  => Fog::AWS::Mock.box_usage,
      'RequestId' => Fog::AWS::Mock.request_id
    }
  else
    response.status = 400
    raise(Excon::Errors.status_error({:expects => 200}, response))

  end
  response
end

#reset_dataObject



43
44
45
# File 'lib/fog/aws/simpledb.rb', line 43

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