Class: Fog::AWS::Elasticache::Mock

Inherits:
Object
  • Object
show all
Includes:
CredentialFetcher::ConnectionMethods
Defined in:
lib/fog/aws/elasticache.rb,
lib/fog/aws/requests/elasticache/describe_events.rb,
lib/fog/aws/requests/elasticache/create_cache_cluster.rb,
lib/fog/aws/requests/elasticache/delete_cache_cluster.rb,
lib/fog/aws/requests/elasticache/modify_cache_cluster.rb,
lib/fog/aws/requests/elasticache/reboot_cache_cluster.rb,
lib/fog/aws/requests/elasticache/describe_cache_clusters.rb,
lib/fog/aws/requests/elasticache/create_cache_subnet_group.rb,
lib/fog/aws/requests/elasticache/delete_cache_subnet_group.rb,
lib/fog/aws/requests/elasticache/describe_cache_parameters.rb,
lib/fog/aws/requests/elasticache/create_cache_security_group.rb,
lib/fog/aws/requests/elasticache/delete_cache_security_group.rb,
lib/fog/aws/requests/elasticache/reset_cache_parameter_group.rb,
lib/fog/aws/requests/elasticache/create_cache_parameter_group.rb,
lib/fog/aws/requests/elasticache/delete_cache_parameter_group.rb,
lib/fog/aws/requests/elasticache/describe_cache_subnet_groups.rb,
lib/fog/aws/requests/elasticache/modify_cache_parameter_group.rb,
lib/fog/aws/requests/elasticache/describe_reserved_cache_nodes.rb,
lib/fog/aws/requests/elasticache/describe_cache_security_groups.rb,
lib/fog/aws/requests/elasticache/describe_cache_parameter_groups.rb,
lib/fog/aws/requests/elasticache/describe_engine_default_parameters.rb,
lib/fog/aws/requests/elasticache/revoke_cache_security_group_ingress.rb,
lib/fog/aws/requests/elasticache/authorize_cache_security_group_ingress.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CredentialFetcher::ConnectionMethods

#refresh_credentials_if_expired

Constructor Details

#initialize(options = {}) ⇒ Mock

Returns a new instance of Mock.



170
171
172
173
174
175
176
177
178
# File 'lib/fog/aws/elasticache.rb', line 170

def initialize(options={})
  @aws_credentials_expire_at = Time::now + 20
  setup_credentials(options)
  @region = options[:region] || 'us-east-1'
  unless ['ap-northeast-1', 'ap-southeast-1', 'eu-west-1', 'us-east-1',
          'us-west-1', 'us-west-2', 'sa-east-1'].include?(@region)
    raise ArgumentError, "Unknown region: #{@region.inspect}"
  end
end

Class Method Details

.dataObject



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/fog/aws/elasticache.rb', line 145

def self.data
  @data ||= Hash.new do |hash, region|
    hash[region] = Hash.new do |region_hash, key|
      region_hash[key] = {
        :clusters  => {}, # cache cluster data, indexed by cluster ID
        :security_groups => {}, # security groups
        :subnet_groups => {},
        :parameter_groups => {"default.memcached1.4" => { "CacheParameterGroupFamily"=>"memcached1.4",
                                                          "Description"=>"Default parameter group for memcached1.4",
                                                          "CacheParameterGroupName"=>"default.memcached1.4"
                                                        },
                              "default.redis2.6" =>     {"CacheParameterGroupFamily"=>"redis2.6",
                                                         "Description"=>"Default parameter group for redis2.6",
                                                         "CacheParameterGroupName"=>"default.redis2.6"
                                                        }
                              }
                          }
    end
  end
end

.resetObject



166
167
168
# File 'lib/fog/aws/elasticache.rb', line 166

def self.reset
  @data = nil
end

Instance Method Details

#authorize_cache_security_group_ingress(name, ec2_name, ec2_owner_id) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/fog/aws/requests/elasticache/authorize_cache_security_group_ingress.rb', line 28

