Project

General

Profile

Revision f8ecc788

Added by Thomas McKay almost 9 years ago

csv-scope - lots of cleanup

View differences:

lib/hammer_cli_csv/products.rb
20 20
#   Count
21 21
#     - Number of times to iterate on this line of the CSV file
22 22

  
23

  
24 23
require 'hammer_cli'
25 24
require 'json'
26 25
require 'csv'
......
28 27

  
29 28
module HammerCLICsv
30 29
  class ProductsCommand < BaseCommand
31

  
30
    LABEL = 'Label'
32 31
    ORGANIZATION = 'Organization'
33 32
    REPOSITORY = 'Repository'
34 33
    REPOSITORY_TYPE = 'Repository Type'
35 34
    REPOSITORY_URL = 'Repository Url'
35
    DESCRIPTION = 'Description'
36 36

  
37 37
    def export
38
      # TODO
38
      CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => false}) do |csv|
39
        csv << [NAME, COUNT, LABEL, ORGANIZATION, REPOSITORY, REPOSITORY_TYPE, REPOSITORY_URL]
40
        @api.resource(:organizations).call(:index, {:per_page => 999999})['results'].each do |organization|
41
          @api.resource(:products).call(:index, {
42
                                          'per_page' => 999999,
43
                                          'enabled' => true,
44
                                          'organization_id' => katello_organization(:name => organization['name'])
45
                                        })['results'].each do |product|
46
            # product = @api.resource(:products).call(:show, {
47
            #                                           'id' => product['id'],
48
            #                                           'fields' => 'full'
49
            #                                         })
50
            product['library_repositories'].each do |repository|
51
              if repository['sync_state'] != 'not_synced'
52
                repository_type = "#{repository['product_type'] == 'custom' ? 'Custom' : 'Red Hat'} #{repository['content_type'].capitalize}"
53
                csv << [namify(product['name'], 1), 1, product['label'], organization['name'], repository['name'],
54
                        repository_type, repository['feed']]
55
                #puts "  HTTPS:                     #{repository['unprotected'] ? 'No' : 'Yes'}"
56
              end
57
            end
58

  
59
          end
60
        end
61
      end
39 62
    end
40 63

  
41 64
    def import
......
65 88
                                      'library' => true,
66 89
                                      'page_size' => 999999, 'paged' => true
67 90
                                    })['results'].each do |repository|
68
              @existing_repositories[line[ORGANIZATION]+product['name']] ||= {}
69
              @existing_repositories[line[ORGANIZATION]+product['name']][repository['label']] = repository['id']
91
              @existing_repositories[line[ORGANIZATION] + product['name']] ||= {}
92
              @existing_repositories[line[ORGANIZATION] + product['name']][repository['label']] = repository['id']
70 93
            end
71 94
          end
72 95
        end
......
75 98
      # Only creating products, not updating
76 99
      line[COUNT].to_i.times do |number|
77 100
        name = namify(line[NAME], number)
101
        puts @existing_products
78 102
        product_id = @existing_products[line[ORGANIZATION]][name]
79 103
        if !product_id
80 104
          print "Creating product '#{name}'..." if option_verbose?
105
          if line[REPOSITORY_TYPE] =~ /Red Hat/
106
            raise "Red Hat product '#{name}' does not exist in '#{line[ORGANIZATION]}'"
107
          end
