Project

General

Profile

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

hammer-cli-csv / lib / hammer_cli_csv / organizations.rb @ 502df2e4

1
module HammerCLICsv
2
  class CsvCommand
3
    class OrganizationsCommand < BaseCommand
4
      command_name 'organizations'
5
      desc         'import or export organizations'
6

    
7
      LABEL = 'Label'
8
      DESCRIPTION = 'Description'
9

    
10
      def export
11
        CSV.open(option_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv|
12
          csv << [NAME, LABEL, DESCRIPTION]
13

    
14
          if @server_status['release'] == 'Headpin'
15
            @headpin.get(:organizations).each do |organization|
16
              next if option_organization && organization['name'] != option_organization
17
              csv << [organization['name'], organization['label'], organization['description']]
18
            end
19
          else
20
            @api.resource(:organizations).call(:index, {:per_page => 999999})['results'].each do |organization|
21
              next if option_organization && organization['name'] != option_organization
22
              csv << [organization['name'], organization['label'], organization['description']]
23
            end
24
          end
25
        end
26
      end
27

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

    
34
        thread_import do |line|
35
          create_organizations_from_csv(line)
36
        end
37
      end
38

    
39
      def create_organizations_from_csv(line)
40
        count(line[COUNT]).times do |number|
41
          name = namify(line[NAME], number)
42
          return if option_organization && name != option_organization
43
          label = namify(line[LABEL], number)
44
          organization_id = @existing[name]
45
          if organization_id.nil?
46
            print "Creating organization '#{name}'... " if option_verbose?
47
            @api.resource(:organizations).call(:create, {
48
                'name' => name,
49
                'organization' => {
50
                    'name' => name,
51
                    'label' => label,
52
                    'description' => line[DESCRIPTION]
53
                }
54
            })
55
          else
56
            print "Updating organization '#{name}'... " if option_verbose?
57
            organization = @api.resource(:organizations).call(:show, {'id' => organization_id})
58
            @api.resource(:organizations).call(:update, {
59
                'id' => organization_id,
60
                'organization' => {
61
                    'id' => organization_id,
62
                    'description' => line[DESCRIPTION]
63
                }
64
            })
65
          end
66
          print "done\n" if option_verbose?
67
        end
68
      end
69
    end
70
  end
71
end