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.



151
152
153
154
155
156
157
158
159
# File 'lib/fog/aws/elasticache.rb', line 151

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



135
136
137
138
139
140
141
142
143
144
145
# File 'lib/fog/aws/elasticache.rb', line 135

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 => {},
      }
    end
  end
end

.resetObject



147
148
149
# File 'lib/fog/aws/elasticache.rb', line 147

def self.reset
  @data = nil
end

Instance Method Details

#authorize_cache_security_group_ingress(name, ec2_name, ec2_owner_id) ⇒ Object



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

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'].detect{|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



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

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' => [],
    '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



178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/fog/aws/elasticache.rb', line 178

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



30
31
32
33
# File 'lib/fog/aws/requests/elasticache/create_cache_parameter_group.rb', line 30

def create_cache_parameter_group(name, description = name,
  family = 'memcached1.4')
  Fog::Mock.not_implemented
end

#create_cache_security_group(name, description = name) ⇒ Object



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

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



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/create_cache_subnet_group.rb', line 33

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



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

def data
  self.region_data[@aws_access_key_id]
end

#delete_cache_cluster(cluster_id) ⇒ Object



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

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



25
26
27
# File 'lib/fog/aws/requests/elasticache/delete_cache_parameter_group.rb', line 25

def delete_cache_parameter_group(name)
  Fog::Mock.not_implemented
end

#delete_cache_security_group(name) ⇒ Object



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

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



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

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



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

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



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

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

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



30
31
32
# File 'lib/fog/aws/requests/elasticache/describe_cache_parameters.rb', line 30

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

#describe_cache_security_groups(name = nil, opts = {}) ⇒ 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
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/fog/aws/requests/elasticache/describe_cache_security_groups.rb', line 28

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



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

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



35
36
37
# File 'lib/fog/aws/requests/elasticache/describe_reserved_cache_nodes.rb', line 35

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

#describe_engine_defalut_parameters(options = {}) ⇒ Object



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

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

#describe_eventsObject



43
44
45
# File 'lib/fog/aws/requests/elasticache/describe_events.rb', line 43

def describe_events
  Fog::Mock.not_implemented
end

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

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



39
40
41
# File 'lib/fog/aws/requests/elasticache/modify_cache_parameter_group.rb', line 39

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

#reboot_cache_cluster(id, nodes_to_reboot) ⇒ Object



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

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



161
162
163
# File 'lib/fog/aws/elasticache.rb', line 161

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

#reset_cache_parameter_group(id, parameter_names) ⇒ Object



40
41
42
# File 'lib/fog/aws/requests/elasticache/reset_cache_parameter_group.rb', line 40

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

#reset_dataObject



169
170
171
# File 'lib/fog/aws/elasticache.rb', line 169

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

#revoke_cache_security_group_ingressObject



31
32
33
# File 'lib/fog/aws/requests/elasticache/revoke_cache_security_group_ingress.rb', line 31

def revoke_cache_security_group_ingress
  Fog::Mock.not_implemented
end

#setup_credentials(options) ⇒ Object



173
174
175
# File 'lib/fog/aws/elasticache.rb', line 173

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