Class: HammerCLICsv::CsvCommand::ProvisioningTemplatesCommand

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

Constant Summary collapse

ORGANIZATIONS =
'Organizations'
LOCATIONS =
'Locations'
OPERATINGSYSTEMS =
'Operating Systems'
ASSOCIATIONS =
'Host Group / Puppet Environment Combinations'
KIND =
'Kind'
TEMPLATE =
'Template'

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_templates_from_csv(line) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/hammer_cli_csv/provisioning_templates.rb', line 54

def create_templates_from_csv(line)
  organizations = collect_column(line[ORGANIZATIONS]) do |organization|
    foreman_organization(:name => organization)
  end
  if option_organization
    org_id = foreman_organization(:name => option_organization)
    return if org_id.nil? || !organizations.include?(org_id)
    organizations = [org_id]
  end
  locations = collect_column(line[LOCATIONS]) do |location|
    foreman_location(:name => location)
  end
  operatingsystems = collect_column(line[OPERATINGSYSTEMS]) do |operatingsystem|
    foreman_operatingsystem(:name => operatingsystem)
  end

  count(line[COUNT]).times do |number|
    name = namify(line[NAME], number)
    if !@existing.include? name
      print _("Creating provisioning template '%{name}'...") % {:name => name } if option_verbose?
      template_id = @api.resource(:config_templates).call(:create, {
          'config_template' => {
            'name' => name,
            'snippet' => line[KIND] == 'snippet',
            'template_kind_id' => line[KIND] == 'snippet' ? nil : foreman_template_kind(:name => line[KIND]),
            'operatingsystem_ids' => operatingsystems,
            'location_ids' => locations,
            'template' => line[TEMPLATE]
          }
      })['id']
    else
      print _("Updating provisioning template '%{name}'...") % {:name => name} if option_verbose?
      template_id = @api.resource(:config_templates).call(:update, {
          'id' => @existing[name],
          'config_template' => {
            'name' => name,
            'snippet' => line[KIND] == 'snippet',
            'template_kind_id' => line[KIND] == 'snippet' ? nil : foreman_template_kind(:name => line[KIND]),
            'operatingsystem_ids' => operatingsystems,
            'location_ids' => locations,
            'template' => line[TEMPLATE]
          }
      })['id']
    end
    @existing[name] = template_id

    # Update associated resources
    @template_organizations ||= {}
    organizations.each do |organization_id|
      if @template_organizations[organization_id].nil?
        @template_organizations[organization_id] = @api.resource(:organizations).call(:show, {
            'id' => organization_id
        })['config_templates'].collect do |template|
          template['id']
        end
      end
      if !@template_organizations[organization_id].include? template_id
        @template_organizations[organization_id] << template_id
        @api.resource(:organizations).call(:update, {
            'id' => organization_id,
            'organization' => {
                'config_template_ids' => @template_organizations[organization_id]
            }
        })
      end
    end
    @template_locations ||= {}
    locations.each do |location_id|
      if @template_locations[location_id].nil?
        @template_locations[location_id] = @api.resource(:locations).call(:show, {
            'id' => location_id
        })['config_templates'].collect do |template|
          template['id']
        end
      end
      if !@template_locations[location_id].include? template_id
        @template_locations[location_id] += [template_id]
        @api.resource(:locations).call(:update, {
            'id' => location_id,
            'location' => {
                'config_template_ids' => @template_locations[location_id]
            }
        })
      end
    end

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

#export(csv) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/hammer_cli_csv/provisioning_templates.rb', line 17

def export(csv)
  csv << [NAME, ORGANIZATIONS, LOCATIONS, OPERATINGSYSTEMS, ASSOCIATIONS, KIND, TEMPLATE]
  params = {
      :per_page => 999999
  }
  params['search'] =  "organization = \"#{option_organization}\"" if option_organization
  params['search'] = "#{params['search']} AND #{option_search}" if option_search
  @api.resource(:config_templates).call(:index, params)['results'].each do |template_id|
    template = @api.resource(:config_templates).call(:show, {:id => template_id['id']})
    next if template['locked'] && !option_include_locked?
    name = template['name']
    kind = template['snippet'] ? 'snippet' : template['template_kind_name']
    organizations = export_column(template, 'organizations', 'name')
    locations = export_column(template, 'locations', 'name')
    operatingsystems = export_column(template, 'operatingsystems', 'fullname')
    # TODO: puppet environments for content views are not present in api
    # http://projects.theforeman.org/issues/10293
    associations = export_associations(template)
    unless name == 'Boot disk iPXE - generic host' || name == 'Boot disk iPXE - host'
      csv << [name, organizations, locations, operatingsystems, associations, kind, template['template']]
    end
  end
end

#export_associations(template) ⇒ Object



144
145
146
147
148
149
150
151
152
# File 'lib/hammer_cli_csv/provisioning_templates.rb', line 144

def export_associations(template)
  return '' unless template['template_combinations']
  values = CSV.generate do |column|
    column << template['template_combinations'].collect do |combo|
      "#{combo['hostgroup_name']}|#{combo['environment_name']}"
    end
  end
  values.delete!("\n")
end

#importObject



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/hammer_cli_csv/provisioning_templates.rb', line 41

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

  thread_import do |line|
    create_templates_from_csv(line)
  end
end