Class: HammerCLICsv::CsvCommand::ContentViewFiltersCommand

Inherits:
BaseCommand
  • Object
show all
Defined in:
lib/hammer_cli_csv/content_view_filters.rb

Constant Summary collapse

CONTENTVIEW =
'Content View'
ORGANIZATION =
'Organization'
DESCRIPTION =
'Description'
TYPE =
'Type'
REPOSITORIES =
'Repositories'
RULES =
'Rules'

Constants inherited from BaseCommand

BaseCommand::COUNT, BaseCommand::NAME

Instance Method Summary collapse

Methods inherited from BaseCommand

#associate_locations, #associate_organizations, #build_os_name, #check_server_status, #collect_column, #execute, #export_column, #foreman_architecture, #foreman_domain, #foreman_environment, #foreman_filter, #foreman_location, #foreman_operatingsystem, #foreman_organization, #foreman_partitiontable, #foreman_permission, #foreman_role, #foreman_template_kind, #hammer, #hammer_context, #katello_contentview, #katello_contentviewversion, #katello_hostcollection, #katello_repository, #katello_subscription, #labelize, #lifecycle_environment, #namify, #pluralize, #split_os_name, #thread_import

Instance Method Details

#create_filters_from_csv(line) ⇒ Object



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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/hammer_cli_csv/content_view_filters.rb', line 86

def create_filters_from_csv(line)
  @existing_filters[line[ORGANIZATION]] ||= {}
  if !@existing_filters[line[ORGANIZATION]][line[CONTENTVIEW]]
    @existing_filters[line[ORGANIZATION]][line[CONTENTVIEW]] ||= {}
    @api.resource(:content_view_filters).call(:index, {
        'per_page' => 999999,
        'content_view_id' => katello_contentview(line[ORGANIZATION], :name => line[CONTENTVIEW])
    })['results'].each do |filter|
      @existing_filters[line[ORGANIZATION]][line[CONTENTVIEW]][filter['name']] = filter['id'] if filter
    end
  end

  repository_ids = collect_column(line[REPOSITORIES]) do |repository|
    katello_repository(line[ORGANIZATION], :name => repository)
  end

  line[COUNT].to_i.times do |number|
    name = namify(line[NAME], number)

    filter_id = @existing_filters[line[ORGANIZATION]][line[CONTENTVIEW]][name]
    if !filter_id
      print "Creating filter '#{name}' for content view filter '#{line[CONTENTVIEW]}'..." if option_verbose?
      filter_id = @api.resource(:content_view_filters).call(:create, {
          'content_view_id' => katello_contentview(line[ORGANIZATION], :name => line[CONTENTVIEW]),
          'name' => name,
          'description' => line[DESCRIPTION],
          'type' => filter_type(line[TYPE]),
          'inclusion' => filter_inclusion?(line[TYPE]),
          'repository_ids' => repository_ids
      })['id']
      @existing_filters[line[ORGANIZATION]][name] = filter_id
    else
      print "Updating filter '#{name}' for content view filter '#{line[CONTENTVIEW]}'..." if option_verbose?
      @api.resource(:content_view_filters).call(:update, {
          'id' => filter_id,
          'description' => line[DESCRIPTION],
          'type' => filter_type(line[TYPE]),
          'inclusion' => filter_inclusion?(line[TYPE]),
          'repository_ids' => repository_ids
      })
    end

    @existing_rules ||= {}
    @existing_rules[line[ORGANIZATION]] ||= {}
    @existing_rules[line[ORGANIZATION]][line[CONTENTVIEW]] ||= {}
    @api.resource(:content_view_filter_rules).call(:index, {
        'per_page' => 999999,
        'content_view_filter_id' => filter_id
    })['results'].each do |rule|
      @existing_rules[line[ORGANIZATION]][line[CONTENTVIEW]][rule['name']] = rule
    end

    collect_column(line[RULES]) do |rule|
      name, type, version = rule.split('|')
      params = {
        'content_view_filter_id' => filter_id,
        'name' => name
      }
      if type == '='
        params['type'] = 'equal',
                         params['version'] = version
      elsif type == '<'
        params['type'] = 'less',
                         params['max_version'] = version
      elsif type == '>'
        params['type'] = 'greater',
                         params['min_version'] = version
      elsif type == '-'
        params['type'] = 'range',
                         min_version, max_version = version.split(',')
        params['min_version'] = min_version
        params['max_version'] = max_version
      else
        raise "Unknown type '#{type}' from '#{line[RULES]}'"
      end

      rule = @existing_rules[line[ORGANIZATION]][line[CONTENTVIEW]][name]
      if !rule
        print "." if option_verbose?
        rule = @api.resource(:content_view_filter_rules).call(:create, params)
        @existing_rules[line[ORGANIZATION]][line[CONTENTVIEW]][rule['name']] = rule
      else
        print "." if option_verbose?
        params['id'] = rule['id']
        @api.resource(:content_view_filter_rules).call(:update, params)
      end
    end

    puts 'done' if option_verbose?
  end

rescue RuntimeError => e
  raise "#{e}\n       #{line}"
end

#exportObject



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
# File 'lib/hammer_cli_csv/content_view_filters.rb', line 28

def export
  CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => false}) do |csv|
    csv << [NAME, COUNT, CONTENTVIEW, ORGANIZATION, TYPE, DESCRIPTION, REPOSITORIES, RULES]
    @api.resource(:organizations).call(:index, {
        :per_page => 999999
    })['results'].each do |organization|
      composite_contentviews = []
      @api.resource(:content_views).call(:index, {
          'per_page' => 999999,
          'organization_id' => organization['id'],
          'nondefault' => true
      })['results'].each do |contentview|
        @api.resource(:content_view_filters).call(:index, {
            'content_view_id' => contentview['id']
        })['results'].collect do |filter|
          filter_type = "#{filter['inclusion'] == true ? 'Include' : 'Exclude'} #{filter['type']}"

          case filter['type']
          when /rpm/
            rules = CSV.generate do |column|
              column << filter['rules'].collect do |rule|
                if rule['version']
                  "#{rule['name']}|=|#{rule['version']}"
                elsif rule['min_version'] && rule['max_version']
                  "#{rule['name']}|-|#{rule['min_version']},#{rule['max_version']}"
                elsif rule['min_version']
                  "#{rule['name']}|>|#{rule['min_version']}"
                elsif rule['max_version']
                  "#{rule['name']}|<|#{rule['max_version']}"
                end
              end
            end
            rules.delete!("\n")
          when /erratum/
          when /package_group/
          else
            raise "Unknown filter rule type '#{filter['type']}'"
          end
          #puts "#{filter['type']} -> #{rule}"

          name = contentview['name']
          repositories = export_column(filter, 'repositories', 'name')
          csv << [name, 1, contentview['name'], organization['name'], filter_type, filter['description'],
                  repositories, rules]
        end
      end
    end
  end
end

#importObject



78
79
80
81
82
83
84
# File 'lib/hammer_cli_csv/content_view_filters.rb', line 78

def import
  @existing_filters = {}

  thread_import do |line|
    create_filters_from_csv(line)
  end
end