Project

General

Profile

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

hammer-cli-csv / lib / hammer_cli_csv / lifecycle_environments.rb @ 8c7ba4af

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
      ORGANIZATION = 'Organization'
35
      PRIORENVIRONMENT = 'Prior Environment'
36
      DESCRIPTION = 'Description'
37

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

    
58
      def import
59
        @existing = {}
60
        @api.resource(:organizations).call(:index, {'per_page' => 999999})['results'].each do |organization|
61
          @api.resource(:lifecycle_environments).call(:index, {
62
                                     'per_page' => 999999,
63
                                     'organization_id' => foreman_organization(:name => organization['name']),
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
          prior = namify(line[PRIORENVIRONMENT], number)
79
          raise "Organization '#{line[ORGANIZATION]}' does not exist" if !@existing.include? line[ORGANIZATION]
80
          if !@existing[line[ORGANIZATION]].include? name
81
            print "Creating environment '#{name}'..." if option_verbose?
82
            @api.resource(:lifecycle_environments).call(:create, {
83
                                        'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
84
                                        'name' => name,
85
                                        'prior' => lifecycle_environment(line[ORGANIZATION], :name => prior),
86
                                        'description' => line[DESCRIPTION]
87
                                      })
88
          else
89
            print "Updating environment '#{name}'..." if option_verbose?
90
            @api.resource(:lifecycle_environments).call(:update, {
91
                                        'id' => @existing[line[ORGANIZATION]][name],
92
                                        'name' => name,
93
                                        'new_name' => name,
94
                                        'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
95
                                        'prior' => prior,
96
                                        'description' => line[DESCRIPTION]
97
                                      })
98
          end
99
          print "done\n" if option_verbose?
100
        end
101
      rescue RuntimeError => e
102
        raise "#{e}\n       #{line}"
103
      end
104
    end
105
  end
106
end