Class: Actions::Katello::ContentView::Remove

Inherits:
EntryAction
  • Object
show all
Includes:
Helpers::ContentViewAutoPublisher
Defined in:
app/lib/actions/katello/content_view/remove.rb

Instance Method Summary collapse

Methods included from Helpers::ContentViewAutoPublisher

#auto_publish_view, #auto_publish_views, included, #trigger_auto_publish

Instance Method Details

#check_version_deletion(versions, cv_envs) ⇒ Object



92
93
94
95
96
97
98
99
100
# File 'app/lib/actions/katello/content_view/remove.rb', line 92

def check_version_deletion(versions, cv_envs)
  versions.each do |version|
    version.environments.each do |env|
      if cv_envs.none? { |cv_env| cv_env.content_view_version == version && cv_env.environment == env }
        fail _("Cannot delete version while it is in environment %s") % env.name
      end
    end
  end
end

#combined_cv_envs(cv_envs, versions) ⇒ Object



148
149
150
# File 'app/lib/actions/katello/content_view/remove.rb', line 148

def combined_cv_envs(cv_envs, versions)
  (cv_envs + versions.flat_map(&:content_view_environments)).uniq
end

#cve_exists?(environment_id, content_view_id) ⇒ Boolean

Returns:

  • (Boolean)


152
153
154
155
156
# File 'app/lib/actions/katello/content_view/remove.rb', line 152

def cve_exists?(environment_id, content_view_id)
  ::Katello::ContentViewEnvironment.where(:environment_id => environment_id,
                                          :content_view_id => content_view_id
                                         ).exists?
end

#cve_exists_by_id?(cv_env_id) ⇒ Boolean

Returns:

  • (Boolean)


158
159
160
# File 'app/lib/actions/katello/content_view/remove.rb', line 158

def cve_exists_by_id?(cv_env_id)
  ::Katello::ContentViewEnvironment.where(:id => cv_env_id).exists?
end

#destroy_host_and_hostgroup_associations(content_view:) ⇒ Object



82
83
84
85
86
87
88
89
90
# File 'app/lib/actions/katello/content_view/remove.rb', line 82

def destroy_host_and_hostgroup_associations(content_view:)
  # Destroy hostgroup content facets associated with this content view
  hostgroup_content_facet_ids = content_view.hostgroup_content_facets.ids
  ::Katello::Hostgroup::ContentFacet.where(:id => hostgroup_content_facet_ids).destroy_all

  host_ids = content_view.hosts.ids
  ::Katello::Host::ContentFacet.where(:host_id => host_ids).destroy_all
  ::Katello::Host::SubscriptionFacet.where(:host_id => host_ids).destroy_all
end

#finalizeObject



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'app/lib/actions/katello/content_view/remove.rb', line 106

def finalize
  if input[:destroy_content_view]
    content_view = ::Katello::ContentView.find(input[:content_view_id])
    content_view.content_view_repositories.each(&:destroy)
    content_view.destroy!
  else
    input[:content_view_history_ids].each do |history_id|
      history = ::Katello::ContentViewHistory.find_by(:id => history_id)
      if history
        history.status = ::Katello::ContentViewHistory::SUCCESSFUL
        history.save!
      end
    end
  end
end

#humanized_nameObject



102
103
104
# File 'app/lib/actions/katello/content_view/remove.rb', line 102

def humanized_name
  _("Remove Versions and Associations")
end

#plan(content_view, options) ⇒ Object

Options: (note that all are optional) content_view_environments - content view environments to delete content_view_versions - view versions to delete system_content_view_id - content view to reassociate systems with system_environment_id - environment to reassociate systems with key_content_view_id - content view to reassociate actvation keys with key_environment_id - environment to reassociate activation keys with hostgroup_content_view_environment_id - content view environment to reassociate host groups with destroy_content_view - delete the CV completely along with all cv versions and environments organization_destroy rubocop:disable Metrics/MethodLength rubocop:disable Metrics/CyclomaticComplexity



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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/lib/actions/katello/content_view/remove.rb', line 21

