Skip to content

Commit

Permalink
Merge pull request #11591 from zgalor/auto_tagging_based_on_kubernete…
Browse files Browse the repository at this point in the history
…s_labels

Label based Auto-Tagging UI
(cherry picked from commit 8d172b4)

https://bugzilla.redhat.com/show_bug.cgi?id=1383405
  • Loading branch information
Dan Clarizio authored and chessbyte committed Nov 1, 2016
1 parent 3212520 commit eb0afaa
Show file tree
Hide file tree
Showing 14 changed files with 470 additions and 2 deletions.
13 changes: 12 additions & 1 deletion app/controllers/ops_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,10 @@ def set_form_locals
end
elsif @sb[:active_tab] == "settings_co_categories" && @in_a_form
action_url = "category_edit"
record_id = @category && @category.id ? @category.id : nil
record_id = @category.try(:id)
elsif @sb[:active_tab] == "settings_label_tag_mapping" && @in_a_form
action_url = "label_tag_mapping_edit"
record_id = @lt_map.try(:id)
elsif @sb[:active_tab] == 'settings_rhn_edit'
locals[:no_cancel] = false
action_url = "settings_update"
Expand Down Expand Up @@ -552,6 +555,14 @@ def settings_replace_right_cell(nodetype, presenter, r)
else
@right_cell_text = _("Editing %{model} \"%{name}\"") % {:name => @category.description, :model => "Category"}
end
when "ltme" # label tag mapping edit
# when editing/adding label tag mapping in settings tree
presenter.update(:settings_label_tag_mapping, r[:partial => "label_tag_mapping_form"])
if !@lt_map
@right_cell_text = _("Adding a new Mapping")
else
@right_cell_text = _("Editing tag mapping from label \"%{name}\"") % {:name => @lt_map.label_name}
end
when "sie" # scanitemset edit
# editing/adding scanitem in settings tree
presenter.update(:settings_list, r[:partial => "ap_form"])
Expand Down
1 change: 1 addition & 0 deletions app/controllers/ops_controller/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module OpsController::Settings
include_concern 'Schedules'
include_concern 'AutomateSchedules'
include_concern 'Tags'
include_concern 'LabelTagMapping'
include_concern 'Upload'
include_concern 'Zones'
include_concern 'RHN'
Expand Down
2 changes: 2 additions & 0 deletions app/controllers/ops_controller/settings/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1150,6 +1150,8 @@ def settings_get_info(nodetype = x_node)
@sb[:good] = nil unless @sb[:show_button]
add_flash(_("Choose the type of custom variables to be imported"))
@in_a_form = true
when "settings_label_tag_mapping"
label_tag_mapping_get_all
when "settings_rhn"
@edit = session[:edit] || {}
@edit[:new] ||= {}
Expand Down
207 changes: 207 additions & 0 deletions app/controllers/ops_controller/settings/label_tag_mapping.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
module OpsController::Settings::LabelTagMapping
extend ActiveSupport::Concern

def label_tag_mapping_edit
case params[:button]
when "cancel"
@lt_map = session[:edit][:lt_map] if session[:edit] && session[:edit][:lt_map]
if !@lt_map || @lt_map.id.blank?
add_flash(_("Add of new %{model} was cancelled by the user") %
{:model => ui_lookup(:model => "ContainerLabelTagMapping")})
else
add_flash(_("Edit of %{model} \"%{name}\" was cancelled by the user") %
{:model => ui_lookup(:model => "ContainerLabelTagMapping"), :name => @lt_map.label_name})
end
get_node_info(x_node)
@lt_map = @edit = session[:edit] = nil # clean out the saved info
replace_right_cell(@nodetype)
when "save", "add"
id = params[:id] ? params[:id] : "new"
return unless load_edit("label_tag_mapping_edit__#{id}", "replace_cell__explorer")
@lt_map = @edit[:lt_map] if @edit && @edit[:lt_map]
if @edit[:new][:label_name].blank?
add_flash(_("Label is required"), :error)
end
if @edit[:new][:category].blank?
add_flash(_("Category is required"), :error)
end
unless @flash_array.nil?
javascript_flash
return
end
if params[:button] == "add"
label_tag_mapping_add(@edit[:new][:entity], @edit[:new][:label_name], @edit[:new][:category])
else # save
label_tag_mapping_update(@lt_map.id, @edit[:new][:category])
end
when "reset", nil # Reset or first time in
if params[:id]
@lt_map = ContainerLabelTagMapping.find(params[:id])
lt_map_set_form_vars
else
lt_map_set_new_form_vars
end
@in_a_form = true
session[:changed] = false
if params[:button] == "reset"
add_flash(_("All changes have been reset"), :warning)
end
replace_right_cell("ltme")
end
end

