Skip to content

Commit

Permalink
(GH-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 235a05c
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how

## Unreleased

### Added

- ([GH-242](https://github.com/puppetlabs/puppet-editor-services/issues/242)) Add Puppet Facts Endpoint

## 0.25.0 - 2020-03-26

### Fixed
Expand Down
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
5 changes: 5 additions & 0 deletions lib/puppet-languageserver/message_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ def request_puppet_getversion(_, _json_rpc_message)
)
end

def request_puppet_getfacts(_, _json_rpc_message)
results = PuppetLanguageServer::PuppetHelper.get_all_facts(documents.store_root_path)
LSP::PuppetFactResponse.new('facts' => results)
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
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,23 @@ def run_sidecar(cmd_options)
end
end

describe 'when running facts_all action' do
let (:cmd_options) { ['--action', 'facts_all'] }

it 'should return a deserializable facts object with all default facts' do
result = run_sidecar(cmd_options)
deserial = PuppetLanguageServer::Sidecar::Protocol::FactList.new
expect { deserial.from_json!(result) }.to_not raise_error
default_fact_names.each do |name|
expect(deserial).to contain_child_with_key(name)
end

module_fact_names.each do |name|
expect(deserial).to_not contain_child_with_key(name)
end
end
end

context 'given a workspace containing a module' do
# Test fixtures used is fixtures/valid_module_workspace
let(:workspace) { File.join($fixtures_dir, 'valid_module_workspace') }
Expand Down

0 comments on commit 235a05c

Please sign in to comment.