Class: Inspec::Resources::MssqlSession

Inherits:
Object
  • Object
show all
Defined in:
lib/resources/mssql_session.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ MssqlSession

Returns a new instance of MssqlSession.



25
26
27
28
29
30
# File 'lib/resources/mssql_session.rb', line 25

def initialize(opts = {})
  @user = opts[:user]
  @pass = opts[:pass]
  @host = opts[:host] || 'localhost'
  @instance = opts[:instance]
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



23
24
25
# File 'lib/resources/mssql_session.rb', line 23

def host
  @host
end

#passObject (readonly)

Returns the value of attribute pass.



23
24
25
# File 'lib/resources/mssql_session.rb', line 23

def pass
  @pass
end

#userObject (readonly)

Returns the value of attribute user.



23
24
25
# File 'lib/resources/mssql_session.rb', line 23

def user
  @user
end

Instance Method Details

#query(q) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/resources/mssql_session.rb', line 32

def query(q)
  escaped_query = q.gsub(/\\/, '\\\\').gsub(/"/, '\\"').gsub(/\$/, '\\$').gsub(/\@/, '`@')
  cmd_string = "sqlcmd -Q \"#{escaped_query}\""
  cmd_string += " -U #{@user} -P #{@pass}" unless @user.nil? or @pass.nil?
  if @instance.nil?
    cmd_string += " -S #{@host}"
  else
    cmd_string += " -S #{@host}\\#{@instance}"
  end
  puts cmd_string
  cmd = inspec.command(cmd_string)
  out = cmd.stdout + "\n" + cmd.stderr
  if out =~ /Sqlcmd: Error/
    skip_resource("Can't connect to the MS SQL Server.")
  end
  cmd
end

#to_sObject



50
51
52
# File 'lib/resources/mssql_session.rb', line 50

def to_s
  'MSSQL session'
end