hammer_cli_csv 0.0.6 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -25,21 +25,21 @@ module HammerCLICsv
25
25
  def export
26
26
  CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => false}) do |csv|
27
27
  csv << [NAME, COUNT, LABEL, ORGANIZATION, REPOSITORY, REPOSITORY_TYPE, REPOSITORY_URL]
28
- @api.resource(:organizations)\
29
- .call(:index, {
30
- :per_page => 999999
31
- })['results'].each do |organization|
32
- @api.resource(:products)\
33
- .call(:index, {
34
- 'per_page' => 999999,
35
- 'enabled' => true,
36
- 'organization_id' => foreman_organization(:name => organization['name'])
37
- })['results'].each do |product|
38
- product['repositories'].each do |repository|
39
- repository_type = repository['product_type'] == 'custom' ? 'Custom' : 'Red Hat'
40
- repository_type += " #{repository['content_type'].capitalize}"
41
- csv << [product['name'], 1, product['label'], organization['name'],
42
- repository['name'], repository_type, repository['url']]
28
+ @api.resource(:organizations).call(:index, {
29
+ :per_page => 999999
30
+ })['results'].each do |organization|
31
+ @api.resource(:products).call(:index, {
32
+ 'per_page' => 999999,
33
+ 'enabled' => true,
34
+ 'organization_id' => organization['id']
35
+ })['results'].each do |product|
36
+ unless product['provider']['name'] == 'Red Hat'
37
+ product['repositories'].each do |repository|
38
+ repository_type = repository['product_type'] == 'custom' ? 'Custom' : 'Red Hat'
39
+ repository_type += " #{repository['content_type'].capitalize}"
40
+ csv << [product['name'], 1, product['label'], organization['name'],
41
+ repository['name'], repository_type, repository['url']]
42
+ end
43
43
  end
44
44
  end
45
45
  end
@@ -58,22 +58,20 @@ module HammerCLICsv
58
58
  def create_products_from_csv(line)
59
59
  if !@existing_products[line[ORGANIZATION]]
60
60
  @existing_products[line[ORGANIZATION]] = {}
61
- @api.resource(:products)\
62
- .call(:index, {
63
- 'per_page' => 999999,
64
- 'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
65
- 'enabled' => true
66
- })['results'].each do |product|
61
+ @api.resource(:products).call(:index, {
62
+ 'per_page' => 999999,
63
+ 'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
64
+ 'enabled' => true
65
+ })['results'].each do |product|
67
66
  @existing_products[line[ORGANIZATION]][product['name']] = product['id']
68
67
 
69
- @api.resource(:repositories)\
70
- .call(:index, {
71
- 'page_size' => 999999, 'paged' => true,
72
- 'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
73
- 'product_id' => product['id'],
74
- 'enabled' => true,
75
- 'library' => true
76
- })['results'].each do |repository|
68
+ @api.resource(:repositories).call(:index, {
69
+ 'page_size' => 999999, 'paged' => true,
70
+ 'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
71
+ 'product_id' => product['id'],
72
+ 'enabled' => true,
73
+ 'library' => true
74
+ })['results'].each do |repository|
77
75
  @existing_repositories[line[ORGANIZATION] + product['name']] ||= {}
78
76
  @existing_repositories[line[ORGANIZATION] + product['name']][repository['label']] = repository['id']
79
77
  end
@@ -89,11 +87,10 @@ module HammerCLICsv
89
87
  raise "Red Hat product '#{name}' does not exist in '#{line[ORGANIZATION]}'"
90
88
  end
91
89
 
92
- product_id = @api.resource(:products)\
93
- .call(:create, {
94
- 'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
95
- 'name' => name
96
- })['id']
90
+ product_id = @api.resource(:products).call(:create, {
91
+ 'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
92
+ 'name' => name
93
+ })['id']
97
94
  @existing_products[line[ORGANIZATION]][name] = product_id
98
95
  else
99
96
  # Nothing to update for products
@@ -105,13 +102,12 @@ module HammerCLICsv
105
102
  repository_name = namify(line[REPOSITORY], number)
106
103
 
107
104
  if !@existing_repositories[line[ORGANIZATION] + name][repository_name]
