Project

General

Profile

Download (4.02 KB) Statistics
| Branch: | Tag: | Revision:

runcible / lib / runcible / extensions / consumer_group.rb @ a5858c9b

1
module Runcible
2
  module Extensions
3
    class ConsumerGroup < Runcible::Resources::ConsumerGroup
4
      # Add consumers by ID to a consumer group
5
      #
6
      # @param  [String]                id            the consumer group ID
7
      # @param  [Array]                 consumer_ids  array of consumer IDs to add to the group
8
      # @return [RestClient::Response]                list of consumer IDs
9
      def add_consumers_by_id(id, consumer_ids)
10
        associate(id, make_consumer_criteria(consumer_ids))
11
      end
12

    
13
      # Remove consumers by ID from a consumer group
14
      #
15
      # @param  [String]                id            the consumer group ID
16
      # @param  [Array]                 consumer_ids  array of consumer IDs to remove from the group
17
      # @return [RestClient::Response]                list of consumer IDs
18
      def remove_consumers_by_id(id, consumer_ids)
19
        unassociate(id, make_consumer_criteria(consumer_ids))
20
      end
21

    
22
      # Generates consumer criteria query
23
      #
24
      # @param  [Array] consumer_ids  array of consumer IDs
25
      # @return [Hash]                the formatted query for consumers
26
      def make_consumer_criteria(consumer_ids)
27
        {:criteria =>
28
              {:filters =>
29
                {:id => {'$in' => consumer_ids}}
30
              }
31
        }
32
      end
33

    
34
      # Install content to a consumer group
35
      #
36
      # @param  [String]               id       the consumer group ID
37
      # @param  [String]               type_id  the type of content to install (e.g. rpm, errata)
38
      # @param  [Array]                units    array of units to install
39
      # @param  [Hash]                 options  to pass to content install
40
      # @return [RestClient::Response]          task representing the install operation
41
      def install_content(id, type_id, units, options = {})
42
        install_units(id, generate_content(type_id, units), options)
43
      end
44

    
45
      # Update content on a consumer group
46
      #
47
      # @param  [String]               id       the consumer group ID
48
      # @param  [String]               type_id  the type of content to update (e.g. rpm, errata)
49
      # @param  [Array]                units    array of units to update
50
      # @param  [Hash]                 options  to pass to content update
51
      # @return [RestClient::Response]          task representing the update operation
52
      def update_content(id, type_id, units, options = {})
53
        update_units(id, generate_content(type_id, units, options), options)
54
      end
55

    
56
      # Uninstall content from a consumer group
57
      #
58
      # @param  [String]               id       the consumer group ID
59
      # @param  [String]               type_id  the type of content to uninstall (e.g. rpm, errata)
60
      # @param  [Array]                units    array of units to uninstall
61
      # @return [RestClient::Response]          task representing the uninstall operation
62
      def uninstall_content(id, type_id, units)
63
        uninstall_units(id, generate_content(type_id, units))
64
      end
65

    
66
      # Generate the content units used by other functions
67
      #
68
      # @param  [String]  type_id the type of content (e.g. rpm, errata)
69
      # @param  [Array]   units   array of units
70
      # @param  [Hash]    options contains options which may impact the format of the content (e.g :all => true)
71
      # @return [Array]           array of formatted content units
72
      def generate_content(type_id, units, options = {})
73
        content = []
74

    
75
        case type_id
76
        when 'rpm', 'package_group'
77
          unit_key = :name
78
        when 'erratum'
79
          unit_key = :id
80
        else
81
          unit_key = :id
82
        end
83

    
84
        if options[:all]
85
          content_unit = {}
86
          content_unit[:type_id] = type_id
87
          content_unit[:unit_key] = {}
88
          content.push(content_unit)
89
        else
90
          units.each do |unit|
91
            content_unit = {}
92
            content_unit[:type_id] = type_id
93
            content_unit[:unit_key] = { unit_key => unit }
94
            content.push(content_unit)
95
          end
96
        end
97
        content
98
      end
99
    end
100
  end
101
end