Project

General

Profile

Revision 88ccfdbe

Added by Thomas McKay over 6 years ago

fixes #16660 - i18n, locations added

View differences:

lib/hammer_cli_csv/domains.rb
25 25
          locations = export_column(domain, 'locations', 'name')
26 26
          description = domain['fullname']
27 27
          capsule = foreman_smart_proxy(:id => domain['dns_id'])
28
          parameters = export_parameters(domain['parameters'])
28
          parameters = export_column(domain, 'parameters') do |parameter|
29
            "#{parameter['name']}#{SEPARATOR}#{parameter['value']}"
30
          end
29 31
          csv << [name, organizations, locations, description, capsule, parameters]
30 32
        end
31 33
      end
......
46 48
        count(line[COUNT]).times do |number|
47 49
          name = namify(line[NAME], number)
48 50
          description = namify(line[DESCRIPTION], number)
51
          params = {
52
            'domain' => {
53
              'name' => name,
54
              'fullname' => description,
55
              'dns_id' => dns_id
56
            }
57
          }
49 58
          if !@existing.include? name
50
            print "Creating domain '#{name}'..." if option_verbose?
51
            domain_id = @api.resource(:domains).call(:create, {
52
                                                       'domain' => {
53
                                                         'name' => name,
54
                                                         'fullname' => description,
55
                                                         'dns_id' => dns_id
56
                                                       }
57
                                                     })['id']
59
            print _("Creating domain '%{name}'...") % {:name => name} if option_verbose?
60
            domain = @api.resource(:domains).call(:create, params)
58 61
          else
59
            print "Updating domain '#{name}'..." if option_verbose?
60
            domain_id = @api.resource(:domains).call(:update, {
61
                                                       'id' => @existing[name],
62
                                                       'domain' => {
63
                                                         'name' => name,
64
                                                         'fullname' => description,
65
                                                         'dns_id' => dns_id
66
                                                       }
67
                                                     })['id']
62
            print _("Updating domain '%{name}'...") % {:name => name} if option_verbose?
63
            params['id'] = @existing[name]
64
            domain = @api.resource(:domains).call(:update, params)
68 65
          end
69 66

  
70
          # Update associated resources
71
          domains ||= {}
72
          CSV.parse_line(line[ORGANIZATIONS]).each do |organization|
73
            organization_id = foreman_organization(:name => organization)
74
            if domains[organization].nil?
75
              domains[organization] = @api.resource(:organizations).call(:show, {'id' => organization_id})['domains'].collect do |domain|
76
                domain['id']
77
              end
78
            end
79
            domains[organization] += [domain_id] if !domains[organization].include? domain_id
67
          update_organizations(line, domain)
68
          update_locations(line, domain)
69
          import_parameters(domain['id'], line[PARAMETERS])
80 70

  
81
            @api.resource(:organizations).call(:update, {
82
                                                 'id' => organization_id,
83
                                                 'organization' => {
84
                                                   'domain_ids' => domains[organization]
85
                                                 }
86
                                               })
87
          end
88

  
89
          import_parameters(domain_id, line[PARAMETERS])
90

  
91
          print "done\n" if option_verbose?
71
          puts _("done") if option_verbose?
92 72
        end
93 73
      end
94 74

  
95
      private
96

  
97
      def export_parameters(parameters)
98
        return '' if parameters.nil? || parameters.empty?
75
      def update_organizations(line, domain)
76
        domains ||= {}
77
        CSV.parse_line(line[ORGANIZATIONS]).each do |organization|
78
          organization_id = foreman_organization(:name => organization)
79
          if domains[organization].nil?
80
            domains[organization] = @api.resource(:organizations).call(:show, {'id' => organization_id})['domains'].collect do |existing_domain|
81
              existing_domain['id']
82
            end
83
          end
84
          domains[organization] += [domain['id']] if !domains[organization].include? domain['id']
85

  
86
          @api.resource(:organizations).call(:update, {
87
                                               'id' => organization_id,
88
                                               'organization' => {
89
                                                 'domain_ids' => domains[organization]
90
                                               }
91
                                             })
