class Fog::AWS::Elasticache::Mock

Public Class Methods

data() click to toggle source
# 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
new(options={}) click to toggle source
# 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-south-1', 'ap-northeast-1', 'ap-northeast-2', 'ap-northeast-3', 'ap-southeast-1', 'ap-southeast-2', 
          'eu-west-1', 'eu-west-2', 'eu-west-3', 'eu-central-1',
          'us-east-1', 'us-east-2', 'us-west-1', 'us-west-2', 'ca-central-1',
          'sa-east-1', 'cn-north-1', 'cn-northwest-1', 'ap-east-1', 'us-gov-west-1'].include?(@region)
    raise ArgumentError, "Unknown region: #{@region.inspect}"
  end
end
reset() click to toggle source
# File lib/fog/aws/elasticache.rb, line 166
def self.reset
  @data = nil
end

Public Instance Methods

authorize_cache_security_group_ingress(name, ec2_name, ec2_owner_id) click to toggle source
# 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 = {}) click to toggle source
# 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') click to toggle source

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

# File lib/fog/aws/elasticache.rb, line 199
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') click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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
data() click to toggle source
# File lib/fog/aws/elasticache.rb, line 186
def data
  self.region_data[@aws_access_key_id]
end
delete_cache_cluster(cluster_id) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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 = {}) click to toggle source
# 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 = {}) click to toggle source
# 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 = {}) click to toggle source
# 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={}) click to toggle source
# 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 = {}) click to toggle source
# 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={}) click to toggle source
# 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 = {}) click to toggle source
# 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_events() click to toggle source
# File lib/fog/aws/requests/elasticache/describe_events.rb, line 41
def describe_events
  Fog::Mock.not_implemented
end
modify_cache_cluster(id, options = {}) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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_data() click to toggle source
# File lib/fog/aws/elasticache.rb, line 182
def region_data
  self.class.data[@region]
end
reset_cache_parameter_group(id, parameter_names) click to toggle source
# 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_data() click to toggle source
# File lib/fog/aws/elasticache.rb, line 190
def reset_data
  self.region_data.delete(@aws_access_key_id)
end
revoke_cache_security_group_ingress() click to toggle source
# 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) click to toggle source
# File lib/fog/aws/elasticache.rb, line 194
def setup_credentials(options)
  @aws_access_key_id = options[:aws_access_key_id]
end