hammer_cli_csv 0.0.6 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- NWRiMTE4MzU5Y2ZmMTI3NDM2MGQ4OWZhNWM5M2RiODIyYmI4OWMyOQ==
4
+ ZTZmY2U4MTYyOGVjYjU0ZDNmMTg3ZjA4ODY4NDFlYjQzNWJmZDA0OQ==
5
5
  data.tar.gz: !binary |-
6
- ZDc0MTdjMjliZDk4NTMyODZhNmFkYjhjN2M1NmM2OGY4NTlkMDczNw==
6
+ YzNlNDUwZDIyZTk4ODA3MTUwOWRhNTY1NGJkNGVlN2U2N2U5MWY5Mg==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- YzIzNzhkYjE5OWJmZjQyNGRkZDJhNTExY2I0YWYxMWE5NWI2YWIxODA1MjIx
10
- ODkwNDNiYWM5NjczZjY3NTlmMzlhNTJkN2ExOTllNmQ3Y2MxNWY0OWVmYTE4
11
- ZTY3YWEwOGFkNmYzMjJlNTMyYjFiZjI0NGY2ZDk4YjA1NTQ3YzQ=
9
+ MThjNzUwYTg4NDJjOTNlZDQzZjUxMjQyNTJmMDVmM2FmOGI3ZjFlYmJmZTMw
10
+ MWYzZWI2ZjNlYWNmNTA0MjI4NmMyYjc0ZTIxMDFhOWJhMTU2ODBkOTRjNzZm
11
+ NDIzYzUzMmJmZTc3NDUwNTZkM2ZkZWZmNzJlZDhhZjgwMjNkN2Y=
12
12
  data.tar.gz: !binary |-
13
- ZWM4ODcwZTdmOGE2OTVkYTgyOWYxMzAxODI3ZmRjYTVjZmFmMGFjYzg4OWUw
14
- ZWI4YmMzYmRhNTg1MDI1OWVkOTcwMTk4YjVkM2Y5N2NkOWUzNWY1MTRjMzlk
15
- NDBmMmUxNGFlMTVjY2ViMDA5OTgxNTEyY2RkNzVhMzg1YjIzNDY=
13
+ ZjM0MWExNTUyOWU5MmY3NGUwM2YzYTVmZDVhZmNjNDg0MzUyNTdlYWE1ZjU1
14
+ NTc3NDU2YWFmOTllMjMwODVhOGIwOGQ3NTAyODQwNjA5NzgwYjNkMjYyMTll
15
+ MzU5OTlkZjJmOTRkZWJmMWQ4ZGNiMTEwMGQzNmZhNGIxNWI5OGM=
@@ -20,17 +20,16 @@ module HammerCLICsv
20
20
  LIMIT = 'Limit'
21
21
  ENVIRONMENT = 'Environment'
22
22
  CONTENTVIEW = 'Content View'
23
- SYSTEMGROUPS = 'System Groups'
23
+ HOSTCOLLECTIONS = 'System Groups'
24
24
  SUBSCRIPTIONS = 'Subscriptions'
25
25
 
26
26
  def export
27
27
  CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => false}) do |csv|
28
28
  csv << [NAME, COUNT, ORGANIZATION, DESCRIPTION, LIMIT, ENVIRONMENT, CONTENTVIEW,
29
- SYSTEMGROUPS, SUBSCRIPTIONS]
29
+ HOSTCOLLECTIONS, SUBSCRIPTIONS]
30
30
  if @server_status['release'] == 'Headpin'
31
31
  @headpin.get(:organizations).each do |organization|
32
32
  @headpin.get("organizations/#{organization['label']}/activation_keys").each do |activationkey|
33
- puts "Writing activation key '#{activationkey['name']}'" if option_verbose?
34
33
  name = namify(activationkey['name'])
35
34
  count = 1
36
35
  description = activationkey['description']
@@ -52,20 +51,17 @@ module HammerCLICsv
52
51
  end
53
52
  end
