Project

General

Profile

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

hammer-cli-csv / lib / hammer_cli_csv / content_views.rb @ f8ecc788

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 ContentViewsCommand < BaseCommand
31

    
32
    ORGANIZATION = 'Organization'
33
    DESCRIPTION = 'Description'
34
    PRODUCT = 'Product'
35
    REPOSITORY = 'Repository'
36

    
37
    def export
38
      # TODO
39
    end
40

    
41
    def import
42
      @existing_contentviews = {}
43

    
44
      thread_import do |line|
45
        create_contentviews_from_csv(line)
46
      end
47
    end
48

    
49
    def create_contentviews_from_csv(line)
50
      if !@existing_contentviews[line[ORGANIZATION]]
51
        @existing_contentviews[line[ORGANIZATION]] ||= {}
52
        @api.resource(:contentviewdefinitions).call(:index, {'organization_id' => line[ORGANIZATION], 'page_size' => 999999, 'paged' => true})['results'].each do |contentview|
53
          @existing_contentviews[line[ORGANIZATION]][contentview['name']] = contentview['id'] if contentview
54
        end
55
      end
56

    
57
      line[COUNT].to_i.times do |number|
58
        name = namify(line[NAME], number)
59
        contentview_id = @existing_contentviews[line[ORGANIZATION]][name]
60
        if !contentview_id
61
          print "Creating content view '#{name}'..." if option_verbose?
62
          contentview_id = @api.resource(:contentviewdefinitions).call(:create, {
63
                                                                 'organization_id' => line[ORGANIZATION],
64
                                                                 'name' => name,
65
                                                                 'label' => labelize(name),
66
                                                                 'description' => line[DESCRIPTION],
67
                                                                 'composite' => false # TODO: add column?
68
                                                               })['id']
69
          @existing_contentviews[line[ORGANIZATION]][name] = contentview_id
70
        else
71
          print "Updating content view '#{name}'..." if option_verbose?
72
          @api.resource(:contentviewdefinitions).call(:create, {
73
                                                'description' => line[DESCRIPTION],
74
                                              })
75
        end
76

    
77
        if line[REPOSITORY]
78
          puts "UPDATING REPOSITORY"
79
        elsif line[PRODUCT]
80
          puts "UPDATING PRODUCT"
81
        end
82
        print "done\n" if option_verbose?
83

    
84
=begin
85
        # Only creating repositories, not updating
86
        repository_name = namify(line[REPOSITORY], number)
87
        if !@existing_repositories[line[ORGANIZATION] + name][labelize(repository_name)]
88
          print "Creating repository '#{repository_name}' in contentview '#{name}'..." if option_verbose?
89
          @api.resource(:repositorys).call(:create, {
90
                                     'name' => repository_name,
91
                                     'label' => labelize(repository_name),
92
                                     'contentview_id' => contentview_id,
93
                                     'url' => line[REPOSITORY_URL],
94
                                     'content_type' => line[REPOSITORY_TYPE]
95
                                   })
96
          print "done\n" if option_verbose?
97
        end
98
=end
99
      end
100

    
101
    rescue RuntimeError => e
102
      raise "#{e}\n       #{line}"
103
    end
104
  end
105

    
106
  HammerCLICsv::CsvCommand.subcommand("content-views",
107
                                      "import or export content-views",
108
                                      HammerCLICsv::ContentViewsCommand)
109
end