Project

General

Profile

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

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

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 LifecycleEnvironmentsCommand < BaseCommand
30

    
31
    LABEL = "Label"
32
    ORGANIZATION = "Organization"
33
    PRIORENVIRONMENT = "Prior Environment"
34
    DESCRIPTION = "Description"
35

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

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

    
70
      thread_import do |line|
71
        create_environments_from_csv(line)
72
      end
73
    end
74

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

    
108
  HammerCLI::MainCommand.subcommand("csv:lifecycleenv", "Import or export lifecycle environments", HammerCLICsv::LifecycleEnvironmentsCommand)
109
end