Project

General

Profile

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

hammer-cli-csv / lib / hammer_cli_csv / smart_proxies.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
require 'hammer_cli'
13
require 'json'
14
require 'csv'
15

    
16
module HammerCLICsv
17
  class CsvCommand
18
    class SmartProxiesCommand < BaseCommand
19
      command_name 'smart-proxies'
20
      desc 'import or export smart proxies'
21

    
22
      ORGANIZATIONS = 'Organizations'
23
      LOCATIONS = 'Locations'
24
      URL = 'URL'
25
      LIFECYCLE_ENVIRONMENTS = 'Lifecycle Environments'
26

    
27
      def export
28
        CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv|
29
          csv << [NAME, COUNT, ORGANIZATIONS, LOCATIONS, URL, LIFECYCLE_ENVIRONMENTS]
30
          @api.resource(:smart_proxies).call(:index, {:per_page => 999999})['results'].each do |smart_proxy|
31
            smart_proxy = @api.resource(:smart_proxies).call(:show, {'id' => smart_proxy['id']})
32
            name = smart_proxy['name']
33
            count = 1
34
            organizations = export_column(smart_proxy, 'organizations', 'name')
35
            locations = export_column(smart_proxy, 'locations', 'name')
36
            url = smart_proxy['url']
37
            csv << [name, count, organizations, locations, url]
38
          end
39
        end
40
      end
41

    
42
      def import
43
        @existing = {}
44
        @api.resource(:smart_proxies).call(:index, {:per_page => 999999})['results'].each do |smart_proxy|
45
          @existing[smart_proxy['url']] = smart_proxy['id'] if smart_proxy
46
        end
47

    
48
        thread_import do |line|
49
          create_smart_proxies_from_csv(line)
50
        end
51
      end
52

    
53
      def create_smart_proxies_from_csv(line)
54
        line[COUNT].to_i.times do |number|
55
          name = namify(line[NAME], number)
56
          if !@existing.include? line[URL]
57
            print "Creating smart proxy '#{name}'..." if option_verbose?
58
            id = @api.resource(:smart_proxies)\
59
              .call(:create, {
60
                      'smart_proxy' => {
61
                        'name' => name,
62
                        'url' => line[URL]
63
                      }
64
                    })['id']
65
          else
66
            print "Updating smart proxy '#{name}'..." if option_verbose?
67
            id = @api.resource(:smart_proxies)\
68
              .call(:update, {
69
                      'id' => @existing[name],
70
                      'smart_proxy' => {
71
                        'name' => name,
72
                        'url' => line[URL]
73
                      }
74
                    })['smart_proxy']['id']
75
          end
76

    
77
          # Update associated resources
78
          associate_organizations(id, line[ORGANIZATIONS], 'smart_proxy')
79
          associate_locations(id, line[LOCATIONS], 'smart_proxy')
80

    
81
          print "done\n" if option_verbose?
82
        end
83
      rescue RuntimeError => e
84
        raise "#{e}\n       #{line}"
85
      end
86
    end
87
  end
88
end