108
- @api.resource(:repositories)\
109
- .call(:index, {
110
- 'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
111
- 'library' => true,
112
- 'all' => false,
113
- 'product_id' => product_id
114
- })['results'].each do |repository|
105
+ @api.resource(:repositories).call(:index, {
106
+ 'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
107
+ 'library' => true,
108
+ 'all' => false,
109
+ 'product_id' => product_id
110
+ })['results'].each do |repository|
115
111
  @existing_repositories[line[ORGANIZATION] + name][repository['name']] = repository
116
112
  end
117
113
  end
@@ -121,15 +117,14 @@ module HammerCLICsv
121
117
  raise "Red Hat product '#{name}' does not have repository '#{repository_name}'" if line[REPOSITORY_TYPE] =~ /Red Hat/
122
118
 
123
119
  print "Creating repository '#{repository_name}' in product '#{name}'..." if option_verbose?
124
- repository = @api.resource(:repositories)\
125
- .call(:create, {
126
- 'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
127
- 'name' => repository_name,
128
- 'label' => labelize(repository_name),
129
- 'product_id' => product_id,
130
- 'url' => line[REPOSITORY_URL],
131
- 'content_type' => content_type(line[REPOSITORY_TYPE])
132
- })
120
+ repository = @api.resource(:repositories).call(:create, {
121
+ 'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
122
+ 'name' => repository_name,
123
+ 'label' => labelize(repository_name),
124
+ 'product_id' => product_id,
125
+ 'url' => line[REPOSITORY_URL],
126
+ 'content_type' => content_type(line[REPOSITORY_TYPE])
127
+ })
133
128
  @existing_repositories[line[ORGANIZATION] + name][line[LABEL]] = repository
134
129
  puts 'done' if option_verbose?
135
130
  end
@@ -138,11 +133,7 @@ module HammerCLICsv
138
133
  if repository['sync_state'] == 'finished'
139
134
  puts 'already done' if option_verbose?
140
135
  else
141
- if line[REPOSITORY_TYPE] =~ /Red Hat/
142
- print 'skipping Red Hat repo sync... so slow!... '
143
- else
144
- sync_repository(line, repository)
145
- end
136
+ sync_repository(line, repository)
146
137
  print "done\n" if option_verbose?
147
138
  end
148
139
  end
@@ -23,10 +23,9 @@ module HammerCLICsv
23
23
  def export
24
24
  CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv|
25
25
  csv << [NAME, COUNT, ORGANIZATIONS, LOCATIONS, KIND, TEMPLATE]
26
- @api.resource(:config_templates)\
27
- .call(:index, {
28
- :per_page => 999999
29
- })['results'].each do |template_id|
26
+ @api.resource(:config_templates).call(:index, {
27
+ :per_page => 999999
28
+ })['results'].each do |template_id|
30
29
  template = @api.resource(:config_templates).call(:show, {:id => template_id['id']})
31
30
  name = template['name']
32
31
  count = 1
@@ -42,10 +41,9 @@ module HammerCLICsv
42
41
 
43
42
  def import
44
43
  @existing = {}
45
- @api.resource(:config_templates)\
46
- .call(:index, {
47
- :per_page => 999999
48
- })['results'].each do |template|
44
+ @api.resource(:config_templates).call(:index, {
45
+ :per_page => 999999
46
+ })['results'].each do |template|
49
47
  @existing[template['name']] = template['id'] if template
50
48
  end
51
49
 
@@ -66,27 +64,25 @@ module HammerCLICsv
66
64
  name = namify(line[NAME], number)
67
65
  if !@existing.include? name
68
66
  print "Creating provisioning template '#{name}'..." if option_verbose?
69
- template_id = @api.resource(:config_templates)\
70
- .call(:create, {
71
- 'name' => name,
72
- 'snippet' => line[KIND] == 'snippet',
73
- 'template_kind_id' => line[KIND] == 'snippet' ? nil : foreman_template_kind(:name => line[KIND]),
74
- 'organization_ids' => organizations,
75
- 'location_ids' => locations,
76
- 'template' => line[TEMPLATE]
77
- })['id']
67
+ template_id = @api.resource(:config_templates).call(:create, {
68
+ 'name' => name,
69
+ 'snippet' => line[KIND] == 'snippet',
70
+ 'template_kind_id' => line[KIND] == 'snippet' ? nil : foreman_template_kind(:name => line[KIND]),
71
+ 'organization_ids' => organizations,
72
+ 'location_ids' => locations,
73
+ 'template' => line[TEMPLATE]
74
+ })['id']
78
75
  else
