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'
TYPE =
'Type'
DESCRIPTION =
'Description'
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_hostcollection, #katello_repository, #katello_subscription, #labelize, #lifecycle_environment, #namify, #pluralize, #split_os_name, #thread_import

Instance Method Details

#create_filters_from_csv(line) ⇒ Object



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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/hammer_cli_csv/content_view_filters.rb', line 40

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 "creating rule '#{rule}'..." if option_verbose?
        rule = @api.resource(:content_view_filter_rules).call(:create, params)
        @existing_rules[line[ORGANIZATION]][line[CONTENTVIEW]][rule['name']] = rule
      else
        print "updating rule '#{rule}'..." 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
# File 'lib/hammer_cli_csv/content_view_filters.rb', line 28

def export
  # TODO
end

#importObject



32
33
34
35
36
37
38
# File 'lib/hammer_cli_csv/content_view_filters.rb', line 32

def import
  @existing_filters = {}

  thread_import do |line|
    create_filters_from_csv(line)
  end
end