def plan(content_view, options)
  cv_envs = options.fetch(:content_view_environments, [])
  versions = options.fetch(:content_view_versions, [])
  organization_destroy = options.fetch(:organization_destroy, false)
  skip_repo_destroy = options.fetch(:skip_repo_destroy, false)
  action_subject(content_view)
  validate_options(content_view, cv_envs, versions, options) unless organization_destroy

  all_cv_envs = combined_cv_envs(cv_envs, versions)
  check_version_deletion(versions, cv_envs)

  sequence do
    unless organization_destroy
      concurrence do
        all_cv_envs.each do |cv_env|
          if cv_env.hosts.any? || cv_env.activation_keys.any? || cv_env.hostgroups.any?
            plan_action(ContentViewEnvironment::ReassignObjects, cv_env, options)
          end
        end
      end
    end

    cv_histories = []
    all_cv_envs.each do |cve|
      cv_histories << ::Katello::ContentViewHistory.create!(:content_view_version => cve.content_view_version,
                                                            :user => ::User.current.,
                                                            :environment => cve.environment,
                                                            :status => ::Katello::ContentViewHistory::IN_PROGRESS,
                                                            :action => ::Katello::ContentViewHistory.actions[:removal],
                                                            :task => self.task)
      plan_action(ContentViewEnvironment::Destroy,
                  cve,
                  :skip_repo_destroy => skip_repo_destroy,
                  :organization_destroy => organization_destroy)
    end

    versions.each do |version|
      ::Katello::ContentViewHistory.create!(:content_view_version => version,
                                            :user => ::User.current.,
                                            :action => ::Katello::ContentViewHistory.actions[:removal],
                                            :status => ::Katello::ContentViewHistory::IN_PROGRESS, :task => self.task)
      plan_action(ContentViewVersion::Destroy, version,
                  :skip_environment_check => true,
                  :skip_destroy_env_content => true)
    end
    if options[:destroy_content_view] && SmartProxy.pulp_primary&.pulp3_enabled?
      plan_action(Actions::Pulp3::ContentView::DeleteRepositoryReferences, content_view, SmartProxy.pulp_primary)
    end
    plan_self(content_view_id: content_view.id,
              destroy_content_view: options[:destroy_content_view],
              environment_ids: cv_envs.map(&:environment_id),
              environment_names: cv_envs.map { |cve| cve.environment.name },
              version_ids: versions.map(&:id),
              content_view_history_ids: cv_histories.map { |history| history.id })

    if organization_destroy
      destroy_host_and_hostgroup_associations(content_view: content_view)
    end
  end
end

#validate_options(_content_view, cv_envs, versions, options) ⇒ Object



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
# File 'app/lib/actions/katello/content_view/remove.rb', line 122

def validate_options(_content_view, cv_envs, versions, options)
  if !options[:destroy_content_view] && cv_envs.empty? && versions.empty?
    fail _("Either environments or versions must be specified.")
  end
  all_cv_envs = combined_cv_envs(cv_envs, versions)

  single_env_hosts_exist = all_cv_envs.flat_map(&:hosts).any? do |host|
    !host.content_facet.multi_content_view_environment?
  end
  if single_env_hosts_exist && !cve_exists?(options[:system_environment_id], options[:system_content_view_id])
    fail _("Unable to reassign systems. Please check system_content_view_id and system_environment_id.")
  end

  single_env_keys_exist = all_cv_envs.flat_map(&:activation_keys).any? do |key|
    !key.multi_content_view_environment?
  end
  if single_env_keys_exist && !cve_exists?(options[:key_environment_id], options[:key_content_view_id])
    fail _("Unable to reassign activation_keys. Please check activation_key_content_view_id and activation_key_environment_id.")
  end

  hostgroups_exist = all_cv_envs.flat_map(&:hostgroups).any?
  if hostgroups_exist && !cve_exists_by_id?(options[:hostgroup_content_view_environment_id])
    fail _("Unable to reassign host groups. Please check hostgroup_content_view_environment_id.")
  end
end