Project

General

Profile

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

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

1
# Copyright (c) 2013-2014 Red Hat
2
#
3
# MIT License
4
#
5
# Permission is hereby granted, free of charge, to any person obtaining
6
# a copy of this software and associated documentation files (the
7
# "Software"), to deal in the Software without restriction, including
8
# without limitation the rights to use, copy, modify, merge, publish,
9
# distribute, sublicense, and/or sell copies of the Software, and to
10
# permit persons to whom the Software is furnished to do so, subject to
11
# the following conditions:
12
#
13
# The above copyright notice and this permission notice shall be
14
# included in all copies or substantial portions of the Software.
15
#
16
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
#
24
#
25
# -= Systems CSV =-
26
#
27
# Columns
28
#   Name
29
#     - System name
30
#     - May contain '%d' which will be replaced with current iteration number of Count
31
#     - eg. "os%d" -> "os1"
32
#   Count
33
#     - Number of times to iterate on this line of the CSV file
34

    
35

    
36
require 'hammer_cli'
37
require 'katello_api'
38
require 'foreman_api'
39
require 'json'
40
require 'csv'
41
require 'uri'
42

    
43
module HammerCLICsv
44
  class ProductsCommand < BaseCommand
45

    
46
    ORGANIZATION = 'Organization'
47
    REPOSITORY = 'Repository'
48
    REPOSITORY_TYPE = 'Repository Type'
49
    REPOSITORY_URL = 'Repository Url'
50

    
51
    def export
52
      # TODO
53
    end
54

    
55
    def import
56
      @existing_products = {}
57
      @existing_repositories = {}
58

    
59
      thread_import do |line|
60
        create_products_from_csv(line)
61
      end
62
    end
63

    
64
    def create_products_from_csv(line)
65
      if !@existing_products[line[ORGANIZATION]]
66
        @existing_products[line[ORGANIZATION]] = {}
67
        @k_product_api.index({
68
                               'organization_id' => katello_organization(:name => line[ORGANIZATION]),
69
                               'page_size' => 999999,
70
                               'paged' => true
71
                             })[0]['results'].each do |product|
72
          @existing_products[line[ORGANIZATION]][product['name']] = product['id'] if product
73

    
74
          if product
75
            @k_repository_api.index({
76
                                      'organization_id' => katello_organization(:name => line[ORGANIZATION]),
77
                                      'product_id' => product['id'],
78
                                      'enabled' => true,
79
                                      'library' => true,
80
                                      'page_size' => 999999, 'paged' => true
81
                                    })[0]['results'].each do |repository|
82
              @existing_repositories[line[ORGANIZATION]+product['name']] ||= {}
83
              @existing_repositories[line[ORGANIZATION]+product['name']][repository['label']] = repository['id']
84
            end
85
          end
86
        end
87
      end
88

    
89
      # Only creating products, not updating
90
      line[COUNT].to_i.times do |number|
91
        name = namify(line[NAME], number)
92
        product_id = @existing_products[line[ORGANIZATION]][name]
93
        if !product_id
94
          print "Creating product '#{name}'..." if option_verbose?
95
          product_id = @k_product_api.create({
96
                                               'organization_id' => katello_organization(:name => line[ORGANIZATION]),
97
                                               'name' => name
98
                                             })[0]['id']
99
          @existing_products[line[ORGANIZATION]][name] = product_id
100
          print "done\n" if option_verbose?
101
        end
102
        @existing_repositories[line[ORGANIZATION] + name] ||= {}
103

    
104
        # Only creating repositories, not updating
105
        repository_name = namify(line[REPOSITORY], number)
106
        if !@existing_repositories[line[ORGANIZATION] + name][labelize(repository_name)]
107
          print "Creating repository '#{repository_name}' in product '#{name}'..." if option_verbose?
108
          @k_repository_api.create({
109
                                     'organization_id' => katello_organization(:name => line[ORGANIZATION]),
110
                                     'name' => repository_name,
111
                                     'label' => labelize(repository_name),
112
                                     'product_id' => product_id,
113
                                     'url' => line[REPOSITORY_URL],
114
                                     'content_type' => line[REPOSITORY_TYPE]
115
                                   })
116
          print "done\n" if option_verbose?
117
        end
118
      end
119

    
120
    rescue RuntimeError => e
121
      raise "#{e}\n       #{line}"
122
    end
123
  end
124

    
125
  HammerCLI::MainCommand.subcommand("csv:products", "import/export products and repositories",
126
                                    HammerCLICsv::ProductsCommand)
127
end