79
76
  print "Updating provisioning template '#{name}'..." if option_verbose?
80
- template_id = @api.resource(:config_templates)\
81
- .call(:update, {
82
- 'id' => @existing[name],
83
- 'name' => name,
84
- 'snippet' => line[KIND] == 'snippet',
85
- 'template_kind_id' => line[KIND] == 'snippet' ? nil : foreman_template_kind(:name => line[KIND]),
86
- 'organization_ids' => organizations,
87
- 'location_ids' => locations,
88
- 'template' => line[TEMPLATE]
89
- })['id']
77
+ template_id = @api.resource(:config_templates).call(:update, {
78
+ 'id' => @existing[name],
79
+ 'name' => name,
80
+ 'snippet' => line[KIND] == 'snippet',
81
+ 'template_kind_id' => line[KIND] == 'snippet' ? nil : foreman_template_kind(:name => line[KIND]),
82
+ 'organization_ids' => organizations,
83
+ 'location_ids' => locations,
84
+ 'template' => line[TEMPLATE]
85
+ })['id']
90
86
  end
91
87
  @existing[name] = template_id
92
88
 
@@ -94,43 +90,39 @@ module HammerCLICsv
94
90
  template_organizations ||= {}
95
91
  organizations.each do |organization_id|
96
92
  if template_organizations[organization_id].nil?
97
- template_organizations[organization_id] = @api.resource(:organizations)\
98
- .call(:show, {
99
- 'id' => organization_id
100
- })['config_templates'].collect do |template|
93
+ template_organizations[organization_id] = @api.resource(:organizations).call(:show, {
94
+ 'id' => organization_id
95
+ })['config_templates'].collect do |template|
101
96
  template['id']
102
97
  end
103
98
  end
104
99
  if !template_organizations[organization_id].include? template_id
105
100
  template_organizations[organization_id] += [template_id]
106
- @api.resource(:organizations)\
107
- .call(:update, {
108
- 'id' => organization_id,
109
- 'organization' => {
110
- 'config_template_ids' => template_organizations[organization_id]
111
- }
112
- })
101
+ @api.resource(:organizations).call(:update, {
102
+ 'id' => organization_id,
103
+ 'organization' => {
104
+ 'config_template_ids' => template_organizations[organization_id]
105
+ }
106
+ })
113
107
  end
114
108
  end
115
109
  template_locations ||= {}
116
110
  locations.each do |location_id|
117
111
  if template_locations[location_id].nil?
118
- template_locations[location_id] = @api.resource(:locations)\
119
- .call(:show, {
120
- 'id' => location_id
121
- })['config_templates'].collect do |template|
112
+ template_locations[location_id] = @api.resource(:locations).call(:show, {
113
+ 'id' => location_id
114
+ })['config_templates'].collect do |template|
122
115
  template['id']
123
116
  end
124
117
  end
125
118
  if !template_locations[location_id].include? template_id
126
119
  template_locations[location_id] += [template_id]
127
- @api.resource(:locations)\
128
- .call(:update, {
129
- 'id' => location_id,
130
- 'location' => {
131
- 'config_template_ids' => template_locations[location_id]
132
- }
133
- })
120
+ @api.resource(:locations).call(:update, {
121
+ 'id' => location_id,
122
+ 'location' => {
123
+ 'config_template_ids' => template_locations[location_id]
124
+ }
125
+ })
134
126
  end
135
127
  end
136
128
 
@@ -56,20 +56,17 @@ module HammerCLICsv
56
56
  CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => false}) do |csv|
57
57
  csv << [NAME, COUNT, ORGANIZATION, ENVIRONMENT, CONTENTVIEW, SYSTEMGROUPS, VIRTUAL, HOST,
58
58
  OPERATINGSYSTEM, ARCHITECTURE, SOCKETS, RAM, CORES, SLA, PRODUCTS, SUBSCRIPTIONS]
59
- @api.resource(:organizations)\
60
- .call(:index, {
61
- :per_page => 999999
62
- })['results'].each do |organization|
63
- @api.resource(:systems)\
64
- .call(:index, {
65
- 'per_page' => 999999,
66
- 'organization_id' => organization['id']
67
- })['results'].each do |system|
68
- system = @api.resource(:systems)\
69
- .call(:show, {
70
- 'id' => system['uuid'],
71
- 'fields' => 'full'
72
- })
59
+ @api.resource(:organizations).call(:index, {
60
+ :per_page => 999999
61
+ })['results'].each do |organization|
62
+ @api.resource(:systems).call(:index, {
63
+ 'per_page' => 999999,
64
+ 'organization_id' => organization['id']
65
+ })['results'].each do |system|
66
+ system = @api.resource(:systems).call(:show, {
67
+ 'id' => system['uuid'],
68
+ 'fields' => 'full'
69
+ })
73
70
 
