Project

General

Profile

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

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

1 2be0739f Tom McKay
module HammerCLICsv
2 7bea883f Grant Gainey
  class CsvCommand
3
    class DomainsCommand < BaseCommand
4 26b7d467 Tom McKay
      command_name 'domains'
5
      desc         'import or export domains'
6 2be0739f Tom McKay
7 7bea883f Grant Gainey
      ORGANIZATIONS = 'Organizations'
8 02387fb5 Tom McKay
      LOCATIONS = 'Locations'
9
      DESCRIPTION = 'Description'
10
      SMART_PROXY = 'DNS Smart Proxy'
11
      PARAMETERS = 'Parameters'
12
13
      SEPARATOR = ' = '
14 7bea883f Grant Gainey
15 c06f1783 Tom McKay
      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 6498291e Tom McKay
        search_options['search'] = "#{search_options['search']} AND #{option_search}" if option_search
20 c06f1783 Tom McKay
        @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 02387fb5 Tom McKay
24 c06f1783 Tom McKay
          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 88ccfdbe Tom McKay
          parameters = export_column(domain, 'parameters') do |parameter|
30
            "#{parameter['name']}#{SEPARATOR}#{parameter['value']}"
31
          end
32 c06f1783 Tom McKay
          csv << [name, organizations, locations, description, capsule, parameters]
33 2be0739f Tom McKay
        end
34
      end
35
36 7bea883f Grant Gainey
      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 2be0739f Tom McKay
42 7bea883f Grant Gainey
        thread_import do |line|
43
          create_domains_from_csv(line)
44
        end
45 2be0739f Tom McKay
      end
46
47 7bea883f Grant Gainey
      def create_domains_from_csv(line)
48 02387fb5 Tom McKay
        dns_id = foreman_smart_proxy(:name => line[SMART_PROXY])
49
        count(line[COUNT]).times do |number|
50 7bea883f Grant Gainey
          name = namify(line[NAME], number)
51 02387fb5 Tom McKay
          description = namify(line[DESCRIPTION], number)
52 88ccfdbe Tom McKay
          params = {
53
            'domain' => {
54
              'name' => name,
55
              'fullname' => description,
56
              'dns_id' => dns_id
57
            }
58
          }
59 7bea883f Grant Gainey
          if !@existing.include? name
60 88ccfdbe Tom McKay
            print _("Creating domain '%{name}'...") % {:name => name} if option_verbose?
61
            domain = @api.resource(:domains).call(:create, params)
62 7bea883f Grant Gainey
          else
63 88ccfdbe Tom McKay
            print _("Updating domain '%{name}'...") % {:name => name} if option_verbose?
64
            params['id'] = @existing[name]
65
            domain = @api.resource(:domains).call(:update, params)
66 7bea883f Grant Gainey
          end
67 6bc031bd Tom McKay
68 88ccfdbe Tom McKay
          update_organizations(line, domain)
69
          update_locations(line, domain)
70
          import_parameters(domain['id'], line[PARAMETERS])
71 7bea883f Grant Gainey
72 88ccfdbe Tom McKay
          puts _("done") if option_verbose?
73 6bc031bd Tom McKay
        end
74 2be0739f Tom McKay
      end
75 02387fb5 Tom McKay
76 88ccfdbe Tom McKay
      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 02387fb5 Tom McKay
96 88ccfdbe Tom McKay
      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 02387fb5 Tom McKay
          end
106 88ccfdbe Tom McKay
          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 02387fb5 Tom McKay
        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 88ccfdbe Tom McKay
          params = {
123
            'domain_id' => domain_id,
124
            'parameter' => {
125
              'name' => parameter_name,
126
              'value' => parameter_value
127
            }
128
          }
129 02387fb5 Tom McKay
          if results.empty?
130 88ccfdbe Tom McKay
            @api.resource(:parameters).call(:create, params)
131 02387fb5 Tom McKay
          else
132 88ccfdbe Tom McKay
            params['id'] = results[0]['id']
133
            @api.resource(:parameters).call(:update, params)
134 02387fb5 Tom McKay
          end
135
        end
136
      end
137 2be0739f Tom McKay
    end
138 7bea883f Grant Gainey
  end
139 2be0739f Tom McKay
end