aboutsummaryrefslogtreecommitdiff
path: root/lib/leap_cli/config/object_list.rb
blob: 9001834bd2be8384c6701fb1535cda560247ab3a (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
module LeapCli
  module Config
    #
    # A list of Config::Object instances (internally stored as a hash)
    #
    class ObjectList < Hash

      def initialize(config=nil)
        if config
          self.add(config['name'], config)
        end
      end

      #
      # If the key is a string, the Config::Object it references is returned.
      #
      # If the key is a hash, we treat it as a condition and filter all the Config::Objects using the condition.
      # A new ObjectList is returned.
      #
      # Examples:
      #
      # nodes['vpn1']
      #   node named 'vpn1'
      #
      # nodes[:public_dns => true]
      #   all nodes with public dns
      #
      # nodes[:services => 'openvpn', :services => 'tor']
      #   nodes with openvpn OR tor service
      #
      # nodes[:services => 'openvpn'][:tags => 'production']
      #   nodes with openvpn AND are production
      #
      def [](key)
        if key.is_a? Hash
          results = Config::ObjectList.new
          key.each do |field, match_value|
            field = field.is_a?(Symbol) ? field.to_s : field
            match_value = match_value.is_a?(Symbol) ? match_value.to_s : match_value
            each do |name, config|
              value = config[field]
              if !value.nil?
                if value.is_a? Array
                  if value.include?(match_value)
                    results[name] = config
                  end
                else
                  if value == match_value
                    results[name] = config
                  end
                end
              end
            end
          end
          results
        else
          super key.to_s
        end
      end

      def each_node(&block)
        self.keys.sort.each do |node_name|
          yield self[node_name]
        end
      end

      # def <<(object)
      #   if object.is_a? Config::ObjectList
      #     self.merge!(object)
      #   elsif object['name']
      #     self[object['name']] = object
      #   else
      #     raise ArgumentError.new('argument must be a Config::Object or a Config::ObjectList')
      #   end
      # end

      def add(name, object)
        self[name] = object
      end

      #
      # converts the hash of configs into an array of hashes, with ONLY the specified fields
      #
      def fields(*fields)
        result = []
        keys.sort.each do |name|
          result << self[name].pick(*fields)
        end
        result
      end

      #
      # like fields(), but returns an array of values instead of an array of hashes.
      #
      def field(field)
        field = field.to_s
        result = []
        keys.sort.each do |name|
          result << self[name].get(field)
        end
        result
      end

      #
      # applies inherit_from! to all objects.
      #
      def inherit_from!(object_list)
        object_list.each do |name, object|
          if self[name]
            self[name].inherit_from!(object)
          else
            self[name] = object.dup
          end
        end
      end

    end
  end
end