Project

General

Profile

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

hammer-cli-csv / lib / hammer_cli_csv / smart_proxies.rb @ f92e695a

1
module HammerCLICsv
2
  class CsvCommand
3
    class SmartProxiesCommand < BaseCommand
4
      command_name 'smart-proxies'
5
      desc 'import or export smart proxies'
6

    
7
      ORGANIZATIONS = 'Organizations'
8
      LOCATIONS = 'Locations'
9
      URL = 'URL'
10
      LIFECYCLE_ENVIRONMENTS = 'Lifecycle Environments'
11

    
12
      def export(csv)
13
        csv << [NAME, ORGANIZATIONS, LOCATIONS, URL, LIFECYCLE_ENVIRONMENTS]
14
        @api.resource(:smart_proxies).call(:index, {
15
            :per_page => 999999,
16
            :search => option_search
17
        })['results'].each do |smart_proxy|
18
          smart_proxy = @api.resource(:smart_proxies).call(:show, {'id' => smart_proxy['id']})
19
          name = smart_proxy['name']
20
          organizations = export_column(smart_proxy, 'organizations', 'name')
21
          locations = export_column(smart_proxy, 'locations', 'name')
22
          url = smart_proxy['url']
23
          csv << [name, organizations, locations, url]
24
        end
25
      end
26

    
27
      def import
28
        @existing = {}
29
        @api.resource(:smart_proxies).call(:index, {:per_page => 999999})['results'].each do |smart_proxy|
30
          @existing[smart_proxy['url']] = smart_proxy['id'] if smart_proxy
31
        end
32

    
33
        thread_import do |line|
34
          create_smart_proxies_from_csv(line)
35
        end
36
      end
37

    
38
      def create_smart_proxies_from_csv(line)
39
        count(line[COUNT]).times do |number|
40
          name = namify(line[NAME], number)
41
          id = @existing[line[URL]]
42
          if id.nil?
43
            print "Creating smart proxy '#{name}'..." if option_verbose?
44
            id = @api.resource(:smart_proxies).call(:create, {
45
                'smart_proxy' => {
46
                    'name' => name,
47
                    'url' => line[URL]
48
                }
49
            })['id']
50
          else
51
            print "Updating smart proxy '#{name}'..." if option_verbose?
52
            @api.resource(:smart_proxies).call(:update, {
53
                'id' => id,
54
                'smart_proxy' => {
55
                    'name' => name,
56
                    'url' => line[URL]
57
                }
58
            })
59
          end
60

    
61
          # Update associated resources
62
          associate_organizations(id, line[ORGANIZATIONS], 'smart_proxy')
63
          associate_locations(id, line[LOCATIONS], 'smart_proxy')
64

    
65
          print "done\n" if option_verbose?
66
        end
67
      end
68
    end
69
  end
70
end