Project

General

Profile

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

hammer-cli-csv / lib / hammer_cli_csv / job_templates.rb @ f92e695a

1
module HammerCLICsv
2
  class CsvCommand
3
    class JobTemplatesCommand < BaseCommand
4
      command_name 'job-templates'
5
      desc         'import or export job templates'
6

    
7
      ORGANIZATIONS = 'Organizations'
8
      LOCATIONS = 'Locations'
9
      DESCRIPTION = 'Description'
10
      JOB = 'Job Category'
11
      PROVIDER = 'Provider'
12
      SNIPPET = 'Snippet'
13
      TEMPLATE = 'Template'
14
      INPUT_NAME = 'Input:Name'
15
      INPUT_DESCRIPTION = 'Input:Description'
16
      INPUT_REQUIRED = 'Input:Required'
17
      INPUT_TYPE = 'Input:Type'
18
      INPUT_PARAMETERS = 'Input:Parameters'
19

    
20
      def export(csv)
21
        csv << [NAME, ORGANIZATIONS, LOCATIONS, DESCRIPTION, JOB, PROVIDER, SNIPPET, TEMPLATE,
22
                INPUT_NAME, INPUT_DESCRIPTION, INPUT_REQUIRED, INPUT_TYPE, INPUT_PARAMETERS]
23
        @api.resource(:job_templates).call(:index, {
24
            :per_page => 999999,
25
            :search => option_search
26
        })['results'].each do |template_id|
27
          template = @api.resource(:job_templates).call(:show, {:id => template_id['id']})
28
          next if template['locked']
29
          next unless option_organization.nil? || template['organizations'].detect { |org| org['name'] == option_organization }
30
          name = template['name']
31
          description = template['description_format']
32
          job = template['job_category']
33
          snippet = template['snippet'] ? 'Yes' : 'No'
34
          provider = template['provider_type']
35
          organizations = export_column(template, 'organizations', 'name')
36
          locations = export_column(template, 'locations', 'name')
37
          csv << [name, organizations, locations, description, job, provider, snippet, template['template']]
38

    
39
          template_columns = [name] + Array.new(7)
40
          @api.resource(:template_inputs).call(:index, {
41
              :template_id => template['id']
42
          })['results'].each  do|input|
43
            input_field = nil
44
            input_options = nil
45
            case input['input_type']
46
            when /user/
47
              input_name = export_column(input, 'options') do |value|
48
                value
49
              end
50
            when /fact/
51
              input_name = input['fact_name']
52
            when /variable/
53
              input_name = input['variable_name']
54
            when /puppet_parameter/
55
              input_name = "#{input['puppet_class_name']}|#{input['puppet_parameter_name']}"
56
            else
57
              raise _("Unknown job template input type '%{type}'") % {:type => input['input_type']}
58
            end
59
            required = input['required'] ? 'Yes' : 'No'
60
            csv << template_columns + [input['name'], input['description'], required, input['input_type'], input_name]
61
          end
62
        end
63
      end
64

    
65
      def import
66
        @existing = {}
67
        @api.resource(:job_templates).call(:index, {
68
            :per_page => 999999
69
        })['results'].each do |template|
70
          @existing[template['name']] = template['id'] if template
71
        end
72

    
73
        thread_import do |line|
74
          create_templates_from_csv(line)
75
        end
76
      end
77

    
78
      def create_templates_from_csv(line)
79
        organizations = collect_column(line[ORGANIZATIONS]) do |organization|
80
          foreman_organization(:name => organization)
81
        end
82
        if option_organization
83
          org_id = foreman_organization(:name => option_organization)
84
          return if org_id.nil? || !organizations.include?(org_id)
85
          organizations = [org_id]
86
        end
87
        locations = collect_column(line[LOCATIONS]) do |location|
88
          foreman_location(:name => location)
89
        end
90

    
91
        count(line[COUNT]).times do |number|
92
          if line[INPUT_NAME].nil? || line[INPUT_NAME].empty?
93
            create_template(line, number)
94
          else
95
            create_template_input(line, number)
96
          end
97

    
98
          # ????
99
          # Update associated resources
100
          # @template_organizations ||= {}
101
          # organizations.each do |organization_id|
102
          #   if @template_organizations[organization_id].nil?
103
          #     @template_organizations[organization_id] = @api.resource(:organizations).call(:show, {
104
          #         'id' => organization_id
105
          #     })['config_templates'].collect do |template|
