Project

General

Profile

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

hammer-cli-csv / lib / hammer_cli_csv / lifecycle_environments.rb @ 9439ade5

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
# -= Environments CSV =-
14
#
15
# Columns
16
#   Name
17
#     - Environment 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

    
28
module HammerCLICsv
29
  class CsvCommand
30
    class LifecycleEnvironmentsCommand < BaseCommand
31

    
32
      command_name "lifecycle-environments"
33
      desc         "import or export lifecycle environments"
34

    
35
      LABEL = "Label"
36
      ORGANIZATION = "Organization"
37
      PRIORENVIRONMENT = "Prior Environment"
38
      DESCRIPTION = "Description"
39

    
40
      def export
41
        CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv|
42
          csv << [NAME, COUNT, LABEL, ORGANIZATION, PRIORENVIRONMENT, DESCRIPTION]
43
          @api.resource(:organizations).call(:index, {'per_page' => 999999})['results'].each do |organization|
44
            @api.resource(:environments).call(:index, {
45
                                       'per_page' => 999999,
46
                                       'organization_id' => organization['label']
47
                                     })['results'].each do |environment|
48
              if environment['label'] != 'Library'
49
                name = environment['name']
50
                count = 1
51
                label = environment['label']
52
                prior = environment['prior']
53
                description = environment['description']
54
                csv << [name, count, label, organization['name'], prior, description]
55
              end
56
            end
57
          end
58
        end
59
      end
60

    
61
      def import
62
        @existing = {}
63
        @api.resource(:organizations).call(:index, {'per_page' => 999999})['results'].each do |organization|
64
          @api.resource(:environments).call(:index, {
65
                                     'per_page' => 999999,
66
                                     'organization_id' => katello_organization(:name => organization['name']),
67
                                     'library' => true
68
                                   })['results'].each do |environment|
69
            @existing[organization['name']] ||= {}
70
            @existing[organization['name']][environment['name']] = environment['id'] if environment
71
          end
72
        end
73

    
74
        thread_import do |line|
75
          create_environments_from_csv(line)
76
        end
77
      end
78

    
79
      def create_environments_from_csv(line)
80
        line[COUNT].to_i.times do |number|
81
          name = namify(line[NAME], number)
82
          label = namify(line[LABEL], number)
83
          prior = namify(line[PRIORENVIRONMENT], number)
84
          raise "Organization '#{line[ORGANIZATION]}' does not exist" if !@existing.include? line[ORGANIZATION]
85
          if !@existing[line[ORGANIZATION]].include? name
86
            print "Creating environment '#{name}'..." if option_verbose?
87
            @api.resource(:environments).call(:create, {
88
                                        'organization_id' => katello_organization(:name => line[ORGANIZATION]),
89
                                        'name' => name,
90
                                        'label' => label,
91
                                        'prior' => katello_environment(line[ORGANIZATION], :name => prior),
92
                                        'description' => line[DESCRIPTION]
93
                                      })
94
          else
95
            print "Updating environment '#{name}'..." if option_verbose?
96
            @api.resource(:environments).call(:update, {
97
                                        'id' => @existing[line[ORGANIZATION]][name],
98
                                        'name' => name,
99
                                        'new_name' => name,
100
                                        'organization_id' => katello_organization(:name => line[ORGANIZATION]),
101
                                        'prior' => prior,
102
                                        'description' => line[DESCRIPTION]
103
                                      })
104
          end
105
          print "done\n" if option_verbose?
106
        end
107
      rescue RuntimeError => e
108
        raise "#{e}\n       #{line}"
109
      end
110
    end
111
  end
112
end