Class: Inspec::Resources::TimeZone

Inherits:
Cmd
  • Object
show all
Defined in:
lib/inspec/resources/timezone.rb

Instance Attribute Summary

Attributes inherited from Cmd

#command

Instance Method Summary collapse

Methods inherited from Cmd

#exist?, #exit_status, #result, #stderr, #stdout

Constructor Details

#initializeTimeZone

Returns a new instance of TimeZone.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/inspec/resources/timezone.rb', line 18

def initialize
  @output = {}
  os = inspec.os
  cmd = if os.windows?
          inspec.command("Get-TimeZone")
        else
          inspec.command("timedatectl status | grep -i 'Time zone'")
        end
  if cmd.exit_status != 0
    raise Inspec::Exceptions::ResourceFailed, "Time Zone resource with error: #{cmd.stderr}"
  else
    if os.windows?
      splitted_output = cmd.stdout.strip.gsub(/\r/, "").split("\n").select { |out| (out.include? "Id") || (out.include? "DisplayName") || (out.include? "BaseUtcOffset") }
      @output["identifier"]  = split_and_fetch_last(splitted_output[1])
      @output["name"]        = split_and_fetch_last(splitted_output[0])
      @output["time_offset"] = split_and_fetch_last(splitted_output[2])
    else
      splitted_output = cmd.stdout.split(":")[-1]&.strip&.gsub(/[(),^]*/, "")&.split(" ") || []
      @output["identifier"]  = splitted_output[0]
      @output["name"]        = splitted_output[1]
      @output["time_offset"] = splitted_output[2]
    end
  end
end

Instance Method Details

#identifierObject



43
44
45
# File 'lib/inspec/resources/timezone.rb', line 43

def identifier
  @output["identifier"]
end

#nameObject



47
48
49
# File 'lib/inspec/resources/timezone.rb', line 47

def name
  @output["name"]
end

#resource_idObject



55
56
57
# File 'lib/inspec/resources/timezone.rb', line 55

def resource_id
  "timezone"
end

#time_offsetObject



51
52
53
# File 'lib/inspec/resources/timezone.rb', line 51

def time_offset
  @output["time_offset"]
end

#to_sObject



59
60
61
# File 'lib/inspec/resources/timezone.rb', line 59

def to_s
  "Time Zone resource"
end