def authorize_cache_security_group_ingress(name, ec2_name, ec2_owner_id)
  opts = {
    'EC2SecurityGroupName' => ec2_name,
    'EC2SecurityGroupOwnerId' => ec2_owner_id
  }

  if sec_group = self.data[:security_groups][name]

    if sec_group['EC2SecurityGroups'].find{|h| h['EC2SecurityGroupName'] == opts['EC2SecurityGroupName']}
      raise Fog::AWS::Elasticache::AuthorizationAlreadyExists.new("AuthorizationAlreadyExists => #{opts['EC2SecurityGroupName']} is alreay defined")
    end
    sec_group['EC2SecurityGroups'] << opts.merge({'Status' => 'authorizing'})

    Excon::Response.new(
        {
            :status => 200,
            :body => {
                'ResponseMetadata'=>{ 'RequestId'=> Fog::AWS::Mock.request_id },
                'CacheSecurityGroup' => sec_group
            }
        }
    )
  else
    raise Fog::AWS::Elasticache::NotFound.new("CacheSecurityGroupNotFound => #{name} not found")
  end
end

#create_cache_cluster(id, options = {}) ⇒ Object



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
91
# File 'lib/fog/aws/requests/elasticache/create_cache_cluster.rb', line 64

def create_cache_cluster(id, options = {})
  response        = Excon::Response.new
  cluster         = { # create an in-memory representation of this cluster
    'CacheClusterId'  => id.strip,
    'NumCacheNodes'   => options[:num_nodes]      || 1,
    'CacheNodeType'   => options[:node_type]      || 'cache.m1.large',
    'Engine'          => options[:engine]         || 'memcached',
    'EngineVersion'   => options[:engine_version] || '1.4.5',
    'CacheClusterStatus'  => 'available',
    'CacheNodes'          => create_cache_nodes(id.strip, options[:num_nodes]),
    'CacheSecurityGroups' => [],
    'SecurityGroups'  => [],
    'CacheParameterGroup' => { 'CacheParameterGroupName' =>
        options[:parameter_group_name] || 'default.memcached1.4' },
    'CacheSubnetGroupName' => options[:cache_subnet_group_name],
    'PendingModifiedValues'       => {},
    'AutoMinorVersionUpgrade'     =>
      options[:auto_minor_version_upgrade]    || 'true',
    'PreferredMaintenanceWindow'  =>
      options[:preferred_maintenance_window]  || 'sun:05:00-sun:09:00',
  }
  self.data[:clusters][id] = cluster  # store the in-memory cluster
  response.body = {
    'CacheCluster' => cluster.merge({'CacheClusterStatus' => 'creating'}),
    'ResponseMetadata' => { 'RequestId' => Fog::AWS::Mock.request_id }
  }
  response
end

#create_cache_nodes(cluster_id, num_nodes = 1, port = '11211') ⇒ Object

returns an Array of (Mock) elasticache nodes, representated as Hashes



197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/fog/aws/elasticache.rb', line 197

def create_cache_nodes(cluster_id, num_nodes = 1, port = '11211')
  (1..num_nodes).map do |node_number|
    node_id = "%04d" % node_number
    { # each hash represents a cache cluster node
      "CacheNodeId"           => node_id,
      "Port"                  => port,
      "ParameterGroupStatus"  => "in-sync",
      "CacheNodeStatus"       => "available",
      "CacheNodeCreateTime"   => Time.now.utc.to_s,
      "Address" =>
        "#{cluster_id}.#{node_id}.use1.cache.amazonaws.com"
    }
  end
end

#create_cache_parameter_group(name, description = name, family = 'memcached1.4') ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/fog/aws/requests/elasticache/create_cache_parameter_group.rb', line 29

def create_cache_parameter_group(name, description = name, family = 'memcached1.4')
  response = Excon::Response.new
  if self.data[:parameter_groups] and self.data[:parameter_groups][name]
    raise Fog::AWS::Elasticache::IdentifierTaken.new("Parameter group #{name} already exists")
  end

  data = {
    'CacheParameterGroupName' => name,
    'CacheParameterGroupFamily' => family.downcase,
    'Description' => description
  }
  self.data[:parameter_groups][name] = data

  response.body = {
    "ResponseMetadata"=>{ "RequestId"=> Fog::AWS::Mock.request_id },
    "CreateCacheParameterGroupResult"=> {"CacheParameterGroup"=> data}
  }
  response.status = 200
  response
end