def entity_ui_name_or_all(entity)
if entity.nil?
_("<All>")
else
ui_lookup(:model => entity)
end
end

def label_tag_mapping_get_all
# Current UI only supports any-value -> category mappings
mapping = ContainerLabelTagMapping.in_my_region.where(:label_value => nil)
@lt_mapping = []
mapping.each do |m|
lt_map = {}
lt_map[:id] = m.id
lt_map[:entity] = entity_ui_name_or_all(m.labeled_resource_type)
lt_map[:label_name] = m.label_name
lt_map[:category] = m.tag.category.description
@lt_mapping.push(lt_map)
end
end

# Set form variables for mapping edit (initially or after reset)
def lt_map_set_form_vars
@edit = {}
@edit[:lt_map] = @lt_map
@edit[:new] = {}
@edit[:key] = "label_tag_mapping_edit__#{@lt_map.id || "new"}"
@edit[:new][:entity] = @lt_map.labeled_resource_type.nil? ? "<All>" : @lt_map.labeled_resource_type
@edit[:new][:label_name] = @lt_map.label_name
@edit[:new][:category] = @lt_map.tag.category.description
@edit[:current] = copy_hash(@edit[:new])
@edit[:new][:options] = ContainerLabelTagMapping::MAPPABLE_ENTITIES.collect do |name|
[entity_ui_name_or_all(name), name]
end
session[:edit] = @edit
end

# Set form variables for mapping add
def lt_map_set_new_form_vars
@edit = {}
# no :lt_map
@edit[:new] = {}
@edit[:key] = "label_tag_mapping_edit__new"
@edit[:new][:entity] = nil
@edit[:new][:label_name] = nil
@edit[:new][:category] = nil
@edit[:current] = copy_hash(@edit[:new])
@edit[:new][:options] = ContainerLabelTagMapping::MAPPABLE_ENTITIES.collect do |name|
[entity_ui_name_or_all(name), name]
end
session[:edit] = @edit
end

# AJAX driven routine to check for changes in ANY field on the user form
def label_tag_mapping_field_changed
return unless load_edit("label_tag_mapping_edit__#{params[:id]}", "replace_cell__explorer")
lt_map_get_form_vars
@changed = (@edit[:new] != @edit[:current])
render :update do |page|
page << javascript_prologue
page.replace(@refresh_div,
:partial => @refresh_partial,
:locals => {:type => "container_label_tag_mapping",
:action_url => 'label_tag_mapping_field_changed'}) if @refresh_div
page << javascript_for_miq_button_visibility_changed(@changed)
end
end

# Get variables from label_tag_mapping edit form
def lt_map_get_form_vars
@lt_map = @edit[:lt_map]
@edit[:new][:entity] = params[:entity] if params[:entity]
@edit[:new][:label_name] = params[:label_name] if params[:label_name]
@edit[:new][:category] = params[:category] if params[:category]
end

def label_tag_mapping_add(entity, label_name, cat_description)
prefix = ContainerLabelTagMapping::AUTOTAG_PREFIX
entity_str = entity.nil? ? "" : entity.underscore
cat_name = "#{prefix}:#{entity_str}:" + Classification.sanitize_name(label_name.tr("/", ":"))

# UI currently can't allow 2 mappings for same (entity, label).
if Classification.find_by_name(cat_name)
add_flash(_("Mapping for %{entity}, %{label} already exists") %
{:entity => entity_ui_name_or_all(entity), :label => label_name}, :error)
javascript_flash
return
end

