Class: HammerCLICsv::CsvCommand::ContentViewsCommand

Inherits:
BaseCommand
  • Object
show all
Includes:
HammerCLIForemanTasks::Helper
Defined in:
lib/hammer_cli_csv/content_views.rb

Constant Summary collapse

LABEL =
'Label'
ORGANIZATION =
'Organization'
DESCRIPTION =
'Description'
COMPOSITE =
'Composite'
REPOSITORIES =
'Repositories or Composites'
ENVIRONMENTS =
"Lifecycle Environments"

Constants inherited from BaseCommand

BaseCommand::COUNT, BaseCommand::NAME

Instance Method Summary collapse

Methods inherited from BaseCommand

#associate_locations, #associate_organizations, #build_os_name, #check_server_status, #collect_column, #execute, #export_column, #foreman_architecture, #foreman_domain, #foreman_environment, #foreman_filter, #foreman_location, #foreman_operatingsystem, #foreman_organization, #foreman_partitiontable, #foreman_permission, #foreman_role, #foreman_template_kind, #hammer, #hammer_context, #katello_contentview, #katello_contentviewversion, #katello_hostcollection, #katello_product, #katello_repository, #katello_subscription, #labelize, #lifecycle_environment, #namify, #pluralize, #split_os_name, #thread_import

Instance Method Details

#create_contentviews_from_csv(line) ⇒ Object



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
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/hammer_cli_csv/content_views.rb', line 82

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

  if !@existing_contentviews[line[ORGANIZATION]]
    @existing_contentviews[line[ORGANIZATION]] ||= {}
    @api.resource(:content_views).call(:index, {
        'per_page' => 999999,
        'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
        'nondefault' => true
    })['results'].each do |contentview|
      @existing_contentviews[line[ORGANIZATION]][contentview['name']] = contentview['id'] if contentview
    end
  end

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

  if is_composite
    composite_ids = collect_column(line[REPOSITORIES]) do |composite|
      # TODO: export version and use it here
      katello_contentviewversion(line[ORGANIZATION], composite)
    end
  else
    repository_ids = collect_column(line[REPOSITORIES]) do |repository|
      katello_repository(line[ORGANIZATION], :name => repository)
    end
  end

  line[COUNT].to_i.times do |number|
    name = namify(line[NAME], number)

    contentview_id = @existing_contentviews[line[ORGANIZATION]][name]
    if !contentview_id
      print _("Creating content view '%{name}'...") % {:name => name} if option_verbose?
      options = {
          'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
          'name' => name,
          'label' => labelize(name),
          'description' => line[DESCRIPTION],
          'composite' => is_composite
      }
      if is_composite
        options['component_ids'] = composite_ids
      else
        options['repository_ids'] = repository_ids
      end
      contentview_id = @api.resource(:content_views).call(:create, options)['id']
      @existing_contentviews[line[ORGANIZATION]][name] = contentview_id
      publish = true
    else
      print _("Updating content view '%{name}'...") % {:name => name} if option_verbose?
      options = {
          'id' => contentview_id,
          'description' => line[DESCRIPTION]
      }
      if is_composite
        options['component_ids'] = composite_ids
      else
        options['repository_ids'] = repository_ids
      end
      contentview = @api.resource(:content_views).call(:update, options)
      contentview_id = contentview['id']
      publish = contentview['versions'].empty?
    end

    # Content views cannot be used in composites unless a publish has occurred
    publish_content_view(contentview_id, line) if publish
    promote_content_view(contentview_id, line)

    puts _('done') if option_verbose?
  end

rescue RuntimeError => e
  raise "#{e}\n       #{line}"
end

#environment_names(contentview) ⇒ Object



157
158
159
160
161
162
163
164
165
# File 'lib/hammer_cli_csv/content_views.rb', line 157

def environment_names(contentview)
  names = []
  contentview['versions'].each do |version|
    version['environment_ids'].each do |environment_id|
      names << lifecycle_environment(contentview['organization']['name'], :id => environment_id)
    end
  end
  names.uniq
end

#exportObject



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
64
65
66
67
68
69
70
71
72
# File 'lib/hammer_cli_csv/content_views.rb', line 32

def export
  CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => false}) do |csv|
    csv << [NAME, COUNT, LABEL, ORGANIZATION, COMPOSITE, REPOSITORIES, ENVIRONMENTS]
    @api.resource(:organizations).call(:index, {
        :per_page => 999999
    })['results'].each do |organization|
      next if option_organization && organization['name'] != option_organization

      composite_contentviews = []
      @api.resource(:content_views).call(:index, {
          'per_page' => 999999,
          'organization_id' => organization['id'],
          'nondefault' => true
      })['results'].each do |contentview|
        name = contentview['name']
        label = contentview['label']
        orgname = organization['name']
        environments = CSV.generate do |column|
          column << environment_names(contentview)
        end
        environments.delete!("\n")
        composite = contentview['composite'] == true ? 'Yes' : 'No'
        if composite == 'Yes'
          contentviews = CSV.generate do |column|
            column << contentview['components'].collect do |component|
              component['content_view']['name']
            end
          end
          contentviews.delete!("\n")
          composite_contentviews << [name, 1, label, orgname, composite, contentviews, environments]
        else
          repositories = export_column(contentview, 'repositories', 'name')
          csv << [name, 1, label, orgname, composite, repositories, environments]
        end
      end
      composite_contentviews.each do |contentview|
        csv << contentview
      end
    end
  end
end

#importObject



74
75
76
77
78
79
80
# File 'lib/hammer_cli_csv/content_views.rb', line 74

def import
  @existing_contentviews = {}

  thread_import do |line|
    create_contentviews_from_csv(line)
  end
end

#promote_content_view(contentview_id, line) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/hammer_cli_csv/content_views.rb', line 173

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

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

    version = contentview['versions'][-1]
    task_progress(@api.resource(:content_view_versions).call(:promote, {
        'id' => version['id'],
        'environment_id' => lifecycle_environment(line[ORGANIZATION], :name => environment_name),
        'force' => true
    }))
  end
end

#publish_content_view(contentview_id, line) ⇒ Object



167
168
169
170
171
# File 'lib/hammer_cli_csv/content_views.rb', line 167

def publish_content_view(contentview_id, line)
  task_progress(@api.resource(:content_views).call(:publish, {
      'id' => contentview_id
  }))
end