#create_cache_security_group(name, description = name) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/fog/aws/requests/elasticache/create_cache_security_group.rb', line 26

def create_cache_security_group(name, description = name)
  if self.data[:security_groups][name]
    raise Fog::AWS::Elasticache::IdentifierTaken.new("CacheClusterAlreadyExists => The security group '#{name}' already exists")
  end

  data = {
    'CacheSecurityGroupName' => name,
    'Description' => description,
    'EC2SecurityGroups' => [],
    'OwnerId' => '0123456789'
  }
  self.data[:security_groups][name] = data

  Excon::Response.new(
      {
          :body => {
              'ResponseMetadata'=>{ 'RequestId'=> Fog::AWS::Mock.request_id },
              'CacheSecurityGroup' => data
          }
      }
  )
end

#create_cache_subnet_group(name, subnet_ids, description = name) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/fog/aws/requests/elasticache/create_cache_subnet_group.rb', line 30

def create_cache_subnet_group(name, subnet_ids, description = name)
  response = Excon::Response.new
  if self.data[:subnet_groups] && self.data[:subnet_groups][name]
    raise Fog::AWS::Elasticache::IdentifierTaken.new("CacheSubnetGroupAlreadyExists => The subnet group '#{name}' already exists")
  end

  collection = Fog::Compute[:aws]
  collection.region = @region
  subnets = collection.subnets

  subnets = subnet_ids.map { |snid| subnets.get(snid) }
  vpc_id = subnets.first.vpc_id

  data = {
    'CacheSubnetGroupName' => name,
    'CacheSubnetGroupDescription' => description,
    'SubnetGroupStatus' => 'Complete',
    'Subnets' => subnet_ids,
    'VpcId' => vpc_id
  }
  self.data[:subnet_groups][name] = data
  response.body = {
    "ResponseMetadata"=>{ "RequestId"=> Fog::AWS::Mock.request_id },
    'CreateCacheSubnetGroupResult' => { 'CacheSubnetGroup' => data }
  }
  response
end

#dataObject



184
185
186
# File 'lib/fog/aws/elasticache.rb', line 184

def data
  self.region_data[@aws_access_key_id]
end

#delete_cache_cluster(cluster_id) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/fog/aws/requests/elasticache/delete_cache_cluster.rb', line 24

def delete_cache_cluster(cluster_id)
  response        = Excon::Response.new
  cluster         = self.data[:clusters][cluster_id]
  cluster['CacheClusterStatus'] = 'deleting'
  response.body = {
    'CacheClusters'     => self.data[:clusters].values,
    'ResponseMetadata'  => { 'RequestId' => Fog::AWS::Mock.request_id }
  }
  self.data[:clusters].delete(cluster_id)
  response
end

#delete_cache_parameter_group(name) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/fog/aws/requests/elasticache/delete_cache_parameter_group.rb', line 24

def delete_cache_parameter_group(name)
  response = Excon::Response.new

  if self.data[:parameter_groups].delete(name)
    response.status = 200
    response.body = {
      "ResponseMetadata"=>{ "RequestId"=> Fog::AWS::Mock.request_id },
    }
    response
  else
    raise Fog::AWS::Elasticache::NotFound.new("CacheParameterGroup not found: #{name}")
  end
end

#delete_cache_security_group(name) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/fog/aws/requests/elasticache/delete_cache_security_group.rb', line 24

def delete_cache_security_group(name)
  if self.data[:security_groups].delete(name)
    Excon::Response.new(
        {
            :status => 200,
            :body =>   { 'ResponseMetadata'=>{ 'RequestId'=> Fog::AWS::Mock.request_id } }
        }
    )
  else
    raise Fog::AWS::RDS::NotFound.new("DBSecurityGroupNotFound => #{name} not found")
  end
end

#delete_cache_subnet_group(name) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/fog/aws/requests/elasticache/delete_cache_subnet_group.rb', line 24

def delete_cache_subnet_group(name)
  if self.data[:subnet_groups].delete(name)
    Excon::Response.new(
        {
            :status => 200,
            :body =>   { 'ResponseMetadata'=>{ 'RequestId'=> Fog::AWS::Mock.request_id } }
        }
    )
  else
    raise Fog::AWS::Elasticache::NotFound.new("CacheSubnetGroupNotFound => #{name} not found")
  end
