Project

General

Profile

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

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

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

    
7
      ORGANIZATIONS = 'Organizations'
8
      LOCATIONS = 'Locations'
9
      DESCRIPTION = 'Description'
10
      SMART_PROXY = 'DNS Smart Proxy'
11
      PARAMETERS = 'Parameters'
12

    
13
      SEPARATOR = ' = '
14

    
15
      def export(csv)
16
        csv << [NAME, ORGANIZATIONS, LOCATIONS, DESCRIPTION, SMART_PROXY, PARAMETERS]
17
        search_options = {:per_page => 999999}
18
        search_options['search'] = "organization=\"#{option_organization}\"" if option_organization
19
        search_options['search'] = "#{search_options['search']} AND #{option_search}" if option_search
20
        @api.resource(:domains).call(:index, search_options)['results'].each do |domain|
21
          domain = @api.resource(:domains).call(:show, {'id' => domain['id']})
22
          raise "Domain 'id=#{domain['id']}' not found" if !domain || domain.empty?
23

    
24
          name = domain['name']
25
          organizations = option_organization ? option_organization : export_column(domain, 'organizations', 'name')
26
          locations = export_column(domain, 'locations', 'name')
27
          description = domain['fullname']
28
          capsule = foreman_smart_proxy(:id => domain['dns_id'])
29
          parameters = export_column(domain, 'parameters') do |parameter|
30
            "#{parameter['name']}#{SEPARATOR}#{parameter['value']}"
31
          end
32
          csv << [name, organizations, locations, description, capsule, parameters]
33
        end
34
      end
35

    
36
      def import
37
        @existing = {}
38
        @api.resource(:domains).call(:index, {:per_page => 999999})['results'].each do |domain|
39
          @existing[domain['name']] = domain['id'] if domain
40
        end
41

    
42
        thread_import do |line|
43
          create_domains_from_csv(line)
44
        end
45
      end
46

    
47
      def create_domains_from_csv(line)
48
        dns_id = foreman_smart_proxy(:name => line[SMART_PROXY])
49
        count(line[COUNT]).times do |number|
50
          name = namify(line[NAME], number)
51
          description = namify(line[DESCRIPTION], number)
52
          params = {
53
            'domain' => {
54
              'name' => name,
55
              'fullname' => description,
56
              'dns_id' => dns_id
57
            }
58
          }
59
          if !@existing.include? name
60
            print _("Creating domain '%{name}'...") % {:name => name} if option_verbose?
61
            domain = @api.resource(:domains).call(:create, params)
62
          else
63
            print _("Updating domain '%{name}'...") % {:name => name} if option_verbose?
64
            params['id'] = @existing[name]
65
            domain = @api.resource(:domains).call(:update, params)
66
          end
67

    
68
          update_organizations(line, domain)
69
          update_locations(line, domain)
70
          import_parameters(domain['id'], line[PARAMETERS])
71

    
72
          puts _("done") if option_verbose?
73
        end
74
      end
75

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

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

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

    
108
          @api.resource(:locations).call(:update, {
109
                                           'id' => location_id,
110
                                           'location' => {
111
                                             'domain_ids' => domains[location]
112
                                           }
113
                                         })
114
        end
115
      end
116

    
117
      def import_parameters(domain_id, parameters)
118
        collect_column(parameters) do |parameter|
119
          (parameter_name, parameter_value) = parameter.split(SEPARATOR)
120

    
121
          results = @api.resource(:parameters).call(:index, { :domain_id => domain_id, :search => "name=\"#{parameter_name}\"" })['results']
122
          params = {
123
            'domain_id' => domain_id,
124
            'parameter' => {
125
              'name' => parameter_name,
126
              'value' => parameter_value
127
            }
128
          }
129
          if results.empty?
130
            @api.resource(:parameters).call(:create, params)
131
          else
132
            params['id'] = results[0]['id']
133
            @api.resource(:parameters).call(:update, params)
134
          end
135
        end
136
      end
137
    end
138
  end
139
end