74
71
  name = system['name']
75
72
  count = 1
@@ -98,10 +95,9 @@ module HammerCLICsv
98
95
  end
99
96
  products.delete!("\n")
100
97
  subscriptions = CSV.generate do |column|
101
- column << @api.resource(:subscriptions)\
102
- .call(:index, {
103
- 'system_id' => system['uuid']
104
- })['results'].collect do |subscription|
98
+ column << @api.resource(:subscriptions).call(:index, {
99
+ 'system_id' => system['uuid']
100
+ })['results'].collect do |subscription|
105
101
  "#{subscription['product_id']}|#{subscription['product_name']}"
106
102
  end
107
103
  end
@@ -123,11 +119,10 @@ module HammerCLICsv
123
119
 
124
120
  print 'Updating host and guest associations...' if option_verbose?
125
121
  @host_guests.each do |host_id, guest_ids|
126
- @api.resource(:systems)\
127
- .call(:update, {
128
- 'id' => host_id,
129
- 'guest_ids' => guest_ids
130
- })
122
+ @api.resource(:systems).call(:update, {
123
+ 'id' => host_id,
124
+ 'guest_ids' => guest_ids
125
+ })
131
126
  end
132
127
  puts 'done' if option_verbose?
133
128
  end
@@ -135,11 +130,10 @@ module HammerCLICsv
135
130
  def create_systems_from_csv(line)
136
131
  if !@existing[line[ORGANIZATION]]
137
132
  @existing[line[ORGANIZATION]] = {}
138
- @api.resource(:systems)\
139
- .call(:index, {
140
- 'organization_id' => line[ORGANIZATION],
141
- 'per_page' => 999999
142
- })['results'].each do |system|
133
+ @api.resource(:systems).call(:index, {
134
+ 'organization_id' => line[ORGANIZATION],
135
+ 'per_page' => 999999
136
+ })['results'].each do |system|
143
137
  @existing[line[ORGANIZATION]][system['name']] = system['uuid'] if system
144
138
  end
145
139
  end
@@ -154,29 +148,27 @@ module HammerCLICsv
154
148
 
155
149
  if !@existing[line[ORGANIZATION]].include? name
156
150
  print "Creating system '#{name}'..." if option_verbose?
157
- system_id = @api.resource(:systems)\
158
- .call(:create, {
159
- 'name' => name,
160
- 'organization_id' => line[ORGANIZATION],
161
- 'environment_id' => lifecycle_environment(line[ORGANIZATION], :name => line[ENVIRONMENT]),
162
- 'content_view_id' => lifecycle_contentview(line[ORGANIZATION], :name => line[CONTENTVIEW]),
163
- 'facts' => facts(line),
164
- 'installed_products' => products(line),
165
- 'type' => 'system'
166
- })['uuid']
151
+ system_id = @api.resource(:systems).call(:create, {
152
+ 'name' => name,
153
+ 'organization_id' => line[ORGANIZATION],
154
+ 'environment_id' => lifecycle_environment(line[ORGANIZATION], :name => line[ENVIRONMENT]),
155
+ 'content_view_id' => lifecycle_contentview(line[ORGANIZATION], :name => line[CONTENTVIEW]),
156
+ 'facts' => facts(line),
157
+ 'installed_products' => products(line),
158
+ 'type' => 'system'
159
+ })['uuid']
167
160
  @existing[line[ORGANIZATION]][name] = system_id
168
161
  else
169
162
  print "Updating system '#{name}'..." if option_verbose?
170
163
  puts line
