From 83c6505e0ffe1e22dc713efabafbf8d66aa9ad7a Mon Sep 17 00:00:00 2001 From: Eugene Howe Date: Wed, 21 Sep 2016 14:51:27 -0400 Subject: [PATCH 1/3] add support endpoint and models/requests for trusted advisor checks --- lib/fog/aws.rb | 2 + .../aws/models/support/flagged_resource.rb | 14 ++ .../aws/models/support/flagged_resources.rb | 11 ++ .../models/support/trusted_advisor_check.rb | 66 +++++++ .../models/support/trusted_advisor_checks.rb | 21 +++ .../describe_trusted_advisor_check_result.rb | 31 ++++ .../describe_trusted_advisor_checks.rb | 29 +++ lib/fog/aws/support.rb | 166 ++++++++++++++++++ tests/models/support/trusted_advisor_tests.rb | 25 +++ tests/requests/support/helper.rb | 43 +++++ .../support/trusted_advisor_check_tests.rb | 11 ++ 11 files changed, 419 insertions(+) create mode 100644 lib/fog/aws/models/support/flagged_resource.rb create mode 100644 lib/fog/aws/models/support/flagged_resources.rb create mode 100644 lib/fog/aws/models/support/trusted_advisor_check.rb create mode 100644 lib/fog/aws/models/support/trusted_advisor_checks.rb create mode 100644 lib/fog/aws/requests/support/describe_trusted_advisor_check_result.rb create mode 100644 lib/fog/aws/requests/support/describe_trusted_advisor_checks.rb create mode 100644 lib/fog/aws/support.rb create mode 100644 tests/models/support/trusted_advisor_tests.rb create mode 100644 tests/requests/support/helper.rb create mode 100644 tests/requests/support/trusted_advisor_check_tests.rb diff --git a/lib/fog/aws.rb b/lib/fog/aws.rb index 3a5d00903..5c6d530f0 100644 --- a/lib/fog/aws.rb +++ b/lib/fog/aws.rb @@ -52,6 +52,7 @@ module Fog autoload :SNS, File.expand_path('../aws/sns', __FILE__) autoload :SQS, File.expand_path('../aws/sqs', __FILE__) autoload :STS, File.expand_path('../aws/sts', __FILE__) + autoload :Support, File.expand_path('../aws/support', __FILE__) autoload :SimpleDB, File.expand_path('../aws/simpledb', __FILE__) service(:auto_scaling, 'AutoScaling') @@ -81,6 +82,7 @@ module Fog service(:sqs, 'SQS') service(:storage, 'Storage') service(:sts, 'STS') + service(:support, 'Support') def self.indexed_param(key, values) params = {} diff --git a/lib/fog/aws/models/support/flagged_resource.rb b/lib/fog/aws/models/support/flagged_resource.rb new file mode 100644 index 000000000..d5b77bded --- /dev/null +++ b/lib/fog/aws/models/support/flagged_resource.rb @@ -0,0 +1,14 @@ +module Fog + module AWS + class Support + class FlaggedResource < Fog::Model + identity :resource_id, :aliases => "resourceId" + + attribute :is_suppressed, :aliases => "isSuppressed", :type => :boolean + attribute :metadata + attribute :region + attribute :status + end + end + end +end diff --git a/lib/fog/aws/models/support/flagged_resources.rb b/lib/fog/aws/models/support/flagged_resources.rb new file mode 100644 index 000000000..362ba067f --- /dev/null +++ b/lib/fog/aws/models/support/flagged_resources.rb @@ -0,0 +1,11 @@ +require 'fog/aws/models/support/flagged_resource' + +module Fog + module AWS + class Support + class FlaggedResources < Fog::Collection + model Fog::AWS::Support::FlaggedResource + end + end + end +end diff --git a/lib/fog/aws/models/support/trusted_advisor_check.rb b/lib/fog/aws/models/support/trusted_advisor_check.rb new file mode 100644 index 000000000..94829c5f3 --- /dev/null +++ b/lib/fog/aws/models/support/trusted_advisor_check.rb @@ -0,0 +1,66 @@ +module Fog + module AWS + class Support + class TrustedAdvisorCheck < Fog::Model + identity :id, :aliases => 'checkId' + + attribute :name + attribute :category + attribute :description + attribute :metadata + attribute :flagged_resources, :aliases => 'flaggedResources' + attribute :resources_summary, :aliases => 'resourcesSummary' + attribute :status + attribute :timestamp + attribute :category_specific_summary, :aliases => 'categorySpecificSummary' + + def populate_extended_attributes(lazy=false) + return if lazy == true + data = service.describe_trusted_advisor_check_result(:id => self.identity).body["result"] + merge_attributes(data) + end + + def flagged_resources(lazy=true) + if attributes[:flagged_resources].nil? + populate_extended_attributes(lazy) + + if attributes[:flagged_resources] + map_flagged_resources! + attributes[:flagged_resources] = map_resources(attributes[:flagged_resources]) + service.flagged_resources.load(attributes[:flagged_resources]) + else + nil + end + else + if attributes[:flagged_resources].first['metadata'].is_a?(Array) + map_flagged_resources! + end + service.flagged_resources.load(attributes[:flagged_resources]) + end + end + + def category_specific_summary(lazy=true) + populate_extended_attributes(lazy) if attributes[:category_specific_summary].nil? + attributes[:category_specific_summary] + end + + def resources_summary(lazy=true) + populate_extended_attributes(lazy) if attributes[:resources_summary].nil? + attributes[:resources_summary] + end + + private + + def map_flagged_resources! + attributes[:flagged_resources].map! do |fr| + fr['metadata'] = fr['metadata'].each_with_index.inject({}) do |hash,(data,index)| + hash[self.metadata[index]] = data + hash + end + fr + end + end + end + end + end +end diff --git a/lib/fog/aws/models/support/trusted_advisor_checks.rb b/lib/fog/aws/models/support/trusted_advisor_checks.rb new file mode 100644 index 000000000..7a7caceb4 --- /dev/null +++ b/lib/fog/aws/models/support/trusted_advisor_checks.rb @@ -0,0 +1,21 @@ +require 'fog/aws/models/support/trusted_advisor_check' + +module Fog + module AWS + class Support + class TrustedAdvisorChecks < Fog::Collection + model Fog::AWS::Support::TrustedAdvisorCheck + + def all + data = service.describe_trusted_advisor_checks.body['checks'] + load(data) + end + + def get(id) + data = service.describe_trusted_advisor_check_result(:id => id).body['result'] + new(data).populate_extended_attributes + end + end + end + end +end diff --git a/lib/fog/aws/requests/support/describe_trusted_advisor_check_result.rb b/lib/fog/aws/requests/support/describe_trusted_advisor_check_result.rb new file mode 100644 index 000000000..92f2632cb --- /dev/null +++ b/lib/fog/aws/requests/support/describe_trusted_advisor_check_result.rb @@ -0,0 +1,31 @@ +module Fog + module AWS + class Support + class Real + # Describe Trusted Advisor Check Result + # http://docs.aws.amazon.com/awssupport/latest/APIReference/API_DescribeTrustedAdvisorCheckResult.html + # ==== Parameters + # * checkId <~String> - Id of the check obtained from #describe_trusted_advisor_checks + # * language <~String> - Language to return. Supported values are 'en' and 'jp' + # ==== Returns + # * response<~Excon::Response>: + # * body<~Hash> + def describe_trusted_advisor_check_result(options={}) + request( + 'Action' => 'DescribeTrustedAdvisorCheckResult', + 'checkId' => options[:id], + 'language' => options[:language] || 'en' + ) + end + end + + class Mock + def describe_trusted_advisor_check_result(options={}) + response = Excon::Response.new + response.body = {'result' => self.data[:trusted_advisor_check_results][options[:id]]} + response + end + end + end + end +end diff --git a/lib/fog/aws/requests/support/describe_trusted_advisor_checks.rb b/lib/fog/aws/requests/support/describe_trusted_advisor_checks.rb new file mode 100644 index 000000000..833d2d0ae --- /dev/null +++ b/lib/fog/aws/requests/support/describe_trusted_advisor_checks.rb @@ -0,0 +1,29 @@ +module Fog + module AWS + class Support + class Real + # Describe Trusted Advisor Checks + # http://docs.aws.amazon.com/awssupport/latest/APIReference/API_DescribeTrustedAdvisorChecks.html + # ==== Parameters + # * language <~String> - Language to return. Supported values are 'en' and 'jp' + # ==== Returns + # * response<~Excon::Response>: + # * body<~Hash> + def describe_trusted_advisor_checks(options={}) + request( + 'Action' => 'DescribeTrustedAdvisorChecks', + 'language' => options[:language] || 'en' + ) + end + end + + class Mock + def describe_trusted_advisor_checks(options={}) + response = Excon::Response.new + response.body = {'checks' => self.data[:trusted_advisor_checks].values} + response + end + end + end + end +end diff --git a/lib/fog/aws/support.rb b/lib/fog/aws/support.rb new file mode 100644 index 000000000..db58872b2 --- /dev/null +++ b/lib/fog/aws/support.rb @@ -0,0 +1,166 @@ +module Fog + module AWS + class Support < Fog::Service + extend Fog::AWS::CredentialFetcher::ServiceMethods + + requires :aws_access_key_id, :aws_secret_access_key + recognizes :host, :path, :port, :scheme, :instrumentor, :instrumentor_name, :region, :persistent, :aws_session_token + + model_path 'fog/aws/models/support' + request_path 'fog/aws/requests/support' + + collection :flagged_resources + collection :trusted_advisor_checks + + model :flagged_resource + model :trusted_advisor_check + + request :describe_trusted_advisor_checks + request :describe_trusted_advisor_check_result + + class Mock + def self.data + @data ||= Hash.new do |hash, region| + hash[region] = Hash.new do |region_hash, key| + tac_id = Fog::Mock.random_hex(5) + region_hash[key] = { + :trusted_advisor_checks => { + tac_id => { + "category"=>"cost_optimizing", + "description"=>"Checks the Amazon Elastic Compute Cloud (Amazon EC2) instances that were running at any time during the last 14 days and alerts you if the daily CPU utilization was 10% or less and network I/O was 5 MB or less on 4 or more days. Running instances generate hourly usage charges. Although some scenarios can result in low utilization by design, you can often lower your costs by managing the number and size of your instances.\n

