Project

General

Profile

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

hammer-cli-csv / lib / hammer_cli_csv / content_views.rb @ 561a8ac9

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
module HammerCLICsv
13
  class CsvCommand
14
    class ContentViewsCommand < BaseCommand
15
      command_name 'content-views'
16
      desc         'import or export content-views'
17

    
18
      ORGANIZATION = 'Organization'
19
      DESCRIPTION = 'Description'
20
      COMPOSITE = 'Composite'
21
      REPOSITORIES = 'Repositories'
22

    
23
      def export
24
        # TODO
25
      end
26

    
27
      def import
28
        @existing_contentviews = {}
29

    
30
        thread_import do |line|
31
          create_contentviews_from_csv(line)
32
        end
33
      end
34

    
35
      def create_contentviews_from_csv(line)
36
        if !@existing_contentviews[line[ORGANIZATION]]
37
          @existing_contentviews[line[ORGANIZATION]] ||= {}
38
          @api.resource(:content_views)\
39
            .call(:index, {
40
                    'per_page' => 999999,
41
                    'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
42
                    'nondefault' => true
43
                  })['results'].each do |contentview|
44
            @existing_contentviews[line[ORGANIZATION]][contentview['name']] = contentview['id'] if contentview
45
          end
46
        end
47

    
48
        repository_ids = collect_column(line[REPOSITORIES]) do |repository|
49
          katello_repository(line[ORGANIZATION], :name => repository)
50
        end
51

    
52
        line[COUNT].to_i.times do |number|
53
          name = namify(line[NAME], number)
54
          composite = line[COMPOSITE] == 'Yes' ? true : false
55

    
56
          contentview_id = @existing_contentviews[line[ORGANIZATION]][name]
57
          if !contentview_id
58
            print "Creating content view '#{name}'..." if option_verbose?
59
            contentview_id = @api.resource(:content_views)\
60
              .call(:create, {
61
                      'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
62
                      'name' => name,
63
                      'label' => labelize(name),
64
                      'description' => line[DESCRIPTION],
65
                      'composite' => composite,
66
                      'repository_ids' => repository_ids
67
                    })['id']
68
            @existing_contentviews[line[ORGANIZATION]][name] = contentview_id
69
          else
70
            print "Updating content view '#{name}'..." if option_verbose?
71
            @api.resource(:content_views)\
72
              .call(:update, {
73
                      'id' => contentview_id,
74
                      'description' => line[DESCRIPTION],
75
                      'repository_ids' => repository_ids
76
                    })
77
          end
78
          puts 'done' if option_verbose?
79
        end
80

    
81
      rescue RuntimeError => e
82
        raise "#{e}\n       #{line}"
83
      end
84
    end
85
  end
86
end