108

  
81 109
          product_id = @api.resource(:products).call(:create, {
82 110
                                               'organization_id' => katello_organization(:name => line[ORGANIZATION]),
83 111
                                               'name' => name
......
87 115
          # Nothing to update for products
88 116
          print "Updating product '#{name}'..." if option_verbose?
89 117
        end
118
        @existing_repositories[line[ORGANIZATION] + name] = {}
90 119
        print "done\n" if option_verbose?
91
        @existing_repositories[line[ORGANIZATION] + name] ||= {}
92 120

  
93
        # Only creating repositories, not updating
94 121
        repository_name = namify(line[REPOSITORY], number)
95
        if !@existing_repositories[line[ORGANIZATION] + name][labelize(repository_name)]
122

  
123
        # Hash the existing repositories for the product
124
        if !@existing_repositories[line[ORGANIZATION] + name][repository_name]
125
          @api.resource(:repositories).call(:index, {
126
                                              'organization_id' => katello_organization(:name => line[ORGANIZATION]),
127
                                              'library' => true,
128
                                              'all' => false,
129
                                              'product_id' => product_id
130
                                            })['results'].each do |repository|
131
            @existing_repositories[line[ORGANIZATION] + name][repository['name']] = repository
132
          end
133
        end
134

  
135
        if !@existing_repositories[line[ORGANIZATION] + name][repository_name]
96 136
          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
                                   })
137
          if line[REPOSITORY_TYPE] =~ /Red Hat/
138
            # TMP
139
            puts "TMP"
140
            @api.resource(:repositories).call(:index, {
141
                                                'organization_id' => katello_organization(:name => line[ORGANIZATION]),
142
                                                'library' => true,
143
                                                'all' => true,
144
                                                'product_id' => product_id
145
                                              })['results'].each do |repository|
146
              puts repository if repository['name'] == repository_name
147
            end
148
            puts "END TMP"
149
            #repository_id = redhat_create_repository(product_id, repository_name)
150

  
151
          else
152
            repository_id = @api.resource(:repositories).call(:create, {
153
                                       'organization_id' => katello_organization(:name => line[ORGANIZATION]),
154
                                       'name' => repository_name,
155
                                       'label' => labelize(repository_name),
156
                                       'product_id' => product_id,
157
                                       'url' => line[REPOSITORY_URL],
158
                                       'content_type' => content_type(line[REPOSITORY_TYPE])
159
                                     })['id']
160
          end
161
          @existing_repositories[line[ORGANIZATION] + name][line[LABEL]] = repository_id
162

  
163
          puts "TODO: skipping sync"
164
          # task_id = @api.resource(:repositories).call(:sync, {
165
          #                                               'organization_id' => katello_organization(:name => line[ORGANIZATION]),
166
          #                                               'id' => repository_id
167
          #                                             })['id']
168
          # TODO: wait for sync task
105 169
          print "done\n" if option_verbose?
106 170
        end
107 171
      end
......
109 173
    rescue RuntimeError => e
110 174
      raise "#{e}\n       #{line}"
111 175
    end
176

  
177
    private
178

  
179
    def redhat_create_repository(product_id, repository_name)
180
      @api.resource(:repository_sets).call(:index, {
181
                                             'product_id' => product_id
182
                                           })['results'].each do |repository|
183
        puts repository
184
        puts @api.resource(:repository_sets).call(:show, {
185
                                                    'product_id' => product_id,
186
                                                    'id' => repository['id']})
187
        return
188
        if repository['name'] == repository_name
189
          puts repository
190
          @api.resource(:repository_sets).call(:enable, {
191
                                                 'product_id' => product_id,
192
                                                 'repository_id' => repository['id']
193
                                               })
194
          return
195
        end
196
        raise "Repository '#{repository_name}' does not exist"
197
      end
198
    end
199

  
200
    def content_type(repository_type)
201
      case repository_type
202
        when /Yum/
203
          'yum'
204
        when /Puppet/
205
          'puppet'
206
        else
207
        raise "Unrecognized repository type '#{repository_type}'"
208
      end
209
    end
112 210
  end
113 211

  
114
  HammerCLI::MainCommand.subcommand("csv:products", "import/export products and repositories",
115
                                    HammerCLICsv::ProductsCommand)
212
  HammerCLICsv::CsvCommand.subcommand('products',
213
                                      'import or export products',
214
                                      HammerCLICsv::ProductsCommand)
116 215
end

Also available in: Unified diff