Class: HammerCLICsv::CsvCommand::JobTemplatesCommand

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

Constant Summary collapse

ORGANIZATIONS =
'Organizations'
LOCATIONS =
'Locations'
DESCRIPTION =
'Description'
JOB =
'Job Category'
PROVIDER =
'Provider'
SNIPPET =
'Snippet'
TEMPLATE =
'Template'
INPUT_NAME =
'Input:Name'
INPUT_DESCRIPTION =
'Input:Description'
INPUT_REQUIRED =
'Input:Required'
INPUT_TYPE =
'Input:Type'
INPUT_PARAMETERS =
'Input:Parameters'

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_template(line, number) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/hammer_cli_csv/job_templates.rb', line 142

def create_template(line, number)
  name = namify(line[NAME], number)
  job_category = namify(line[JOB], number)
  options = {
        'job_template' => {
          'name' => name,
          'description_format' => line[DESCRIPTION],
          'job_category' => job_category,
          'snippet' => line[SNIPPET] == 'Yes' ? true : false,
          'provider_type' => line[PROVIDER],
          #'organization_ids' => organizations,
          #'location_ids' => locations,
          'template' => line[TEMPLATE]
        }
  }
  template_id = @existing[name]
  if !template_id
    print _("Creating job template '%{name}'...") % {:name => name } if option_verbose?
    template_id = @api.resource(:job_templates).call(:create, options)['id']
    @existing[name] = template_id
  else
    print _("Updating job template '%{name}'...") % {:name => name} if option_verbose?
    options['id'] = template_id
    template_id = @api.resource(:job_templates).call(:update, options)['id']
  end
  puts _('done') if option_verbose?
end

#create_template_input(line, number) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/hammer_cli_csv/job_templates.rb', line 170

def create_template_input(line, number)
  name = namify(line[NAME], number)
  template_id = @existing[name]
  raise "Job template '#{name}' must exist before setting inputs" unless template_id

  options = {
    'template_id' => template_id,
    'template_input' => {
      'name' => line[INPUT_NAME],
      'description' => line[INPUT_DESCRIPTION],
      'input_type' => line[INPUT_TYPE],
      'required' => line[INPUT_REQUIRED] == 'Yes' ? true : false
    }
  }
  case line[INPUT_TYPE]
  when /user/
    options['template_input']['options'] = line[INPUT_PARAMETERS]
  when /fact/
    options['template_input']['fact_name'] = line[INPUT_PARAMETERS]
  when /variable/
    options['template_input']['variable_name'] = line[INPUT_PARAMETERS]
  when /puppet_parameter/
    options['template_input']['puppet_class_name'], options['template_input']['puppet_parameter_name'] = line[INPUT_PARAMETERS].split('|')
  else
    raise _("Unknown job template input type '%{type}'") % {:type => line[INPUT_TYPE]}
  end

  template_input = @api.resource(:template_inputs).call(:index, {
      :template_id => template_id,
      :search => "name = \"#{line[INPUT_NAME]}\""
  })['results']
  if template_input.empty?
    print _("Creating job template input '%{input_name}' on '%{name}'...") % {:input_name => line[INPUT_NAME], :name => name}
    @api.resource(:template_inputs).call(:create, options)
  else
    print _("Updating job template input '%{input_name}' on '%{name}'...") % {:input_name => line[INPUT_NAME], :name => name}
    options['id'] = template_input[0]['id']
    @api.resource(:template_inputs).call(:update, options)
  end
  puts _('done') if option_verbose?
end

#create_templates_from_csv(line) ⇒ Object



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
# File 'lib/hammer_cli_csv/job_templates.rb', line 78

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

  count(line[COUNT]).times do |number|
    if line[INPUT_NAME].nil? || line[INPUT_NAME].empty?
      create_template(line, number)
    else
      create_template_input(line, number)
    end

    # ????
    # 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

  end
end

#export(csv) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/hammer_cli_csv/job_templates.rb', line 20

def export(csv)
  csv << [NAME, ORGANIZATIONS, LOCATIONS, DESCRIPTION, JOB, PROVIDER, SNIPPET, TEMPLATE,
          INPUT_NAME, INPUT_DESCRIPTION, INPUT_REQUIRED, INPUT_TYPE, INPUT_PARAMETERS]
  @api.resource(:job_templates).call(:index, {
      :per_page => 999999,
      :search => option_search
  })['results'].each do |template_id|
    template = @api.resource(:job_templates).call(:show, {:id => template_id['id']})
    next if template['locked']
    next unless option_organization.nil? || template['organizations'].detect { |org| org['name'] == option_organization }
    name = template['name']
    description = template['description_format']
    job = template['job_category']
    snippet = template['snippet'] ? 'Yes' : 'No'
    provider = template['provider_type']
    organizations = export_column(template, 'organizations', 'name')
    locations = export_column(template, 'locations', 'name')
    csv << [name, organizations, locations, description, job, provider, snippet, template['template']]

    template_columns = [name] + Array.new(7)
    @api.resource(:template_inputs).call(:index, {
        :template_id => template['id']
    })['results'].each  do|input|
      input_field = nil
      input_options = nil
      case input['input_type']
      when /user/
        input_name = export_column(input, 'options') do |value|
          value
        end
      when /fact/
        input_name = input['fact_name']
      when /variable/
        input_name = input['variable_name']
      when /puppet_parameter/
        input_name = "#{input['puppet_class_name']}|#{input['puppet_parameter_name']}"
      else
        raise _("Unknown job template input type '%{type}'") % {:type => input['input_type']}
      end
      required = input['required'] ? 'Yes' : 'No'
      csv << template_columns + [input['name'], input['description'], required, input['input_type'], input_name]
    end
  end
end

#export_associations(template) ⇒ Object



212
213
214
215
216
217
218
219
220
# File 'lib/hammer_cli_csv/job_templates.rb', line 212

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



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/hammer_cli_csv/job_templates.rb', line 65

def import
  @existing = {}
  @api.resource(:job_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