Project

General

Profile

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

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

1
require 'hammer_cli_foreman'
2
require 'hammer_cli_foreman_tasks'
3

    
4
module HammerCLICsv
5
  class CsvCommand
6
    class ContentViewsCommand < BaseCommand
7
      include ::HammerCLIForemanTasks::Helper
8

    
9
      command_name 'content-views'
10
      desc         'import or export content-views'
11

    
12
      LABEL = 'Label'
13
      ORGANIZATION = 'Organization'
14
      DESCRIPTION = 'Description'
15
      COMPOSITE = 'Composite'
16
      REPOSITORIES = 'Repositories or Composites'
17
      ENVIRONMENTS = "Lifecycle Environments"
18

    
19
      def export(csv)
20
        csv << [NAME, LABEL, ORGANIZATION, COMPOSITE, REPOSITORIES, ENVIRONMENTS]
21
        @api.resource(:organizations).call(:index, {
22
            :per_page => 999999,
23
            :search => option_search
24
        })['results'].each do |organization|
25
          next if option_organization && organization['name'] != option_organization
26

    
27
          composite_contentviews = []
28
          @api.resource(:content_views).call(:index, {
29
              'per_page' => 999999,
30
              'organization_id' => organization['id'],
31
              'nondefault' => true
32
          })['results'].each do |contentview|
33
            name = contentview['name']
34
            label = contentview['label']
35
            orgname = organization['name']
36
            environments = CSV.generate do |column|
37
              column << environment_names(contentview)
38
            end
39
            environments.delete!("\n")
40
            composite = contentview['composite'] == true ? 'Yes' : 'No'
41
            if composite == 'Yes'
42
              contentviews = CSV.generate do |column|
43
                column << contentview['components'].collect do |component|
44
                  component['content_view']['name']
45
                end
46
              end
47
              contentviews.delete!("\n")
48
              composite_contentviews << [name, label, orgname, composite, contentviews, environments]
49
            else
50
              repositories = export_column(contentview, 'repositories', 'name')
51
              csv << [name, label, orgname, composite, repositories, environments]
52
            end
53
          end
54
          composite_contentviews.each do |contentview|
55
            csv << contentview
56
          end
57
        end
58
      end
59

    
60
      def import
61
        @existing_contentviews = {}
62

    
63
        thread_import do |line|
64
          create_contentviews_from_csv(line)
65
        end
66
      end
67

    
68
      def create_contentviews_from_csv(line)
69
        return if option_organization && line[ORGANIZATION] != option_organization
70

    
71
        if !@existing_contentviews[line[ORGANIZATION]]
72
          @existing_contentviews[line[ORGANIZATION]] ||= {}
73
          @api.resource(:content_views).call(:index, {
74
              'per_page' => 999999,
75
              'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
76
              'nondefault' => true
77
          })['results'].each do |contentview|
78
            @existing_contentviews[line[ORGANIZATION]][contentview['name']] = contentview['id'] if contentview
79
          end
80
        end
81

    
82
        is_composite = line[COMPOSITE] == 'Yes' ? true : false
83

    
84
        if is_composite
85
          composite_ids = collect_column(line[REPOSITORIES]) do |composite|
86
            # TODO: export version and use it here
87
            katello_contentviewversion(line[ORGANIZATION], composite)
88
          end
89
        else
90
          repository_ids = collect_column(line[REPOSITORIES]) do |repository|
91
            katello_repository(line[ORGANIZATION], :name => repository)
92
          end
93
        end
94

    
95
        count(line[COUNT]).times do |number|
96
          name = namify(line[NAME], number)
97

    
98
          contentview_id = @existing_contentviews[line[ORGANIZATION]][name]
99
          if !contentview_id
100
            print _("Creating content view '%{name}'...") % {:name => name} if option_verbose?
101
            options = {
102
                'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
103
                'name' => name,
104
                'label' => labelize(name),
105
                'description' => line[DESCRIPTION],
106
                'composite' => is_composite
107
            }
108
            contentview_id = @api.resource(:content_views).call(:create, options)['id']
109
            @existing_contentviews[line[ORGANIZATION]][name] = contentview_id
110
          else
111
            print _("Updating content view '%{name}'...") % {:name => name} if option_verbose?
112
          end
113

    
114
          options = {
115
            'id' => contentview_id,
116
            'description' => line[DESCRIPTION]
117
          }
118
          if is_composite
119
            options['component_ids'] = composite_ids
120
          else
121
            options['repository_ids'] = repository_ids
122
          end
123
          contentview = @api.resource(:content_views).call(:update, options)
124
          contentview_id = contentview['id']
125

    
126
          # Content views cannot be used in composites unless a publish has occurred
127
          if contentview['versions'].empty? && !line[ENVIRONMENTS].empty?
128
            publish_content_view(contentview_id, line)
129
          end
130

    
131
          unless line[ENVIRONMENTS].empty?
132
            promote_content_view(contentview_id, line)
133
          end
134

    
135
          puts _('done') if option_verbose?
136
        end
137

    
138
      end
139

    
140
      def environment_names(contentview)
141
        names = []
142
        contentview['versions'].each do |version|
143
          version['environment_ids'].each do |environment_id|
144
            names << lifecycle_environment(contentview['organization']['name'], :id => environment_id)
145
          end
146
        end
147
        names.uniq
148
      end
149

    
150
      def publish_content_view(contentview_id, line)
151
        task_progress(@api.resource(:content_views).call(:publish, {
152
            'id' => contentview_id
153
        }))
154
      end
155

    
156
      def promote_content_view(contentview_id, line)
157
        contentview = @api.resource(:content_views).call(:show, {'id' => contentview_id})
158
        existing_names = environment_names(contentview)
159

    
160
        CSV.parse_line(line[ENVIRONMENTS]).each do |environment_name|
161
          next if environment_name == 'Library' || existing_names.include?(environment_name)
162

    
163
          version = contentview['versions'][-1]
164
          task_progress(@api.resource(:content_view_versions).call(:promote, {
165
              'id' => version['id'],
166
              'environment_id' => lifecycle_environment(line[ORGANIZATION], :name => environment_name),
167
              'force' => true
168
          }))
169
        end
170
      end
171
    end
172
  end
173
end