После загрузки и просмотра исходного кода aws-sdk-ruby, освежая в памяти ruby себя и найдя этот пример, я смог построить следующий как рубиновые классы в папке библиотеки coobkook:
отл.рб:
class Chef
class Resource
class AmazonElb < Chef::Resource::Amazon
def initialize(name, run_context=nil)
super(name, run_context)
@resource_name = :amazon_elb
@provider = Chef::Provider::AmazonElb
@action = :attach
@allowed_actions = [:attach, :detach]
end
end
def instance_id (arg=nil)
set_or_return(
:instance_id,
arg,
:kind_of => [String, NilClass]
)
end
def elb_name (arg=nil)
set_or_return(
:elb_name,
arg,
:kind_of => [String, NilClass]
)
end
end
end
elb_provider.rb:
class Chef
class Provider
class AmazonElb < Chef::Provider::Amazon
def initialize(new_resource, run_context)
#super(new_resource, runcontext)
super
@ec2 = AWS::EC2.new
@elb = AWS::ELB.new
end
def load_current_resource
@current_resource = @new_resource
@current_resource
end
def define_resource_requirements
requirements.assert(:attach, :detach) do |a|
a.assertion {
@new_resource.instance_id != nil &&
@new_resource.elb_name != nil
}
a.failure_message(Chef::Exceptions::ELBException, "The attributes instance_id and elb_name must be specified.")
end
end
def action_attach
new_resource = @new_resource
instance = @ec2.instances[new_resource.instance_id]
@elb.load_balancers["#{new_resource.elb_name}"].instances.register(instance)
end
def action_detach
new_resource = @new_resource
instance = @ec2.instances[new_resource.instance_id]
@elb.load_balancers["#{new_resource.elb_name}"].instances.deregister(instance)
end
end
end
end
Теперь я могу назвать это так:
amazon_elb "something" do
instance_id "i-xxx"
elb_name "name of elb"
aws_access_key xxx
aws_secret_key xxx
end
Примечание: это зависит от другого набора классов, которые также были созданы для предоставления aws-sdk кулинарной книге, получения ключей доступа и зависит от установленного драгоценного камня aws-sdk. Вышеупомянутое не будет работать как есть, но должно дать хорошую отправную точку.
person
Aaron Dodd
schedule
18.11.2014