<commit_before>class CloudSubnetController < ApplicationController
  before_action :check_privileges
  before_action :get_session_data
  after_action :cleanup_action
  after_action :set_session_data

  include Mixins::GenericButtonMixin
  include Mixins::GenericListMixin
  include Mixins::GenericSessionMixin
  include Mixins::GenericShowMixin
  include Mixins::GenericFormMixin

  def self.display_methods
    %w(instances cloud_subnets)
  end

  def button
    @edit = session[:edit] # Restore @edit for adv search box
    params[:display] = @display if %w(vms instances images).include?(@display)
    params[:page] = @current_page unless @current_page.nil? # Save current page for list refresh

    @refresh_div = "main_div"

    case params[:pressed]
    when "cloud_subnet_tag"
      return tag("CloudSubnet")
    when 'cloud_subnet_delete'
      delete_subnets
    when "cloud_subnet_edit"
      javascript_redirect :action => "edit", :id => checked_item_id
    when params[:pressed] == "custom_button"
      custom_buttons
      return
    else
      if params[:pressed] == "cloud_subnet_new"
        javascript_redirect :action => "new"
      elsif !flash_errors? && @refresh_div == "main_div" && @lastaction == "show_list"
        replace_gtl_main_div
      else
        render_flash
      end
    end
  end

  def new
    assert_privileges("cloud_subnet_new")
    @subnet = CloudSubnet.new
    @in_a_form = true
    @network_provider_choices = {}
    ExtManagementSystem.where(:type => "ManageIQ::Providers::Openstack::NetworkManager").find_each do |ems|
      @network_provider_choices[ems.name] = ems.id
    end
    drop_breadcrumb(
      :name => _("Add New Subnet"),
      :url  => "/cloud_subnet/new"
    )
  end

  def create
    assert_privileges("cloud_subnet_new")
    case params[:button]
    when "cancel"
      javascript_redirect :action    => 'show_list',
                          :flash_msg => _("Creation of a Cloud Subnet was cancelled by the user")

    when "add"
      @subnet = CloudSubnet.new
      options = new_form_params
      ems = ExtManagementSystem.find(options[:ems_id])
      if CloudSubnet.class_by_ems(ems).supports_create?
        options.delete(:ems_id)
        task_id = ems.create_cloud_subnet_queue(session[:userid], options)

        if task_id.kind_of?(Integer)
          initiate_wait_for_task(:task_id => task_id, :action => "create_finished")
        else
          javascript_flash(
            :text        => _("Cloud Subnet creation: Task start failed: ID [%{id}]") % {:id => task_id.to_s},
            :severity    => :error,
            :spinner_off => true
          )
        end
      else
        @in_a_form = true
        add_flash(_(CloudSubnet.unsupported_reason(:create)), :error)
        drop_breadcrumb(:name => _("Add new Cloud Subnet "), :url => "/subnet/new")
        javascript_flash
      end
    end
  end

  def create_finished
    task_id = session[:async][:params][:task_id]
    subnet_name = session[:async][:params][:name]
    task = MiqTask.find(task_id)
    if MiqTask.status_ok?(task.status)
      add_flash(_("Cloud Subnet \"%{name}\" created") % { :name  => subnet_name })
    else
      add_flash(_("Unable to create Cloud Subnet: %{details}") %
                { :name => subnet_name, :details => task.message }, :error)
    end

    @breadcrumbs.pop if @breadcrumbs
    session[:edit] = nil
    session[:flash_msgs] = @flash_array.dup if @flash_array
    javascript_redirect :action => "show_list"
  end

  def delete_subnets
    assert_privileges("cloud_subnet_delete")
    subnets = find_records_with_rbac(CloudSubnet, checked_or_params)
    if subnets.empty?
      add_flash(_("No Cloud Subnet were selected for deletion."), :error)
    end

    subnets_to_delete = []
    subnets.each do |subnet|
      if subnet.nil?
        add_flash(_("Cloud Subnet no longer exists."), :error)
      elsif subnet.supports_delete?
        subnets_to_delete.push(subnet)
      end
    end
    unless subnets_to_delete.empty?
      process_cloud_subnets(subnets_to_delete, "destroy")
    end

    # refresh the list if applicable
    if @lastaction == "show_list"
      show_list
      @refresh_partial = "layouts/gtl"
    elsif @lastaction == "show" && @layout == "cloud_subnet"
      @single_delete = true unless flash_errors?
      if @flash_array.nil?
        add_flash(_("The selected Cloud Subnet was deleted"))
      end
      javascript_redirect :action => 'show_list', :flash_msg => @flash_array[0][:message]
    else
      drop_breadcrumb(:name => 'dummy', :url  => " ") # missing a bc to get correctly back so here's a dummy
      session[:flash_msgs] = @flash_array.dup if @flash_array
      redirect_to(previous_breadcrumb_url)
    end
  end

  def edit
    params[:id] = checked_item_id unless params[:id].present?
    assert_privileges("cloud_subnet_edit")
    @subnet = find_record_with_rbac(CloudSubnet, params[:id])
    @in_a_form = true
    drop_breadcrumb(
      :name => _("Edit Subnet \"%{name}\"") % {:name => @subnet.name},
      :url  => "/cloud_subnet/edit/#{@subnet.id}"
    )
  end

  def update
    assert_privileges("cloud_subnet_edit")
    @subnet = find_record_with_rbac(CloudSubnet, params[:id])
    options = changed_form_params
    case params[:button]
    when "cancel"
      cancel_action(_("Edit of Subnet \"%{name}\" was cancelled by the user") % {:name  => @subnet.name})

    when "save"
      if @subnet.supports_create?
        task_id = @subnet.update_cloud_subnet_queue(session[:userid], options)

        if task_id.kind_of?(Integer)
          initiate_wait_for_task(:task_id => task_id, :action => "update_finished")
        else
          javascript_flash(
            :text        => _("Cloud Subnet update failed: Task start failed: ID [%{id}]") % {:id => task_id.to_s},
            :severity    => :error,
            :spinner_off => true
          )
        end
      else
        add_flash(_("Couldn't initiate update of Cloud Subnet \"%{name}\": %{details}") % {
          :name    => @subnet.name,
          :details => @subnet.unsupported_reason(:update)
        }, :error)
      end
    end
  end

  def update_finished
    task_id = session[:async][:params][:task_id]
    subnet_name = session[:async][:params][:name]
    task = MiqTask.find(task_id)
    if MiqTask.status_ok?(task.status)
      add_flash(_("Cloud Subnet \"%{name}\" updated") % {:name  => subnet_name })
    else
      add_flash(
        _("Unable to update Cloud Subnet \"%{name}\": %{details}") % { :name    => subnet_name,
                                                                       :details => task.message }, :error)
    end

    session[:edit] = nil
    session[:flash_msgs] = @flash_array.dup if @flash_array
    javascript_redirect previous_breadcrumb_url
  end

  private

  def textual_group_list
    [%i(properties relationships), %i(tags)]
  end
  helper_method :textual_group_list

  def switch_to_bol(option)
    if option && option =~ /on|true/i
      true
    else
      false
    end
  end

  def changed_form_params
    # Fields allowed for update are: name, enable_dhcp, dns_nameservers, allocation_pools, host_routes, gateway_ip
    options = {}
    options[:name] = params[:name] unless @subnet.name == params[:name]

    # A gateway address is automatically assigned by Openstack when gateway is null
    unless @subnet.gateway == params[:gateway]
      options[:gateway_ip] = params[:gateway].blank? ? nil : params[:gateway]
    end
    unless @subnet.dhcp_enabled == switch_to_bol(params[:dhcp_enabled])
      options[:enable_dhcp] = switch_to_bol(params[:dhcp_enabled])
    end
    # TODO: Add dns_nameservers, allocation_pools, host_routes
    options
  end

  def new_form_params
    params[:network_protocol] ||= "ipv4"
    params[:dhcp_enabled] ||= false
    options = {}
    options[:name] = params[:name] if params[:name]
    options[:ems_id] = params[:ems_id] if params[:ems_id]
    options[:cidr] = params[:cidr] if params[:cidr]
    # An address is automatically assigned by Openstack when gateway is null
    if params[:gateway]
      options[:gateway_ip] = params[:gateway].blank? ? nil : params[:gateway]
    end
    options[:ip_version] = params[:network_protocol] =~ /4/ ? 4 : 6
    options[:cloud_tenant] = find_record_with_rbac(CloudTenant, from_cid(params[:cloud_tenant_id])) if params[:cloud_tenant_id]
    options[:network_id] = params[:network_id] if params[:network_id]
    options[:enable_dhcp] = params[:dhcp_enabled]
    # TODO: Add extra fields
    options[:availability_zone_id] = params[:availability_zone_id] if params[:availability_zone_id]
    if params[:ipv6_router_advertisement_mode]
      options[:ipv6_router_advertisement_mode] = params[:ipv6_router_advertisement_mode]
    end
    options[:ipv6_address_mode] = params[:ipv6_address_mode] if params[:ipv6_address_mode]
    options[:network_group_id] = params[:network_group_id] if params[:network_group_id]
    options[:parent_cloud_subnet_id] = params[:parent_cloud_subnet_id] if params[:parent_cloud_subnet_id]
    options
  end

  # dispatches operations to multiple subnets
  def process_cloud_subnets(subnets, operation)
    return if subnets.empty?

    if operation == "destroy"
      deleted_subnets = 0
      subnets.each do |subnet|
        audit = {
          :event        => "cloud_subnet_record_delete_initiated",
          :message      => "[#{subnet.name}] Record delete initiated",
          :target_id    => subnet.id,
          :target_class => "CloudSubnet",
          :userid       => session[:userid]
        }
        AuditEvent.success(audit)
        subnet.delete_cloud_subnet_queue(session[:userid])
      end
    end
  end

  menu_section :net

  has_custom_buttons
