Project

General

Profile

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

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

1
# Copyright (c) 2014 Red Hat
2
#
3
# MIT License
4
#
5
# Permission is hereby granted, free of charge, to any person obtaining
6
# a copy of this software and associated documentation files (the
7
# "Software"), to deal in the Software without restriction, including
8
# without limitation the rights to use, copy, modify, merge, publish,
9
# distribute, sublicense, and/or sell copies of the Software, and to
10
# permit persons to whom the Software is furnished to do so, subject to
11
# the following conditions:
12
#
13
# The above copyright notice and this permission notice shall be
14
# included in all copies or substantial portions of the Software.
15
#
16
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
#
24
#
25
# -= Environments CSV =-
26
#
27
# Columns
28
#   Name
29
#     - Environment name
30
#     - May contain '%d' which will be replaced with current iteration number of Count
31
#     - eg. "os%d" -> "os1"
32
#   Count
33
#     - Number of times to iterate on this line of the CSV file
34
#
35

    
36
require 'hammer_cli'
37
require 'json'
38
require 'csv'
39

    
40
module HammerCLICsv
41
  class LifecycleEnvironmentsCommand < BaseCommand
42

    
43
    LABEL = "Label"
44
    ORGANIZATION = "Organization"
45
    PRIORENVIRONMENT = "Prior Environment"
46
    DESCRIPTION = "Description"
47

    
48
    def export
49
      CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv|
50
        csv << [NAME, COUNT, LABEL, ORGANIZATION, PRIORENVIRONMENT, DESCRIPTION]
51
        @k_organization_api.index({'per_page' => 999999})[0]['results'].each do |organization|
52
          @k_environment_api.index({
53
                                     'per_page' => 999999,
54
                                     'organization_id' => organization['label']
55
                                   })[0]['results'].each do |environment|
56
            if environment['label'] != 'Library'
57
              name = environment['name']
58
              count = 1
59
              label = environment['label']
60
              prior = environment['prior']
61
              description = environment['description']
62
              csv << [name, count, label, organization['name'], prior, description]
63
            end
64
          end
65
        end
66
      end
67
    end
68

    
69
    def import
70
      @existing = {}
71
      @k_organization_api.index({'per_page' => 999999})[0]['results'].each do |organization|
72
        @k_environment_api.index({
73
                                   'per_page' => 999999,
74
                                   'organization_id' => katello_organization(:name => organization['name'])
75
                                 })[0]['results'].each do |environment|
76
          @existing[organization['name']] ||= {}
77
          @existing[organization['name']][environment['name']] = environment['id'] if environment
78
        end
79
      end
80

    
81
      thread_import do |line|
82
        create_environments_from_csv(line)
83
      end
84
    end
85

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

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