Class: Chef::Compliance::Profile

Inherits:
Object
  • Object
show all
Defined in:
lib/chef/compliance/profile.rb

Constant Summary collapse

HIDDEN_IVARS =
[ :@events ].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(events, data, path, cookbook_name) ⇒ Profile

Returns a new instance of Profile.



39
40
41
42
43
44
45
46
47
# File 'lib/chef/compliance/profile.rb', line 39

def initialize(events, data, path, cookbook_name)
  @events = events
  @data = data
  @path = path
  @cookbook_name = cookbook_name
  @pathname = File.basename(File.dirname(path))
  disable!
  validate!
end

Instance Attribute Details

#cookbook_nameString (readonly)

Returns The name of the cookbook that the profile is in.

Returns:

  • (String)

    The name of the cookbook that the profile is in



28
29
30
# File 'lib/chef/compliance/profile.rb', line 28

def cookbook_name
  @cookbook_name
end

#dataObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



37
38
39
# File 'lib/chef/compliance/profile.rb', line 37

def data
  @data
end

#enabledBoolean

Returns if the profile has been enabled.

Returns:

  • (Boolean)

    if the profile has been enabled



22
23
24
# File 'lib/chef/compliance/profile.rb', line 22

def enabled
  @enabled
end

#eventsChef::EventDispatch::Dispatcher (readonly)

Returns Event dispatcher for this run.

Returns:



34
35
36
# File 'lib/chef/compliance/profile.rb', line 34

def events
  @events
end

#pathString (readonly)

Returns The full path on the host to the profile inspec.yml.

Returns:

  • (String)

    The full path on the host to the profile inspec.yml



25
26
27
# File 'lib/chef/compliance/profile.rb', line 25

def path
  @path
end

#pathnameString

Returns the pathname in the cookbook.

Returns:

  • (String)

    the pathname in the cookbook



31
32
33
# File 'lib/chef/compliance/profile.rb', line 31

def pathname
  @pathname
end

Class Method Details

.from_file(events, filename, cookbook_name) ⇒ Object

Parameters:

  • filename (String)

    full path to the inspec.yml file in the cookbook

  • cookbook_name (String)

    cookbook that the profile is in



117
118
119
# File 'lib/chef/compliance/profile.rb', line 117

def self.from_file(events, filename, cookbook_name)
  from_yaml(events, IO.read(filename), filename, cookbook_name)
end

.from_hash(events, hash, path, cookbook_name) ⇒ Object

Helper to construct a profile object from a hash. Since the path and cookbook_name are required this is probably not externally useful.



103
104
105
# File 'lib/chef/compliance/profile.rb', line 103

def self.from_hash(events, hash, path, cookbook_name)
  new(events, hash, path, cookbook_name)
end

.from_yaml(events, string, path, cookbook_name) ⇒ Object

Helper to construct a profile object from a yaml string. Since the path and cookbook_name are required this is probably not externally useful.



110
111
112
# File 'lib/chef/compliance/profile.rb', line 110

def self.from_yaml(events, string, path, cookbook_name)
  from_hash(events, YAML.safe_load(string, permitted_classes: [Date]), path, cookbook_name)
end

Instance Method Details

#disable!Object

Set the profile as being disabled



79
80
81
# File 'lib/chef/compliance/profile.rb', line 79

def disable!
  @enabled = false
end

#enable!Object

Set the profile to being enabled



72
73
74
75
# File 'lib/chef/compliance/profile.rb', line 72

def enable!
  events.compliance_profile_enabled(self)
  @enabled = true
end

#enabled?Boolean

Returns if the profile has been enabled.

Returns:

  • (Boolean)

    if the profile has been enabled



66
67
68
# File 'lib/chef/compliance/profile.rb', line 66

def enabled?
  !!@enabled
end

#inspec_dataObject

Render the profile in a way that it can be consumed by inspec



85
86
87
# File 'lib/chef/compliance/profile.rb', line 85

def inspec_data
  { name: name, path: File.dirname(path) }
end

#inspectObject

Omit the event object from error output



93
94
95
96
97
98
# File 'lib/chef/compliance/profile.rb', line 93

def inspect
  ivar_string = (instance_variables.map(&:to_sym) - HIDDEN_IVARS).map do |ivar|
    "#{ivar}=#{instance_variable_get(ivar).inspect}"
  end.join(", ")
  "#<#{self.class}:#{object_id} #{ivar_string}>"
end

#nameString

Returns name of the inspec profile from parsing the inspec.yml.

Returns:

  • (String)

    name of the inspec profile from parsing the inspec.yml



50
51
52
# File 'lib/chef/compliance/profile.rb', line 50

def name
  @data["name"]
end

#validate!Object

Raises if the inspec profile is not valid.



61
62
63
# File 'lib/chef/compliance/profile.rb', line 61

def validate!
  raise "Inspec profile at #{path} has no name" unless name
end

#versionString

Returns version of the inspec profile from parsing the inspec.yml.

Returns:

  • (String)

    version of the inspec profile from parsing the inspec.yml



55
56
57
# File 'lib/chef/compliance/profile.rb', line 55

def version
  @data["version"]
end