Project

General

Profile

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

hammer-cli-csv / lib / hammer_cli_csv / content_views.rb @ 9d9e4494

1
# Copyright (c) 2013 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 ContentViewsCommand < BaseCommand
45

    
46
    ORGANIZATION = 'Organization'
47
    DESCRIPTION = 'Description'
48
    PRODUCT = 'Product'
49
    REPOSITORY = 'Repository'
50

    
51
    def export
52
      # TODO
53
    end
54

    
55
    def import
56
      @existing_contentviews = {}
57

    
58
      thread_import do |line|
59
        create_contentviews_from_csv(line)
60
      end
61
    end
62

    
63
    def create_contentviews_from_csv(line)
64
      if !@existing_contentviews[line[ORGANIZATION]]
65
        @existing_contentviews[line[ORGANIZATION]] ||= {}
66
        @k_contentviewdefinition_api.index({'organization_id' => line[ORGANIZATION], 'page_size' => 999999, 'paged' => true})[0]['results'].each do |contentview|
67
          @existing_contentviews[line[ORGANIZATION]][contentview['name']] = contentview['id'] if contentview
68
        end
69
      end
70

    
71
      line[COUNT].to_i.times do |number|
72
        name = namify(line[NAME], number)
73
        contentview_id = @existing_contentviews[line[ORGANIZATION]][name]
74
        if !contentview_id
75
          print "Creating content view '#{name}'..." if verbose?
76
          contentview_id = @k_contentviewdefinition_api.create({
77
                                                                 'organization_id' => line[ORGANIZATION],
78
                                                                 'name' => name,
79
                                                                 'label' => labelize(name),
80
                                                                 'description' => line[DESCRIPTION],
81
                                                                 'composite' => false # TODO: add column?
82
                                                               })[0]['id']
83
          @existing_contentviews[line[ORGANIZATION]][name] = contentview_id
84
        else
85
          print "Updating content view '#{name}'..." if verbose?
86
          @k_contentviewdefinition_api.create({
87
                                                'description' => line[DESCRIPTION],
88
                                              })
89
        end
90

    
91
        if line[REPOSITORY]
92
          puts "UPDATING REPOSITORY"
93
        elsif line[PRODUCT]
94
          puts "UPDATING PRODUCT"
95
        end
96
        print "done\n" if verbose?
97

    
98
=begin
99
        # Only creating repositories, not updating
100
        repository_name = namify(line[REPOSITORY], number)
101
        if !@existing_repositories[line[ORGANIZATION] + name][labelize(repository_name)]
102
          print "Creating repository '#{repository_name}' in contentview '#{name}'..." if verbose?
103
          @k_repository_api.create({
104
                                     'name' => repository_name,
105
                                     'label' => labelize(repository_name),
106
                                     'contentview_id' => contentview_id,
107
                                     'url' => line[REPOSITORY_URL],
108
                                     'content_type' => line[REPOSITORY_TYPE]
109
                                   })
110
          print "done\n" if verbose?
111
        end
112
=end
113
      end
114

    
115
    rescue RuntimeError => e
116
      raise RuntimeError.new("#{e}\n       #{line}")
117
    end
118
  end
119

    
120
  HammerCLI::MainCommand.subcommand("csv:contentviews", "import/export content views", HammerCLICsv::ContentViewsCommand)
121
end