Validate provider

This commit is contained in:
sophia 2022-09-06 17:54:28 -04:00
parent 571daea639
commit 6d4f769c87
2 changed files with 27 additions and 3 deletions

View File

@ -7,6 +7,7 @@ import (
"github.com/hashicorp/go-argmapper"
"github.com/hashicorp/go-hclog"
"github.com/pkg/errors"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/proto"
@ -18,6 +19,7 @@ import (
"github.com/hashicorp/vagrant-plugin-sdk/internal-shared/cleanup"
"github.com/hashicorp/vagrant-plugin-sdk/internal-shared/dynamic"
"github.com/hashicorp/vagrant-plugin-sdk/internal-shared/protomappers"
"github.com/hashicorp/vagrant-plugin-sdk/localizer"
"github.com/hashicorp/vagrant-plugin-sdk/proto/vagrant_plugin_sdk"
"github.com/hashicorp/vagrant/internal/plugin"
"github.com/hashicorp/vagrant/internal/server/proto/vagrant_server"
@ -444,7 +446,7 @@ func (v *Vagrantfile) Target(
return nil, err
}
conf, err := v.TargetConfig(name, provider, false)
conf, err := v.TargetConfig(name, provider, true)
if err != nil {
return
}
@ -496,7 +498,6 @@ func (v *Vagrantfile) Target(
// Generate a new Vagrantfile for the given target
// NOTE: This function may return a nil result without an error
// TODO(spox): Provider validation is not currently implemented
// TODO(spox): Needs box configuration applied
func (v *Vagrantfile) TargetConfig(
name, // name of the target
@ -511,6 +512,29 @@ func (v *Vagrantfile) TargetConfig(
return nil, err
}
if provider != "" {
pp, err := v.factory.plugins.Find(provider, component.ProviderType)
if err != nil {
return nil, err
}
if validateProvider {
usable, err := pp.Component.(core.Provider).Usable()
if err != nil {
return nil, err
}
if !usable {
// TODO: include message provided in the error
return nil, errors.New(
localizer.LocalizeMsg(
"provider_not_usable",
map[string]string{"Provider": provider, "Machine": name},
),
)
}
}
}
cid := name + "+" + provider
if cv := v.cache.Get(cid); cv != nil {
return cv.(core.Vagrantfile), nil

View File

@ -20,7 +20,7 @@ module VagrantPlugins
def usable(req, ctx)
with_plugin(ctx, :providers, broker: broker) do |plugin|
is_usable = plugin.usable?
is_usable = plugin.usable?(true)
SDK::Provider::UsableResp.new(
is_usable: is_usable,
)