54
53
  else
55
- @api.resource(:organizations)\
56
- .call(:index, {
57
- :per_page => 999999
58
- })['results'].each do |organization|
59
- @api.resource(:activation_keys)\
60
- .call(:index, {
61
- 'per_page' => 999999,
62
- 'organization_id' => organization['id']
63
- })['results'].each do |activationkey|
64
- puts "Writing activation key '#{activationkey['name']}'" if option_verbose?
54
+ @api.resource(:organizations).call(:index, {
55
+ :per_page => 999999
56
+ })['results'].each do |organization|
57
+ @api.resource(:activation_keys).call(:index, {
58
+ 'per_page' => 999999,
59
+ 'organization_id' => organization['id']
60
+ })['results'].each do |activationkey|
65
61
  name = namify(activationkey['name'])
66
62
  count = 1
67
63
  description = activationkey['description']
68
- limit = activationkey['usage_limit'].to_i < 0 ? 'Unlimited' : sytemgroup['usage_limit']
64
+ limit = activationkey['usage_limit'].to_i < 0 ? 'Unlimited' : activationkey['usage_limit']
69
65
  environment = activationkey['environment']['label']
70
66
  contentview = activationkey['content_view']['name']
71
67
  hostcollections = export_column(activationkey, 'systemGroups', 'name')
@@ -97,11 +93,10 @@ module HammerCLICsv
97
93
  def create_activationkeys_from_csv(line)
98
94
  if !@existing[line[ORGANIZATION]]
99
95
  @existing[line[ORGANIZATION]] = {}
100
- @api.resource(:activation_keys)\
101
- .call(:index, {
102
- 'per_page' => 999999,
103
- 'organization_id' => foreman_organization(:name => line[ORGANIZATION])
104
- })['results'].each do |activationkey|
96
+ @api.resource(:activation_keys).call(:index, {
97
+ 'per_page' => 999999,
98
+ 'organization_id' => foreman_organization(:name => line[ORGANIZATION])
99
+ })['results'].each do |activationkey|
105
100
  @existing[line[ORGANIZATION]][activationkey['name']] = activationkey['id'] if activationkey
106
101
  end
107
102
  end
@@ -111,30 +106,30 @@ module HammerCLICsv
111
106
 
112
107
  if !@existing[line[ORGANIZATION]].include? name
113
108
  print "Creating activation key '#{name}'..." if option_verbose?
114
- activationkey = @api.resource(:activation_keys)\
115
- .call(:create, {
116
- 'name' => name,
117
- 'environment_id' => lifecycle_environment(line[ORGANIZATION],
118
- :name => line[ENVIRONMENT]),
119
- 'content_view_id' => katello_contentview(line[ORGANIZATION],
120
- :name => line[CONTENTVIEW]),
121
- 'description' => line[DESCRIPTION],
122
- 'usage_limit' => usage_limit(line[LIMIT])
123
- })
109
+ activationkey = @api.resource(:activation_keys).call(:create, {
110
+ 'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
111
+ 'name' => name,
112
+ 'environment_id' => lifecycle_environment(line[ORGANIZATION],
113
+ :name => line[ENVIRONMENT]),
114
+ 'content_view_id' => katello_contentview(line[ORGANIZATION],
115
+ :name => line[CONTENTVIEW]),
116
+ 'description' => line[DESCRIPTION],
117
+ 'usage_limit' => usage_limit(line[LIMIT])
118
+ })
124
119
  @existing[line[ORGANIZATION]][activationkey['name']] = activationkey['id']
125
120
  else
126
121
  print "Updating activation key '#{name}'..." if option_verbose?
