Skip to content

Commit 675f974

Browse files
committed
Merge pull request #69 from cyberious/FM1901
FM1901 Add delete user capabilities
2 parents 58be6dd + ae28a21 commit 675f974

File tree

7 files changed

+66
-7
lines changed

7 files changed

+66
-7
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
---
2+
sudo: false
23
language: ruby
34
bundler_args: --without development
45
script: "bundle exec rake spec SPEC_OPTS='--format documentation'"

lib/puppet/provider/sqlserver.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,10 @@ def self.run_authenticated_sqlcmd(query, opts)
5555
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
5656
debug("Return result #{result}")
5757
if opts[:failonfail] && result.match(/THROW CAUGHT/)
58-
fail(result.gsub('THROW CAUGHT:',''))
58+
fail(result.gsub('THROW CAUGHT:', ''))
59+
end
60+
if result.match(/Msg \d+, Level 16/)
61+
fail(result)
5962
end
6063
return result
6164
ensure

lib/puppet/templates/authenticated_query.ps1.erb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ $result = sqlcmd.exe -i '<%= input_file %>' -h-1 -W -s ',' <% if @instance != 'M
3434
Write-Error -Message ($result | where {$_ -match "Incorrect syntax"} | select -First 1)
3535
exit(10)
3636
}
37+
if($result -match "Msg \d+, Level 16"){
38+
$msg = $result -join ' '
39+
Write-Host $msg
40+
Write-Error -Message "ERROR: $msg"
41+
exit(10)
42+
}
3743
}
3844
catch{
3945
Write-Host $_

manifests/user.pp

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,38 @@
11
##
2+
# == Define Resource Type: sqlserver::user
23
#
4+
# === Requirement/Dependencies:
35
#
6+
# Requires defined type {sqlserver::config} in order to execute against the SQL Server instance
7+
#
8+
# === Examples
9+
#
10+
# sqlserver::user{'myUser':
11+
# database => 'loggingDatabase',
12+
# login => 'myUser',
13+
# }
14+
#
15+
# === Parameters
16+
# [user]
17+
# The username you want to manage, defaults to the title
18+
#
19+
# [database]
20+
# The database you want the user to be created as
21+
#
22+
# [ensure]
23+
# Ensure present or absent
24+
#
25+
# [default_schema]
26+
# SQL schema you would like to default to, typically 'dbo'
27+
#
28+
# [instance]
29+
# The named instance you want to manage against
30+
#
31+
# [login]
32+
# The login to associate the user with, by default SQL Server will assume user and login match if left empty
33+
#
34+
# [password]
35+
# The password for the user, can only be used when the database is a contained database.
436
#
537
##
638
define sqlserver::user (
@@ -11,20 +43,19 @@
1143
$instance = 'MSSQLSERVER',
1244
$login = undef,
1345
$password = undef,
14-
$force_delete = false,
1546
)
1647
{
1748
sqlserver_validate_instance_name($instance)
1849

1950
$is_windows_user = sqlserver_is_domain_or_local_user($login)
2051

2152
if $password {
22-
validate_re($password, '^.{1,128}$', 'Password must be equal or less than 128 characters')
53+
sqlserver_validate_range($password, 1, 128, 'Password must be equal or less than 128 characters')
2354
if $is_windows_user and $login != undef{
2455
fail('Can not provide password when using a Windows Login')
2556
}
2657
}
27-
validate_re($database, '^.{1,128}$','Database name must be between 1 and 128 characters')
58+
sqlserver_validate_range($database, 1, 128, 'Database name must be between 1 and 128 characters')
2859

2960
$create_delete = $ensure ? {
3061
present => 'create',

spec/defines/user_spec.rb

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
let(:additional_params) { {:user => 'myMachineName/myUser'} }
7979
let(:sqlserver_tsql_title) { 'user-MSSQLSERVER-myDatabase-myMachineName/myUser' }
8080
let(:should_contain_command) { [
81-
"USE [myDatabase]",
81+
"USE [myDatabase];",
8282
'CREATE USER [myMachineName/myUser]'
8383
] }
8484
it_should_behave_like 'sqlserver_tsql command'
@@ -93,9 +93,24 @@
9393
] }
9494
it_should_behave_like 'sqlserver_tsql command'
9595
end
96+
9697
describe 'have dependency on Sqlserver::Config[MSSQLSERVER]' do
9798
it 'should require ::config' do
9899
should contain_sqlserver_tsql(sqlserver_tsql_title).with_require('Sqlserver::Config[MSSQLSERVER]')
99100
end
100101
end
102+
103+
describe 'when ensure => absent' do
104+
let(:additional_params) { {:ensure => 'absent'} }
105+
let(:sqlserver_contain_command) { [
106+
'USE [loggingDb];\nDROP [loggingUser]',
107+
"\nIF EXISTS(SELECT name FROM sys.database_principals WHERE name = 'loggingUser')\n THROW",
108+
] }
109+
let(:sqlserver_contain_onlyif) { [
110+
"\nIF EXISTS(SELECT name FROM sys.database_principals WHERE type in ('U','S','G') AND name = 'loggingUser')\n",
111+
] }
112+
it_should_behave_like 'sqlserver_tsql command'
113+
it_should_behave_like 'sqlserver_tsql onlyif'
114+
end
115+
101116
end

templates/create/user.sql.erb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
-- Need to use exec instead of use statement as this will trigger try catch
21
USE [<%= @database %>];
32
<% if @password %>
43
IF EXISTS(select containment from sys.databases WHERE name = '<%= @database %>' AND containment = 0)
5-
THROW 51000, 'Database must be contained in order to use passwords', 0
4+
THROW 51000, 'Database must be contained in order to use passwords', 10
65
<% end %>
76
CREATE USER [<%= @user %>]
87
<% if @login -%>

templates/delete/user.sql.erb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
USE [<%= @database %>];
2+
DROP USER [<%= @user %>];
3+
IF EXISTS(SELECT name FROM sys.database_principals WHERE name = '<%= @user %>')
4+
THROW 51000, 'Failed to drop user <%= @user %>', 10

0 commit comments

Comments
 (0)