Project

General

Profile

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

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

1 ee942928 Tom McKay
module HammerCLICsv
2 7bea883f Grant Gainey
  class CsvCommand
3
    class OrganizationsCommand < BaseCommand
4
      command_name 'organizations'
5
      desc         'import or export organizations'
6 ee942928 Tom McKay
7 a389840d Tom McKay
      LABEL = 'Label'
8 7bea883f Grant Gainey
      DESCRIPTION = 'Description'
9
10 c06f1783 Tom McKay
      def export(csv)
11
        csv << [NAME, LABEL, DESCRIPTION]
12 c2496839 Tom McKay
13 6498291e Tom McKay
        @api.resource(:organizations).call(:index, {:per_page => 999999})['results'].each do |organization|
14
          next if option_organization && organization['name'] != option_organization
15
          csv << [organization['name'], organization['label'], organization['description']]
16 ee942928 Tom McKay
        end
17
      end
18
19 7bea883f Grant Gainey
      def import
20
        @existing = {}
21
        @api.resource(:organizations).call(:index, {:per_page => 999999})['results'].each do |organization|
22 a77acc4a Tom McKay
          @existing[organization['name']] = organization['id'] if organization
23 7bea883f Grant Gainey
        end
24 ee942928 Tom McKay
25 7bea883f Grant Gainey
        thread_import do |line|
26
          create_organizations_from_csv(line)
27
        end
28 ee942928 Tom McKay
      end
29
30 7bea883f Grant Gainey
      def create_organizations_from_csv(line)
31 02387fb5 Tom McKay
        count(line[COUNT]).times do |number|
32 7bea883f Grant Gainey
          name = namify(line[NAME], number)
33 c4dbc7a3 Tom McKay
          return if option_organization && name != option_organization
34 a389840d Tom McKay
          label = namify(line[LABEL], number)
35 502df2e4 Tom McKay
          organization_id = @existing[name]
36
          if organization_id.nil?
37 7bea883f Grant Gainey
            print "Creating organization '#{name}'... " if option_verbose?
38
            @api.resource(:organizations).call(:create, {
39 3e5a297d Tom McKay
                'name' => name,
40
                'organization' => {
41
                    'name' => name,
42
                    'label' => label,
43
                    'description' => line[DESCRIPTION]
44
                }
45
            })
46 7bea883f Grant Gainey
          else
47
            print "Updating organization '#{name}'... " if option_verbose?
48 502df2e4 Tom McKay
            organization = @api.resource(:organizations).call(:show, {'id' => organization_id})
49 7bea883f Grant Gainey
            @api.resource(:organizations).call(:update, {
50 502df2e4 Tom McKay
                'id' => organization_id,
51 3e5a297d Tom McKay
                'organization' => {
52 502df2e4 Tom McKay
                    'id' => organization_id,
53 3e5a297d Tom McKay
                    'description' => line[DESCRIPTION]
54
                }
55
            })
56 7bea883f Grant Gainey
          end
57
          print "done\n" if option_verbose?
58 ee942928 Tom McKay
        end
59
      end
60
    end
61
  end
62
end