Project

General

Profile

Revision 9d9e4494

Added by Thomas McKay almost 9 years ago

cv - provider, products, repo creation

View differences:

lib/hammer_cli_csv/base.rb
35 35
    NAME = 'Name'
36 36
    COUNT = 'Count'
37 37

  
38
    HEADERS = {'Accept' => 'version=2,application/json'}
39

  
40 38
    option ["-v", "--verbose"], :flag, "be verbose"
41 39
    option ['--threads'], 'THREAD_COUNT', 'Number of threads to hammer with', :default => 1
42 40
    option ['--csv-export'], :flag, 'Export current data instead of importing'
......
61 59
      @k_systemgroup_api ||= KatelloApi::Resources::SystemGroup.new(@init_options.merge({:base_url => "#{@init_options[:base_url]}"}))
62 60
      @k_environment_api ||= KatelloApi::Resources::Environment.new(@init_options.merge({:base_url => "#{@init_options[:base_url]}"}))
63 61
      @k_contentview_api ||= KatelloApi::Resources::ContentView.new(@init_options.merge({:base_url => "#{@init_options[:base_url]}"}))
62
      @k_provider_api ||= KatelloApi::Resources::Provider.new(@init_options.merge({:base_url => "#{@init_options[:base_url]}"}))
63
      @k_product_api ||= KatelloApi::Resources::Product.new(@init_options.merge({:base_url => "#{@init_options[:base_url]}"}))
64
      @k_repository_api ||= KatelloApi::Resources::Repository.new(@init_options.merge({:base_url => "#{@init_options[:base_url]}"}))
65
      @k_contentviewdefinition_api ||= KatelloApi::Resources::ContentViewDefinition.new(@init_options.merge({:base_url => "#{@init_options[:base_url]}"}))
66
      @k_subscription_api ||= KatelloApi::Resources::Subscription.new(@init_options.merge({:base_url => "#{@init_options[:base_url]}"}))
64 67

  
65 68
      @f_architecture_api ||= ForemanApi::Resources::Architecture.new(@init_options)
66 69
      @f_domain_api ||= ForemanApi::Resources::Domain.new(@init_options)
......
91 94
      end
92 95
    end
93 96

  
97
    def labelize(name)
98
      name.gsub(/[^a-z0-9\-_]/i, "_")
99
    end
100

  
94 101
    def thread_import(return_headers=false)
95 102
      csv = []
96 103
      CSV.foreach(csv_file, {:skip_blanks => true, :headers => :first_row, :return_headers => return_headers}) do |line|
......
124 131
        return nil if options[:name].nil? || options[:name].empty?
125 132
        options[:id] = @organizations[options[:name]]
126 133
        if !options[:id]
127
          organization = @f_organization_api.index({'search' => "name=\"#{options[:name]}\""}, HEADERS)[0]['results']
134
          organization = @f_organization_api.index({'search' => "name=\"#{options[:name]}\""})[0]['results']
128 135
          raise RuntimeError.new("Organization '#{options[:name]}' not found") if !organization || organization.empty?
129 136
          options[:id] = organization[0]['id']
130 137
          @organizations[options[:name]] = options[:id]
......
134 141
        return nil if options[:id].nil?
135 142
        options[:name] = @organizations.key(options[:id])
136 143
        if !options[:name]
137
          organization = @f_organization_api.show({'id' => options[:id]}, HEADERS)[0]
144
          organization = @f_organization_api.show({'id' => options[:id]})[0]
138 145
          raise RuntimeError.new("Organization 'id=#{options[:id]}' not found") if !organization || organization.empty?
139 146
          options[:name] = organization['name']
140 147
          @organizations[options[:name]] = options[:id]
......
152 159
        return nil if options[:name].nil? || options[:name].empty?
153 160
        options[:id] = @environments[options[:name]]
154 161
        if !options[:id]
155
          environment = @f_environment_api.index({'search' => "name=\"#{options[:name]}\""}, HEADERS)[0]['results']
162
          environment = @f_environment_api.index({'search' => "name=\"#{options[:name]}\""})[0]['results']
156 163
          raise RuntimeError.new("Puppet environment '#{options[:name]}' not found") if !environment || environment.empty?
157 164
          options[:id] = environment[0]['id']
158 165
          @environments[options[:name]] = options[:id]
......
162 169
        return nil if options[:id].nil?
163 170
        options[:name] = @environments.key(options[:id])
164 171
        if !options[:name]
165
          environment = @f_environment_api.show({'id' => options[:id]}, HEADERS)[0]
172
          environment = @f_environment_api.show({'id' => options[:id]})[0]
