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

#apipie_check_param, #associate_locations, #associate_organizations, #authenticate_request, #build_os_name, #check_server_status, #collect_column, #count, #execute, #export_column, #foreman_architecture, #foreman_container, #foreman_domain, #foreman_environment, #foreman_filter, #foreman_host, #foreman_hostgroup, #foreman_location, #foreman_medium, #foreman_operatingsystem, #foreman_organization, #foreman_partitiontable, #foreman_permission, #foreman_provisioning_template, #foreman_role, #foreman_smart_proxy, #foreman_template_kind, #hammer, #hammer_context, #help, help_columns, #katello_contentview, #katello_contentviewversion, #katello_hostcollection, #katello_product, #katello_repository, #labelize, #lifecycle_environment, #namify, #pluralize, #split_os_name, #supported?, supported?, #thread_import

Methods included from Utils::Config

#api_connection

Instance Method Details

#create_filters_from_csv(line) ⇒ Object

rubocop:disable CyclomaticComplexity



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

def create_filters_from_csv(line)
  return if option_organization && line[ORGANIZATION] != option_organization

  @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

  count(line[COUNT]).times do |number|
    filter_name = namify(line[NAME], number)

    filter_id = @existing_filters[line[ORGANIZATION]][line[CONTENTVIEW]][filter_name]
    filter_type = import_filter_type(line[TYPE])
    if !filter_id
      print "Creating filter '#{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' => filter_name,
          'description' => line[DESCRIPTION],
          'type' => filter_type,
          'inclusion' => filter_inclusion?(line[TYPE]),
          'repository_ids' => repository_ids
      })['id']
      @existing_filters[line[ORGANIZATION]][line[CONTENTVIEW]][filter_name] = filter_id
    else
      print "Updating filter '#{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,
          'inclusion' => filter_inclusion?(line[TYPE]),
          'repository_ids' => repository_ids
      })
    end

    # drop existing rules
    @api.resource(:content_view_filter_rules).call(:index, {
        'per_page' => 999999,
        'content_view_filter_id' => filter_id
    })['results'].each do |rule|
      @api.resource(:content_view_filter_rules).call(:destroy, {
          'content_view_filter_id' => filter_id,
          'id' =>  rule['id']
      })
    end

    collect_column(line[RULES]) do |rule|
      name, type, version = rule.split('|')
      params = {
        'content_view_filter_id' => filter_id,
        'name' => name
      }
      if type == 'all'
        # empty
      elsif type == '='
        params['version'] = version
      elsif type == '<'
        params['max_version'] = version
      elsif type == '>'
        params['min_version'] = version
      elsif type == '-'
        min_version, max_version = version.split(',')
        params['min_version'] = min_version
        params['max_version'] = max_version
      elsif filter_type == 'package_group'
        params['uuid'] = name # TODO: this is not right
      else
        raise "Unknown type '#{type}' from '#{line[RULES]}'"
      end

      print "." if option_verbose?
      @api.resource(:content_view_filter_rules).call(:create, params)
    end

    puts 'done' if option_verbose?
  end

end

#export(csv) ⇒ Object



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

def export(csv)
  csv << [NAME, CONTENTVIEW, ORGANIZATION, TYPE, DESCRIPTION, REPOSITORIES, RULES]
  @api.resource(:organizations).call(:index, {
      :per_page => 999999
  })['results'].each do |organization|
    next if option_organization && organization['name'] != option_organization

    @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'} #{export_filter_type(filter['type'])}"

        rules = nil
        case filter['type']
        when /rpm/
          rules = export_rpm_rules(filter)
        when /erratum/
          rules = export_erratum_rules(filter)
        when /package_group/
          rules = export_package_group_rules(filter)
        else
          raise "Unknown filter rule type '#{filter['type']}'"
        end

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

#importObject



55
56
57
58
59
60
61
# File 'lib/hammer_cli_csv/content_view_filters.rb', line 55

def import
  @existing_filters = {}

  thread_import do |line|
    create_filters_from_csv(line)
  end
end