Class: ChefDK::Policyfile::PolicyfileLocationSpecification

Inherits:
Object
  • Object
show all
Defined in:
lib/chef-dk/policyfile/policyfile_location_specification.rb

Overview

A PolicyfileLocationSpecification specifies where a policyfile lock is to be fetched from. Using this information, it provides a fetcher that is capable loading the policyfile lock.

Constant Summary collapse

LOCATION_TYPES =
%i{path remote server git}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, source_options, storage_config, chef_config = nil) ⇒ PolicyfileLocationSpecification

Initialize a location spec

Parameters:

  • name (String)

    the name of the policyfile

  • source_options (Hash)

    options describing where the policyfile lock lives

  • storage_config (Policyfile::StorageConfig)
  • chef_config (Chef::Config) (defaults to: nil)

    chef config that will be used when communication with a chef server is required



50
51
52
53
54
55
56
# File 'lib/chef-dk/policyfile/policyfile_location_specification.rb', line 50

def initialize(name, source_options, storage_config, chef_config = nil)
  @name = name
  @source_options = source_options
  @storage_config = storage_config
  @ui = nil
  @chef_config = chef_config
end

Instance Attribute Details

#chef_configObject (readonly)

Returns the value of attribute chef_config.



38
39
40
# File 'lib/chef-dk/policyfile/policyfile_location_specification.rb', line 38

def chef_config
  @chef_config
end

#nameString (readonly)

The name of the policyfile

Returns:

  • (String)

    the current value of name



33
34
35
# File 'lib/chef-dk/policyfile/policyfile_location_specification.rb', line 33

def name
  @name
end

#source_optionsHash (readonly)

Options describing how to get the policyfile lock

Returns:

  • (Hash)

    the current value of source_options



33
34
35
# File 'lib/chef-dk/policyfile/policyfile_location_specification.rb', line 33

def source_options
  @source_options
end

#storage_configObject (readonly)

Returns the value of attribute storage_config.



37
38
39
# File 'lib/chef-dk/policyfile/policyfile_location_specification.rb', line 37

def storage_config
  @storage_config
end

#uiObject (readonly)

Returns the value of attribute ui.



39
40
41
# File 'lib/chef-dk/policyfile/policyfile_location_specification.rb', line 39

def ui
  @ui
end

Instance Method Details

#apply_locked_source_options(options_from_lock) ⇒ Object

Applies source options from a lock file. This is used to make sure that the same policyfile lock is loaded that was locked

Parameters:

  • options_from_lock (Hash)

    The source options loaded from a policyfile lock



123
124
125
# File 'lib/chef-dk/policyfile/policyfile_location_specification.rb', line 123

def apply_locked_source_options(options_from_lock)
  fetcher.apply_locked_source_options(options_from_lock)
end

#errorsArray<String>

Check the options provided when craeting this class for errors

Returns:

  • (Array<String>)

    A list of errors found



91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/chef-dk/policyfile/policyfile_location_specification.rb', line 91

def errors
  error_messages = []

  if LOCATION_TYPES.all? { |l| source_options[l].nil? }
    error_messages << "include_policy must use one of the following sources: #{LOCATION_TYPES.join(", ")}"
  else
    unless fetcher.nil?
      error_messages += fetcher.errors
    end
  end

  error_messages
end

#fetcherObject

Returns A policyfile lock fetcher compatible with the given source_options.

Returns:

  • A policyfile lock fetcher compatible with the given source_options



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/chef-dk/policyfile/policyfile_location_specification.rb', line 64

def fetcher
  @fetcher ||= begin
                 if source_options[:path] && !source_options[:git]
                   Policyfile::LocalLockFetcher.new(name, source_options, storage_config)
                 elsif source_options[:remote]
                   Policyfile::RemoteLockFetcher.new(name, source_options)
                 elsif source_options[:server]
                   Policyfile::ChefServerLockFetcher.new(name, source_options, chef_config)
                 elsif source_options[:git]
                   Policyfile::GitLockFetcher.new(name, source_options, storage_config)
                 else
                   raise ChefDK::InvalidPolicyfileLocation.new(
                     "Invalid policyfile lock location type. The supported locations are: #{LOCATION_TYPES.join(", ")}"
                   )
                 end
               end
end

#policyfile_lockPolicyfileLock

Fetches and loads the policyfile lock

Returns:



108
109
110
111
112
# File 'lib/chef-dk/policyfile/policyfile_location_specification.rb', line 108

def policyfile_lock
  @policyfile_lock ||= begin
                         PolicyfileLock.new(storage_config, ui: ui).build_from_lock_data(fetcher.lock_data)
                       end
end

#revision_idObject

Returns The revision id from the fetched lock.

Returns:

  • The revision id from the fetched lock



59
60
61
# File 'lib/chef-dk/policyfile/policyfile_location_specification.rb', line 59

def revision_id
  fetcher.lock_data["revision_id"]
end

#source_options_for_lockHash

Returns The source_options that describe how to fetch this exact lock again.

Returns:

  • (Hash)

    The source_options that describe how to fetch this exact lock again



115
116
117
# File 'lib/chef-dk/policyfile/policyfile_location_specification.rb', line 115

def source_options_for_lock
  fetcher.source_options_for_lock
end

#valid?True, False

Returns:

  • (True)

    if there were no errors with the provided source_options

  • (False)

    if there were errors with the provided source_options



84
85
86
# File 'lib/chef-dk/policyfile/policyfile_location_specification.rb', line 84

def valid?
  errors.empty?
end