Skip to content

Commit

Permalink
(puppetlabsGH-242) Puppet Facts Endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
jpogran committed Apr 29, 2020
1 parent f2e758e commit f157f3f
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 0 deletions.
21 changes: 21 additions & 0 deletions lib/lsp/lsp_custom.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,27 @@ def from_h!(value)
end
end

# export interface GetPuppetFactResponse {
# data: string;
# error: string;
# }
class PuppetFactResponse < LSPBase
attr_accessor :facts # type: string
attr_accessor :error # type: string

def initialize(initial_hash = nil)
super
@optional_method_names = %i[error]
end

def from_h!(value)
value = {} if value.nil?
self.facts = value['facts']
self.error = value['error']
self
end
end

# export interface GetPuppetResourceResponse {
# data: string;
# error: string;
Expand Down
10 changes: 10 additions & 0 deletions lib/puppet-languageserver/message_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@ def request_puppet_getversion(_, _json_rpc_message)
)
end

def request_puppet_getfacts(_, json_rpc_message)
begin
results = PuppetLanguageServer::PuppetHelper.get_all_facts(documents.store_root_path)
LSP::PuppetFactResponse.new('facts' => results)
rescue StandardError => e
PuppetLanguageServer.log_message(:error, "(puppet/getFacts) Error getting facts. #{e}")
LSP::PuppetFactResponse.new('error' => 'An internal error occured while getting facts. Please see the debug log files for more information.')
end
end

def request_puppet_getresource(_, json_rpc_message)
type_name = json_rpc_message.params['typename']
title = json_rpc_message.params['title']
Expand Down
9 changes: 9 additions & 0 deletions lib/puppet-languageserver/puppet_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ def self.get_node_graph(content, local_workspace)
end
end

def self.get_all_facts(local_workspace)
ap = PuppetLanguageServer::Sidecar::Protocol::ActionParams.new

args = ['--action-parameters=' + ap.to_json]
args << "--local-workspace=#{local_workspace}" unless local_workspace.nil?

sidecar_queue.execute_sync('facts_all', args, false)
end

def self.get_puppet_resource(typename, title, local_workspace)
ap = PuppetLanguageServer::Sidecar::Protocol::ActionParams.new
ap['typename'] = typename
Expand Down
10 changes: 10 additions & 0 deletions lib/puppet-languageserver/sidecar_queue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,16 @@ def execute_sync(action, additional_args, handle_errors = false)

PuppetLanguageServer::FacterHelper.assert_facts_loaded

when 'facts_all'
list = {}
PuppetLanguageServer::Sidecar::Protocol::FactList
.new
.from_json!(result)
.map { |element| list[element.key] = element.value }
PuppetLanguageServer.log_message(:debug, "SidecarQueue Thread: facts returned #{list.count} items")

return list

when 'node_graph'
return PuppetLanguageServer::Sidecar::Protocol::PuppetNodeGraph.new.from_json!(result)

Expand Down
9 changes: 9 additions & 0 deletions lib/puppet_languageserver_sidecar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ def self.require_gems(options)
workspace_functions
workspace_types
facts
facts_all
].freeze

class CommandLineParser
Expand Down Expand Up @@ -399,6 +400,14 @@ def self.execute(options)
inject_workspace_as_environment unless injected
PuppetLanguageServerSidecar::FacterHelper.retrieve_facts(cache)

when 'facts_all'
# Can't cache for facts
cache = PuppetLanguageServerSidecar::Cache::Null.new
# Inject the workspace etc. if present
injected = inject_workspace_as_module
inject_workspace_as_environment unless injected
PuppetLanguageServerSidecar::FacterHelper.retrieve_facts(cache)

else
log_message(:error, "Unknown action #{options[:action]}. Expected one of #{ACTION_LIST}")
end
Expand Down

0 comments on commit f157f3f

Please sign in to comment.