Project

General

Profile

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

hammer-cli-csv / lib / hammer_cli_csv / lifecycle_environments.rb @ 561a8ac9

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)\
42
            .call(:index, {
43
                    'per_page' => 999999
44
                  })['results'].each do |organization|
45
            @api.resource(:lifecycle_environments)\
46
              .call(:index, {
47
                      'per_page' => 999999,
48
                      'organization_id' => organization['id']
49
                    })['results'].each do |environment|
50
              if environment['name'] != 'Library'
51
                name = environment['name']
52
                count = 1
53
                prior = environment['prior']
54
                description = environment['description']
55
                csv << [name, count, organization['name'], prior, description]
56
              end
57
            end
58
          end
59
        end
60
      end
61

    
62
      def import
63
        @existing = {}
64
        @api.resource(:organizations)\
65
          .call(:index, {
66
                  'per_page' => 999999
67
                })['results'].each do |organization|
68
          @api.resource(:lifecycle_environments)\
69
            .call(:index, {
70
                    'per_page' => 999999,
71
                    'organization_id' => foreman_organization(:name => organization['name'])
72
                  })['results'].each do |environment|
73
            @existing[organization['name']] ||= {}
74
            @existing[organization['name']][environment['name']] = environment['id'] if environment
75
          end
76
        end
77

    
78
        thread_import do |line|
79
          create_environments_from_csv(line)
80
        end
81
      end
82

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