127
- activationkey = @api.resource(:activation_keys)\
128
- .call(:update, {
129
- 'id' => @existing[line[ORGANIZATION]][name],
130
- 'name' => name,
131
- 'environment_id' => lifecycle_environment(line[ORGANIZATION],
132
- :name => line[ENVIRONMENT]),
133
- 'content_view_id' => katello_contentview(line[ORGANIZATION],
134
- :name => line[CONTENTVIEW]),
135
- 'description' => line[DESCRIPTION],
136
- 'usage_limit' => usage_limit(line[LIMIT])
137
- })
122
+ activationkey = @api.resource(:activation_keys).call(:update, {
123
+ 'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
124
+ 'id' => @existing[line[ORGANIZATION]][name],
125
+ 'name' => name,
126
+ 'environment_id' => lifecycle_environment(line[ORGANIZATION],
127
+ :name => line[ENVIRONMENT]),
128
+ 'content_view_id' => katello_contentview(line[ORGANIZATION],
129
+ :name => line[CONTENTVIEW]),
130
+ 'description' => line[DESCRIPTION],
131
+ 'usage_limit' => usage_limit(line[LIMIT])
132
+ })
138
133
 
139
134
  update_subscriptions(activationkey, line)
140
135
  update_groups(activationkey, line)
@@ -145,14 +140,13 @@ module HammerCLICsv
145
140
  end
146
141
 
147
142
  def update_groups(activationkey, line)
148
- if line[SYSTEMGROUPS] && line[SYSTEMGROUPS] != ''
143
+ if line[HOSTCOLLECTIONS] && line[HOSTCOLLECTIONS] != ''
149
144
  # TODO: note that existing system groups are not removed
150
- CSV.parse_line(line[SYSTEMGROUPS], {:skip_blanks => true}).each do |name|
151
- @api.resource(:host_collections)\
152
- .call(:add_activation_keys, {
153
- 'id' => katello_hostcollection(line[ORGANIZATION], :name => name),
154
- 'activation_key_ids' => [activationkey['id']]
155
- })
145
+ CSV.parse_line(line[HOSTCOLLECTIONS], {:skip_blanks => true}).each do |name|
146
+ @api.resource(:host_collections).call(:add_activation_keys, {
147
+ 'id' => katello_hostcollection(line[ORGANIZATION], :name => name),
148
+ 'activation_key_ids' => [activationkey['id']]
149
+ })
156
150
  end
157
151
  end
158
152
  end
@@ -160,31 +154,29 @@ module HammerCLICsv
160
154
  def update_subscriptions(activationkey, line)
161
155
  if line[SUBSCRIPTIONS] && line[SUBSCRIPTIONS] != ''
162
156
  subscriptions = CSV.parse_line(line[SUBSCRIPTIONS], {:skip_blanks => true}).collect do |subscription_details|
163
- (amount, name) = subscription_details.split('|')
157
+ (amount, sku, name) = subscription_details.split('|')
164
158
  {
165
159
  :id => katello_subscription(line[ORGANIZATION], :name => name),
166
- :quantity => amount
160
+ :quantity => (amount.nil? || amount == 'Automatic') ? 0 : amount
167
161
  }
168
162
  end
169
163
 
170
- # TODO: should there be a destroy_all similar to systems?
171
- @api.resource(:subscriptions)\
172
- .call(:index, {
173
- 'per_page' => 999999,
174
- 'activation_key_id' => activationkey['id']
175
- })['results'].each do |subscription|
176
- @api.resource(:subscriptions)\
177
- .call(:destroy, {
178
- 'id' => subscription['id'],
179
- 'activation_key_id' => activationkey['id']
180
- })
164
+ existing_subscriptions = @api.resource(:subscriptions).call(:index, {
165
+ 'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
166
+ 'per_page' => 999999,
167
+ 'activation_key_id' => activationkey['id']
168
+ })['results']
169
+ if existing_subscriptions.length > 0
170
+ @api.resource(:activation_keys).call(:remove_subscriptions, {
171
+ 'id' => activationkey['id'],
172
+ 'subscriptions' => existing_subscriptions
173
+ })
181
174
  end
182
175
 
