Class: Inspec::Resources::MssqlSession
- Inherits:
-
Object
- Object
- Inspec::Resources::MssqlSession
- Defined in:
- lib/resources/mssql_session.rb
Overview
STABILITY: Experimental This resource needs further testing and refinement
This requires the ‘sqlcmd` tool available on platform
Instance Attribute Summary collapse
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#password ⇒ Object
readonly
Returns the value of attribute password.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
Instance Method Summary collapse
-
#initialize(opts = {}) ⇒ MssqlSession
constructor
A new instance of MssqlSession.
- #query(q) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ MssqlSession
Returns a new instance of MssqlSession.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/resources/mssql_session.rb', line 35 def initialize(opts = {}) @user = opts[:user] @password = opts[:password] || opts[:pass] if opts[:pass] warn '[DEPRECATED] use `password` option to supply password instead of `pass`' end @host = opts[:host] || 'localhost' @instance = opts[:instance] # check if sqlcmd is available return skip_resource('sqlcmd is missing') if !inspec.command('sqlcmd').exist? # check that database is reachable return skip_resource("Can't connect to the MS SQL Server.") if !test_connection end |
Instance Attribute Details
#host ⇒ Object (readonly)
Returns the value of attribute host.
34 35 36 |
# File 'lib/resources/mssql_session.rb', line 34 def host @host end |
#password ⇒ Object (readonly)
Returns the value of attribute password.
34 35 36 |
# File 'lib/resources/mssql_session.rb', line 34 def password @password end |
#user ⇒ Object (readonly)
Returns the value of attribute user.
34 35 36 |
# File 'lib/resources/mssql_session.rb', line 34 def user @user end |
Instance Method Details
#query(q) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/resources/mssql_session.rb', line 50 def query(q) escaped_query = q.gsub(/\\/, '\\\\').gsub(/"/, '\\"').gsub(/\$/, '\\$') # surpress 'x rows affected' in SQLCMD with 'set nocount on;' cmd_string = "sqlcmd -Q \"set nocount on; #{escaped_query}\" -W -w 1024 -s ','" cmd_string += " -U #{@user} -P '#{@password}'" unless @user.nil? || @password.nil? if @instance.nil? cmd_string += " -S #{@host}" else cmd_string += " -S #{@host}\\#{@instance}" end cmd = inspec.command(cmd_string) out = cmd.stdout + "\n" + cmd.stderr if cmd.exit_status != 0 || out =~ /Sqlcmd: Error/ # TODO: we need to throw an exception here # change once https://github.com/chef/inspec/issues/1205 is in warn "Could not execute the sql query #{out}" DatabaseHelper::SQLQueryResult.new(cmd, Hashie::Mash.new({})) else DatabaseHelper::SQLQueryResult.new(cmd, parse_csv_result(cmd)) end end |
#to_s ⇒ Object
72 73 74 |
# File 'lib/resources/mssql_session.rb', line 72 def to_s 'MSSQL session' end |