Project

General

Profile

Revision 7b00acc6

Added by Thomas McKay almost 9 years ago

fixes #4926 - systems test and rubocop cleanup

View differences:

lib/hammer_cli_csv/systems.rb
33 33

  
34 34
module HammerCLICsv
35 35
  class SystemsCommand < BaseCommand
36

  
37 36
    ORGANIZATION = 'Organization'
38 37
    ENVIRONMENT = 'Environment'
39 38
    CONTENTVIEW = 'Content View'
......
52 51
    def export
53 52
      CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => false}) do |csv|
54 53
        csv << [NAME, COUNT, ORGANIZATION, ENVIRONMENT, CONTENTVIEW, SYSTEMGROUPS, VIRTUAL, HOST,
55
               OPERATINGSYSTEM, ARCHITECTURE, SOCKETS, RAM, CORES, SLA, PRODUCTS, SUBSCRIPTIONS]
54
                OPERATINGSYSTEM, ARCHITECTURE, SOCKETS, RAM, CORES, SLA, PRODUCTS, SUBSCRIPTIONS]
56 55
        @api.resource(:organizations).call(:index, {:per_page => 999999})['results'].each do |organization|
57 56
          @api.resource(:systems).call(:index, {
58 57
                                'per_page' => 999999,
......
72 71
              column << system['systemGroups'].collect do |systemgroup|
73 72
                systemgroup['name']
74 73
              end
75
            end.delete!("\n")
74
            end
75
            systemgroups.delete!("\n")
76 76
            virtual = system['facts']['virt.is_guest'] == 'true' ? 'Yes' : 'No'
77 77
            host = system['host']
78 78
            operatingsystem = "#{system['facts']['distribution.name']} " if system['facts']['distribution.name']
......
81 81
            sockets = system['facts']['cpu.cpu_socket(s)']
82 82
            ram = system['facts']['memory.memtotal']
83 83
            cores = system['facts']['cpu.core(s)_per_socket']
84
            sla = ""
84
            sla = ''
85 85
            products = CSV.generate do |column|
86 86
              column << system['installedProducts'].collect do |product|
87 87
                "#{product['productId']}|#{product['productName']}"
88 88
              end
89
            end.delete!("\n")
89
            end
90
            products.delete!("\n")
90 91
            subscriptions = CSV.generate do |column|
91 92
              column << @api.resource(:subscriptions).call(:index, {
92 93
                                                    'system_id' => system['uuid']
93 94
                                                  })['results'].collect do |subscription|
94 95
                "#{subscription['product_id']}|#{subscription['product_name']}"
95 96
              end
96
            end.delete!("\n")
97
            end
98
            subscriptions.delete!("\n")
97 99
            csv << [name, count, organization_name, environment, contentview, systemgroups, virtual, host,
98 100
                    operatingsystem, architecture, sockets, ram, cores, sla, products, subscriptions]
99 101
          end
......
109 111
        create_systems_from_csv(line)
110 112
      end
111 113

  
112
      print "Updating host and guest associations..." if option_verbose?
114
      print 'Updating host and guest associations...' if option_verbose?
113 115
      @host_guests.each do |host_id, guest_ids|
114 116
        @api.resource(:systems).call(:update, {
115 117
                               'id' => host_id,
116 118
                               'guest_ids' => guest_ids
117 119
                             })
118 120
      end
119
      puts "done" if option_verbose?
121
      puts 'done' if option_verbose?
120 122
    end
121 123

  
122 124
    def create_systems_from_csv(line)
......
144 146
                                                     'organization_id' => katello_organization(:name => line[ORGANIZATION]),
145 147
                                                     'environment_id' => katello_environment(line[ORGANIZATION], :name => line[ENVIRONMENT]),
146 148
                                                     'content_view_id' => katello_contentview(line[ORGANIZATION], :name => line[CONTENTVIEW]),
147
                                                     'facts' => facts(line),
149
                                                     'facts' => facts(name, line),
148 150
                                                     'installed_products' => products(line),
149 151
                                                     'type' => 'system'
150 152
                                                   })['uuid']
......
157 159
                                                       'name' => name,
158 160
                                                       'environment_id' => katello_environment(line[ORGANIZATION], :name => line[ENVIRONMENT]),
159 161
                                                       'content_view_id' => katello_contentview(line[ORGANIZATION], :name => line[CONTENTVIEW]),
160
                                                       'facts' => facts(line),
162
                                                       'facts' => facts(name, line),
161 163
                                                       'installed_products' => products(line)
162 164
                                                     }
163 165
                                                   })['uuid']
......
171 173

  
172 174
        set_system_groups(system_id, line)
173 175

  
174
        puts "done" if option_verbose?
176
        puts 'done' if option_verbose?
175 177
      end
176 178
    rescue RuntimeError => e
177 179
      raise "#{e}\n       #{line}"
......
179 181

  
180 182
    private
181 183

  
182
    def facts(line)
184
    def facts(name, line)
183 185
      facts = {}
184 186
      facts['cpu.core(s)_per_socket'] = line[CORES]
185 187
      facts['cpu.cpu_socket(s)'] = line[SOCKETS]
......
191 193
        (facts['distribution.name'], facts['distribution.version']) = ['RHEL', line[OPERATINGSYSTEM]]
192 194
      end
193 195
      facts['virt.is_guest'] = line[VIRTUAL] == 'Yes' ? true : false
196
      facts['virt.uuid'] = "#{line[ORGANIZATION]}/#{name}" if facts['virt.is_guest']
194 197
      facts
195 198
    end
196 199

  
......
204 207
    end
205 208

  
206 209
    def products(line)
210
      return nil if !line[PRODUCTS]
207 211
      products = CSV.parse_line(line[PRODUCTS]).collect do |product_details|
208 212
        product = {}
209 213
        # TODO: these get passed straight through to candlepin; probably would be better to process in server
......
215 219
    end
216 220

  
217 221
    def subscriptions(line)
222
      return nil if !line[SUBSCRIPTIONS]
218 223
      subscriptions = CSV.parse_line(line[SUBSCRIPTIONS]).collect do |subscription_details|
219 224
        subscription = {}
220 225
        (subscription[:number], subscription[:name]) = subscription_details.split('|')
......
222 227
      end
223 228
      subscriptions
224 229
    end
225

  
226 230
  end
227 231

  
228
  HammerCLI::MainCommand.subcommand("csv:systems", "import/export systems", HammerCLICsv::SystemsCommand)
232
  HammerCLI::MainCommand.subcommand('csv:systems', 'import/export systems', HammerCLICsv::SystemsCommand)
229 233
end

Also available in: Unified diff