begin
ActiveRecord::Base.transaction do
category = Classification.create_category!(:name => cat_name,
:description => cat_description,
:single_value => true,
:read_only => true)
ContainerLabelTagMapping.create!(:labeled_resource_type => entity, :label_name => label_name,
:tag => category.tag)
end
rescue StandardError => bang
add_flash(_("Error during 'add': %{message}") % {:message => bang.message}, :error)
javascript_flash
else
add_flash(_("%{model} \"%{name}\" was added") % {:model => ui_lookup(:model => "ContainerLabelTagMapping"),
:name => label_name})
get_node_info(x_node)
@lt_map = @edit = session[:edit] = nil # clean out the saved info
replace_right_cell("root")
end
end

def label_tag_mapping_update(id, cat_description)
mapping = ContainerLabelTagMapping.find(id)
update_category = mapping.tag.classification
update_category.description = cat_description
begin
update_category.save!
rescue StandardError => bang
add_flash(_("Error during 'save': %{message}") % {:message => bang.message}, :error)
javascript_flash
else
add_flash(_("%{model} \"%{name}\" was saved") % {:model => ui_lookup(:model => "ContainerLabelTagMapping"),
:name => mapping.label_name})
get_node_info(x_node)
@lt_map = @edit = session[:edit] = nil # clean out the saved info
replace_right_cell("root")
end
end

def label_tag_mapping_delete
mapping = ContainerLabelTagMapping.find(params[:id])
category = mapping.tag.category
label_name = mapping.label_name

deleted = false
# delete mapping and category - will indirectly delete tags
ActiveRecord::Base.transaction do
deleted = mapping.destroy && category.destroy
end

if deleted
add_flash(_("%{model} \"%{name}\": Delete successful") %
{:model => ui_lookup(:model => "ContainerLabelTagMapping"), :name => label_name})
label_tag_mapping_get_all
render :update do |page|
page << javascript_prologue
page.replace_html 'settings_label_tag_mapping', :partial => 'settings_label_tag_mapping_tab'
end
else
mapping.errors.each { |field, msg| add_flash("#{field.to_s.capitalize} #{msg}", :error) }
category.errors.each { |field, msg| add_flash("#{field.to_s.capitalize} #{msg}", :error) }
javascript_flash
end
end
end
9 changes: 9 additions & 0 deletions app/models/container_label_tag_mapping.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ class ContainerLabelTagMapping < ApplicationRecord
#
# All involved tags must also have a Classification.

AUTOTAG_PREFIX = "kubernetes".freeze
MAPPABLE_ENTITIES = [nil,
"ContainerProject",
"ContainerRoute",
"ContainerNode",
"ContainerReplicator",
"ContainerService",
"ContainerGroup",
"ContainerBuild"].freeze
belongs_to :tag

