Project

General

Profile

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

hammer-cli-csv / lib / hammer_cli_csv / compute_profiles.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
# TODO: waiting for https://github.com/theforeman/foreman/pull/1326
17

    
18
module HammerCLICsv
19
  class CsvCommand
20
    class ComputeProfilesCommand < BaseCommand
21
      command_name 'compute-profiles'
22
      desc 'import or export compute profiles'
23

    
24
      ORGANIZATIONS = 'Organizations'
25
      LOCATIONS = 'Locations'
26
      DESCRIPTION = 'Description'
27
      PROVIDER = 'Provider'
28
      URL = 'URL'
29

    
30
      def export
31
        CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv|
32
          csv << [NAME, COUNT, ORGANIZATIONS, LOCATIONS, DESCRIPTION, PROVIDER, URL]
33
          @api.resource(:compute_profiles).call(:index, {:per_page => 999999})['results'].each do |compute_profile|
34
            puts compute_profile
35
            compute_profile = @api.resource(:compute_profiles).call(:show, {'id' => compute_profile['id']})
36
            name = compute_profile['name']
37
            count = 1
38
            organizations = export_column(compute_profile, 'organizations', 'name')
39
            locations = export_column(compute_profile, 'locations', 'name')
40
            description = compute_profile['description']
41
            provider = compute_profile['provider']
42
            url = compute_profile['url']
43
            csv << [name, count, organizations, locations, description, provider, url]
44
          end
45
        end
46
      end
47

    
48
      def import
49
        @existing = {}
50
        @api.resource(:compute_profiles).call(:index, {:per_page => 999999})['results'].each do |compute_profile|
51
          @existing[compute_profile['name']] = compute_profile['id'] if compute_profile
52
        end
53

    
54
        thread_import do |line|
55
          create_compute_profiles_from_csv(line)
56
        end
57
      end
58

    
59
      def create_compute_profiles_from_csv(line)
60
        line[COUNT].to_i.times do |number|
61
          name = namify(line[NAME], number)
62
          if !@existing.include? name
63
            print "Creating compute profile '#{name}'..." if option_verbose?
64
            id = @api.resource(:compute_profiles)\
65
              .call(:create, {
66
                      'compute_profile' => {
67
                        'name' => name,
68
                        'url' => line[URL]
69
                      }
70
                    })['id']
71
          else
72
            print "Updating compute profile '#{name}'..." if option_verbose?
73
            id = @api.resource(:compute_profiles)\
74
              .call(:update, {
75
                      'id' => @existing[name],
76
                      'compute_profile' => {
77
                        'name' => name,
78
                        'url' => line[URL]
79
                      }
80
                    })['compute_profile']['id']
81
          end
82

    
83
          # Update associated profiles
84
          associate_organizations(id, line[ORGANIZATIONS], 'compute_profile')
85
          associate_locations(id, line[LOCATIONS], 'compute_profile')
86

    
87
          print "done\n" if option_verbose?
88
        end
89
      rescue RuntimeError => e
90
        raise "#{e}\n       #{line}"
91
      end
92
    end
93
  end
94
end