end

#describe_cache_clusters(id = nil, options = {}) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/fog/aws/requests/elasticache/describe_cache_clusters.rb', line 31

def describe_cache_clusters(id = nil, options = {})
  response        = Excon::Response.new
  all_clusters    = self.data[:clusters].values.map do |cluster|
    cluster.merge!(options[:show_node_info] ? {
      'CacheClusterCreateTime'    => DateTime.now - 60,
      'PreferredAvailabilityZone' => 'us-east-1a'
    } : {})
  end
  if (id != nil) && (all_clusters.empty?)
    raise Fog::AWS::Elasticache::NotFound
  end
  response.body = {
    'CacheClusters'     => all_clusters,
    'ResponseMetadata'  => { 'RequestId' => Fog::AWS::Mock.request_id }
  }
  response
end

#describe_cache_parameter_groups(name = nil, options = {}) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/fog/aws/requests/elasticache/describe_cache_parameter_groups.rb', line 26

def describe_cache_parameter_groups(name = nil, options = {})
  response = Excon::Response.new
  parameter_set = []
  if name
    if server = self.data[:parameter_groups][name]
      parameter_set << server
    else
      raise Fog::AWS::Elasticache::NotFound.new("CacheParameterGroup #{name} not found")
    end
  else
    parameter_set = self.data[:parameter_groups].values
  end

  response.status = 200

  response.body = { "CacheParameterGroups" => parameter_set }
  response
end

#describe_cache_parameters(name = nil, options = {}) ⇒ Object



28
29
30
# File 'lib/fog/aws/requests/elasticache/describe_cache_parameters.rb', line 28

def describe_cache_parameters(name = nil, options = {})
  Fog::Mock.not_implemented
end

#describe_cache_security_groups(name = nil, opts = {}) ⇒ Object



26
27
28
29
30
31
32
33
34
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
60
# File 'lib/fog/aws/requests/elasticache/describe_cache_security_groups.rb', line 26

def describe_cache_security_groups(name = nil, opts={})
  if name
    sec_group_set = [self.data[:security_groups][name]].compact
    raise Fog::AWS::Elasticache::NotFound.new("Security Group #{name} not found") if sec_group_set.empty?
  else
    sec_group_set = self.data[:security_groups].values
  end

  # TODO: refactor to not delete items that we're iterating over. Causes
  # model tests to fail (currently pending)
  sec_group_set.each do |sec_group|
    # TODO: refactor to not delete items that we're iterating over. Causes
    # model tests to fail (currently pending)
    sec_group["EC2SecurityGroups"].each do |ec2_secg|
      if ec2_secg["Status"] == "authorizing" || ec2_secg["Status"] == "revoking"
        ec2_secg[:tmp] ||= Time.now + Fog::Mock.delay * 2
        if ec2_secg[:tmp] <= Time.now
          ec2_secg["Status"] = "authorized" if ec2_secg["Status"] == "authorizing"
          ec2_secg.delete(:tmp)
          sec_group["EC2SecurityGroups"].delete(ec2_secg) if ec2_secg["Status"] == "revoking"
        end
      end
    end
  end

  Excon::Response.new(
      {
          :status => 200,
          :body => {
              "ResponseMetadata"=>{ "RequestId"=> Fog::AWS::Mock.request_id },
              "CacheSecurityGroups" => sec_group_set
          }
      }
  )
end

#describe_cache_subnet_groups(name = nil, opts = {}) ⇒ Object



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

def describe_cache_subnet_groups(name = nil, opts = {})
  response = Excon::Response.new

  subnet_group_set = []
  if name
    if subnet_group = self.data[:subnet_groups][name]
      subnet_group_set << subnet_group
    else
      raise Fog::AWS::Elasticache::NotFound.new("Subnet Group #{name} not found")
    end
  else
    subnet_group_set = self.data[:subnet_groups].values
  end

  response.status = 200
  response.body = {
    "ResponseMetadata"=>{ "RequestId"=> Fog::AWS::Mock.request_id },
    "DescribeCacheSubnetGroupsResult" => { "CacheSubnetGroups" => subnet_group_set }
  }
  response
end

