Project

General

Profile

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

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

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_PREFIX = 'Network Prefix'
12
      NETWORK_FROM = 'From'
13
      NETWORK_TO = 'To'
14
      DOMAINS = 'Domains'
15
      GATEWAY = 'Gateway'
16
      DHCP_PROXY = 'DHCP Proxy'
17
      TFTP_PROXY = 'TFTP Proxy'
18
      DNS_PROXY = 'DNS Proxy'
19
      DNS_PRIMARY = 'DNS Primary'
20
      DNS_SECONDARY = 'DNS Secondary'
21
      VLAN_ID = 'VLAN ID'
22

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

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

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

    
58
        thread_import do |line|
59
          create_subnets_from_csv(line)
60
        end
61
      end
62

    
63
      def create_subnets_from_csv(line)
64
        return if option_organization && line[ORGANIZATION] != option_organization
65

    
66
        line[DOMAINS] = (CSV.parse_line(line[DOMAINS]) || []).collect do |domain|
67
          foreman_domain(:name => domain)
68
        end
69

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

    
94
          associate_organizations(id, line[ORGANIZATIONS], 'subnet')
95
          associate_locations(id, line[LOCATIONS], 'subnet')
96

    
97
          puts _("done") if option_verbose?
98
        end
99
      end
100
    end
101
  end
102
end