Merge pull request #2896 from mitchellh/f-provider-caps
Provider Capabilities
This commit is contained in:
commit
6cd79d67da
|
@ -114,6 +114,7 @@ module Vagrant
|
||||||
# Initializes the provider last so that it has access to all the
|
# Initializes the provider last so that it has access to all the
|
||||||
# state we setup on this machine.
|
# state we setup on this machine.
|
||||||
@provider = provider_cls.new(self)
|
@provider = provider_cls.new(self)
|
||||||
|
@provider._initialize(@provider_name, self)
|
||||||
end
|
end
|
||||||
|
|
||||||
# This calls an action on the provider. The provider may or may not
|
# This calls an action on the provider. The provider may or may not
|
||||||
|
|
|
@ -49,6 +49,11 @@ module Vagrant
|
||||||
# @return [Hash<Symbol, Registry>]
|
# @return [Hash<Symbol, Registry>]
|
||||||
attr_reader :providers
|
attr_reader :providers
|
||||||
|
|
||||||
|
# This contains all the registered provider capabilities.
|
||||||
|
#
|
||||||
|
# @return [Hash<Symbol, Registry>]
|
||||||
|
attr_reader :provider_capabilities
|
||||||
|
|
||||||
# This contains all the synced folder implementations by name.
|
# This contains all the synced folder implementations by name.
|
||||||
#
|
#
|
||||||
# @return [Registry<Symbol, Array<Class, Integer>>]
|
# @return [Registry<Symbol, Array<Class, Integer>>]
|
||||||
|
@ -65,6 +70,7 @@ module Vagrant
|
||||||
@hosts = Registry.new
|
@hosts = Registry.new
|
||||||
@host_capabilities = Hash.new { |h, k| h[k] = Registry.new }
|
@host_capabilities = Hash.new { |h, k| h[k] = Registry.new }
|
||||||
@providers = Registry.new
|
@providers = Registry.new
|
||||||
|
@provider_capabilities = Hash.new { |h, k| h[k] = Registry.new }
|
||||||
@synced_folders = Registry.new
|
@synced_folders = Registry.new
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -113,7 +113,6 @@ module Vagrant
|
||||||
results
|
results
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
# This returns all registered providers.
|
# This returns all registered providers.
|
||||||
#
|
#
|
||||||
# @return [Hash]
|
# @return [Hash]
|
||||||
|
@ -125,6 +124,21 @@ module Vagrant
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# This returns all the registered provider capabilities.
|
||||||
|
#
|
||||||
|
# @return [Hash]
|
||||||
|
def provider_capabilities
|
||||||
|
results = Hash.new { |h, k| h[k] = Registry.new }
|
||||||
|
|
||||||
|
@registered.each do |plugin|
|
||||||
|
plugin.components.provider_capabilities.each do |provider, caps|
|
||||||
|
results[provider].merge!(caps)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
results
|
||||||
|
end
|
||||||
|
|
||||||
# This returns all the config classes for the various providers.
|
# This returns all the config classes for the various providers.
|
||||||
#
|
#
|
||||||
# @return [Hash]
|
# @return [Hash]
|
||||||
|
|
|
@ -193,6 +193,18 @@ module Vagrant
|
||||||
nil
|
nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Defines a capability for the given provider. The block should return
|
||||||
|
# a class/module that has a method with the capability name, ready
|
||||||
|
# to be executed. This means that if it is an instance method,
|
||||||
|
# the block should return an instance of the class.
|
||||||
|
#
|
||||||
|
# @param [String] provider The name of the provider
|
||||||
|
# @param [String] cap The name of the capability
|
||||||
|
def self.provider_capability(provider, cap, &block)
|
||||||
|
components.provider_capabilities[provider.to_sym].register(cap.to_sym, &block)
|
||||||
|
nil
|
||||||
|
end
|
||||||
|
|
||||||
# Registers additional provisioners to be available.
|
# Registers additional provisioners to be available.
|
||||||
#
|
#
|
||||||
# @param [String] name Name of the provisioner.
|
# @param [String] name Name of the provisioner.
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
require "vagrant/capability_host"
|
||||||
|
|
||||||
module Vagrant
|
module Vagrant
|
||||||
module Plugin
|
module Plugin
|
||||||
module V2
|
module V2
|
||||||
|
@ -5,6 +7,8 @@ module Vagrant
|
||||||
# is responsible for creating compute resources to match the needs
|
# is responsible for creating compute resources to match the needs
|
||||||
# of a Vagrant-configured system.
|
# of a Vagrant-configured system.
|
||||||
class Provider
|
class Provider
|
||||||
|
include CapabilityHost
|
||||||
|
|
||||||
# Initialize the provider to represent the given machine.
|
# Initialize the provider to represent the given machine.
|
||||||
#
|
#
|
||||||
# @param [Vagrant::Machine] machine The machine that this provider
|
# @param [Vagrant::Machine] machine The machine that this provider
|
||||||
|
@ -63,6 +67,18 @@ module Vagrant
|
||||||
def state
|
def state
|
||||||
nil
|
nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# This is an internal initialize function that should never be
|
||||||
|
# overridden. It is used to initialize some common internal state
|
||||||
|
# that is used in a provider.
|
||||||
|
def _initialize(name, machine)
|
||||||
|
initialize_capabilities!(
|
||||||
|
name.to_sym,
|
||||||
|
{ name.to_sym => [Class.new, nil] },
|
||||||
|
Vagrant.plugin("2").manager.provider_capabilities,
|
||||||
|
machine,
|
||||||
|
)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -7,7 +7,11 @@ describe Vagrant::Machine do
|
||||||
include_context "unit"
|
include_context "unit"
|
||||||
|
|
||||||
let(:name) { "foo" }
|
let(:name) { "foo" }
|
||||||
let(:provider) { double("provider") }
|
let(:provider) do
|
||||||
|
double("provider").tap do |obj|
|
||||||
|
obj.stub(:_initialize => nil)
|
||||||
|
end
|
||||||
|
end
|
||||||
let(:provider_cls) do
|
let(:provider_cls) do
|
||||||
obj = double("provider_cls")
|
obj = double("provider_cls")
|
||||||
obj.stub(:new => provider)
|
obj.stub(:new => provider)
|
||||||
|
@ -48,9 +52,14 @@ describe Vagrant::Machine do
|
||||||
#
|
#
|
||||||
# @yield [machine] Yields the machine that the provider initialization
|
# @yield [machine] Yields the machine that the provider initialization
|
||||||
# method received so you can run additional tests on it.
|
# method received so you can run additional tests on it.
|
||||||
def provider_init_test
|
def provider_init_test(instance=nil)
|
||||||
received_machine = nil
|
received_machine = nil
|
||||||
|
|
||||||
|
if !instance
|
||||||
|
instance = double("instance")
|
||||||
|
instance.stub(:_initialize => nil)
|
||||||
|
end
|
||||||
|
|
||||||
provider_cls = double("provider_cls")
|
provider_cls = double("provider_cls")
|
||||||
provider_cls.should_receive(:new) do |machine|
|
provider_cls.should_receive(:new) do |machine|
|
||||||
# Store this for later so we can verify that it is the
|
# Store this for later so we can verify that it is the
|
||||||
|
@ -62,7 +71,8 @@ describe Vagrant::Machine do
|
||||||
|
|
||||||
# Yield our machine if we want to do additional tests
|
# Yield our machine if we want to do additional tests
|
||||||
yield machine if block_given?
|
yield machine if block_given?
|
||||||
end
|
true
|
||||||
|
end.and_return(instance)
|
||||||
|
|
||||||
# Initialize a new machine and verify that we properly receive
|
# Initialize a new machine and verify that we properly receive
|
||||||
# the machine we expect.
|
# the machine we expect.
|
||||||
|
@ -117,6 +127,17 @@ describe Vagrant::Machine do
|
||||||
machine.provider.should be_nil
|
machine.provider.should be_nil
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "should initialize the capabilities" do
|
||||||
|
instance = double("instance")
|
||||||
|
instance.should_receive(:_initialize).with do |p, m|
|
||||||
|
expect(p).to eq(provider_name)
|
||||||
|
expect(m.name).to eq(name)
|
||||||
|
true
|
||||||
|
end
|
||||||
|
|
||||||
|
provider_init_test(instance)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -281,6 +281,16 @@ describe Vagrant::Plugin::V2::Plugin do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "provider capabilities" do
|
||||||
|
it "should register host capabilities" do
|
||||||
|
plugin = Class.new(described_class) do
|
||||||
|
provider_capability("foo", "bar") { "baz" }
|
||||||
|
end
|
||||||
|
|
||||||
|
plugin.components.provider_capabilities[:foo][:bar].should == "baz"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
describe "provisioners" do
|
describe "provisioners" do
|
||||||
it "should register provisioner classes" do
|
it "should register provisioner classes" do
|
||||||
plugin = Class.new(described_class) do
|
plugin = Class.new(described_class) do
|
||||||
|
|
|
@ -1,9 +1,13 @@
|
||||||
require File.expand_path("../../../../base", __FILE__)
|
require File.expand_path("../../../../base", __FILE__)
|
||||||
|
|
||||||
describe Vagrant::Plugin::V2::Provider do
|
describe Vagrant::Plugin::V2::Provider do
|
||||||
|
include_context "unit"
|
||||||
|
|
||||||
let(:machine) { Object.new }
|
let(:machine) { Object.new }
|
||||||
let(:instance) { described_class.new(machine) }
|
let(:instance) { described_class.new(machine) }
|
||||||
|
|
||||||
|
subject { instance }
|
||||||
|
|
||||||
it "should return nil by default for actions" do
|
it "should return nil by default for actions" do
|
||||||
instance.action(:whatever).should be_nil
|
instance.action(:whatever).should be_nil
|
||||||
end
|
end
|
||||||
|
@ -15,4 +19,32 @@ describe Vagrant::Plugin::V2::Provider do
|
||||||
it "should return nil by default for state" do
|
it "should return nil by default for state" do
|
||||||
instance.state.should be_nil
|
instance.state.should be_nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context "capabilities" do
|
||||||
|
before do
|
||||||
|
register_plugin("2") do |p|
|
||||||
|
p.provider_capability("bar", "foo") {}
|
||||||
|
|
||||||
|
p.provider_capability("foo", "bar") do
|
||||||
|
Class.new do
|
||||||
|
def self.bar(machine)
|
||||||
|
raise "bar #{machine.id}"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
machine.stub(id: "YEAH")
|
||||||
|
|
||||||
|
instance._initialize("foo", machine)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "can execute capabilities" do
|
||||||
|
expect(subject.capability?(:foo)).to be_false
|
||||||
|
expect(subject.capability?(:bar)).to be_true
|
||||||
|
|
||||||
|
expect { subject.capability(:bar) }.
|
||||||
|
to raise_error("bar YEAH")
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue