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

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



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

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

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

    contentview_id = @existing_contentviews[line[ORGANIZATION]][name]
    if !contentview_id
      puts _("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
      }
      contentview_id = @api.resource(:content_views).call(:create, options)['id']
      @existing_contentviews[line[ORGANIZATION]][name] = contentview_id
    else
      puts _("Updating content view '%{name}'...") % {:name => name} if option_verbose?
    end

    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']

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

      if !line[ENVIRONMENTS].empty? && option_promote?
        promote_content_view(contentview_id, line)
      end
    end

    puts _('done') if option_verbose?
  end

end

#environment_names(contentview) ⇒ Object



154
155
156
157
158
159
160
161
162
# File 'lib/hammer_cli_csv/content_views.rb', line 154

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

#export(csv) ⇒ Object



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
64
65
66
# File 'lib/hammer_cli_csv/content_views.rb', line 23

def export(csv)
  if (options.keys & %w(option_promote option_publish)).any?
    fail _("Cannot pass publish or promote options on export")
  end

  csv << [NAME, LABEL, ORGANIZATION, COMPOSITE, REPOSITORIES, ENVIRONMENTS]
  @api.resource(:organizations).call(:index, {
      :per_page => 999999,
      :search => option_search
  })['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, label, orgname, composite, contentviews, environments]
      else
        repositories = export_column(contentview, 'repositories', 'name')
        csv << [name, label, orgname, composite, repositories, environments]
      end
    end
    composite_contentviews.each do |contentview|
      csv << contentview
    end
  end
end

#importObject



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/hammer_cli_csv/content_views.rb', line 68

def import
  if options.keys.include?('option_publish') && !option_publish? && option_promote?
    fail _("Cannot pass in --promote with --no-publish")
  end

  @existing_contentviews = {}

  thread_import do |line|
    create_contentviews_from_csv(line)
  end
end

#promote_content_view(contentview_id, line) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/hammer_cli_csv/content_views.rb', line 170

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



164
165
166
167
168
# File 'lib/hammer_cli_csv/content_views.rb', line 164

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