183
- @api.resource(:subscriptions)\
184
- .call(:create, {
185
- 'activation_key_id' => activationkey['id'],
186
- 'subscriptions' => subscriptions
187
- })
176
+ @api.resource(:activation_keys).call(:add_subscriptions, {
177
+ 'id' => activationkey['id'],
178
+ 'subscriptions' => subscriptions
179
+ })
188
180
  end
189
181
  end
190
182
 
@@ -23,50 +23,37 @@ module HammerCLICsv
23
23
  option %w(--csv-export), :flag, 'Export current data instead of importing'
24
24
  option %w(--csv-file), 'FILE_NAME', 'CSV file (default to /dev/stdout with --csv-export, otherwise required)'
25
25
  option %w(--prefix), 'PREFIX', 'Prefix for all name columns'
26
- option %w(--server), 'SERVER', 'Server URL'
27
- option %w(-u --username), 'USERNAME', 'Username to access server'
28
- option %w(-p --password), 'PASSWORD', 'Password to access server'
29
26
 
30
27
  NAME = 'Name'
31
28
  COUNT = 'Count'
32
29
 
33
30
  def execute
34
- if !option_csv_file
35
- if option_csv_export?
36
- # rubocop:disable UselessAssignment
37
- option_csv_file = '/dev/stdout'
38
- else
39
- # rubocop:disable UselessAssignment
40
- option_csv_file = '/dev/stdin'
41
- end
42
- end
43
-
44
- server = option_server ||
31
+ @server = HammerCLI::Settings.settings[:_params][:host] ||
45
32
  HammerCLI::Settings.get(:csv, :host) ||
46
33
  HammerCLI::Settings.get(:katello, :host) ||
47
34
  HammerCLI::Settings.get(:foreman, :host)
48
- username = option_username ||
35
+ @username = HammerCLI::Settings.settings[:_params][:username] ||
49
36
  HammerCLI::Settings.get(:csv, :username) ||
50
37
  HammerCLI::Settings.get(:katello, :username) ||
51
38
  HammerCLI::Settings.get(:foreman, :username)
52
- password = option_password ||
39
+ @password = HammerCLI::Settings.settings[:_params][:password] ||
53
40
  HammerCLI::Settings.get(:csv, :password) ||
54
41
  HammerCLI::Settings.get(:katello, :password) ||
55
42
  HammerCLI::Settings.get(:foreman, :password)
56
43
 
57
- @server_status = check_server_status
44
+ @server_status = check_server_status(@server, @username, @password)
58
45
 
59
46
  if @server_status['release'] == 'Headpin'
60
47
  @headpin = HeadpinApi.new({
61
- :server => server,
62
- :username => username,
63
- :password => password
48
+ :server => @server,
49
+ :username => @username,
50
+ :password => @password
64
51
  })
65
52
  else
66
53
  @api = ApipieBindings::API.new({
67
- :uri => server,
68
- :username => username,
69
- :password => password,
54
+ :uri => @server,
55
+ :username => @username,
56
+ :password => @password,
70
57
  :api_version => 2
71
58
  })
72
59
  end
@@ -75,10 +62,7 @@ module HammerCLICsv
75
62
  HammerCLI::EX_OK
76
63
  end
77
64
 
78
- def check_server_status
79
- server = option_server || HammerCLI::Settings.get(:csv, :host)
80
- username = option_username || HammerCLI::Settings.get(:csv, :username)
81
- password = option_password || HammerCLI::Settings.get(:csv, :password)
65
+ def check_server_status(server, username, password)
82
66
  url = "#{server}/api/status"
83
67
  uri = URI(url)
84
68
  nethttp = Net::HTTP.new(uri.host, uri.port)
@@ -492,11 +476,10 @@ module HammerCLICsv
492
476
  return nil if options[:name].nil? || options[:name].empty?
493
477
  options[:id] = @lifecycle_environments[organization][options[:name]]
494
478
  if !options[:id]
