require 'yaml' require "aws-sdk" require "aws_client/version" require "wrapper" require "s32_wrapper" require "s3_wrapper" require "cf_wrapper" require "rds_wrapper" require "ec2_wrapper" require "elb_wrapper" require "elb" require "elasticache_wrapper" require "elasticsearch_wrapper" module AwsClient CREDENTIALS_FILE = ".fog" PATH_TO_CREDENTIALS = File.join(Dir.home, CREDENTIALS_FILE) class Container def initialize(credentials_key, aws_region, provisioning_region) @aws_account_credentials_key = credentials_key @aws_region = aws_region @provisioning_region = provisioning_region validate end def rds @rds ||= ::AwsClient::RdsWrapper.new(client: client_connect(::Aws::RDS::Client)) end def cloudformation @cloudformation ||= ::AwsClient::CfWrapper.new(client: client_connect(::Aws::CloudFormation::Client)) end def s32 @s32 ||= ::AwsClient::S32Wrapper.new(client: client_connect(::Aws::S3::Client), credentials_key: @aws_account_credentials_key, region: @aws_region) end def s3 @s3 ||= ::AwsClient::S3Wrapper.new(client: client_connect(::Aws::S3::Client), credentials_key: @aws_account_credentials_key, region: @aws_region) end def ec2 @ec2 ||= ::AwsClient::Ec2Wrapper.new(client: client_connect(::Aws::EC2::Client)) end def elb @elb ||= ::AwsClient::ElbWrapper.new(client: client_connect(::Aws::ElasticLoadBalancing::Client)) end def elasticache @elasticache ||= ::AwsClient::ElastiCacheWrapper.new(client: client_connect(::Aws::ElastiCache::Client)) end def elasticsearch @elasticsearch ||= ::AwsClient::ElasticSearchWrapper.new(client: client_connect(::Aws::ElasticsearchService::Client)) end protected def credentials all_credentials = YAML.load_file(::AwsClient::PATH_TO_CREDENTIALS) raise "Cannot find credentials for '#{@aws_account_credentials_key}'" unless all_credentials.has_key?(@aws_account_credentials_key) creds_for_key = all_credentials[@aws_account_credentials_key] return Aws::Credentials.new(creds_for_key["aws_access_key_id"], creds_for_key["aws_secret_access_key"]) end def validate raise "Missing Aws Account Credentials Key" unless @aws_account_credentials_key raise "Missing credentials file: cannot find #{::AwsClient::CREDENTIALS_FILE} in your home directory" unless File.exists?(::AwsClient::PATH_TO_CREDENTIALS) end private def client_connect(client_klass) return client_klass.new(credentials: credentials, region: @provisioning_region) end end end