92
        end
93
      end
99 94

  
100
        values = CSV.generate do |column|
101
          column  << parameters.collect do |parameter|
102
            "#{parameter['name']}#{SEPARATOR}#{parameter['value']}"
95
      def update_locations(line, domain)
96
        return if line[LOCATIONS].nil? || line[LOCATIONS].empty?
97
        domains ||= {}
98
        CSV.parse_line(line[LOCATIONS]).each do |location|
99
          location_id = foreman_location(:name => location)
100
          if domains[location].nil?
101
            domains[location] = @api.resource(:locations).call(:show, {'id' => location_id})['domains'].collect do |existing_domain|
102
              existing_domain['id']
103
            end
103 104
          end
105
          domains[location] += [domain['id']] if !domains[location].include? domain['id']
106

  
107
          @api.resource(:locations).call(:update, {
108
                                           'id' => location_id,
109
                                           'location' => {
110
                                             'domain_ids' => domains[location]
111
                                           }
112
                                         })
104 113
        end
105
        values.delete!("\n")
106 114
      end
107 115

  
108 116
      def import_parameters(domain_id, parameters)
......
110 118
          (parameter_name, parameter_value) = parameter.split(SEPARATOR)
111 119

  
112 120
          results = @api.resource(:parameters).call(:index, { :domain_id => domain_id, :search => "name=\"#{parameter_name}\"" })['results']
121
          params = {
122
            'domain_id' => domain_id,
123
            'parameter' => {
124
              'name' => parameter_name,
125
              'value' => parameter_value
126
            }
127
          }
113 128
          if results.empty?
114
            @api.resource(:parameters).call(:create, {
115
                                                       'domain_id' => domain_id,
116
                                                       'parameter' => {
117
                                                         'name' => parameter_name,
118
                                                         'value' => parameter_value
119
                                                       }
120
                                                     })
129
            @api.resource(:parameters).call(:create, params)
121 130
          else
122
            @api.resource(:parameters).call(:create, {
123
                                                       'id' => results[0]['id'],
124
                                                       'domain_id' => domain_id,
125
                                                       'parameter' => {
126
                                                         'name' => parameter_name,
127
                                                         'value' => parameter_value
128
                                                       }
129
                                                     })
131
            params['id'] = results[0]['id']
132
            @api.resource(:parameters).call(:update, params)
130 133
          end
131 134
        end
132 135
      end
test/data/setup/domains.csv
1
Name,Description,Organizations
2
test.com,,Test Corporation
1
Name,Organizations,Locations,Description,DNS Smart Proxy,Parameters
2
test.com,Test Corporation,"","",,""
test/resources/domains_test.rb
1
require './test/csv_test_helper'
2
require './lib/hammer_cli_csv'
3

  
4
module Resources
5
  class TestDomains < MiniTest::Unit::TestCase
6
    def test_usage
7
      start_vcr
8
      set_user 'admin'
9

  
10
      stdout,stderr = capture {
11
        hammer.run(%W{--reload-cache csv domains --help})
12
      }
13
      assert_equal '', stderr
14
      assert_equal stdout, <<-HELP
15
**** This command is unsupported and is provided as tech preview. ****
16
Usage:
17
     csv domains [OPTIONS]
18

  
19
Options:
20
 --continue-on-error           Continue processing even if individual resource error
21
 --export                      Export current data instead of importing
22
 --file FILE_NAME              CSV file (default to /dev/stdout with --export, otherwise required)
23
 --organization ORGANIZATION   Only process organization matching this name
24
 -h, --help                    print help
25
 -v, --verbose                 be verbose
26
HELP
27
      stop_vcr
28
    end
29
  end
30
end
test/setup/setup_domains.rb
9 9
      stdout,stderr = capture {
10 10
        hammer.run(%W{--reload-cache csv domains --verbose --file test/data/setup/domains.csv})
11 11
      }
12
      assert_equal stderr, ''
12
      assert_equal '', stderr
13 13

  
14 14
      stop_vcr
15 15
    end

Also available in: Unified diff