106
          #       template['id']
107
          #     end
108
          #   end
109
          #   if !@template_organizations[organization_id].include? template_id
110
          #     @template_organizations[organization_id] << template_id
111
          #     @api.resource(:organizations).call(:update, {
112
          #         'id' => organization_id,
113
          #         'organization' => {
114
          #             'config_template_ids' => @template_organizations[organization_id]
115
          #         }
116
          #     })
117
          #   end
118
          # end
119
          # @template_locations ||= {}
120
          # locations.each do |location_id|
121
          #   if @template_locations[location_id].nil?
122
          #     @template_locations[location_id] = @api.resource(:locations).call(:show, {
123
          #         'id' => location_id
124
          #     })['config_templates'].collect do |template|
125
          #       template['id']
126
          #     end
127
          #   end
128
          #   if !@template_locations[location_id].include? template_id
129
          #     @template_locations[location_id] += [template_id]
130
          #     @api.resource(:locations).call(:update, {
131
          #         'id' => location_id,
132
          #         'location' => {
133
          #             'config_template_ids' => @template_locations[location_id]
134
          #         }
135
          #     })
136
          #   end
137
          # end
138

    
139
        end
140
      end
141

    
142
      def create_template(line, number)
143
        name = namify(line[NAME], number)
144
        job_category = namify(line[JOB], number)
145
        options = {
146
              'job_template' => {
147
                'name' => name,
148
                'description_format' => line[DESCRIPTION],
149
                'job_category' => job_category,
150
                'snippet' => line[SNIPPET] == 'Yes' ? true : false,
151
                'provider_type' => line[PROVIDER],
152
                #'organization_ids' => organizations,
153
                #'location_ids' => locations,
154
                'template' => line[TEMPLATE]
155
              }
156
        }
157
        template_id = @existing[name]
158
        if !template_id
159
          print _("Creating job template '%{name}'...") % {:name => name } if option_verbose?
160
          template_id = @api.resource(:job_templates).call(:create, options)['id']
161
          @existing[name] = template_id
162
        else
163
          print _("Updating job template '%{name}'...") % {:name => name} if option_verbose?
164
          options['id'] = template_id
165
          template_id = @api.resource(:job_templates).call(:update, options)['id']
166
        end
167
        puts _('done') if option_verbose?
168
      end
169

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

    
175
        options = {
176
          'template_id' => template_id,
177
          'template_input' => {
178
            'name' => line[INPUT_NAME],
179
            'description' => line[INPUT_DESCRIPTION],
180
            'input_type' => line[INPUT_TYPE],
181
            'required' => line[INPUT_REQUIRED] == 'Yes' ? true : false
182
          }
183
        }
184
        case line[INPUT_TYPE]
185
        when /user/
186
          options['template_input']['options'] = line[INPUT_PARAMETERS]
187
        when /fact/
188
          options['template_input']['fact_name'] = line[INPUT_PARAMETERS]
189
        when /variable/
190
          options['template_input']['variable_name'] = line[INPUT_PARAMETERS]
191
        when /puppet_parameter/
192
          options['template_input']['puppet_class_name'], options['template_input']['puppet_parameter_name'] = line[INPUT_PARAMETERS].split('|')
193
        else
194
          raise _("Unknown job template input type '%{type}'") % {:type => line[INPUT_TYPE]}
195
        end
196

    
197
        template_input = @api.resource(:template_inputs).call(:index, {
198
            :template_id => template_id,
199
            :search => "name = \"#{line[INPUT_NAME]}\""
200
        })['results']
201
        if template_input.empty?
202
          print _("Creating job template input '%{input_name}' on '%{name}'...") % {:input_name => line[INPUT_NAME], :name => name}
203
          @api.resource(:template_inputs).call(:create, options)
204
        else
205
          print _("Updating job template input '%{input_name}' on '%{name}'...") % {:input_name => line[INPUT_NAME], :name => name}
206
          options['id'] = template_input[0]['id']
207
          @api.resource(:template_inputs).call(:update, options)
208
        end
209
        puts _('done') if option_verbose?
210
      end
211

    
212
      def export_associations(template)
213
        return '' unless template['template_combinations']
214
        values = CSV.generate do |column|
215
          column << template['template_combinations'].collect do |combo|
216
            "#{combo['hostgroup_name']}|#{combo['environment_name']}"
217
          end
218
        end
219
        values.delete!("\n")
220
      end
221
    end
222
  end
223
end