Save point: use target ref to retrieve info from target index

This commit is contained in:
sophia 2021-08-03 12:08:14 -05:00 committed by Paul Hinze
parent 663fbab87a
commit 1ea5b2ba91
No known key found for this signature in database
GPG Key ID: B69DEDF2D55501C0
6 changed files with 51 additions and 46 deletions

View File

@ -30,31 +30,20 @@ func (t *TargetIndex) Delete(target core.Target) (err error) {
return
}
func (t *TargetIndex) Get(uuid string) (entry core.Target, err error) {
target, err := t.client.GetTarget(
t.ctx,
&vagrant_server.GetTargetRequest{
Target: &vagrant_plugin_sdk.Ref_Target{ResourceId: uuid},
},
)
refTarget := &vagrant_plugin_sdk.Ref_Target{
ResourceId: target.Target.ResourceId,
Project: target.Target.Project,
Name: target.Target.Name,
}
func (t *TargetIndex) Get(ref *vagrant_plugin_sdk.Ref_Target) (entry core.Target, err error) {
// TODO: check if this actually gets back a full target
entry, err = NewTarget(
t.ctx,
WithTargetRef(refTarget),
WithTargetRef(ref),
)
return
}
func (t *TargetIndex) Includes(uuid string) (exists bool, err error) {
func (t *TargetIndex) Includes(ref *vagrant_plugin_sdk.Ref_Target) (exists bool, err error) {
resp, err := t.client.GetTarget(
t.ctx,
&vagrant_server.GetTargetRequest{
Target: &vagrant_plugin_sdk.Ref_Target{ResourceId: uuid},
Target: ref,
},
)
if err != nil {

View File

@ -35,7 +35,10 @@ module Vagrant
if !@client.nil?
machine_index_client = @client.machine_index
@machine_index ||= Vagrant::MachineIndex.new()
@machine_index.set_client(machine_index_client)
@logger.debug("setting machine index client")
@machine_index.client = machine_index_client
@logger.debug("setting machine index project ref")
@machine_index.project_ref = @client.ref
end
@machine_index
end

View File

@ -3,6 +3,10 @@ module Vagrant
# This module enables the MachineIndex for server mode
module Remote
attr_accessor :client
attr_accessor :project_ref
# Add an attribute reader for the client
# when applied to the MachineIndex class
def self.prepended(klass)
@ -17,11 +21,6 @@ module Vagrant
@machines = {}
end
def set_client(client)
@logger.debug("setting machine index client")
@client = client
end
# Deletes a machine by UUID.
#
# @param [Entry] entry The entry to delete.
@ -33,18 +32,26 @@ module Vagrant
# Accesses a machine by UUID
#
# @param [String] uuid UUID for the machine to access.
# @param [String] name for the machine to access.
# @return [MachineIndex::Entry]
def get(uuid)
@client.get(machine)
def get(name)
ref = Hashicorp::Vagrant::Sdk::Ref::Target.new(
name: name,
project: @project_ref
)
@client.get(ref)
end
# Tests if the index has the given UUID.
#
# @param [String] uuid
# @param [String] name
# @return [Boolean]
def include?(uuid)
@client.include?(uuid)
def include?(name)
ref = Hashicorp::Vagrant::Sdk::Ref::Target.new(
name: name,
project: @project_ref
)
@client.include?(ref)
end
def release(entry)

View File

@ -26,24 +26,19 @@ module VagrantPlugins
true
end
# @param [String] uuid UUID for the machine to access.
# @return [Hashicorp::Vagrant::Sdk::Args::Target]
def get(uuid)
@logger.debug("getting machine with uuid #{uuid} from index")
req = TargetIndex::GetRequest.new(
uuid: uuid
)
@client.get(req)
# @param [Hashicorp::Vagrant::Sdk::Ref::Target] a ref for the machine to access.
# @return [Hashicorp::Vagrant::Sdk::Ref::Target]
def get(ref)
@logger.debug("getting machine with ref #{ref} from index")
resp = @client.get(ref)
return resp.target
end
# @param [String] uuid
# @param [Hashicorp::Vagrant::Sdk::Ref::Target]
# @return [Boolean]
def include?(uuid)
@logger.debug("checking for machine with uuid #{uuid} in index")
req = TargetIndex::IncludesRequest.new(
uuid: uuid
)
@client.includes(req).exists
def include?(ref)
@logger.debug("checking for machine with ref #{ref} in index")
@client.includes(red).exists
end
# @param [Hashicorp::Vagrant::Sdk::Args::Target] target

View File

@ -4,6 +4,7 @@ module VagrantPlugins
class Project
attr_reader :client
attr_reader :resource_id
def initialize(conn, broker=nil)
@logger = Log4r::Logger.new("vagrant::command::serve::client::project")
@ -17,6 +18,10 @@ module VagrantPlugins
self.new(conn.to_s, broker)
end
def ref
SDK::Ref::Project.new(resource_id: resource_id)
end
# Returns a machine client for the given name
# return [VagrantPlugins::CommandServe::Client::Machine]
def target(name)

View File

@ -76,10 +76,16 @@ module VagrantPlugins
raise "Failed to locate command plugin for: #{plugin_name}"
end
cmd_klass = plugin.call
cmd_args = req.command_args.to_a[1..] + arguments.args.to_a
cmd = cmd_klass.new(cmd_args, env)
result = cmd.execute
begin
cmd_klass = plugin.call
cmd_args = req.command_args.to_a[1..] + arguments.args.to_a
cmd = cmd_klass.new(cmd_args, env)
result = cmd.execute
rescue => err
LOGGER.error(err)
LOGGER.debug("#{err.class}: #{err}\n#{err.backtrace.join("\n")}")
raise
end
if !result.is_a?(Integer)
result = 1