Skip to content

Commit 3fd2931

Browse files
author
Travis Fields
committed
FM-2110 Prep TSQL provider to be released and backport for all existing usage on new expected behavior
1 parent 71f0870 commit 3fd2931

18 files changed

+77
-44
lines changed

CHANGELOG.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,11 @@
1-
##2014-12-08 - 1.0.0
1+
##2015-01-xx - 1.1.0
2+
3+
###Summary
4+
Add sqlserver_tsql provider that to enable users to run sql against a given instance
5+
6+
###Features
7+
* sqlserver_tsql added
8+
* Better logging for ::login ::database and ::sp_configure
9+
10+
u#2014-12-08 - 1.0.0
211
Initial release
File renamed without changes.

lib/puppet/property/sqlserver_tsql.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
require 'puppet/property'
2+
3+
class Puppet::Property::SqlserverTsql < Puppet::Property
4+
desc 'TSQL property that we are going to wrap with a try catch'
5+
munge do |value|
6+
erb_template = <<-TEMPLATE
7+
BEGIN TRY
8+
#{value}
9+
END TRY
10+
BEGIN CATCH
11+
DECLARE @msg as VARCHAR(max);
12+
SELECT @msg = 'THROW CAUGHT: ' + ERROR_MESSAGE();
13+
THROW 51000, @msg, 10
14+
END CATCH
15+
TEMPLATE
16+
value = erb_template
17+
end
18+
19+
end

lib/puppet/provider/sqlserver.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,12 @@ def self.run_authenticated_sqlcmd(query, opts)
5151
temp_ps1.write(ps1.result(b))
5252
temp_ps1.flush
5353
temp_ps1.close
54+
#We want to not fail the exec but fail the overall process once we get the clean result back, otherwise we report the temp file which is meaningless
5455
result = Puppet::Util::Execution.execute(['powershell.exe', '-noprofile', '-executionpolicy', 'unrestricted', temp_ps1.path], {:failonfail => false}) #We expect some things to fail in order to run as an only if
55-
debug("Return result #{result.exitstatus}")
56+
debug("Return result #{result}")
57+
if opts[:failonfail] && result.match(/ERROR/)
58+
fail(result)
59+
end
5660
return result
5761
ensure
5862
temp_ps1.close

lib/puppet/provider/sqlserver_tsql/mssql.rb

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,15 @@
22

33
Puppet::Type::type(:sqlserver_tsql).provide(:mssql, :parent => Puppet::Provider::Sqlserver) do
44

5-
def run(query)
6-
debug("Running resource #{query} against #{resource[:instance]}")
7-
result = Puppet::Provider::Sqlserver.run_authenticated_sqlcmd(query, {:instance_name => resource[:instance]})
8-
return result
9-
end
10-
11-
def run_check
12-
result = self.run(resource[:onlyif])
5+
def run(query, opts)
6+
debug("Running resource #{query} against #{resource[:instance]} with failonfail set to #{opts[:failonfail]}")
7+
opts[:instance_name] = resource[:instance]
8+
result = Puppet::Provider::Sqlserver.run_authenticated_sqlcmd(query, opts)
139
return result
1410
end
1511

1612
def run_update
17-
result = self.run(resource[:command])
13+
result = self.run(resource[:command], {:failonfail => true})
1814
return result
1915
end
2016

lib/puppet/templates/authenticated_query.ps1.erb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@ if (!(Get-Command 'sqlcmd.exe' -ErrorAction SilentlyContinue)){
2525

2626
$result = sqlcmd.exe -i '<%= input_file %>' -h-1 -W -s ',' <% if @instance != 'MSSQLSERVER' %>-S localhost\<%= @instance %><%end%>
2727
if($result -match "ERROR"){
28-
Write-Error -Message ($result | where {$_ -match "ERROR"} | select -First 1)
28+
Write-Host ($result | where {$_ -match "THROW CAUGHT"} | select -First 1)
29+
Write-Error -Message ($result | where {$_ -match "THROW CAUGHT"} | select -First 1)
2930
exit(10)
3031
}
3132
if($result -match "Incorrect syntax near "){
33+
Write-Host ($result | where {$_ -match "Incorrect syntax near"} | select -First 1)
3234
Write-Error -Message ($result | where {$_ -match "Incorrect syntax"} | select -First 1)
3335
exit(10)
3436
}

lib/puppet/type/sqlserver_features.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'property/login'))
1+
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'property/sqlserver_login'))
22
require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'puppet_x/sqlserver/server_helper'))
33

44
Puppet::Type::newtype(:sqlserver_features) do

lib/puppet/type/sqlserver_instance.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'property/login'))
1+
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'property/sqlserver_login'))
22
require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'puppet_x/sqlserver/server_helper'))
33

44
Puppet::Type::newtype(:sqlserver_instance) do

lib/puppet/type/sqlserver_tsql.rb

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require 'puppet'
2+
require 'puppet/property/sqlserver_tsql'
23

34
Puppet::Type::newtype(:sqlserver_tsql) do
45
newparam :name, :namevar => true
@@ -15,21 +16,23 @@ def self.checks
1516
end
1617

1718
desc 'command to run against an instance with the authenticated credentials used in sqlserver::config'
18-
newparam(:command) do
19+
newparam(:command, :parent => Puppet::Property::SqlserverTsql) do
1920

2021
end
2122

2223
desc 'requires the usage of sqlserver::config with the user and password'
2324
newparam(:instance) do
24-
25+
munge do |value|
26+
value.upcase
27+
end
2528
end
2629

2730
desc 'SQL Query to run and only run if exits with non-zero'
28-
newcheck(:onlyif) do
29-
# Return true if the command returns 0.
31+
newcheck(:onlyif, :parent => Puppet::Property::SqlserverTsql) do
32+
#Runs in the event that our TSQL exits with anything other than 0
3033
def check(value)
3134
begin
32-
output = provider.run(value)
35+
output = provider.run(value, :failonfail => false)
3336
end
3437
debug("OnlyIf returned exitstatus of #{output.exitstatus}")
3538
output.exitstatus != 0

metadata.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "puppetlabs-sqlserver",
3-
"version": "1.0.0",
3+
"version": "1.1.0",
44
"author": "puppetlabs",
55
"summary": "The `sqlserver` module installs and manages MS SQL Server 2012 and 2014 on Windows systems.",
66
"license": "PuppetLabs-Enterprise",

0 commit comments

Comments
 (0)