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
- 
  
    
      #db_name  ⇒ Object 
    
    
  
  
  
  
    
      readonly
    
    
  
  
  
  
  
  
    Returns the value of attribute db_name. 
- 
  
    
      #host  ⇒ Object 
    
    
  
  
  
  
    
      readonly
    
    
  
  
  
  
  
  
    Returns the value of attribute host. 
- 
  
    
      #instance  ⇒ Object 
    
    
  
  
  
  
    
      readonly
    
    
  
  
  
  
  
  
    Returns the value of attribute instance. 
- 
  
    
      #local_mode  ⇒ Object 
    
    
  
  
  
  
    
      readonly
    
    
  
  
  
  
  
  
    Returns the value of attribute local_mode. 
- 
  
    
      #password  ⇒ Object 
    
    
  
  
  
  
    
      readonly
    
    
  
  
  
  
  
  
    Returns the value of attribute password. 
- 
  
    
      #port  ⇒ Object 
    
    
  
  
  
  
    
      readonly
    
    
  
  
  
  
  
  
    Returns the value of attribute port. 
- 
  
    
      #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 
    
    
  
  
  
  
  
  
  
  
  
    rubocop:disable Metrics/PerceivedComplexity. 
- #to_s ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ MssqlSession
Returns a new instance of MssqlSession.
| 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | # File 'lib/resources/mssql_session.rb', line 33 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 @local_mode = opts[:local_mode] unless local_mode? @host = opts[:host] || 'localhost' if opts.key?(:port) @port = opts[:port] else @port = '1433' end end @instance = opts[:instance] @db_name = opts[:db_name] # check if sqlcmd is available raise Inspec::Exceptions::ResourceSkipped, 'sqlcmd is missing' unless inspec.command('sqlcmd').exist? # check that database is reachable raise Inspec::Exceptions::ResourceSkipped, "Can't connect to the MS SQL Server." unless test_connection end | 
Instance Attribute Details
#db_name ⇒ Object (readonly)
Returns the value of attribute db_name.
| 32 33 34 | # File 'lib/resources/mssql_session.rb', line 32 def db_name @db_name end | 
#host ⇒ Object (readonly)
Returns the value of attribute host.
| 32 33 34 | # File 'lib/resources/mssql_session.rb', line 32 def host @host end | 
#instance ⇒ Object (readonly)
Returns the value of attribute instance.
| 32 33 34 | # File 'lib/resources/mssql_session.rb', line 32 def instance @instance end | 
#local_mode ⇒ Object (readonly)
Returns the value of attribute local_mode.
| 32 33 34 | # File 'lib/resources/mssql_session.rb', line 32 def local_mode @local_mode end | 
#password ⇒ Object (readonly)
Returns the value of attribute password.
| 32 33 34 | # File 'lib/resources/mssql_session.rb', line 32 def password @password end | 
#port ⇒ Object (readonly)
Returns the value of attribute port.
| 32 33 34 | # File 'lib/resources/mssql_session.rb', line 32 def port @port end | 
#user ⇒ Object (readonly)
Returns the value of attribute user.
| 32 33 34 | # File 'lib/resources/mssql_session.rb', line 32 def user @user end | 
Instance Method Details
#query(q) ⇒ Object
rubocop:disable Metrics/PerceivedComplexity
| 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | # File 'lib/resources/mssql_session.rb', line 57 def query(q) # rubocop:disable Metrics/PerceivedComplexity 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? cmd_string += " -d '#{@db_name}'" unless @db_name.nil? unless local_mode? if @port.nil? cmd_string += " -S '#{@host}" else cmd_string += " -S '#{@host},#{@port}" end if @instance.nil? cmd_string += "'" else cmd_string += "\\#{@instance}'" end end cmd = inspec.command(cmd_string) out = cmd.stdout + "\n" + cmd.stderr if cmd.exit_status != 0 || out =~ /Sqlcmd: Error/ raise Inspec::Exceptions::ResourceFailed, "Could not execute the sql query #{out}" else DatabaseHelper::SQLQueryResult.new(cmd, parse_csv_result(cmd)) end end | 
#to_s ⇒ Object
| 84 85 86 | # File 'lib/resources/mssql_session.rb', line 84 def to_s 'MSSQL session' end |