Class: Chef::Provider::Database::SqlServerUser

Inherits:
SqlServer show all
Includes:
Mixin::ShellOut
Defined in:
lib/cookbooks/database/libraries/provider_database_sql_server_user.rb

Instance Method Summary collapse

Methods inherited from SqlServer

#action_query

Instance Method Details

#action_createObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/cookbooks/database/libraries/provider_database_sql_server_user.rb', line 35

def action_create
  begin
    unless exists?(:logins)
      db.execute("CREATE LOGIN #{@new_resource.username} WITH PASSWORD = '#{@new_resource.password}', CHECK_POLICY = OFF").do
      @new_resource.updated_by_last_action(true)
    end
    unless exists?(:users)
      if @new_resource.database_name
        Chef::Log.info("#{@new_resource} creating user in '#{@new_resource.database_name}' database context.")
        db.execute("USE #{@new_resource.database_name}").do
      else
        Chef::Log.info("#{@new_resource} database_name not provided, creating user in global context.")
      end
      db.execute("CREATE USER #{@new_resource.username} FOR LOGIN #{@new_resource.username}").do
      @new_resource.updated_by_last_action(true)
    end
  ensure
    close
  end
end

#action_dropObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/cookbooks/database/libraries/provider_database_sql_server_user.rb', line 56

def action_drop
  begin
    if exists?(:users)
      db.execute("DROP USER #{@new_resource.username}").do
      @new_resource.updated_by_last_action(true)
    end
    if exists?(:logins)
      db.execute("DROP LOGIN #{@new_resource.username}").do
      @new_resource.updated_by_last_action(true)
    end
  ensure
    close
  end
end

#action_grantObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/cookbooks/database/libraries/provider_database_sql_server_user.rb', line 71

def action_grant
  begin
    if @new_resource.password
      action_create
    end
    Chef::Application.fatal!('Please provide a database_name, SQL Server does not support global GRANT statements.') unless @new_resource.database_name
    grant_statement = "GRANT #{@new_resource.privileges.join(', ')} ON DATABASE::#{@new_resource.database_name} TO #{@new_resource.username}"
    Chef::Log.info("#{@new_resource} granting access with statement [#{grant_statement}]")
    db.execute("USE #{@new_resource.database_name}").do
    db.execute(grant_statement).do
    @new_resource.updated_by_last_action(true)
  ensure
    close
  end
end

#load_current_resourceObject



27
28
29
30
31
32
33
# File 'lib/cookbooks/database/libraries/provider_database_sql_server_user.rb', line 27

def load_current_resource
  Gem.clear_paths
  require 'tiny_tds'
  @current_resource = Chef::Resource::DatabaseUser.new(@new_resource.name)
  @current_resource.username(@new_resource.name)
  @current_resource
end