Project

General

Profile

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

hammer-cli-csv / lib / hammer_cli_csv / subnets.rb @ c06f1783

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

    
7
      ORGANIZATIONS = 'Organizations'
8
      LOCATIONS = 'Locations'
9
      NETWORK = 'Network'
10
      NETWORK_MASK = 'Network Mask'
11
      NETWORK_FROM = 'From'
12
      NETWORK_TO = 'To'
13
      DOMAINS = 'Domains'
14
      GATEWAY = 'Gateway'
15
      DHCP_PROXY = 'DHCP Proxy'
16
      TFTP_PROXY = 'TFTP Proxy'
17
      DNS_PROXY = 'DNS Proxy'
18
      DNS_PRIMARY = 'DNS Primary'
19
      DNS_SECONDARY = 'DNS Secondary'
20
      VLAN_ID = 'VLAN ID'
21

    
22
      def export(csv)
23
        csv << [NAME, ORGANIZATIONS, LOCATIONS, NETWORK, NETWORK_MASK,
24
                NETWORK_FROM, NETWORK_TO, DOMAINS, GATEWAY, DHCP_PROXY, TFTP_PROXY, DNS_PROXY,
25
                DNS_PRIMARY, DNS_SECONDARY, VLAN_ID]
26
        @api.resource(:subnets).call(:index, {:per_page => 999999})['results'].each do |subnet|
27
          subnet = @api.resource(:subnets).call(:show, {'id' => subnet['id']})
28

    
29
          name = subnet['name']
30
          organizations = export_column(subnet, 'organizations', 'name')
31
          locations = export_column(subnet, 'locations', 'name')
32
          network = subnet['network']
33
          network_mask = subnet['mask']
34
          network_from = subnet['from']
35
          network_to = subnet['to']
36
          domains = export_column(subnet, 'domains', 'name')
37
          gateway = subnet['gateway']
38
          dhcp_proxy = (subnet['dhcp'] && subnet['dhcp'].key?('name')) ? subnet['dhcp']['name'] : ''
39
          tftp_proxy = (subnet['tftp'] && subnet['tftp'].key?('name')) ? subnet['tftp']['name'] : ''
40
          dns_proxy = (subnet['dns'] && subnet['dns'].key?('name')) ? subnet['dns']['name'] : ''
41
          dns_primary = subnet['dns_primary']
42
          dns_secondary = subnet['dns_secondary']
43
          vlan_id = subnet['vlanid']
44
          csv << [name, organizations, locations, network, network_mask,
45
                  network_from, network_to, domains, gateway, dhcp_proxy, tftp_proxy, dns_proxy,
46
                  dns_primary, dns_secondary, vlan_id]
47
        end
48
      end
49

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

    
56
        thread_import do |line|
57
          create_subnets_from_csv(line)
58
        end
59
      end
60

    
61
      def create_subnets_from_csv(line)
62
        line[DOMAINS] = (CSV.parse_line(line[DOMAINS]) || []).collect do |domain|
63
          foreman_domain(:name => domain)
64
        end
65

    
66
        count(line[COUNT]).times do |number|
67
          name = namify(line[NAME], number)
68
          if !@existing.include? name
69
            print "Creating subnet '#{name}'..." if option_verbose?
70
            id = @api.resource(:subnets).call(:create, {
71
                'subnet' => {
72
                  'name' => name,
73
                  'network' => line[NETWORK],
74
                  'mask' => line[NETWORK_MASK],
75
                  #'from' => line[NETWORK_FROM],
76
                  #'to' => line[NETWORK_TO],
77
                  #'domain_ids' => line[DOMAINS]
78
                }
79
            })['id']
80
          else
81
            print "Updating subnet '#{name}'..." if option_verbose?
82
            id = @api.resource(:subnets).call(:update, {
83
                'id' => @existing[name],
84
                'subnet' => {
85
                    'name' => name,
86
                    'network' => line[NETWORK],
87
                    'mask' => line[NETWORK_MASK],
88
                    'from' => line[NETWORK_FROM],
89
                    'to' => line[NETWORK_TO],
90
                    'domain_ids' => line[DOMAINS]
91
                }
92
            })['id']
93
          end
94

    
95
          # Update associated resources
96
          associate_organizations(id, line[ORGANIZATIONS], 'subnet')
97
          associate_locations(id, line[LOCATIONS], 'subnet')
98

    
99
          print "done\n" if option_verbose?
100
        end
101
      rescue RuntimeError => e
102
        raise "#{e}\n       #{line}"
103
      end
104
    end
105
  end
106
end