Project

General

Profile

Revision c4dbc7a3

Added by Thomas McKay over 7 years ago

updates to tests and functionality

+ added export containers
+ export content-view-filters erratum and rpm types
+ 'hammer csv import' now works from --dir https://somewhere
+ 'hammer csv import' tests and fixes

View differences:

lib/hammer_cli_csv/content_view_filters.rb
22 22
          })['results'].each do |organization|
23 23
            next if option_organization && organization['name'] != option_organization
24 24

  
25
            composite_contentviews = []
26 25
            @api.resource(:content_views).call(:index, {
27 26
                'per_page' => 999999,
28 27
                'organization_id' => organization['id'],
......
31 30
              @api.resource(:content_view_filters).call(:index, {
32 31
                  'content_view_id' => contentview['id']
33 32
              })['results'].collect do |filter|
34
                filter_type = "#{filter['inclusion'] == true ? 'Include' : 'Exclude'} #{filter['type']}"
33
                filter_type = "#{filter['inclusion'] == true ? 'Include' : 'Exclude'} #{export_filter_type(filter['type'])}"
35 34

  
35
                rules = nil
36 36
                case filter['type']
37 37
                when /rpm/
38
                  rules = CSV.generate do |column|
39
                    column << filter['rules'].collect do |rule|
40
                      if rule['version']
41
                        "#{rule['name']}|=|#{rule['version']}"
42
                      elsif rule['min_version'] && rule['max_version']
43
                        "#{rule['name']}|-|#{rule['min_version']},#{rule['max_version']}"
44
                      elsif rule['min_version']
45
                        "#{rule['name']}|>|#{rule['min_version']}"
46
                      elsif rule['max_version']
47
                        "#{rule['name']}|<|#{rule['max_version']}"
48
                      end
49
                    end
50
                  end
51
                  rules.delete!("\n")
38
                  rules = export_rpm_rules(filter)
52 39
                when /erratum/
53
                  rules = CSV.generate do |column|
54
                    rule = filter['rules'][0]
55
                    conditions = []
56
                    conditions << "start = #{DateTime.parse(rule['start_date']).strftime('%F')}" if rule['start_date']
57
                    conditions << "end = #{DateTime.parse(rule['end_date']).strftime('%F')}" if rule['end_date']
58
                    conditions += rule['types']
59
                    column << conditions
60
                  end
61
                  rules.delete!("\n")
40
                  rules = export_erratum_rules(filter)
62 41
                when /package_group/
42
                  rules = export_package_group_rules(filter)
63 43
                else
64 44
                  raise "Unknown filter rule type '#{filter['type']}'"
65 45
                end
66 46

  
67
                name = contentview['name']
47
                name = filter['name']
68 48
                repositories = export_column(filter, 'repositories', 'name')
69 49
                csv << [name, 1, contentview['name'], organization['name'], filter_type, filter['description'],
70 50
                        repositories, rules]
......
82 62
        end
83 63
      end
84 64

  
65
      # rubocop:disable CyclomaticComplexity
85 66
      def create_filters_from_csv(line)
86 67
        return if option_organization && line[ORGANIZATION] != option_organization
87 68

  
......
101 82
        end
102 83

  
103 84
        line[COUNT].to_i.times do |number|
104
          name = namify(line[NAME], number)
85
          filter_name = namify(line[NAME], number)
105 86

  
106
          filter_id = @existing_filters[line[ORGANIZATION]][line[CONTENTVIEW]][name]
87
          filter_id = @existing_filters[line[ORGANIZATION]][line[CONTENTVIEW]][filter_name]
88
          filter_type = import_filter_type(line[TYPE])
107 89
          if !filter_id
108
            print "Creating filter '#{name}' for content view filter '#{line[CONTENTVIEW]}'..." if option_verbose?
90
            print "Creating filter '#{filter_name}' for content view filter '#{line[CONTENTVIEW]}'..." if option_verbose?
109 91
            filter_id = @api.resource(:content_view_filters).call(:create, {
110 92
                'content_view_id' => katello_contentview(line[ORGANIZATION], :name => line[CONTENTVIEW]),
111
                'name' => name,
93
                'name' => filter_name,
112 94
                'description' => line[DESCRIPTION],
113
                'type' => filter_type(line[TYPE]),
95
                'type' => filter_type,
114 96
                'inclusion' => filter_inclusion?(line[TYPE]),
115 97
                'repository_ids' => repository_ids
116 98
            })['id']
117
            @existing_filters[line[ORGANIZATION]][name] = filter_id
99
            @existing_filters[line[ORGANIZATION]][filter_name] = filter_id
118 100
          else
119
            print "Updating filter '#{name}' for content view filter '#{line[CONTENTVIEW]}'..." if option_verbose?
101
            print "Updating filter '#{filter_name}' for content view filter '#{line[CONTENTVIEW]}'..." if option_verbose?
120 102
            @api.resource(:content_view_filters).call(:update, {
121 103
                'id' => filter_id,
122 104
                'description' => line[DESCRIPTION],
123
                'type' => filter_type(line[TYPE]),
105
                'type' => filter_type,
124 106
                'inclusion' => filter_inclusion?(line[TYPE]),
125 107
                'repository_ids' => repository_ids
126 108
            })
127 109
          end
128 110

  
129
          @existing_rules ||= {}
130
          @existing_rules[line[ORGANIZATION]] ||= {}
131
          @existing_rules[line[ORGANIZATION]][line[CONTENTVIEW]] ||= {}
111
          existing_rules = {}
132 112
          @api.resource(:content_view_filter_rules).call(:index, {
133 113
              'per_page' => 999999,
134 114
              'content_view_filter_id' => filter_id
135 115
          })['results'].each do |rule|
136
            @existing_rules[line[ORGANIZATION]][line[CONTENTVIEW]][rule['name']] = rule
116
            existing_rules[rule['name']] = rule
137 117
          end
138 118

  
139 119
          collect_column(line[RULES]) do |rule|
......
142 122
              'content_view_filter_id' => filter_id,
143 123
              'name' => name
144 124
            }
145
            if type == '='
146
              params['type'] = 'equal',
147
                               params['version'] = version
125
            if type == 'all'
126
              # empty
127
            elsif type == '='
128
              params['version'] = version
148 129
            elsif type == '<'
149
              params['type'] = 'less',
150
                               params['max_version'] = version
130
              params['max_version'] = version
151 131
            elsif type == '>'
152
              params['type'] = 'greater',
153
                               params['min_version'] = version
132
              params['min_version'] = version
154 133
            elsif type == '-'
155
              params['type'] = 'range',
156
                               min_version, max_version = version.split(',')
134
              min_version, max_version = version.split(',')
157 135
              params['min_version'] = min_version
158 136
              params['max_version'] = max_version
137
            elsif filter_type == 'package_group'
138
              params['uuid'] = name # TODO: this is not right
159 139
            else
160 140
              raise "Unknown type '#{type}' from '#{line[RULES]}'"
161 141
            end
162 142

  
163
            rule = @existing_rules[line[ORGANIZATION]][line[CONTENTVIEW]][name]
143
            rule = existing_rules[name]
164 144
            if !rule
165 145
              print "." if option_verbose?
166 146
              rule = @api.resource(:content_view_filter_rules).call(:create, params)
167
              @existing_rules[line[ORGANIZATION]][line[CONTENTVIEW]][rule['name']] = rule
147
              existing_rules[rule['name']] = rule
168 148
            else
169 149
              print "." if option_verbose?
170 150
              params['id'] = rule['id']
......
181 161

  
182 162
      private
183 163

  
184
      def filter_type(type)
185
        if type.split[1] == 'RPM'
164
      def import_filter_type(type)
165
        case type.split[1..-1].join(' ')
166
        when /packages/i
186 167
          'rpm'
168
        when /package groups/i
169
          'package_group'
170
        else
171
          'unknown'
172
        end
173
      end
174

  
175
      def export_filter_type(type)
176
        case type.split[1]
177
        when /rpm/i
178
          'Packages'
179
        when /package_group/i
180
          'Package Groups'
187 181
        else
188 182
          'unknown'
189 183
        end
......
196 190
          false
197 191
        end
198 192
      end
193

  
194
      def export_rpm_rules(filter)
195
        rules = CSV.generate do |column|
196
          column << filter['rules'].collect do |rule|
197
            if rule['version']
198
              "#{rule['name']}|=|#{rule['version']}"
199
            elsif rule['min_version'] && rule['max_version']
200
              "#{rule['name']}|-|#{rule['min_version']},#{rule['max_version']}"
201
            elsif rule['min_version']
202
              "#{rule['name']}|>|#{rule['min_version']}"
203
            elsif rule['max_version']
204
              "#{rule['name']}|<|#{rule['max_version']}"
205
            else
206
              "#{rule['name']}|all"
207
            end
208
          end
209
        end
210
        rules.delete!("\n")
211
      end
212

  
213
      def export_erratum_rules(filter)
214
        rules = CSV.generate do |column|
215
          rule = filter['rules'][0]
216
          conditions = []
217
          conditions << "start = #{DateTime.parse(rule['start_date']).strftime('%F')}" if rule['start_date']
218
          conditions << "end = #{DateTime.parse(rule['end_date']).strftime('%F')}" if rule['end_date']
219
          conditions << "types = #{rule['types'].join(',')}" if rule['types']
220
          conditions << "errata = #{rule['errata_id']}" if rule['errata_id']
221
          column << conditions
222
        end
223
        rules.delete!("\n")
224
      end
225

  
226
      def export_package_group_rules(filter)
227
        rules = CSV.generate do |column|
228
          column << filter['rules'].collect do |rule|
229
            rule['name']
230
          end
231
        end
232
        rules.delete!("\n")
233
      end
199 234
    end
200 235
  end
201 236
end

Also available in: Unified diff