\nEstimated monthly savings are calculated by using the current usage rate for On-Demand Instances and the estimated number of days the instance might be underutilized. Actual savings will vary if you are using Reserved Instances or Spot Instances, or if the instance is not running for a full day. To get daily utilization data, download the report for this check. \n
\n
\nAlert Criteria
\nYellow: An instance had 10% or less daily average CPU utilization and 5 MB or less network I/O on at least 4 of the previous 14 days.
\n
\nRecommended Action
\nConsider stopping or terminating instances that have low utilization, or scale the number of instances by using Auto Scaling. For more information, see Stop and Start Your Instance, Terminate Your Instance, and What is Auto Scaling?
\n
\nAdditional Resources
\nMonitoring Amazon EC2
\nInstance Metadata and User Data
\nAmazon CloudWatch Developer Guide
\nAuto Scaling Developer Guide", + "id"=>tac_id, + "metadata"=>["Region/AZ", "Instance ID", "Instance Name", "Instance Type", "Estimated Monthly Savings", "Day 1", "Day 2", "Day 3", "Day 4", "Day 5", "Day 6", "Day 7", "Day 8", "Day 9", "Day 10", "Day 11", "Day 12", "Day 13", "Day 14", "14-Day Average CPU Utilization", "14-Day Average Network I/O", "Number of Days Low Utilization"], + "name"=>"Low Utilization Amazon EC2 Instances" + } + }, + :trusted_advisor_check_results => { + tac_id => { + 'checkId' => tac_id, + 'status' => "warning", + 'timestamp' => "2016-09-18T13:19:35Z", + 'resourcesSummary' => { + "resourcesFlagged" => 40, + "resourcesIgnored" => 0, + "resourcesProcessed" => 47, + "resourcesSuppressed" => 0 + }, + 'categorySpecificSummary' => { + "costOptimizing" => { + "estimatedMonthlySavings" => 4156.920000000003, + "estimatedPercentMonthlySavings" => 0.9918398900532555 + } + }, + 'flaggedResources' => [{ + "region" => "us-west-2", + "resourceId" => Fog::Mock.random_hex(22), + "status" => "warning", + "isSuppressed" => false, + "metadata" => ["us-west-2a", "i-#{Fog::Mock.random_hex(5)}", "instance_tags", "m3.large", "$95.76", "2.3% 0.23MB", "2.3% 0.20MB", "2.3% 0.21MB", "2.4% 0.28MB", "2.3% 0.20MB", "2.3% 0.20MB", "2.3% 0.20MB", "2.3% 0.20MB", "2.3% 0.20MB", "2.3% 0.20MB", "2.6% 0.54MB", "2.4% 0.31MB", "2.3% 0.21MB", "2.3% 0.20MB", "2.3%", "0.24MB", "14 days"] + }] + } + } + } + end + end + end + + def self.reset + @data = nil + end + + attr_accessor :region + + def initialize(options={}) + @region = 'us-east-1' + end + + def data + self.class.data[@region][@aws_access_key_id] + end + end + + class Real + include Fog::AWS::CredentialFetcher::ConnectionMethods + + def initialize(options={}) + @connection_options = options[:connection_options] || {} + @instrumentor = options[:instrumentor] + @instrumentor_name = options[:instrumentor_name] || 'fog.aws.support' + + @region = 'us-east-1' + @host = options[:host] || "support.#{@region}.amazonaws.com" + @path = options[:path] || "/" + @port = options[:port] || 443 + @scheme = options[:scheme] || "https" + @persistent = options[:persistent] || false + @connection = Fog::XML::Connection.new("#{@scheme}://#{@host}:#{@port}#{@path}", @persistent, @connection_options) + @version = options[:version] || '2013-04-15' + + setup_credentials(options) + end + + def reload + @connection.reset + end + + def setup_credentials(options) + @aws_access_key_id = options[:aws_access_key_id] + @aws_secret_access_key = options[:aws_secret_access_key] + @aws_session_token = options[:aws_session_token] + @aws_credentials_expire_at = options[:aws_credentials_expire_at] + + #global services that have no region are signed with the us-east-1 region + #the only exception is GovCloud, which requires the region to be explicitly specified as us-gov-west-1 + @signer = Fog::AWS::SignatureV4.new(@aws_access_key_id, @aws_secret_access_key, @region, 'support') + end + + def request(params) + refresh_credentials_if_expired + idempotent = params.delete(:idempotent) + parser = params.delete(:parser) + action = params.delete('Action') + request_body = Fog::JSON.encode(params) + + body, headers = Fog::AWS.signed_params_v4( + params, + { + 'Content-Type' => "application/x-amz-json-1.1", + "X-Amz-Target" => "AWSSupport_#{@version.gsub("-", "")}.#{action}" + }, + { + :host => @host, + :path => @path, + :port => @port, + :version => @version, + :signer => @signer, + :aws_session_token => @aws_session_token, + :method => 'POST', + :body => request_body + } + ) + + if @instrumentor + @instrumentor.instrument("#{@instrumentor_name}.request", params) do + _request(body, headers, idempotent, parser) + end + else + _request(body, headers, idempotent, parser) + end + end + + def _request(body, headers, idempotent, parser) + response = @connection.request({ + :body => body, + :expects => 200, + :idempotent => idempotent, + :headers => headers, + :method => 'POST', + :parser => parser + }) + response.body = Fog::JSON.decode(response.body) + response + end + end + end + end +end diff --git a/tests/models/support/trusted_advisor_tests.rb b/tests/models/support/trusted_advisor_tests.rb new file mode 100644 index 000000000..946d57570 --- /dev/null +++ b/tests/models/support/trusted_advisor_tests.rb @@ -0,0 +1,25 @@ +Shindo.tests("AWS::Support | trusted_advisor_checks", ["aws", "support"]) do + tests("collection#all").succeeds do + Fog::AWS[:support].trusted_advisor_checks.all + end + + @identity = Fog::AWS[:support].trusted_advisor_checks.all.first.identity + + tests("collection#get(#{@identity})").returns(@identity) do + Fog::AWS[:support].trusted_advisor_checks.get(@identity).identity + end + + @model = Fog::AWS[:support].trusted_advisor_checks.all.detect { |tac| tac.id == @identity } + + tests("model#flagged_resources").returns(nil) do + @model.flagged_resources + end + + tests("model#flagged_resources").returns(true) do + @model.flagged_resources(false).is_a?(Fog::AWS::Support::FlaggedResources) + end + + tests("model#flagged_resources").returns(true) do + @model.flagged_resources.first.metadata.keys == @model.metadata + end +end diff --git a/tests/requests/support/helper.rb b/tests/requests/support/helper.rb new file mode 100644 index 000000000..0f6dfd85d --- /dev/null +++ b/tests/requests/support/helper.rb @@ -0,0 +1,43 @@ +class AWS + module Support + module Formats + TRUSTED_ADVISOR_CHECK_FORMAT = { + 'id' => String, + 'name' => String, + 'description' => String, + 'metadata' => Array, + 'category' => String, + } + + FLAGGED_RESOURCE = { + 'isSuppressed' => Fog::Boolean, + 'metadata' => Array, + 'region' => String, + 'resourceId' => String, + 'status' => String + } + + TRUSTED_ADVISOR_CHECK_RESULT_FORMAT = { + 'categorySpecificSummary' => Hash, + 'checkId' => String, + 'flaggedResources' => [FLAGGED_RESOURCE], + 'resourcesSummary' => { + 'resourcesFlagged' => Integer, + 'resourcesIgnored' => Integer, + 'resourcesProcessed' => Integer, + 'resourcesSuppressed' => Integer + }, + 'status' => String, + 'timestamp' => String + } + + DESCRIBE_TRUSTED_ADVISOR_CHECKS = { + 'checks' => [TRUSTED_ADVISOR_CHECK_FORMAT] + } + + DESCRIBE_TRUSTED_ADVISOR_CHECK_RESULT = { + 'result' => TRUSTED_ADVISOR_CHECK_RESULT_FORMAT + } + end + end +end diff --git a/tests/requests/support/trusted_advisor_check_tests.rb b/tests/requests/support/trusted_advisor_check_tests.rb new file mode 100644 index 000000000..9eddfc7ab --- /dev/null +++ b/tests/requests/support/trusted_advisor_check_tests.rb @@ -0,0 +1,11 @@ +Shindo.tests("AWS::Support | describe_trusted_advisor_checks", ['aws', 'support']) do + tests("#describe_trusted_advisor_checks").formats(AWS::Support::Formats::DESCRIBE_TRUSTED_ADVISOR_CHECKS) do + Fog::AWS[:support].describe_trusted_advisor_checks.body + end + + @check_id = Fog::AWS[:support].describe_trusted_advisor_checks.body['checks'].first['id'] + + tests("#describe_trusted_advisor_check_result(id: #{@check_id})").formats(AWS::Support::Formats::DESCRIBE_TRUSTED_ADVISOR_CHECK_RESULT) do + Fog::AWS[:support].describe_trusted_advisor_check_result(:id => @check_id).body + end +end From 1f69f616ad7aba1bae932df4d4360c2d6a3321a9 Mon Sep 17 00:00:00 2001 From: Eugene Howe Date: Wed, 21 Sep 2016 15:12:32 -0400 Subject: [PATCH 2/3] fix tests --- lib/fog/aws/models/support/trusted_advisor_check.rb | 3 +-- lib/fog/aws/support.rb | 4 ++++ tests/requests/support/trusted_advisor_check_tests.rb | 5 +++++ 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/fog/aws/models/support/trusted_advisor_check.rb b/lib/fog/aws/models/support/trusted_advisor_check.rb index 94829c5f3..aedf92e65 100644 --- a/lib/fog/aws/models/support/trusted_advisor_check.rb +++ b/lib/fog/aws/models/support/trusted_advisor_check.rb @@ -26,7 +26,6 @@ module Fog if attributes[:flagged_resources] map_flagged_resources! - attributes[:flagged_resources] = map_resources(attributes[:flagged_resources]) service.flagged_resources.load(attributes[:flagged_resources]) else nil @@ -41,7 +40,7 @@ module Fog def category_specific_summary(lazy=true) populate_extended_attributes(lazy) if attributes[:category_specific_summary].nil? - attributes[:category_specific_summary] + attributes[:category_stecific_summary] end def resources_summary(lazy=true) diff --git a/lib/fog/aws/support.rb b/lib/fog/aws/support.rb index db58872b2..089b3a472 100644 --- a/lib/fog/aws/support.rb +++ b/lib/fog/aws/support.rb @@ -68,6 +68,10 @@ module Fog @data = nil end + def reset + self.class.reset + end + attr_accessor :region def initialize(options={}) diff --git a/tests/requests/support/trusted_advisor_check_tests.rb b/tests/requests/support/trusted_advisor_check_tests.rb index 9eddfc7ab..366afd9d8 100644 --- a/tests/requests/support/trusted_advisor_check_tests.rb +++ b/tests/requests/support/trusted_advisor_check_tests.rb @@ -3,6 +3,11 @@ Shindo.tests("AWS::Support | describe_trusted_advisor_checks", ['aws', 'support' Fog::AWS[:support].describe_trusted_advisor_checks.body end + # things get weird in the mocked data depending on the order the model and requests run in + if Fog.mocking? + Fog::AWS[:support].reset + end + @check_id = Fog::AWS[:support].describe_trusted_advisor_checks.body['checks'].first['id'] tests("#describe_trusted_advisor_check_result(id: #{@check_id})").formats(AWS::Support::Formats::DESCRIBE_TRUSTED_ADVISOR_CHECK_RESULT) do From 76187c484be7654378d01b4ff263650448d8dc91 Mon Sep 17 00:00:00 2001 From: Eugene Howe Date: Thu, 22 Sep 2016 09:02:08 -0400 Subject: [PATCH 3/3] fix 1.8 --- tests/models/support/trusted_advisor_tests.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/models/support/trusted_advisor_tests.rb b/tests/models/support/trusted_advisor_tests.rb index 946d57570..85eecd630 100644 --- a/tests/models/support/trusted_advisor_tests.rb +++ b/tests/models/support/trusted_advisor_tests.rb @@ -20,6 +20,6 @@ Shindo.tests("AWS::Support | trusted_advisor_checks", ["aws", "support"]) do end tests("model#flagged_resources").returns(true) do - @model.flagged_resources.first.metadata.keys == @model.metadata + @model.flagged_resources.first.metadata.keys.sort == @model.metadata.sort end end