Chris Roberts b195772160
Update capability platform service and host/guest usages
Update default arguments to be a map instead of an array so
    expected result values can be provided. Add new method for
    handling arguments so custom adjustments can be applied before
    calling the capability. Add custom argument adjustments to
    guest service to provide machine if one is not given.
2022-04-25 12:25:59 -05:00

87 lines
2.6 KiB
Ruby

require "google/protobuf/well_known_types"
module VagrantPlugins
module CommandServe
module Service
class HostService < Hashicorp::Vagrant::Sdk::HostService::Service
include CapabilityPlatformService
def initialize(*args, **opts, &block)
caps = Vagrant.plugin("2").manager.host_capabilities
default_args = {
Vagrant::Environment => SDK::FuncSpec::Value.new(
type: "hashicorp.vagrant.sdk.Args.Project",
name: "",
),
}
initialize_capability_platform!(caps, default_args)
end
def detect_spec(*_)
# TODO: Add statebad as an arg
SDK::FuncSpec.new(
name: "detect_spec",
args: [
SDK::FuncSpec::Value.new(
type: "hashicorp.vagrant.sdk.Args.StateBag",
name: "",
)
],
result: [
type: "hashicorp.vagrant.sdk.Platform.DetectResp",
name: "",
]
)
end
def detect(req, ctx)
with_info(ctx) do |info|
plugin_name = info.plugin_name
statebag = mapper.funcspec_map(req, expect: Client::StateBag)
plugin = Vagrant.plugin("2").manager.hosts[plugin_name.to_s.to_sym].to_a.first
if !plugin
raise "Failed to locate host plugin for: #{plugin_name.inspect}"
end
host = plugin.new
begin
detected = host.detect?(statebag)
rescue => err
logger.debug("error encountered detecting host: #{err.class} - #{err}")
detected = false
end
logger.debug("detected #{detected} for host #{plugin_name}")
SDK::Platform::DetectResp.new(
detected: detected,
)
end
end
def parent_spec(*_)
SDK::FuncSpec.new(
name: "parent_spec",
result: [
type: "hashicorp.vagrant.sdk.Host.ParentResp",
name: "",
]
)
end
def parent(req, ctx)
with_info(ctx) do |info|
plugin_name = info.plugin_name
host_hash = Vagrant.plugin("2").manager.hosts[plugin_name.to_s.to_sym].to_a
plugin = host_hash.first
if !plugin
raise "Failed to locate host plugin for: #{plugin_name.inspect}"
end
SDK::Platform::ParentResp.new(
parent: host_hash.last
)
end
end
end
end
end
end