171
- system_id = @api.resource(:systems)\
172
- .call(:update, {
173
- 'id' => @existing[line[ORGANIZATION]][name],
174
- 'name' => name,
175
- 'environment_id' => katello_environment(line[ORGANIZATION], :name => line[ENVIRONMENT]),
176
- 'content_view_id' => katello_contentview(line[ORGANIZATION], :name => line[CONTENTVIEW]),
177
- 'facts' => facts(line),
178
- 'installed_products' => products(line)
179
- })['uuid']
164
+ system_id = @api.resource(:systems).call(:update, {
165
+ 'id' => @existing[line[ORGANIZATION]][name],
166
+ 'name' => name,
167
+ 'environment_id' => katello_environment(line[ORGANIZATION], :name => line[ENVIRONMENT]),
168
+ 'content_view_id' => katello_contentview(line[ORGANIZATION], :name => line[CONTENTVIEW]),
169
+ 'facts' => facts(line),
170
+ 'installed_products' => products(line)
171
+ })['uuid']
180
172
  end
181
173
 
182
174
  if line[VIRTUAL] == 'Yes' && line[HOST]
@@ -212,11 +204,10 @@ module HammerCLICsv
212
204
 
213
205
  def set_host_collections(system_id, line)
214
206
  CSV.parse_line(line[SYSTEMGROUPS]).each do |hostcollection_name|
215
- @api.resource(:hostcollections)\
216
- .call(:add_systems, {
217
- 'id' => katello_hostcollection(line[ORGANIZATION], :name => hostcollection_name),
218
- 'system_ids' => [system_id]
219
- })
207
+ @api.resource(:hostcollections).call(:add_systems, {
208
+ 'id' => katello_hostcollection(line[ORGANIZATION], :name => hostcollection_name),
209
+ 'system_ids' => [system_id]
210
+ })
220
211
  end
221
212
  end
222
213
 
@@ -27,10 +27,9 @@ module HammerCLICsv
27
27
  def export
28
28
  CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => false}) do |csv|
29
29
  csv << [NAME, COUNT]
30
- @api.resource(:reports)\
31
- .call(:index, {
32
- 'per_page' => 999999
33
- })['results'].each do |report|
30
+ @api.resource(:reports).call(:index, {
31
+ 'per_page' => 999999
32
+ })['results'].each do |report|
34
33
  csv << [report['host_name'], 1, report['metrics'].to_json]
35
34
  end
36
35
  end
@@ -40,10 +39,9 @@ module HammerCLICsv
40
39
 
41
40
  def import
42
41
  @existing_reports = {}
43
- @api.resource(:reports)\
44
- .call(:index, {
45
- 'per_page' => 999999
46
- })['results'].each do |report|
42
+ @api.resource(:reports).call(:index, {
43
+ 'per_page' => 999999
44
+ })['results'].each do |report|
47
45
  @existing_reports[report['name']] = report['id']
48
46
  end
49
47
 
@@ -59,28 +57,26 @@ module HammerCLICsv
59
57
  if !@existing_reports[name]
60
58
  print "Creating report '#{name}'..." if option_verbose?
61
59
  reported_at = line[TIME] || Time.now
62
- report = @api.resource(:reports)\
63
- .call(:create, {
64
- 'host' => name,
65
- 'reported_at' => reported_at,
66
- 'status' => {
67
- 'applied' => line[APPLIED],
68
- 'restarted' => line[RESTARTED],
69
- 'failed' => line[FAILED],
70
- 'failed_restarts' => line[FAILED_RESTARTS],
71
- 'skipped' => line[SKIPPED],
72
- 'pending' => line[PENDING]
73
- },
74
- 'metrics' => JSON.parse(line[METRICS]),
75
- 'logs' => []
76
- })
60
+ report = @api.resource(:reports).call(:create, {
61
+ 'host' => name,
62
+ 'reported_at' => reported_at,
63
+ 'status' => {
64
+ 'applied' => line[APPLIED],
65
+ 'restarted' => line[RESTARTED],
66
+ 'failed' => line[FAILED],
67
+ 'failed_restarts' => line[FAILED_RESTARTS],
68
+ 'skipped' => line[SKIPPED],
69
+ 'pending' => line[PENDING]
70
+ },
71
+ 'metrics' => JSON.parse(line[METRICS]),
72
+ 'logs' => []
73
+ })
77
74
  @existing_reports[name] = report['id']
78
75
  else
79
76
  print "Updating report '#{name}'..." if option_verbose?
80
- @api.resource(:reports)\
81
- .call(:update, {
82
- 'id' => @existing_reports[name]
83
- })
77
+ @api.resource(:reports).call(:update, {
78
+ 'id' => @existing_reports[name]
79
+ })
84
80
  end
85
81
 
86
82
  puts 'done' if option_verbose?