# Pass the data this returns to map_* methods.
Expand Down
2 changes: 1 addition & 1 deletion app/views/layouts/_edit_form_buttons.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
- serialize ||= false
- continue_button ||= nil
- restful ||= false
- show_cancel_button = %w(user_edit user_update menu_tree zone_edit zone_update ldap_seq_edit rbac_group_edit rbac_group_update rbac_group_field_changed category_edit category_update timeprofile_edit timeprofile_update timeprofile_copy)
- show_cancel_button = %w(user_edit user_update menu_tree zone_edit zone_update ldap_seq_edit rbac_group_edit rbac_group_update rbac_group_field_changed category_edit category_update label_tag_mapping_edit label_tag_mapping_update timeprofile_edit timeprofile_update timeprofile_copy)
- force_cancel_button ||= false
&nbsp;
%table{:width => "100%"}
Expand Down
4 changes: 4 additions & 0 deletions app/views/ops/_all_tabs.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@
= _("Import Tags")
= miq_tab_header("settings_import", @sb[:active_tab]) do
= _("Import Variables")
= miq_tab_header("settings_label_tag_mapping", @sb[:active_tab]) do
= _("Map Tags")
= miq_tab_header("settings_rhn", @sb[:active_tab]) do
= _("Red Hat Updates")
= miq_tab_header("settings_replication", @sb[:active_tab]) do
Expand All @@ -77,6 +79,8 @@
= render :partial => "settings_import_tags_tab"
= miq_tab_content("settings_import", @sb[:active_tab]) do
= render :partial => "settings_import_tab"
= miq_tab_content("settings_label_tag_mapping", @sb[:active_tab]) do
= render :partial => "settings_label_tag_mapping_tab"
= miq_tab_content("settings_rhn", @sb[:active_tab]) do
= render :partial => "settings_rhn_tab"
= miq_tab_content("settings_replication", @sb[:active_tab]) do
Expand Down
47 changes: 47 additions & 0 deletions app/views/ops/_label_tag_mapping_form.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
- url = url_for(:action => 'label_tag_mapping_field_changed', :id => @lt_map && @lt_map.id ? @lt_map.id.to_s : "new")
#main_div
= render :partial => "layouts/flash_msg"
= form_tag({:action => 'label_tag_mapping_update'},
{:id => "label_tag_mapping_form",
:class => "form-horizontal",
:method => :post}) do
- disabled = !@lt_map.nil?
- if disabled
%h3= _("Container entity and label")
- else
%h3= _("Choose a container entity and label")
.form-group
%label.col-md-2.control-label
= _("Entity")
.col-md-8
= select_tag('entity',
options_for_select(@edit[:new][:options],
@edit[:new][:entity]),
:class => "selectpicker",
:disabled => disabled)
:javascript
miqInitSelectPicker();
miqSelectPickerEvent('entity', "#{url}")
.form-group
%label.col-md-2.control-label
= _("Label")
.col-md-8
= text_field_tag("label_name", @edit[:new][:label_name],
:maxlength => 25,
:class => "form-control",
:disabled => disabled,
"data-miq_observe" => {:interval => '.5',
:url => url}.to_json)
%h3= _("Choose a tag category to map to")
.form-group
%label.col-md-2.control-label
= _("Category")
.col-md-8
= text_field_tag("category", @edit[:new][:category],
:maxlength => 50,
:class => "form-control",
"data-miq_observe" => {:interval => '.5',
:url => url}.to_json)
.form-group
%label.col-md-2.control-label
.col-md-8
32 changes: 32 additions & 0 deletions app/views/ops/_settings_label_tag_mapping_tab.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
- if @sb[:active_tab] == "settings_label_tag_mapping"
= render :partial => "layouts/flash_msg"
%table.table.table-striped.table-bordered.table-hover
%thead
%tr
%th= _("Container Entity")
%th= _("Container Label")
%th= _("Tag Category")
%th= _("Actions")
%tbody
%tr#new_tr{:onclick => remote_function(:url => {:action => 'label_tag_mapping_edit', :id => nil}), :title => _("map a new label to tag")}
%td{:colspan => 3}
= _("<Click on this row to create a new mapping rule>")

%td.action-cell
%button.btn.btn-default.btn-block.btn-sm
= _("Add")
- @lt_mapping.each do |lt_map|
%tr
- t = _("Click to edit this mapping rule")
%td{:onclick => remote_function(:url => {:action => 'label_tag_mapping_edit', :id => lt_map[:id]}), :title => t}
= h(lt_map[:entity])
%td{:onclick => remote_function(:url => {:action => 'label_tag_mapping_edit', :id => lt_map[:id]}), :title => t}
= h(lt_map[:label_name])
%td{:onclick => remote_function(:url => {:action => 'label_tag_mapping_edit', :id => lt_map[:id]}), :title => t}
= h(lt_map[:category])
%td.action-cell{:onclick => remote_function(:url => {:action => 'label_tag_mapping_delete',
:id => lt_map[:id]},
:confirm => _("Are you sure you want to delete mapping '%{label_name}'?") % {:label_name => lt_map[:label_name]}),
:title => _("Click to delete this mapping")}
%button.btn.btn-default.btn-block.btn-sm
= _("Delete")
4 changes: 4 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2272,6 +2272,10 @@
forest_delete
forest_form_field_changed
forest_select
label_tag_mapping_delete
label_tag_mapping_edit
label_tag_mapping_update
label_tag_mapping_field_changed
log_depot_edit
log_depot_field_changed
log_depot_validate
Expand Down

0 comments on commit eb0afaa

Please sign in to comment.