#describe_db_reserved_instances(identifier = nil, opts = {}) ⇒ Object



32
33
34
# File 'lib/fog/aws/requests/elasticache/describe_reserved_cache_nodes.rb', line 32

def describe_db_reserved_instances(identifier=nil, opts={})
  Fog::Mock.not_implemented
end

#describe_engine_defalut_parameters(options = {}) ⇒ Object



27
28
29
# File 'lib/fog/aws/requests/elasticache/describe_engine_default_parameters.rb', line 27

def describe_engine_defalut_parameters(options = {})
  Fog::Mock.not_implemented
end

#describe_eventsObject



41
42
43
# File 'lib/fog/aws/requests/elasticache/describe_events.rb', line 41

def describe_events
  Fog::Mock.not_implemented
end

#modify_cache_cluster(id, options = {}) ⇒ Object



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
91
92
93
94
95
# File 'lib/fog/aws/requests/elasticache/modify_cache_cluster.rb', line 62

def modify_cache_cluster(id, options = {})
  response        = Excon::Response.new
  cluster         = self.data[:clusters][id]
  pending_values  = Hash.new
  # For any given option, update the cluster's corresponding value
  { :auto_minor_version_upgrade   => 'AutoMinorVersionUpgrade',
    :preferred_maintenance_window => 'PreferredMaintenanceWindow',
    :engine_version               => 'EngineVersion',
    :num_nodes                    => 'NumCacheNodes',
  }.each do |option, cluster_key|
    if options[option] != nil
      cluster[cluster_key] = options[option].to_s
      pending_values[cluster_key] = options[option]
    end
  end
  cache['CacheParameterGroup'] = {
    'CacheParameterGroupName' => options[:parameter_group_name]
  } if options[:parameter_group_name]
  if options[:num_nodes] || options[:engine_version]
    cluster['CacheNodes'] =
      create_cache_nodes(cluster['CacheClusterId'], options[:num_nodes])
    cluster['NumCacheNodes'] = cluster['CacheNodes'].size
  end
  if options[:nodes_to_remove]
    pending_values['CacheNodeId'] = options[:nodes_to_remove].join(',')
  end
  response.body = {
    'CacheCluster' => cluster.merge({
      'PendingModifiedValues' => pending_values
    }),
    'ResponseMetadata' => { 'RequestId' => Fog::AWS::Mock.request_id }
  }
  response
end

#modify_cache_parameter_group(id, new_parameters) ⇒ Object



37
38
39
# File 'lib/fog/aws/requests/elasticache/modify_cache_parameter_group.rb', line 37

def modify_cache_parameter_group(id, new_parameters)
  Fog::Mock.not_implemented
end

#reboot_cache_cluster(id, nodes_to_reboot) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/fog/aws/requests/elasticache/reboot_cache_cluster.rb', line 36

def reboot_cache_cluster(id, nodes_to_reboot)
  response        = Excon::Response.new
  response.body   = {
    'CacheCluster' => self.data[:clusters][id].merge({
      'CacheClusterStatus' => 'rebooting cache cluster nodes'
    }),
    'ResponseMetadata'  => { 'RequestId' => Fog::AWS::Mock.request_id }
  }
  response
end

#region_dataObject



180
181
182
# File 'lib/fog/aws/elasticache.rb', line 180

def region_data
  self.class.data[@region]
end

#reset_cache_parameter_group(id, parameter_names) ⇒ Object



38
39
40
# File 'lib/fog/aws/requests/elasticache/reset_cache_parameter_group.rb', line 38

def reset_cache_parameter_group(id, parameter_names)
   Fog::Mock.not_implemented
end

#reset_dataObject



188
189
190
# File 'lib/fog/aws/elasticache.rb', line 188

def reset_data
  self.region_data.delete(@aws_access_key_id)
end

#revoke_cache_security_group_ingressObject



29
30
31
# File 'lib/fog/aws/requests/elasticache/revoke_cache_security_group_ingress.rb', line 29

def revoke_cache_security_group_ingress
  Fog::Mock.not_implemented
end

#setup_credentials(options) ⇒ Object



192
193
194
# File 'lib/fog/aws/elasticache.rb', line 192

def setup_credentials(options)
  @aws_access_key_id = options[:aws_access_key_id]
end