-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(GH-40) Add Node Graph generation to Sidecar capability
This commit adds the ability for the sidecar to generate a node graph for a manifest file on disk. The path the manifest is passed via the source Action Parameter. This commit also adds the required sidecar protocol additions and tests for the behaviour.
- Loading branch information
1 parent
33dde6c
commit f653f3c
Showing
8 changed files
with
255 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
module PuppetLanguageServerSidecar | ||
module PuppetParserHelper | ||
def self.compile_node_graph(content) | ||
result = PuppetLanguageServerSidecar::Protocol::NodeGraph.new | ||
|
||
begin | ||
# The fontsize is inserted in the puppet code. Need to remove it so the client can render appropriately. Need to | ||
# set it to blank. The graph label is set to editorservices so that we can do text replacement client side to inject the | ||
# appropriate styling. | ||
options = { | ||
'fontsize' => '""', | ||
'name' => 'editorservices' | ||
} | ||
node_graph = compile_to_pretty_relationship_graph(content) | ||
if node_graph.vertices.count.zero? | ||
result.set_error('There were no resources created in the node graph. Is there an include statement missing?') | ||
else | ||
result.dot_content = node_graph.to_dot(options) | ||
end | ||
rescue StandardError => exception | ||
result.set_error("Error while parsing the file. #{exception}") | ||
end | ||
|
||
result | ||
end | ||
|
||
# Reference - https://github.com/puppetlabs/puppet/blob/master/spec/lib/puppet_spec/compiler.rb | ||
def self.compile_to_catalog(string, node = Puppet::Node.new('test')) | ||
Puppet[:code] = string | ||
# see lib/puppet/indirector/catalog/compiler.rb#filter | ||
Puppet::Parser::Compiler.compile(node).filter(&:virtual?) | ||
end | ||
|
||
def self.compile_to_ral(manifest, node = Puppet::Node.new('test')) | ||
# Add the node facts if they don't already exist | ||
node.merge(Facter.to_hash) if node.facts.nil? | ||
|
||
catalog = compile_to_catalog(manifest, node) | ||
ral = catalog.to_ral | ||
ral.finalize | ||
ral | ||
end | ||
|
||
def self.compile_to_relationship_graph(manifest, prioritizer = Puppet::Graph::SequentialPrioritizer.new) | ||
ral = compile_to_ral(manifest) | ||
graph = Puppet::Graph::RelationshipGraph.new(prioritizer) | ||
graph.populate_from(ral) | ||
graph | ||
end | ||
|
||
def self.compile_to_pretty_relationship_graph(manifest, prioritizer = Puppet::Graph::SequentialPrioritizer.new) | ||
graph = compile_to_relationship_graph(manifest, prioritizer) | ||
|
||
# Remove vertexes which just clutter the graph | ||
|
||
# Remove all of the Puppet::Type::Whit nodes. This is an internal only class | ||
list = graph.vertices.select { |node| node.is_a?(Puppet::Type::Whit) } | ||
list.each { |node| graph.remove_vertex!(node) } | ||
|
||
# Remove all of the Puppet::Type::Schedule nodes | ||
list = graph.vertices.select { |node| node.is_a?(Puppet::Type::Schedule) } | ||
list.each { |node| graph.remove_vertex!(node) } | ||
|
||
# Remove all of the Puppet::Type::Filebucket nodes | ||
list = graph.vertices.select { |node| node.is_a?(Puppet::Type::Filebucket) } | ||
list.each { |node| graph.remove_vertex!(node) } | ||
|
||
graph | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
...uageserver-sidecar/integration/puppet-languageserver-sidecar/puppet_parser_helper_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
require 'spec_helper' | ||
|
||
describe 'PuppetLanguageServerSidecar::PuppetParserHelper' do | ||
let (:subject) { PuppetLanguageServerSidecar::PuppetParserHelper } | ||
|
||
describe '#compile_node_graph' do | ||
context 'a valid manifest' do | ||
let(:manifest) { "user { 'test':\nensure => present\n}\n "} | ||
|
||
it 'should compile succesfully' do | ||
result = subject.compile_node_graph(manifest) | ||
expect(result).to_not be_nil | ||
# Make sure it's a DOT graph file | ||
expect(result.dot_content).to match(/digraph/) | ||
# Make sure the resource is there | ||
expect(result.dot_content).to match(/User\[test\]/) | ||
# Make sure the fontsize is set to empty | ||
expect(result.dot_content).to match(/fontsize = \"\"/) | ||
# Make sure the label is editorservices | ||
expect(result.dot_content).to match(/label = \"editorservices\"/) | ||
# Expect no errors | ||
expect(result.error_content.to_s).to eq('') | ||
end | ||
end | ||
|
||
context 'a valid manifest with no resources' do | ||
let(:manifest) { "" } | ||
|
||
it 'should compile with an error' do | ||
result = subject.compile_node_graph(manifest) | ||
expect(result).to_not be_nil | ||
expect(result.dot_content).to eq("") | ||
expect(result.error_content).to match(/no resources created in the node graph/) | ||
end | ||
end | ||
|
||
context 'an invalid manifest' do | ||
let(:manifest) { "I am an invalid manifest" } | ||
|
||
it 'should compile with an error' do | ||
result = subject.compile_node_graph(manifest) | ||
expect(result).to_not be_nil | ||
expect(result.dot_content).to eq("") | ||
expect(result.error_content).to match(/Error while parsing the file./) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters