How can we map identifier (service) against their associated class names? #3148
-
|
We use the service in the An example of how we do this: arn = "arn:aws:ec2:eu-west-1:123456789012:i-1234abcde"
Boar::Aws::Service.new(arn)
=> instance of Aws::EC2::ClientHere is an example of how we implemented this. It worked with version 3.203. module Boar
module AWS
class Service
class << self
def client(resource_arn)
new(resource_arn).client
end
def client_for(service)
unless services[service]
load_services
end
client = services[service]
raise ServiceNotLoaded, "The service for #{service} has not been loaded." unless client
client
end
def services
@@services ||= {}.with_indifferent_access
end
def load_services
Seahorse::Client::Base.descendants.each do |klass|
if klass.respond_to? :identifier
services[klass.identifier] = klass
end
end
end
end
attr_reader :arn
def initialize(resource_arn)
@arn = Aws::ARN.parse(resource_arn)
end
def client
@client ||= begin
arguments = {region: arn.region}
arguments.merge!(credentials: Boar.credentials[arn.account_id]) if Boar.credentials[arn.account_id]
self.class.client_for(arn.service).new(arguments)
end
end
def tags
TagsReader.new(client, arn.to_s).read
end
end
end
endProvided that the gems
With autoloading, Is there another way to accommodate this mapping, or is there anything you can do to make such mapping available? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
|
Rather than use def load_services
Aws.constants.each do |c|
m = Aws.const_get(c)
if m.is_a?(Module) && m.const_defined?(:Client) && m.const_get(:Client).respond_to?(:identifier)
klass = m.const_get(:Client)
services[klass.identifier] = klass
end
end
endThis works with the autoload (though of course, it does trigger the loading). |
Beta Was this translation helpful? Give feedback.
-
|
Thank you, @alextwoods - That works perfectly. |
Beta Was this translation helpful? Give feedback.
-
|
Hello! Reopening this discussion to make it searchable. |
Beta Was this translation helpful? Give feedback.
Rather than use
Seahorse::Client::Base.descendantsyou can use theAwsnamespace - something like:This works with the autoload (though of course, it does trigger the loading).