Project

General

Profile

Download (4.54 KB) Statistics
| Branch: | Tag: | Revision:

hammer-cli-csv / lib / hammer_cli_csv / products.rb @ bf81b9cf

1
# Copyright 2013-2014 Red Hat, Inc.
2
#
3
# This software is licensed to you under the GNU General Public
4
# License as published by the Free Software Foundation; either version
5
# 2 of the License (GPLv2) or (at your option) any later version.
6
# There is NO WARRANTY for this software, express or implied,
7
# including the implied warranties of MERCHANTABILITY,
8
# NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should
9
# have received a copy of GPLv2 along with this software; if not, see
10
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
11

    
12
#
13
# -= Systems CSV =-
14
#
15
# Columns
16
#   Name
17
#     - System name
18
#     - May contain '%d' which will be replaced with current iteration number of Count
19
#     - eg. "os%d" -> "os1"
20
#   Count
21
#     - Number of times to iterate on this line of the CSV file
22

    
23

    
24
require 'hammer_cli'
25
require 'json'
26
require 'csv'
27
require 'uri'
28

    
29
module HammerCLICsv
30
  class ProductsCommand < BaseCommand
31

    
32
    ORGANIZATION = 'Organization'
33
    REPOSITORY = 'Repository'
34
    REPOSITORY_TYPE = 'Repository Type'
35
    REPOSITORY_URL = 'Repository Url'
36

    
37
    def export
38
      # TODO
39
    end
40

    
41
    def import
42
      @existing_products = {}
43
      @existing_repositories = {}
44

    
45
      thread_import do |line|
46
        create_products_from_csv(line)
47
      end
48
    end
49

    
50
    def create_products_from_csv(line)
51
      if !@existing_products[line[ORGANIZATION]]
52
        @existing_products[line[ORGANIZATION]] = {}
53
        @api.resource(:products).call(:index, {
54
                               'organization_id' => katello_organization(:name => line[ORGANIZATION]),
55
                               'page_size' => 999999,
56
                               'paged' => true
57
                             })['results'].each do |product|
58
          @existing_products[line[ORGANIZATION]][product['name']] = product['id'] if product
59

    
60
          if product
61
            @api.resource(:repositories).call(:index, {
62
                                      'organization_id' => katello_organization(:name => line[ORGANIZATION]),
63
                                      'product_id' => product['id'],
64
                                      'enabled' => true,
65
                                      'library' => true,
66
                                      'page_size' => 999999, 'paged' => true
67
                                    })['results'].each do |repository|
68
              @existing_repositories[line[ORGANIZATION]+product['name']] ||= {}
69
              @existing_repositories[line[ORGANIZATION]+product['name']][repository['label']] = repository['id']
70
            end
71
          end
72
        end
73
      end
74

    
75
      # Only creating products, not updating
76
      line[COUNT].to_i.times do |number|
77
        name = namify(line[NAME], number)
78
        product_id = @existing_products[line[ORGANIZATION]][name]
79
        if !product_id
80
          print "Creating product '#{name}'..." if option_verbose?
81
          product_id = @api.resource(:products).call(:create, {
82
                                               'organization_id' => katello_organization(:name => line[ORGANIZATION]),
83
                                               'name' => name
84
                                             })['id']
85
          @existing_products[line[ORGANIZATION]][name] = product_id
86
        else
87
          # Nothing to update for products
88
          print "Updating product '#{name}'..." if option_verbose?
89
        end
90
        print "done\n" if option_verbose?
91
        @existing_repositories[line[ORGANIZATION] + name] ||= {}
92

    
93
        # Only creating repositories, not updating
94
        repository_name = namify(line[REPOSITORY], number)
95
        if !@existing_repositories[line[ORGANIZATION] + name][labelize(repository_name)]
96
          print "Creating repository '#{repository_name}' in product '#{name}'..." if option_verbose?
97
          @api.resource(:repositories).call(:create, {
98
                                     'organization_id' => katello_organization(:name => line[ORGANIZATION]),
99
                                     'name' => repository_name,
100
                                     'label' => labelize(repository_name),
101
                                     'product_id' => product_id,
102
                                     'url' => line[REPOSITORY_URL],
103
                                     'content_type' => line[REPOSITORY_TYPE]
104
                                   })
105
          print "done\n" if option_verbose?
106
        end
107
      end
108

    
109
    rescue RuntimeError => e
110
      raise "#{e}\n       #{line}"
111
    end
112
  end
113

    
114
  HammerCLI::MainCommand.subcommand("csv:products", "import/export products and repositories",
115
                                    HammerCLICsv::ProductsCommand)
116
end