summaryrefslogtreecommitdiff
path: root/lib/chef/run_list/run_list_item.rb
blob: 30b9bbe5628e0f3bebbb69fae65bd01d200bf08e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#
# Author:: Daniel DeLeo (<dan@chef.io>)
# Copyright:: Copyright 2010-2016, Chef Software Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

class Chef
  class RunList
    class RunListItem
      QUALIFIED_RECIPE             = /^recipe\[([^\]@]+)(@([0-9]+(\.[0-9]+){1,2}))?\]$/.freeze
      QUALIFIED_ROLE               = /^role\[([^\]]+)\]$/.freeze
      VERSIONED_UNQUALIFIED_RECIPE = /^([^@]+)(@([0-9]+(\.[0-9]+){1,2}))$/.freeze
      FALSE_FRIEND                 = /[\[\]]/.freeze

      attr_reader :name, :type, :version

      def initialize(item)
        @version = nil
        case item
        when Hash
          assert_hash_is_valid_run_list_item!(item)
          @type = (item["type"] || item[:type]).to_sym
          @name = item["name"] || item[:name]
          if item.key?("version") || item.key?(:version)
            @version = item["version"] || item[:version]
          end
        when String
          if match = QUALIFIED_RECIPE.match(item)
            # recipe[recipe_name]
            # recipe[recipe_name@1.0.0]
            @type = :recipe
            @name = match[1]
            @version = match[3] if match[3]
          elsif match = QUALIFIED_ROLE.match(item)
            # role[role_name]
            @type = :role
            @name = match[1]
          elsif match = VERSIONED_UNQUALIFIED_RECIPE.match(item)
            # recipe_name@1.0.0
            @type = :recipe
            @name = match[1]
            @version = match[3] if match[3]
          elsif match = FALSE_FRIEND.match(item)
            # Recipe[recipe_name]
            # roles[role_name]
            name = match[1]
            raise ArgumentError, "Unable to create #{self.class} from #{item.class}:#{item.inspect}: must be recipe[#{name}] or role[#{name}]"

          else
            # recipe_name
            @type = :recipe
            @name = item
          end
        else
          raise ArgumentError, "Unable to create #{self.class} from #{item.class}:#{item.inspect}: must be a Hash or String"
        end
      end

      def to_s
        "#{@type}[#{@name}#{@version ? "@#{@version}" : ""}]"
      end

      def role?
        @type == :role
      end

      def recipe?
        @type == :recipe
      end

      def ==(other)
        if other.is_a?(String)
          to_s == other.to_s
        else
          other.respond_to?(:type) && other.respond_to?(:name) && other.respond_to?(:version) && other.type == @type && other.name == @name && other.version == @version
        end
      end

      def assert_hash_is_valid_run_list_item!(item)
        unless (item.key?("type") || item.key?(:type)) && (item.key?("name") || item.key?(:name))
          raise ArgumentError, "Initializing a #{self.class} from a hash requires that it have a 'type' and 'name' key"
        end
      end

    end
  end
end