From a6a6c3eb4bbcc669599e7a18deaddbd216561443 Mon Sep 17 00:00:00 2001 From: Benton Roberts Date: Wed, 7 Sep 2011 16:22:31 -0400 Subject: [PATCH] [aws|elasticache] add Cache Cluster model and collection --- lib/fog/aws/elasticache.rb | 4 +- lib/fog/aws/models/elasticache/cluster.rb | 73 +++++++++++++++++++ lib/fog/aws/models/elasticache/clusters.rb | 30 ++++++++ .../elasticache/create_cache_cluster.rb | 1 - tests/aws/models/elasticache/cluster_tests.rb | 38 ++++++++++ .../elasticache/cache_cluster_tests.rb | 16 +++- 6 files changed, 157 insertions(+), 5 deletions(-) create mode 100644 lib/fog/aws/models/elasticache/cluster.rb create mode 100644 lib/fog/aws/models/elasticache/clusters.rb create mode 100644 tests/aws/models/elasticache/cluster_tests.rb diff --git a/lib/fog/aws/elasticache.rb b/lib/fog/aws/elasticache.rb index e16ca57c1..0d16fc3db 100644 --- a/lib/fog/aws/elasticache.rb +++ b/lib/fog/aws/elasticache.rb @@ -32,8 +32,8 @@ module Fog #request :describe_events model_path 'fog/aws/models/elasticache' - # model :server - # collection :servers + model :cluster + collection :clusters model :security_group collection :security_groups # model :parameter_group diff --git a/lib/fog/aws/models/elasticache/cluster.rb b/lib/fog/aws/models/elasticache/cluster.rb new file mode 100644 index 000000000..f32b18a80 --- /dev/null +++ b/lib/fog/aws/models/elasticache/cluster.rb @@ -0,0 +1,73 @@ +require 'fog/core/model' + +module Fog + module AWS + class Elasticache + + class Cluster < Fog::Model + # simple attributes + identity :id, :aliases => 'CacheClusterId' + attribute :auto_upgrade, :aliases => 'AutoMinorVersionUpgrade' + attribute :status, :aliases => 'CacheClusterStatus' + attribute :node_type, :aliases => 'CacheNodeType' + attribute :engine, :aliases => 'Engine' + attribute :engine_version, :aliases => 'EngineVersion' + attribute :port, :aliases => 'Port' + attribute :num_nodes, :aliases => 'NumCacheNodes' + attribute :zone, :aliases => 'PreferredAvailabilityZone' + attribute :maintenance_window, :aliases => 'PreferredMaintenanceWindow' + # complex attributes + attribute :nodes, :aliases => 'CacheNodes', :type => :array + attribute :parameter_group, + :aliases => 'CacheParameterGroup', :type => :hash + attribute :pending_values, + :aliases => 'PendingModifiedValues', :type => :hash + attribute :create_time, + :aliases => 'CacheClusterCreateTime', :type => :date_time + attribute :security_groups, + :aliases => 'CacheSecurityGroupNames', :type => :array + attribute :notification_config, + :aliases => 'NotificationConfiguration', :type => :hash + + def ready? + status == 'available' + end + + def destroy + requires :id + connection.delete_cache_cluster(id) + true + end + + def save + requires :id + requires :node_type + requires :security_groups + requires :engine + requires :num_nodes + + parameter_group ||= Hash.new + notification_config ||= Hash.new + security_groups ||= Array.new + + connection.create_cache_cluster( + :cluster_id => id, + :node_type => node_type, + :security_group_names => security_groups, + :num_nodes => num_nodes, + :auto_minor_version_upgrade => auto_upgrade, + :parameter_group_name => parameter_group['CacheParameterGroupName'], + :engine => engine, + :engine_version => engine_version, + :notification_topic_arn => notification_config['TopicArn'], + :port => port, + :preferred_availablility_zone => zone, + :preferred_maintenance_window => maintenance_window + ) + end + + end + + end + end +end diff --git a/lib/fog/aws/models/elasticache/clusters.rb b/lib/fog/aws/models/elasticache/clusters.rb new file mode 100644 index 000000000..7bae30b36 --- /dev/null +++ b/lib/fog/aws/models/elasticache/clusters.rb @@ -0,0 +1,30 @@ +require 'fog/core/collection' +require 'fog/aws/models/elasticache/cluster' + +module Fog + module AWS + class Elasticache + + class Clusters < Fog::Collection + model Fog::AWS::Elasticache::Cluster + + def all + load( + connection.describe_cache_clusters.body['CacheClusters'] + ) + end + + def get(identity) + new( + connection.describe_cache_security_groups( + 'CacheSecurityGroupName' => identity + ).body['CacheSecurityGroups'].first + ) + rescue Fog::AWS::Elasticache::NotFound + nil + end + end + + end + end +end diff --git a/lib/fog/aws/requests/elasticache/create_cache_cluster.rb b/lib/fog/aws/requests/elasticache/create_cache_cluster.rb index e6fbe0572..45eb5fde0 100644 --- a/lib/fog/aws/requests/elasticache/create_cache_cluster.rb +++ b/lib/fog/aws/requests/elasticache/create_cache_cluster.rb @@ -18,7 +18,6 @@ module Fog # * :engine <~String> - The Cluster's caching software (memcached) # * :engine_version <~String> - The Cluster's caching software version # * :notification_topic_arn <~String> - Amazon SNS Resource Name - # * :cluster_id <~String> - The name of the Cache Cluster # * :port <~Integer> - The memcached port number # * :preferred_availablility_zone <~String> # * :preferred_maintenance_window <~String> diff --git a/tests/aws/models/elasticache/cluster_tests.rb b/tests/aws/models/elasticache/cluster_tests.rb new file mode 100644 index 000000000..f3a4c6caf --- /dev/null +++ b/tests/aws/models/elasticache/cluster_tests.rb @@ -0,0 +1,38 @@ +Shindo.tests('AWS::Elasticache | cache clusters', ['aws', 'elasticache']) do + cluster_params = { + :id => 'fog-test-cluster', + :node_type => 'cache.m1.large', + :security_groups => ['default'], + :engine => 'memcached', + :num_nodes => 1 + } + + pending if Fog.mocking? + + model_tests(AWS[:elasticache].clusters, cluster_params, false) do + # Reload to get the cluster info + @instance.reload + puts "Waiting for cluster #{@instance.id} to become available..." + #@instance.wait_for {ready?} # This doesn't work (entity disappears) + while (@instance.status != "available") do + puts "Waiting for cluster #{@instance.id} (#{@instance.status})" + sleep 20 + #@instance.reload # This doesn't work either! (no changes) + @instance = AWS[:elasticache].clusters.find {|c| c.id == @instance.id} + end + end + + collection_tests(AWS[:elasticache].clusters, cluster_params, false) do + # Reload to get the cluster info + @instance.reload + puts "Waiting for cluster #{@instance.id} to become available..." + #@instance.wait_for {ready?} # This doesn't work (entity disappears) + while (@instance.status != "available") do + puts "Waiting for cluster #{@instance.id} (#{@instance.status})" + sleep 20 + #@instance.reload # This doesn't work either! (no changes) + @instance = AWS[:elasticache].clusters.find {|c| c.id == @instance.id} + end + end + +end diff --git a/tests/aws/requests/elasticache/cache_cluster_tests.rb b/tests/aws/requests/elasticache/cache_cluster_tests.rb index 5ac07b911..f0d977451 100644 --- a/tests/aws/requests/elasticache/cache_cluster_tests.rb +++ b/tests/aws/requests/elasticache/cache_cluster_tests.rb @@ -26,6 +26,11 @@ Shindo.tests('AWS::Elasticache | cache cluster requests', ['aws', 'elasticache'] cluster['CacheClusterId'] == cluster_id end end + # The DESCRIBE_CACHE_CLUSTERS format must include only one cluster + # So remove all but the relevant cluster from the response body + test_cluster = body['CacheClusters'].delete_if do |cluster| + cluster['CacheClusterId'] != cluster_id + end body end @@ -42,8 +47,15 @@ Shindo.tests('AWS::Elasticache | cache cluster requests', ['aws', 'elasticache'] body end - #cluster = AWS[:elasticache].clusters.get(cluster_id) - #cluster.wait_for {ready?} + puts "Waiting for cluster #{cluster_id} to become available..." + cluster = AWS[:elasticache].clusters.find {|c| c.id == cluster_id} + #cluster.wait_for {ready?} # This doesn't work (entity disappears) + while (cluster.status != "available") do + puts "Waiting for cluster #{cluster.id} (#{cluster.status})" + sleep 20 + #cluster.reload # This doesn't work either! (no changes) + cluster = AWS[:elasticache].clusters.find {|c| c.id == cluster_id} + end tests( '#delete_cache_security_group'