Class: HammerCLICsv::CsvCommand::DomainsCommand

Inherits:
BaseCommand
  • Object
show all
Defined in:
lib/hammer_cli_csv/domains.rb

Constant Summary collapse

ORGANIZATIONS =
'Organizations'
LOCATIONS =
'Locations'
DESCRIPTION =
'Description'
SMART_PROXY =
'DNS Smart Proxy'
PARAMETERS =
'Parameters'
SEPARATOR =
' = '

Constants inherited from BaseCommand

BaseCommand::COUNT, BaseCommand::NAME

Instance Method Summary collapse

Methods inherited from BaseCommand

#apipie_check_param, #associate_locations, #associate_organizations, #authenticate_request, #build_os_name, #check_server_status, #collect_column, #count, #execute, #export_column, #foreman_architecture, #foreman_container, #foreman_domain, #foreman_environment, #foreman_filter, #foreman_host, #foreman_hostgroup, #foreman_location, #foreman_medium, #foreman_operatingsystem, #foreman_organization, #foreman_partitiontable, #foreman_permission, #foreman_provisioning_template, #foreman_role, #foreman_smart_proxy, #foreman_template_kind, #hammer, #hammer_context, #help, help_columns, #katello_contentview, #katello_contentviewversion, #katello_hostcollection, #katello_product, #katello_repository, #labelize, #lifecycle_environment, #namify, #pluralize, #split_os_name, #supported?, supported?, #thread_import

Methods included from Utils::Config

#api_connection

Instance Method Details

#create_domains_from_csv(line) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/hammer_cli_csv/domains.rb', line 47

def create_domains_from_csv(line)
  dns_id = foreman_smart_proxy(:name => line[SMART_PROXY])
  count(line[COUNT]).times do |number|
    name = namify(line[NAME], number)
    description = namify(line[DESCRIPTION], number)
    params = {
      'domain' => {
        'name' => name,
        'fullname' => description,
        'dns_id' => dns_id
      }
    }
    if !@existing.include? name
      print _("Creating domain '%{name}'...") % {:name => name} if option_verbose?
      domain = @api.resource(:domains).call(:create, params)
    else
      print _("Updating domain '%{name}'...") % {:name => name} if option_verbose?
      params['id'] = @existing[name]
      domain = @api.resource(:domains).call(:update, params)
    end

    update_organizations(line, domain)
    update_locations(line, domain)
    import_parameters(domain['id'], line[PARAMETERS])

    puts _("done") if option_verbose?
  end
end

#export(csv) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/hammer_cli_csv/domains.rb', line 15

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

    name = domain['name']
    organizations = option_organization ? option_organization : export_column(domain, 'organizations', 'name')
    locations = export_column(domain, 'locations', 'name')
    description = domain['fullname']
    capsule = foreman_smart_proxy(:id => domain['dns_id'])
    parameters = export_column(domain, 'parameters') do |parameter|
      "#{parameter['name']}#{SEPARATOR}#{parameter['value']}"
    end
    csv << [name, organizations, locations, description, capsule, parameters]
  end
end

#importObject



36
37
38
39
40
41
42
43
44
45
# File 'lib/hammer_cli_csv/domains.rb', line 36

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

  thread_import do |line|
    create_domains_from_csv(line)
  end
end

#import_parameters(domain_id, parameters) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/hammer_cli_csv/domains.rb', line 117

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

    results = @api.resource(:parameters).call(:index, { :domain_id => domain_id, :search => "name=\"#{parameter_name}\"" })['results']
    params = {
      'domain_id' => domain_id,
      'parameter' => {
        'name' => parameter_name,
        'value' => parameter_value
      }
    }
    if results.empty?
      @api.resource(:parameters).call(:create, params)
    else
      params['id'] = results[0]['id']
      @api.resource(:parameters).call(:update, params)
    end
  end
end

#update_locations(line, domain) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/hammer_cli_csv/domains.rb', line 96

def update_locations(line, domain)
  return if line[LOCATIONS].nil? || line[LOCATIONS].empty?
  domains ||= {}
  CSV.parse_line(line[LOCATIONS]).each do |location|
    location_id = foreman_location(:name => location)
    if domains[location].nil?
      domains[location] = @api.resource(:locations).call(:show, {'id' => location_id})['domains'].collect do |existing_domain|
        existing_domain['id']
      end
    end
    domains[location] += [domain['id']] if !domains[location].include? domain['id']

    @api.resource(:locations).call(:update, {
                                     'id' => location_id,
                                     'location' => {
                                       'domain_ids' => domains[location]
                                     }
                                   })
  end
end

#update_organizations(line, domain) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/hammer_cli_csv/domains.rb', line 76

def update_organizations(line, domain)
  domains ||= {}
  CSV.parse_line(line[ORGANIZATIONS]).each do |organization|
    organization_id = foreman_organization(:name => organization)
    if domains[organization].nil?
      domains[organization] = @api.resource(:organizations).call(:show, {'id' => organization_id})['domains'].collect do |existing_domain|
        existing_domain['id']
      end
    end
    domains[organization] += [domain['id']] if !domains[organization].include? domain['id']

    @api.resource(:organizations).call(:update, {
                                         'id' => organization_id,
                                         'organization' => {
                                           'domain_ids' => domains[organization]
                                         }
                                       })
  end
end