Skip to content

Commit

Permalink
Automate method: add location and list of providers to model.
Browse files Browse the repository at this point in the history
  • Loading branch information
martinpovolny committed Mar 5, 2019
1 parent 8901142 commit 9c93036
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 10 deletions.
@@ -1,7 +1,7 @@
ManageIQ.angular.app.controller('aeMethodFormController', aeMethodFormController);
aeMethodFormController.$inject = ['$http', '$scope', 'aeMethodFormId', 'currentRegion', 'miqService', 'playbookReusableCodeMixin'];
aeMethodFormController.$inject = ['$http', '$scope', 'aeMethodFormId', 'currentRegion', 'miqService', 'playbookReusableCodeMixin', 'location'];

function aeMethodFormController($http, $scope, aeMethodFormId, currentRegion, miqService, playbookReusableCodeMixin) {
function aeMethodFormController($http, $scope, aeMethodFormId, currentRegion, miqService, playbookReusableCodeMixin, location) {
var vm = this;
var init = function() {
vm.aeMethodModel = {
Expand All @@ -16,6 +16,7 @@ function aeMethodFormController($http, $scope, aeMethodFormId, currentRegion, mi
key_value: '',
key_type: 'string',
available_datatypes: '',
providers: [],
provisioning_repository_id: '',
provisioning_playbook_id: '',
provisioning_machine_credential_id: '',
Expand Down Expand Up @@ -44,7 +45,7 @@ function aeMethodFormController($http, $scope, aeMethodFormId, currentRegion, mi

ManageIQ.angular.scope = $scope;

$http.get('method_form_fields/' + aeMethodFormId)
$http.get('method_form_fields/' + aeMethodFormId + '?location=' + location)
.then(getMethodFormData)
.catch(miqService.handleFailure);
vm.saveable = miqService.saveable;
Expand All @@ -61,6 +62,7 @@ function aeMethodFormController($http, $scope, aeMethodFormId, currentRegion, mi
vm.aeMethodModel.namespace_path = data.namespace_path;
vm.aeMethodModel.location = data.location;
vm.aeMethodModel.location_fancy_name = data.location_fancy_name;
vm.aeMethodModel.providers = data.providers;
vm.aeMethodModel.class_id = data.class_id;
vm.aeMethodModel.language = data.language;
vm.aeMethodModel.scope = data.scope;
Expand Down
46 changes: 41 additions & 5 deletions app/controllers/miq_ae_class_controller.rb
Expand Up @@ -499,9 +499,11 @@ def edit_method
end
@ae_method = find_record_with_rbac(MiqAeMethod, id[1])
@selectable_methods = embedded_method_regex(@ae_method.fqname)
if @ae_method.location == "playbook"
if written_in_angular(@ae_method.location)
# these variants are implemented in Angular
angular_form_specific_data
else
# other variants are implemented server side
set_method_form_vars
@in_a_form = true
end
Expand Down Expand Up @@ -863,6 +865,10 @@ def fields_form_field_changed
end
end

def written_in_angular(location)
%w(playbook ansible_job_template ansible_workflow_template).include?(@edit[:new][:location])
end

# AJAX driven routine to check for changes in ANY field on the form
def form_method_field_changed
if !@sb[:form_vars_set] # workaround to prevent an error that happens when IE sends a transaction form form even after save button is clicked when there is text_area in the form
Expand Down Expand Up @@ -905,12 +911,19 @@ def form_method_field_changed
end
@changed = (@edit[:new] != @edit[:current])
@edit[:default_verify_status] = @edit[:new][:location] == "inline" && @edit[:new][:data] && @edit[:new][:data] != ""
angular_form_specific_data if @edit[:new][:location] == "playbook"

in_angular = written_in_angular(@edit[:new][:location])
angular_form_specific_data if in_angular

render :update do |page|
page << javascript_prologue
page.replace_html('form_div', :partial => 'method_form', :locals => {:prefix => ""}) if @edit[:new][:location] == 'expression'
if @edit[:new][:location] == "playbook"
page.replace_html(@refresh_div, :partial => 'angular_method_form')
if in_angular
page.replace_html(
@refresh_div,
:partial => 'angular_method_form',
:locals => {:location => @edit[:new][:location]}
)
page << javascript_hide("form_buttons_div")
elsif @refresh_div && (params[:cls_method_location] || params[:exp_object] || params[:cls_exp_object])
page.replace_html(@refresh_div, :partial => @refresh_partial)
Expand Down Expand Up @@ -977,8 +990,30 @@ def form_method_field_changed
end
end

def list_playbooks_internal(location, provider_id)
provider = Provider.find(provider_id)
case location
when 'ansible_job_template'
ManageIQ::Providers::AnsibleTower::AutomationManager::ConfigurationScript.
where(:manager_id => provider.automation_manager.id).pluck(:id, :name)
when 'ansible_workflow_template'
ManageIQ::Providers::AnsibleTower::AutomationManager::ConfigurationWorkflow.
where(:manager_id => provider.automation_manager.id).pluck(:id, :name)
else
[]
end
end
private :list_playbooks_internal

def method_form_fields
location = 'playbook' # FIXME: add new ones
assert_privileges("miq_ae_method_edit")
location = params['location'] || 'playbook'
list_of_providers = if %w(ansible_job_template ansible_workflow_template).include?(location)
#ManageIQ::Providers::AnsibleTower::Provider.where('zone_id != ?', Zone.maintenance_zone.id)
ManageIQ::Providers::AnsibleTower::Provider.all.pluck(:id, :name).
map { |r| {:id => r[0], :name => r[1]} }
end

method = params[:id] == "new" ? MiqAeMethod.new : MiqAeMethod.find(params[:id])
method_hash = {
:name => method.name,
Expand All @@ -989,6 +1024,7 @@ def method_form_fields
:location_fancy_name => location_fancy_name(location),
:language => 'ruby',
:scope => "instance",
:providers => list_of_providers,
:available_datatypes => MiqAeField.available_datatypes_for_ui,
:config_info => { :repository_id => method.options[:repository_id] || '',
:playbook_id => method.options[:playbook_id] || '',
Expand Down
Expand Up @@ -17,7 +17,20 @@
:title => _("Copy from Provisioning")}
= _('Copy from Provisioning')
.form-group{"ng-class" => "{'has-error': angularForm.#{prefix}_repository_id.$invalid}"}
.form-group{"ng-class" => "{'has-error': angularForm.#{prefix}_provider_id.$invalid}",
"ng-if" => "#{ng_model}.location!=='playbook'"}
%label.col-md-3.control-label{"for" => "#{ng_model}.provider_id"}
= _('Provider')
.col-md-9
%select{"ng-model" => "#{ng_model}.provider",
"name" => "#{ng_model}_provider_id",
'ng-options' => "provider as provider.name for provider in #{ng_model}.providers",
'miq-select' => true}
%option{"value" => ""}
= "<#{_('Choose')}>"

.form-group{"ng-class" => "{'has-error': angularForm.#{prefix}_repository_id.$invalid}",
"ng-if" => "#{ng_model}.location==='playbook'"}
%label.col-md-3.control-label{"for" => "vm.#{prefix}_repository_id"}
= _('Repository')
.col-md-9
Expand Down
1 change: 1 addition & 0 deletions app/views/miq_ae_class/_angular_method_form.html.haml
Expand Up @@ -59,4 +59,5 @@
:javascript
ManageIQ.angular.app.value('aeMethodFormId', '#{@record.id || "new"}');
ManageIQ.angular.app.value('currentRegion', '#{@current_region}');
ManageIQ.angular.app.value('location', '#{location}');
miq_bootstrap('#form_div');
2 changes: 1 addition & 1 deletion app/views/miq_ae_class/_class_methods.html.haml
Expand Up @@ -32,4 +32,4 @@
- elsif @angular_form || @edit[:new][:fields]
.form_div
- partial_name = @angular_form ? 'angular_method_form' : 'method_form'
= render :partial => partial_name, :locals => {:prefix => "cls_"}
= render :partial => partial_name, :locals => {:prefix => "cls_", :location => @edit[:new][:location]}

0 comments on commit 9c93036

Please sign in to comment.