Skip to content

Commit

Permalink
Merge pull request #47 from k1LoW/add-type-elasticache
Browse files Browse the repository at this point in the history
Add elasticache type
  • Loading branch information
k1LoW committed Sep 17, 2015
2 parents 3cb2f26 + ff20183 commit 9a7c375
Show file tree
Hide file tree
Showing 11 changed files with 210 additions and 1 deletion.
34 changes: 34 additions & 0 deletions doc/resource_types.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
| [iam_group](#iam_group)
| [iam_role](#iam_role)
| [iam_policy](#iam_policy)
| [elasticache](#elasticache)

## <a name="ec2">ec2</a>

Expand Down Expand Up @@ -347,3 +348,36 @@ IamPolicy resource type.
### be_attached_to_user

#### its(:policy_name), its(:policy_id), its(:arn), its(:path), its(:default_version_id), its(:attachment_count), its(:is_attachable), its(:description), its(:create_date), its(:update_date)
## <a name="elasticache">elasticache</a>

Elasticache resource type.

### exist

### be_available

### be_creating

### be_deleted

### be_deleting

### be_incompatible_network

### be_modifying

### be_rebooting_cache_cluster_nodes

### be_restore_failed

### be_snapshotting

### have_cache_parameter_group

### belong_to_cache_subnet_group

### belong_to_replication_group

### belong_to_vpc

#### its(:cache_cluster_id), its(:configuration_endpoint), its(:client_download_landing_page), its(:cache_node_type), its(:engine), its(:engine_version), its(:cache_cluster_status), its(:num_cache_nodes), its(:preferred_availability_zone), its(:cache_cluster_create_time), its(:preferred_maintenance_window), its(:notification_configuration), its(:cache_subnet_group_name), its(:auto_minor_version_upgrade), its(:replication_group_id), its(:snapshot_retention_limit), its(:snapshot_window)
17 changes: 17 additions & 0 deletions lib/awspec/generator/doc/type/elasticache.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module Awspec::Generator
module Doc
module Type
class Elasticache < Base
def initialize
super
@type_name = 'Elasticache'
@type = Awspec::Type::Elasticache.new('my-rep-group-001')
@ret = @type.resource
@matchers = %w(belong_to_vpc belong_to_replication_group belong_to_cache_subnet_group)
@ignore_matchers = []
@describes = []
end
end
end
end
end
3 changes: 3 additions & 0 deletions lib/awspec/helper/finder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
require 'awspec/helper/finder/elb'
require 'awspec/helper/finder/lambda'
require 'awspec/helper/finder/iam'
require 'awspec/helper/finder/elasticache'

module Awspec::Helper
module Finder
Expand All @@ -25,6 +26,7 @@ module Finder
include Awspec::Helper::Finder::Elb
include Awspec::Helper::Finder::Lambda
include Awspec::Helper::Finder::Iam
include Awspec::Helper::Finder::Elasticache

# rubocop:disable all
def initialize(id = nil)
Expand All @@ -36,6 +38,7 @@ def initialize(id = nil)
@elb_client = Aws::ElasticLoadBalancing::Client.new
@lambda_client = Aws::Lambda::Client.new
@iam_client = Aws::IAM::Client.new
@elasticache_client = Aws::ElastiCache::Client.new
end
end
end
21 changes: 21 additions & 0 deletions lib/awspec/helper/finder/elasticache.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module Awspec::Helper
module Finder
module Elasticache
def find_cache_cluster(id)
res = @elasticache_client.describe_cache_clusters({
cache_cluster_id: id
})
res[:cache_clusters].first if res[:cache_clusters].count == 1
rescue
nil
end

def find_cache_subnet_group(group_name)
res = @elasticache_client.describe_cache_subnet_groups({
cache_subnet_group_name: group_name
})
res[:cache_subnet_groups].first if res[:cache_subnet_groups].count == 1
end
end
end
end
2 changes: 1 addition & 1 deletion lib/awspec/helper/type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Type
base ec2 rds rds_db_parameter_group security_group
vpc s3 route53_hosted_zone auto_scaling_group subnet
route_table ebs elb lambda iam_user iam_group iam_role
iam_policy
iam_policy elasticache
)

TYPES.each do |type|
Expand Down
4 changes: 4 additions & 0 deletions lib/awspec/matcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@

# IAM User/Group/Role
require 'awspec/matcher/be_allowed_action'

# ElastiCache
require 'awspec/matcher/belong_to_replication_group'
require 'awspec/matcher/belong_to_cache_subnet_group'
5 changes: 5 additions & 0 deletions lib/awspec/matcher/belong_to_cache_subnet_group.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
RSpec::Matchers.define :belong_to_cache_subnet_group do |cache_subnet_group_name|
match do |resource|
return true if resource.resource[:cache_subnet_group_name] == cache_subnet_group_name
end
end
6 changes: 6 additions & 0 deletions lib/awspec/matcher/belong_to_replication_group.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
RSpec::Matchers.define :belong_to_replication_group do |group_id|
match do |resource|
# ElastiCache
resource.resource[:replication_group_id] == group_id
end
end
77 changes: 77 additions & 0 deletions lib/awspec/stub/elasticache.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
Aws.config[:elasticache] = {
stub_responses: {
describe_cache_clusters: {
cache_clusters:
[
{
cache_cluster_id: 'my-rep-group-001',
configuration_endpoint: nil,
client_download_landing_page:
'https://console.aws.amazon.com/elasticache/home#client-download:',
cache_node_type: 'cache.m3.medium',
engine: 'redis',
engine_version: '2.8.21',
cache_cluster_status: 'available',
num_cache_nodes: 1,
preferred_availability_zone: 'ap-northeast-1b',
cache_cluster_create_time: Time.new(2015, 1, 2, 10, 00, 00, '+00:00'),
preferred_maintenance_window: 'wed:15:30-wed:16:30',
pending_modified_values:
{
num_cache_nodes: nil,
cache_node_ids_to_remove: [],
engine_version: nil
},
notification_configuration: nil,
cache_security_groups: [],
cache_parameter_group:
{
cache_parameter_group_name: 'my-cache-parameter-group',
parameter_apply_status: 'in-sync',
cache_node_ids_to_reboot: []
},
cache_subnet_group_name: 'my-cache-subnet-group',
cache_nodes: [],
auto_minor_version_upgrade: true,
security_groups: [
{
security_group_id: 'sg-da1bc2ef',
status: 'active'
}
],
replication_group_id: 'my-rep-group',
snapshot_retention_limit: 0,
snapshot_window: '17:30-18:30'
}
]
},
describe_cache_subnet_groups: {
cache_subnet_groups: [
{
cache_subnet_group_name: 'my-cache-subnet-group',
cache_subnet_group_description: 'stub',
vpc_id: 'vpc-ab123cde',
subnets: []
}
]
}
}
}

Aws.config[:ec2] = {
stub_responses: {
describe_vpcs: {
vpcs: [
{
vpc_id: 'vpc-ab123cde',
tags: [
{
key: 'Name',
value: 'my-vpc'
}
]
}
]
}
}
}
31 changes: 31 additions & 0 deletions lib/awspec/type/elasticache.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module Awspec::Type
class Elasticache < Base
def initialize(id)
super
@resource = find_cache_cluster(id)
@id = @resource[:cache_cluster_id] if @resource
end

states = %w(
available creating deleted deleting
incompatible-network modifying
rebooting-cache-cluster-nodes restore-failed
snapshotting
)

states.each do |state|
define_method state.tr('-', '_') + '?' do
@resource[:cache_cluster_status] == state
end
end

def has_cache_parameter_group?(group_name)
@resource[:cache_parameter_group][:cache_parameter_group_name] == group_name
end

def vpc_id
cache_subnet_group = find_cache_subnet_group(@resource[:cache_subnet_group_name])
cache_subnet_group[:vpc_id] if cache_subnet_group
end
end
end
11 changes: 11 additions & 0 deletions spec/type/elasticache_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require 'spec_helper'
Awspec::Stub.load 'elasticache'

describe elasticache('my-rep-group-001') do
it { should exist }
it { should be_available }
it { should have_cache_parameter_group('my-cache-parameter-group') }
it { should belong_to_replication_group('my-rep-group') }
it { should belong_to_cache_subnet_group('my-cache-subnet-group') }
it { should belong_to_vpc('my-vpc') }
end

0 comments on commit 9a7c375

Please sign in to comment.