Project

General

Profile

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

hammer-cli-csv / lib / hammer_cli_csv / compute_profiles.rb @ f4fb9c20

1
# TODO: waiting for https://github.com/theforeman/foreman/pull/1326
2
module HammerCLICsv
3
  class CsvCommand
4
    class ComputeProfilesCommand < BaseCommand
5
      command_name 'compute-profiles'
6
      desc 'import or export compute profiles'
7

    
8
      ORGANIZATIONS = 'Organizations'
9
      LOCATIONS = 'Locations'
10
      DESCRIPTION = 'Description'
11
      PROVIDER = 'Provider'
12
      URL = 'URL'
13

    
14
      def export
15
        csv << [NAME, ORGANIZATIONS, LOCATIONS, DESCRIPTION, PROVIDER, URL]
16
        @api.resource(:compute_profiles).call(:index, {:per_page => 999999})['results'].each do |compute_profile|
17
          puts compute_profile
18
          compute_profile = @api.resource(:compute_profiles).call(:show, {'id' => compute_profile['id']})
19
          name = compute_profile['name']
20
          organizations = export_column(compute_profile, 'organizations', 'name')
21
          locations = export_column(compute_profile, 'locations', 'name')
22
          description = compute_profile['description']
23
          provider = compute_profile['provider']
24
          url = compute_profile['url']
25
          csv << [name, organizations, locations, description, provider, url]
26
        end
27
      end
28

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

    
35
        thread_import do |line|
36
          create_compute_profiles_from_csv(line)
37
        end
38
      end
39

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

    
62
          # Update associated profiles
63
          associate_organizations(id, line[ORGANIZATIONS], 'compute_profile')
64
          associate_locations(id, line[LOCATIONS], 'compute_profile')
65

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