166 173
          raise RuntimeError.new("Puppet environment '#{options[:name]}' not found") if !environment || environment.empty?
167 174
          options[:name] = environment['name']
168 175
          @environments[options[:name]] = options[:id]
......
182 189
        if !options[:id]
183 190
          (osname, major, minor) = split_os_name(options[:name])
184 191
          search = "name=\"#{osname}\" and major=\"#{major}\" and minor=\"#{minor}\""
185
          operatingsystems = @f_operatingsystem_api.index({'search' => search}, HEADERS)[0]['results']
192
          operatingsystems = @f_operatingsystem_api.index({'search' => search})[0]['results']
186 193
          operatingsystem = operatingsystems[0]
187 194
          raise RuntimeError.new("Operating system '#{options[:name]}' not found") if !operatingsystem || operatingsystem.empty?
188 195
          options[:id] = operatingsystem['id']
......
193 200
        return nil if options[:id].nil?
194 201
        options[:name] = @operatingsystems.key(options[:id])
195 202
        if !options[:name]
196
          operatingsystem = @f_operatingsystem_api.show({'id' => options[:id]}, HEADERS)[0]
203
          operatingsystem = @f_operatingsystem_api.show({'id' => options[:id]})[0]
197 204
          raise RuntimeError.new("Operating system 'id=#{options[:id]}' not found") if !operatingsystem || operatingsystem.empty?
