Project

General

Profile

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

hammer-cli-csv / lib / hammer_cli_csv / lifecycle_environments.rb @ a77acc4a

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
      command_name 'lifecycle-environments'
32
      desc         'import or export lifecycle environments'
33

    
34
      LABEL = 'Label'
35
      ORGANIZATION = 'Organization'
36
      PRIORENVIRONMENT = 'Prior Environment'
37
      DESCRIPTION = 'Description'
38

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

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

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

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