495
- @api.resource(:lifecycle_environments)\
496
- .call(:index, {
497
- :per_page => 999999,
498
- 'organization_id' => foreman_organization(:name => organization)
499
- })['results'].each do |environment|
479
+ @api.resource(:lifecycle_environments).call(:index, {
480
+ :per_page => 999999,
481
+ 'organization_id' => foreman_organization(:name => organization)
482
+ })['results'].each do |environment|
500
483
  @lifecycle_environments[organization][environment['name']] = environment['id']
501
484
  end
502
485
  options[:id] = @lifecycle_environments[organization][options[:name]]
@@ -527,9 +510,9 @@ module HammerCLICsv
527
510
  options[:id] = @contentviews[organization][options[:name]]
528
511
  if !options[:id]
529
512
  @api.resource(:content_views).call(:index, {
530
- :per_page => 999999,
531
- 'organization_id' => foreman_organization(:name => organization)
532
- })['results'].each do |contentview|
513
+ :per_page => 999999,
514
+ 'organization_id' => foreman_organization(:name => organization)
515
+ })['results'].each do |contentview|
533
516
  @contentviews[organization][contentview['name']] = contentview['id']
534
517
  end
535
518
  options[:id] = @contentviews[organization][options[:name]]
@@ -551,6 +534,30 @@ module HammerCLICsv
551
534
  result
552
535
  end
553
536
 
537
+ def katello_contentviewversion(organization, name, version)
538
+ @contentviewversions ||= {}
539
+ @contentviewversions[organization] ||= {}
540
+ versionname = "#{version}|#{name}"
541
+
542
+ return nil if name.nil? || name.empty?
543
+ id = @contentviewversions[organization][versionname]
544
+ if !id
545
+ contentview_id = katello_contentview(organization, :name => name)
546
+ @api.resource(:content_view_versions).call(:index, {
547
+ :per_page => 999999,
548
+ 'content_view_id' => contentview_id
549
+ })['results'].each do |contentviewversion|
550
+ if contentviewversion['version'] == version.to_i
551
+ @contentviewversions[organization][versionname] = contentviewversion['id']
552
+ end
553
+ end
554
+ id = @contentviewversions[organization][versionname]
555
+ raise "Content view version '#{name}' with version '#{version}' not found" if !id
556
+ end
557
+
558
+ id
559
+ end
560
+
554
561
  def katello_repository(organization, options = {})
555
562
  @repositories ||= {}
556
563
  @repositories[organization] ||= {}
@@ -559,11 +566,10 @@ module HammerCLICsv
559
566
  return nil if options[:name].nil? || options[:name].empty?
560
567
  options[:id] = @repositories[organization][options[:name]]
561
568
  if !options[:id]
562
- @api.resource(:repositories)\
563
- .call(:index, {
564
- :per_page => 999999,
565
- 'organization_id' => foreman_organization(:name => organization)
566
- })['results'].each do |repository|
569
+ @api.resource(:repositories).call(:index, {
570
+ :per_page => 999999,
571
+ 'organization_id' => foreman_organization(:name => organization)
572
+ })['results'].each do |repository|
567
573
  @repositories[organization][repository['name']] = repository['id']
568
574
  end
569
575
  options[:id] = @repositories[organization][options[:name]]
@@ -594,10 +600,10 @@ module HammerCLICsv
594
600
  options[:id] = @subscriptions[organization][options[:name]]
595
601
  if !options[:id]
596
602
  results = @api.resource(:subscriptions).call(:index, {
597
- :per_page => 999999,
598
- 'organization_id' => foreman_organization(:name => organization),
599
- 'search' => "name:\"#{options[:name]}\""
600
- })
603
+ :per_page => 999999,
604
+ 'organization_id' => foreman_organization(:name => organization),
605
+ 'search' => "name:\"#{options[:name]}\""
606
+ })
601
607
  raise "No subscriptions match '#{options[:name]}'" if results['subtotal'] == 0
602
608
  raise "Too many subscriptions match '#{options[:name]}'" if results['subtotal'] > 1
603
609
  subscription = results['results'][0]