198 205
          options[:name] = build_os_name(operatingsystem['name'],
199 206
                                         operatingsystem['major'],
......
213 220
        return nil if options[:name].nil? || options[:name].empty?
214 221
        options[:id] = @architectures[options[:name]]
215 222
        if !options[:id]
216
          architecture = @f_architecture_api.index({'search' => "name=\"#{options[:name]}\""}, HEADERS)[0]['results']
223
          architecture = @f_architecture_api.index({'search' => "name=\"#{options[:name]}\""})[0]['results']
217 224
          raise RuntimeError.new("Architecture '#{options[:name]}' not found") if !architecture || architecture.empty?
218 225
          options[:id] = architecture[0]['id']
219 226
          @architectures[options[:name]] = options[:id]
......
223 230
        return nil if options[:id].nil?
224 231
        options[:name] = @architectures.key(options[:id])
225 232
        if !options[:name]
226
          architecture = @f_architecture_api.show({'id' => options[:id]}, HEADERS)[0]
233
          architecture = @f_architecture_api.show({'id' => options[:id]})[0]
227 234
          raise RuntimeError.new("Architecture 'id=#{options[:id]}' not found") if !architecture || architecture.empty?
228 235
          options[:name] = architecture['name']
229 236
          @architectures[options[:name]] = options[:id]
......
241 248
        return nil if options[:name].nil? || options[:name].empty?
242 249
        options[:id] = @domains[options[:name]]
243 250
        if !options[:id]
244
          domain = @f_domain_api.index({'search' => "name=\"#{options[:name]}\""}, HEADERS)[0]['results']
251
          domain = @f_domain_api.index({'search' => "name=\"#{options[:name]}\""})[0]['results']
245 252
          raise RuntimeError.new("Domain '#{options[:name]}' not found") if !domain || domain.empty?
246 253
          options[:id] = domain[0]['id']
247 254
          @domains[options[:name]] = options[:id]
......
251 258
        return nil if options[:id].nil?
252 259
        options[:name] = @domains.key(options[:id])
253 260
        if !options[:name]
254
          domain = @f_domain_api.show({'id' => options[:id]}, HEADERS)[0]
261
          domain = @f_domain_api.show({'id' => options[:id]})[0]
255 262
          raise RuntimeError.new("Domain 'id=#{options[:id]}' not found") if !domain || domain.empty?
256 263
          options[:name] = domain['name']
257 264
          @domains[options[:name]] = options[:id]
......
269 276
        return nil if options[:name].nil? || options[:name].empty?
270 277
        options[:id] = @ptables[options[:name]]
271 278
        if !options[:id]
272
          ptable = @f_partitiontable_api.index({'search' => "name=\"#{options[:name]}\""}, HEADERS)[0]['results']
279
          ptable = @f_partitiontable_api.index({'search' => "name=\"#{options[:name]}\""})[0]['results']
273 280
          raise RuntimeError.new("Partition table '#{options[:name]}' not found") if !ptable || ptable.empty?
274 281
          options[:id] = ptable[0]['id']
275 282
          @ptables[options[:name]] = options[:id]
......
279 286
        return nil if options[:id].nil?
280 287
        options[:name] = @ptables.key(options[:id])
281 288
        if !options[:name]
282
          ptable = @f_partitiontable_api.show({'id' => options[:id]}, HEADERS)[0]
289
          ptable = @f_partitiontable_api.show({'id' => options[:id]})[0]
283 290
          options[:name] = ptable['name']
284 291
          @ptables[options[:name]] = options[:id]
285 292
        end
......
299 306
        return nil if options[:name].nil? || options[:name].empty?
300 307
        options[:id] = @environments[organization][options[:name]]
301 308
        if !options[:id]
302
          @k_environment_api.index({'organization_id' => organization}, HEADERS)[0].each do |environment|
309
          @k_environment_api.index({'organization_id' => organization})[0].each do |environment|
303 310
            @environments[organization][environment['environment']['name']] = environment['environment']['id']
304 311
          end
305 312
          options[:id] = @environments[organization][options[:name]]
......
310 317
        return nil if options[:id].nil?
311 318
        options[:name] = @environments.key(options[:id])
312 319
        if !options[:name]
313
          environment = @k_environment_api.show({'id' => options[:id]}, HEADERS)[0]
320
          environment = @k_environment_api.show({'id' => options[:id]})[0]
314 321
          raise RuntimeError.new("Puppet environment '#{options[:name]}' not found") if !environment || environment.empty?
315 322
          options[:name] = environment['name']
316 323
          @environments[options[:name]] = options[:id]
......
329 336
        return nil if options[:name].nil? || options[:name].empty?
330 337
        options[:id] = @contentviews[organization][options[:name]]
331 338
        if !options[:id]
332
          puts @k_contentview_api.index({'organization_id' => organization, 'environment_id' => 2, 'label' => 'Default_Organization_View'}, HEADERS)[0]
333
          @k_contentview_api.index({'organization_id' => organization, 'label' => options[:name]}, HEADERS)[0].each do |contentview|
339
          puts "CONTENTVIEWS=#{@k_contentview_api.index({'organization_id' => organization, 'environment_id' => 2, 'label' => 'Default_Organization_View'})[0]}"
340
          @k_contentview_api.index({
341
                                     'organization_id' => organization,
342
                                     'label' => options[:name]
343
                                   })[0].each do |contentview|
334 344
            puts contentview
335 345
            @contentviews[organization][contentview['contentview']['name']] = contentview['contentview']['id']
336 346
          end
......
342 352
        return nil if options[:id].nil?
343 353
        options[:name] = @contentviews.key(options[:id])
344 354
        if !options[:name]
345
          contentview = @k_contentview_api.show({'id' => options[:id]}, HEADERS)[0]
355
          contentview = @k_contentview_api.show({'id' => options[:id]})[0]
346 356
          raise RuntimeError.new("Puppet contentview '#{options[:name]}' not found") if !contentview || contentview.empty?
347 357
          options[:name] = contentview['name']
348 358
          @contentviews[options[:name]] = options[:id]
......
353 363
      result
354 364
    end
355 365

  
366
    def katello_subscription(organization, options={})
367
      @subscriptions ||= {}
368
      @subscriptions[organization] ||= {}
369

  
370
      if options[:name]
371
        return nil if options[:name].nil? || options[:name].empty?
372
        options[:id] = @subscriptions[organization][options[:name]]
373
        if !options[:id]
374
          puts @k_subscription_api.index({
375
                                           'organization_id' => organization,
376
                                           'search' => "product_id:#{options[:name]}"
377
                                         })[0]
378
          @k_subscription_api.index({
379
                                      'organization_id' => organization,
380
                                      'search' => "'product_id:#{name}'"
381
                                    })[0]['results'][0].each do |subscription|
382
            puts subscription
383
            @subscriptions[organization][subscription['subscription']['name']] = subscription['subscription']['id']
384
          end
385
          options[:id] = @subscriptions[organization][options[:name]]
386
          raise RuntimeError.new("Puppet subscription '#{options[:name]}' not found") if !options[:id]
387
        end
388
        result = options[:id]
389
      else
390
        return nil if options[:id].nil?
391
        options[:name] = @subscriptions.key(options[:id])
392
        if !options[:name]
393
          subscription = @k_subscription_api.show({'id' => options[:id]})[0]
394
          raise RuntimeError.new("Puppet subscription '#{options[:name]}' not found") if !subscription || subscription.empty?
395
          options[:name] = subscription['name']
396
          @subscriptions[options[:name]] = options[:id]
397
        end
398
        result = options[:name]
399
      end
400

  
401
      result
402
    end
403

  
356 404
    def build_os_name(name, major, minor)
357 405
      name += " #{major}" if major && major != ""
358 406
      name += ".#{minor}" if minor && minor != ""

Also available in: Unified diff