Project

General

Profile

Revision 370106bb

Added by Thomas McKay about 9 years ago

foreman-env - out / in / update working

View differences:

lib/hammer_cli_csv.rb
17 17
  require 'hammer_cli_csv/activation_keys'
18 18

  
19 19
  require 'hammer_cli_csv/operating_systems'
20
  require 'hammer_cli_csv/environments'
20 21

  
21 22
end
lib/hammer_cli_csv/environments.rb
1
# Copyright (c) 2013 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 'katello_api'
38
require 'foreman_api'
39
require 'json'
40
require 'csv'
41

  
42
module HammerCLICsv
43
  class EnvironmentsCommand < BaseCommand
44

  
45
    def initialize(*args)
46
      super(args)
47
      @environment_api = ForemanApi::Resources::Environment.new(@init_options[:foreman])
48
    end
49

  
50
    def execute
51
      csv_export? ? export : import
52

  
53
      HammerCLI::EX_OK
54
    end
55

  
56
    def export
57
      CSV.open(csv_file, 'wb') do |csv|
58
        csv << ['Name']
59
        @environment_api.index({}, HEADERS)[0].each do |environment|
60
          environment = environment['environment']
61
          name = environment['name']
62
          count = 1
63
          csv << [name, count]
64
        end
65
      end
66
    end
67

  
68
    def import
69
      @existing = {}
70
      @environment_api.index[0].each do |environment|
71
        environment = environment['environment']
72
        @existing[environment['name']] = environment['id']
73
      end
74

  
75
      thread_import do |line|
76
        create_environments_from_csv(line)
77
      end
78
    end
79

  
80
    def create_environments_from_csv(line)
81
      details = parse_environment_csv(line)
82

  
83
      details[:count].times do |number|
84
        name = namify(details[:name_format], number)
85
        if !@existing.include? name
86
          print "Creating environment '#{name}'..." if verbose?
87
          @environment_api.create({
88
                             'environment' => {
89
                               'name' => name
90
                             }
91
                           }, HEADERS)
92
          print "done\n" if verbose?
93
        else
94
          print "Updating environment '#{name}'..." if verbose?
95
          @environment_api.create({
96
                             'id' => @existing["#{name}-#{details[:major]}-#{details[:minor]}"],
97
                             'environment' => {
98
                               'name' => name
99
                             }
100
                           }, HEADERS)
101
          print "done\n" if verbose?
102
        end
103
      end
104
    end
105

  
106
    def parse_environment_csv(line)
107
      keys = [:name_format, :count]
108
      details = CSV.parse(line).map { |a| Hash[keys.zip(a)] }[0]
109

  
110
      details[:count] = details[:count].to_i
111

  
112
      details
113
    end
114
  end
115

  
116
  HammerCLI::MainCommand.subcommand("csv:environments", "ping the katello server", HammerCLICsv::EnvironmentsCommand)
117
end
lib/hammer_cli_csv/operating_systems.rb
60 60
      CSV.open(csv_file, 'wb') do |csv|
61 61
        csv << ['Name','Count','Major','Minor', 'Family']
62 62
        @operatingsystem_api.index({}, HEADERS)[0].each do |operatingsystem|
63
          name = operatingsystem['operatingsystem']['name']
63
          operatingsystem = operatingsystem['operatingsystem']
64
          name = operatingsystem['name']
64 65
          count = 1
65
          major = operatingsystem['operatingsystem']['major']
66
          minor = operatingsystem['operatingsystem']['minor']
67
          family = operatingsystem['operatingsystem']['family']
66
          major = operatingsystem['major']
67
          minor = operatingsystem['minor']
68
          family = operatingsystem['family']
68 69
          csv << [name, count, major, minor, family]
69 70
        end
70 71
      end
test/data/environments.csv
1
Name
2
Library,1
3
Development,1
4
Quality,1
5
Production,1
6
Retired,1
7

  
test/data/operating_systems.csv
1
Name,Count,Major,Minor,Family
2
RHEL,1,"6","4",Redhat
3
RHEL,1,"6","3",Redhat

Also available in: Unified diff