@@ -714,13 +720,12 @@ module HammerCLICsv
714
720
  end
715
721
  end
716
722
  associations[organization] += [id] if !associations[organization].include? id
717
- @api.resource(:organizations)\
718
- .call(:update, {
719
- 'id' => organization_id,
720
- 'organization' => {
721
- "#{name}_ids" => associations[organization]
722
- }
723
- })
723
+ @api.resource(:organizations).call(:update, {
724
+ 'id' => organization_id,
725
+ 'organization' => {
726
+ "#{name}_ids" => associations[organization]
727
+ }
728
+ })
724
729
  end if organizations && !organizations.empty?
725
730
  end
726
731
 
@@ -737,13 +742,12 @@ module HammerCLICsv
737
742
  end
738
743
  associations[location] += [id] if !associations[location].include? id
739
744
 
740
- @api.resource(:locations)\
741
- .call(:update, {
742
- 'id' => location_id,
743
- 'location' => {
744
- "#{name}_ids" => associations[location]
745
- }
746
- })
745
+ @api.resource(:locations).call(:update, {
746
+ 'id' => location_id,
747
+ 'location' => {
748
+ "#{name}_ids" => associations[location]
749
+ }
750
+ })
747
751
  end if locations && !locations.empty?
748
752
  end
749
753
  end
@@ -61,23 +61,21 @@ module HammerCLICsv
61
61
  name = namify(line[NAME], number)
62
62
  if !@existing.include? name
63
63
  print "Creating compute profile '#{name}'..." if option_verbose?
64
- id = @api.resource(:compute_profiles)\
65
- .call(:create, {
66
- 'compute_profile' => {
67
- 'name' => name,
68
- 'url' => line[URL]
69
- }
70
- })['id']
64
+ id = @api.resource(:compute_profiles).call(:create, {
65
+ 'compute_profile' => {
66
+ 'name' => name,
67
+ 'url' => line[URL]
68
+ }
69
+ })['id']
71
70
  else
72
71
  print "Updating compute profile '#{name}'..." if option_verbose?
73
- id = @api.resource(:compute_profiles)\
74
- .call(:update, {
75
- 'id' => @existing[name],
76
- 'compute_profile' => {
77
- 'name' => name,
78
- 'url' => line[URL]
79
- }
80
- })['compute_profile']['id']
72
+ id = @api.resource(:compute_profiles).call(:update, {
73
+ 'id' => @existing[name],
74
+ 'compute_profile' => {
75
+ 'name' => name,
76
+ 'url' => line[URL]
77
+ }
78
+ })['compute_profile']['id']
81
79
  end
82
80
 
83
81
  # Update associated profiles
@@ -59,23 +59,21 @@ module HammerCLICsv
59
59
  name = namify(line[NAME], number)
60
60
  if !@existing.include? name
61
61
  print "Creating compute resource '#{name}'..." if option_verbose?
62
- id = @api.resource(:compute_resources)\
63
- .call(:create, {
64
- 'compute_resource' => {
65
- 'name' => name,
66
- 'url' => line[URL]
67
- }
68
- })['id']
62
+ id = @api.resource(:compute_resources).call(:create, {
63
+ 'compute_resource' => {
64
+ 'name' => name,
65
+ 'url' => line[URL]
66
+ }
67
+ })['id']
69
68
  else
70
69
  print "Updating compute resource '#{name}'..." if option_verbose?
71
- id = @api.resource(:compute_resources)\
72
- .call(:update, {
73
- 'id' => @existing[name],
74
- 'compute_resource' => {
75
- 'name' => name,
76
- 'url' => line[URL]
77
- }
78
- })['compute_resource']['id']
70
+ id = @api.resource(:compute_resources).call(:update, {
71
+ 'id' => @existing[name],
72
+ 'compute_resource' => {
73
+ 'name' => name,
74
+ 'url' => line[URL]
75
+ }
76
+ })['compute_resource']['id']
79
77
  end
80
78
 
81
79
  # Update associated resources