1
|
|
2
|
|
3
|
|
4
|
|
5
|
|
6
|
|
7
|
|
8
|
|
9
|
|
10
|
|
11
|
|
12
|
|
13
|
|
14
|
|
15
|
module HammerCLICsv
|
16
|
class CsvCommand
|
17
|
class ContentViewFiltersCommand < BaseCommand
|
18
|
command_name 'content-view-filters'
|
19
|
desc 'import or export content-view-filters'
|
20
|
|
21
|
CONTENTVIEW = 'Content View'
|
22
|
ORGANIZATION = 'Organization'
|
23
|
TYPE = 'Type'
|
24
|
DESCRIPTION = 'Description'
|
25
|
REPOSITORIES = 'Repositories'
|
26
|
RULES = 'Rules'
|
27
|
|
28
|
def export
|
29
|
|
30
|
end
|
31
|
|
32
|
def import
|
33
|
@existing_filters = {}
|
34
|
|
35
|
thread_import do |line|
|
36
|
create_filters_from_csv(line)
|
37
|
end
|
38
|
end
|
39
|
|
40
|
def create_filters_from_csv(line)
|
41
|
@existing_filters[line[ORGANIZATION]] ||= {}
|
42
|
if !@existing_filters[line[ORGANIZATION]][line[CONTENTVIEW]]
|
43
|
@existing_filters[line[ORGANIZATION]][line[CONTENTVIEW]] ||= {}
|
44
|
@api.resource(:content_view_filters)\
|
45
|
.call(:index, {
|
46
|
'per_page' => 999999,
|
47
|
'content_view_id' => katello_contentview(line[ORGANIZATION], :name => line[CONTENTVIEW])
|
48
|
})['results'].each do |filter|
|
49
|
@existing_filters[line[ORGANIZATION]][line[CONTENTVIEW]][filter['name']] = filter['id'] if filter
|
50
|
end
|
51
|
end
|
52
|
|
53
|
repository_ids = collect_column(line[REPOSITORIES]) do |repository|
|
54
|
katello_repository(line[ORGANIZATION], :name => repository)
|
55
|
end
|
56
|
|
57
|
line[COUNT].to_i.times do |number|
|
58
|
name = namify(line[NAME], number)
|
59
|
|
60
|
filter_id = @existing_filters[line[ORGANIZATION]][line[CONTENTVIEW]][name]
|
61
|
if !filter_id
|
62
|
print "Creating filter '#{name}' for content view filter '#{line[CONTENTVIEW]}'..." if option_verbose?
|
63
|
filter_id = @api.resource(:content_view_filters)\
|
64
|
.call(:create, {
|
65
|
'content_view_id' => katello_contentview(line[ORGANIZATION], :name => line[CONTENTVIEW]),
|
66
|
'name' => name,
|
67
|
'description' => line[DESCRIPTION],
|
68
|
'type' => filter_type(line[TYPE]),
|
69
|
'inclusion' => filter_inclusion?(line[TYPE]),
|
70
|
'repository_ids' => repository_ids
|
71
|
})['id']
|
72
|
@existing_filters[line[ORGANIZATION]][name] = filter_id
|
73
|
else
|
74
|
print "Updating filter '#{name}' for content view filter '#{line[CONTENTVIEW]}'..." if option_verbose?
|
75
|
@api.resource(:content_view_filters)\
|
76
|
.call(:update, {
|
77
|
'id' => filter_id,
|
78
|
'description' => line[DESCRIPTION],
|
79
|
'type' => filter_type(line[TYPE]),
|
80
|
'inclusion' => filter_inclusion?(line[TYPE]),
|
81
|
'repository_ids' => repository_ids
|
82
|
})
|
83
|
end
|
84
|
|
85
|
@existing_rules ||= {}
|
86
|
@existing_rules[line[ORGANIZATION]] ||= {}
|
87
|
@existing_rules[line[ORGANIZATION]][line[CONTENTVIEW]] ||= {}
|
88
|
@api.resource(:content_view_filter_rules)\
|
89
|
.call(:index, {
|
90
|
'per_page' => 999999,
|
91
|
'content_view_filter_id' => filter_id
|
92
|
})['results'].each do |rule|
|
93
|
@existing_rules[line[ORGANIZATION]][line[CONTENTVIEW]][rule['name']] = rule
|
94
|
end
|
95
|
|
96
|
collect_column(line[RULES]) do |rule|
|
97
|
name, type, version = rule.split('|')
|
98
|
params = {
|
99
|
'content_view_filter_id' => filter_id,
|
100
|
'name' => name
|
101
|
}
|
102
|
if type == '='
|
103
|
params['type'] = 'equal',
|
104
|
params['version'] = version
|
105
|
elsif type == '<'
|
106
|
params['type'] = 'less',
|
107
|
params['max_version'] = version
|
108
|
elsif type == '>'
|
109
|
params['type'] = 'greater',
|
110
|
params['min_version'] = version
|
111
|
elsif type == '-'
|
112
|
params['type'] = 'range',
|
113
|
min_version, max_version = version.split(',')
|
114
|
params['min_version'] = min_version
|
115
|
params['max_version'] = max_version
|
116
|
else
|
117
|
raise "Unknown type '#{type}' from '#{line[RULES]}'"
|
118
|
end
|
119
|
|
120
|
rule = @existing_rules[line[ORGANIZATION]][line[CONTENTVIEW]][name]
|
121
|
if !rule
|
122
|
print "creating rule '#{rule}'..." if option_verbose?
|
123
|
rule = @api.resource(:content_view_filter_rules).call(:create, params)
|
124
|
@existing_rules[line[ORGANIZATION]][line[CONTENTVIEW]][rule['name']] = rule
|
125
|
else
|
126
|
print "updating rule '#{rule}'..." if option_verbose?
|
127
|
params['id'] = rule['id']
|
128
|
@api.resource(:content_view_filter_rules).call(:update, params)
|
129
|
end
|
130
|
end
|
131
|
|
132
|
puts 'done' if option_verbose?
|
133
|
end
|
134
|
|
135
|
rescue RuntimeError => e
|
136
|
raise "#{e}\n #{line}"
|
137
|
end
|
138
|
|
139
|
private
|
140
|
|
141
|
def filter_type(type)
|
142
|
if type.split[1] == 'RPM'
|
143
|
'rpm'
|
144
|
else
|
145
|
'unknown'
|
146
|
end
|
147
|
end
|
148
|
|
149
|
def filter_inclusion?(type)
|
150
|
if type.split[0] == 'Include'
|
151
|
true
|
152
|
else
|
153
|
false
|
154
|
end
|
155
|
end
|
156
|
end
|
157
|
end
|
158
|
end
|