1
0
Fork 0
mirror of https://github.com/fog/fog-aws.git synced 2022-11-09 13:50:52 -05:00

Merge pull request #300 from engineyard/trusted_advisor

add support endpoint and models/requests for trusted advisor checks
This commit is contained in:
Josh Lane 2016-09-22 14:11:25 -04:00 committed by GitHub
commit 864b95ca8d
11 changed files with 427 additions and 0 deletions

View file

@ -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 = {}

View file

@ -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

View file

@ -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

View file

@ -0,0 +1,65 @@
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!
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_stecific_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

View file

@ -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

View file

@ -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

View file

@ -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

170
lib/fog/aws/support.rb Normal file
View file

@ -0,0 +1,170 @@
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<br><br>\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<br>\n<br>\n<b>Alert Criteria</b><br>\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.<br>\n<br>\n<b>Recommended Action</b><br>\nConsider stopping or terminating instances that have low utilization, or scale the number of instances by using Auto Scaling. For more information, see <a href=\"http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/Stop_Start.html\" target=\"_blank\">Stop and Start Your Instance</a>, <a href=\"http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/terminating-instances.html\" target=\"_blank\">Terminate Your Instance</a>, and <a href=\"http://docs.aws.amazon.com/AutoScaling/latest/DeveloperGuide/WhatIsAutoScaling.html\" target=\"_blank\">What is Auto Scaling?</a><br>\n<br>\n<b>Additional Resources</b><br>\n<a href=\"http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-monitoring.html\" target=\"_blank\">Monitoring Amazon EC2</a><br>\n<a href=\"http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AESDG-chapter-instancedata.html\" target=\"_blank\">Instance Metadata and User Data</a><br>\n<a href=\"http://docs.aws.amazon.com/AmazonCloudWatch/latest/DeveloperGuide/Welcome.html\" target=\"_blank\">Amazon CloudWatch Developer Guide</a><br>\n<a href=\"http://docs.aws.amazon.com/AutoScaling/latest/DeveloperGuide/WhatIsAutoScaling.html\" target=\"_blank\">Auto Scaling Developer Guide</a>",
"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
def reset
self.class.reset
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

View file

@ -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.sort == @model.metadata.sort
end
end

View file

@ -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

View file

@ -0,0 +1,16 @@
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
# 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
Fog::AWS[:support].describe_trusted_advisor_check_result(:id => @check_id).body
end
end