end
<commit_msg>Adding ovirt's 'network manager' to the 'cloud subnet' 'provider choices'<commit_after>class CloudSubnetController < ApplicationController
  before_action :check_privileges
  before_action :get_session_data
  after_action :cleanup_action
  after_action :set_session_data

  include Mixins::GenericButtonMixin
  include Mixins::GenericListMixin
  include Mixins::GenericSessionMixin
  include Mixins::GenericShowMixin
  include Mixins::GenericFormMixin

  def self.display_methods
    %w(instances cloud_subnets)
  end

  def button
    @edit = session[:edit] # Restore @edit for adv search box
    params[:display] = @display if %w(vms instances images).include?(@display)
    params[:page] = @current_page unless @current_page.nil? # Save current page for list refresh

    @refresh_div = "main_div"

    case params[:pressed]
    when "cloud_subnet_tag"
      return tag("CloudSubnet")
    when 'cloud_subnet_delete'
      delete_subnets
    when "cloud_subnet_edit"
      javascript_redirect :action => "edit", :id => checked_item_id
    when params[:pressed] == "custom_button"
      custom_buttons
      return
    else
      if params[:pressed] == "cloud_subnet_new"
        javascript_redirect :action => "new"
      elsif !flash_errors? && @refresh_div == "main_div" && @lastaction == "show_list"
        replace_gtl_main_div
      else
        render_flash
      end
    end
  end

  def new
    assert_privileges("cloud_subnet_new")
    @subnet = CloudSubnet.new
    @in_a_form = true
    @network_provider_choices = {}
    ExtManagementSystem.where(:type => ['ManageIQ::Providers::Openstack::NetworkManager', 'ManageIQ::Providers::Redhat::NetworkManager']).find_each do |ems|
      @network_provider_choices[ems.name] = ems.id
    end
    drop_breadcrumb(
      :name => _("Add New Subnet"),
      :url  => "/cloud_subnet/new"
    )
  end

  def create
    assert_privileges("cloud_subnet_new")
    case params[:button]
    when "cancel"
      javascript_redirect :action    => 'show_list',
                          :flash_msg => _("Creation of a Cloud Subnet was cancelled by the user")

    when "add"
      @subnet = CloudSubnet.new
      options = new_form_params
      ems = ExtManagementSystem.find(options[:ems_id])
      if CloudSubnet.class_by_ems(ems).supports_create?
        options.delete(:ems_id)
        task_id = ems.create_cloud_subnet_queue(session[:userid], options)

        if task_id.kind_of?(Integer)
          initiate_wait_for_task(:task_id => task_id, :action => "create_finished")
        else
          javascript_flash(
            :text        => _("Cloud Subnet creation: Task start failed: ID [%{id}]") % {:id => task_id.to_s},
            :severity    => :error,
            :spinner_off => true
          )
        end
      else
        @in_a_form = true
        add_flash(_(CloudSubnet.unsupported_reason(:create)), :error)
        drop_breadcrumb(:name => _("Add new Cloud Subnet "), :url => "/subnet/new")
        javascript_flash
      end
    end
  end

  def create_finished
    task_id = session[:async][:params][:task_id]
    subnet_name = session[:async][:params][:name]
    task = MiqTask.find(task_id)
    if MiqTask.status_ok?(task.status)
      add_flash(_("Cloud Subnet \"%{name}\" created") % { :name  => subnet_name })
    else
      add_flash(_("Unable to create Cloud Subnet: %{details}") %
                { :name => subnet_name, :details => task.message }, :error)
    end

    @breadcrumbs.pop if @breadcrumbs
    session[:edit] = nil
    session[:flash_msgs] = @flash_array.dup if @flash_array
    javascript_redirect :action => "show_list"
  end

  def delete_subnets
    assert_privileges("cloud_subnet_delete")
    subnets = find_records_with_rbac(CloudSubnet, checked_or_params)
    if subnets.empty?
      add_flash(_("No Cloud Subnet were selected for deletion."), :error)
    end

    subnets_to_delete = []
    subnets.each do |subnet|
      if subnet.nil?
        add_flash(_("Cloud Subnet no longer exists."), :error)
      elsif subnet.supports_delete?
        subnets_to_delete.push(subnet)
      end
    end
    unless subnets_to_delete.empty?
      process_cloud_subnets(subnets_to_delete, "destroy")
    end

    # refresh the list if applicable
    if @lastaction == "show_list"
      show_list
      @refresh_partial = "layouts/gtl"
    elsif @lastaction == "show" && @layout == "cloud_subnet"
      @single_delete = true unless flash_errors?
      if @flash_array.nil?
        add_flash(_("The selected Cloud Subnet was deleted"))
      end
      javascript_redirect :action => 'show_list', :flash_msg => @flash_array[0][:message]
    else
      drop_breadcrumb(:name => 'dummy', :url  => " ") # missing a bc to get correctly back so here's a dummy
      session[:flash_msgs] = @flash_array.dup if @flash_array
      redirect_to(previous_breadcrumb_url)
    end
  end

  def edit
    params[:id] = checked_item_id unless params[:id].present?
    assert_privileges("cloud_subnet_edit")
    @subnet = find_record_with_rbac(CloudSubnet, params[:id])
    @in_a_form = true
    drop_breadcrumb(
      :name => _("Edit Subnet \"%{name}\"") % {:name => @subnet.name},
      :url  => "/cloud_subnet/edit/#{@subnet.id}"
    )
  end

  def update
    assert_privileges("cloud_subnet_edit")
    @subnet = find_record_with_rbac(CloudSubnet, params[:id])
    options = changed_form_params
    case params[:button]
    when "cancel"
      cancel_action(_("Edit of Subnet \"%{name}\" was cancelled by the user") % {:name  => @subnet.name})

    when "save"
      if @subnet.supports_create?
        task_id = @subnet.update_cloud_subnet_queue(session[:userid], options)

        if task_id.kind_of?(Integer)
          initiate_wait_for_task(:task_id => task_id, :action => "update_finished")
        else
          javascript_flash(
            :text        => _("Cloud Subnet update failed: Task start failed: ID [%{id}]") % {:id => task_id.to_s},
            :severity    => :error,
            :spinner_off => true
          )
        end
      else
        add_flash(_("Couldn't initiate update of Cloud Subnet \"%{name}\": %{details}") % {
          :name    => @subnet.name,
          :details => @subnet.unsupported_reason(:update)
        }, :error)
      end
    end
  end

  def update_finished
    task_id = session[:async][:params][:task_id]
    subnet_name = session[:async][:params][:name]
    task = MiqTask.find(task_id)
    if MiqTask.status_ok?(task.status)
      add_flash(_("Cloud Subnet \"%{name}\" updated") % {:name  => subnet_name })
    else
      add_flash(
        _("Unable to update Cloud Subnet \"%{name}\": %{details}") % { :name    => subnet_name,
                                                                       :details => task.message }, :error)
    end

    session[:edit] = nil
    session[:flash_msgs] = @flash_array.dup if @flash_array
    javascript_redirect previous_breadcrumb_url
  end

  private

  def textual_group_list
    [%i(properties relationships), %i(tags)]
  end
  helper_method :textual_group_list

  def switch_to_bol(option)
    if option && option =~ /on|true/i
      true
    else
      false
    end
  end

  def changed_form_params
    # Fields allowed for update are: name, enable_dhcp, dns_nameservers, allocation_pools, host_routes, gateway_ip
    options = {}
    options[:name] = params[:name] unless @subnet.name == params[:name]

    # A gateway address is automatically assigned by Openstack when gateway is null
    unless @subnet.gateway == params[:gateway]
      options[:gateway_ip] = params[:gateway].blank? ? nil : params[:gateway]
    end
    unless @subnet.dhcp_enabled == switch_to_bol(params[:dhcp_enabled])
      options[:enable_dhcp] = switch_to_bol(params[:dhcp_enabled])
    end
    # TODO: Add dns_nameservers, allocation_pools, host_routes
    options
  end

  def new_form_params
    params[:network_protocol] ||= "ipv4"
    params[:dhcp_enabled] ||= false
    options = {}
    options[:name] = params[:name] if params[:name]
    options[:ems_id] = params[:ems_id] if params[:ems_id]
    options[:cidr] = params[:cidr] if params[:cidr]
    # An address is automatically assigned by Openstack when gateway is null
    if params[:gateway]
      options[:gateway_ip] = params[:gateway].blank? ? nil : params[:gateway]
    end
    options[:ip_version] = params[:network_protocol] =~ /4/ ? 4 : 6
    options[:cloud_tenant] = find_record_with_rbac(CloudTenant, from_cid(params[:cloud_tenant_id])) if params[:cloud_tenant_id]
    options[:network_id] = params[:network_id] if params[:network_id]
    options[:enable_dhcp] = params[:dhcp_enabled]
    # TODO: Add extra fields
    options[:availability_zone_id] = params[:availability_zone_id] if params[:availability_zone_id]
    if params[:ipv6_router_advertisement_mode]
      options[:ipv6_router_advertisement_mode] = params[:ipv6_router_advertisement_mode]
    end
    options[:ipv6_address_mode] = params[:ipv6_address_mode] if params[:ipv6_address_mode]
    options[:network_group_id] = params[:network_group_id] if params[:network_group_id]
    options[:parent_cloud_subnet_id] = params[:parent_cloud_subnet_id] if params[:parent_cloud_subnet_id]
    options
  end

  # dispatches operations to multiple subnets
  def process_cloud_subnets(subnets, operation)
    return if subnets.empty?

    if operation == "destroy"
      deleted_subnets = 0
      subnets.each do |subnet|
        audit = {
          :event        => "cloud_subnet_record_delete_initiated",
          :message      => "[#{subnet.name}] Record delete initiated",
          :target_id    => subnet.id,
          :target_class => "CloudSubnet",
          :userid       => session[:userid]
        }
        AuditEvent.success(audit)
        subnet.delete_cloud_subnet_queue(session[:userid])
      end
    end
  end

  menu_section :net

  has_custom_buttons
end

{'fraction_non_alphanumeric': 0.11357949032225116, 'fraction_numerical': 0.0012256779531178183, 'mean_word_length': 3.1637252817350627, 'pattern_counts': {'":': 4, '<': 5, '<?xml version=': 0, '>': 91, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3619122', 'n_tokens_mistral': 6365, 'n_tokens_neox': 6215, 'n_words': 1324}