Skip to content

Commit

Permalink
Addressed some of the code climate warnings.
Browse files Browse the repository at this point in the history
Added 'playbookReusableCodeMixin' for playbook related common methods.

https://www.pivotaltracker.com/story/show/149747321
  • Loading branch information
h-kataria committed Sep 15, 2017
1 parent c95f917 commit b2c5e2d
Show file tree
Hide file tree
Showing 10 changed files with 337 additions and 516 deletions.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

199 changes: 199 additions & 0 deletions app/assets/javascripts/controllers/playbook-reusable-code-mixin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
ManageIQ.angular.app.service('playbookReusableCodeMixin', ['API', function(API) {
var sortOptions = "&sort_by=name&sort_order=ascending";

var getSortedHash = function(inputHash) {
var sortedHash = Object.keys(inputHash)
.map(function(key) {
return ({"k": key, "v": inputHash[key]});
})
.sort(function(a, b) {
return a.v.localeCompare(b.v);
})
.reduce(function(o, e) {
o[e.k] = e.v;
return o;
}, {});
return sortedHash;
};

var getVerbosityTypes = function() {
return {
"0": "0 (Normal)",
"1": "1 (Verbose)",
"2": "2 (More Verbose)",
"3": "3 (Debug)",
"4": "4 (Connection Debug)",
"5": "5 (WinRM Debug)",
};
};

var checkFormPristine = function(model, modelCopy, form) {
if (angular.equals(model, modelCopy)) {
form.$setPristine();
} else {
form.$setDirty();
}
};

var getCloudCredentialsforType = function(prefix, typ, vm) {
// list of cloud credentials based upon selected cloud type
var url = '/api/authentications?collection_class=' + typ + '&expand=resources&attributes=id,name' + sortOptions;
API.get(url).then(function(data) {
vm[prefix + '_cloud_credentials'] = data.resources;
findObjectForDropDown(prefix, '_cloud_credential', '_cloud_credentials', vm);
});
};

var findObjectForDropDown = function(prefix, fieldName, listName, vm) {
vm['_' + prefix + fieldName] = _.find(vm[prefix + listName], {id: vm[vm.model][prefix + fieldName + '_id']});
};

var setIfDefined = function(value) {
return (typeof value !== 'undefined') ? value : '';
};

var cloudCredentialsList = function(vm, provisionCredentialId, retirementCredentialId) {
if (provisionCredentialId) {
getCredentialType('provisioning', provisionCredentialId, vm);
} else {
vm._provisioning_cloud_type = '';
vm._provisioning_cloud_credential_id = '';
}
if (vm[vm.model].retirement_cloud_credential_id !== undefined) {
if (retirementCredentialId) {
getCredentialType('retirement', retirementCredentialId, vm);
} else {
vm._retirement_cloud_type = '';
vm._retirement_cloud_credential_id = '';
}
}
};

var getCredentialType = function(prefix, credentialId, vm) {
var url = '/api/authentications/' + credentialId;
API.get(url).then(function(data) {
vm[prefix + '_cloud_type'] = data.type;
if (vm.cloudTypes[vm[prefix + '_cloud_type']] !== 'undefined') {
vm['_' + prefix + '_cloud_type'] = data.type;
getCloudCredentialsforType(prefix, data.type, vm);
}
});
};

// list of service catalogs
var formOptions = function(vm) {
if (vm[vm.model].catalog_id !== undefined) {
API.get('/api/service_catalogs/?expand=resources&attributes=id,name' + sortOptions).then(function(data) {
vm.catalogs = data.resources;
vm._catalog = _.find(vm.catalogs, {id: vm[vm.model].catalog_id});
});
}

// list of service dialogs
if (vm[vm.model].provisioning_dialog_id !== undefined) {
API.get('/api/service_dialogs/?expand=resources&attributes=id,label&sort_by=label&sort_order=ascending').then(
function(data) {
vm.dialogs = data.resources;
vm._provisioning_dialog = _.find(vm.dialogs, {id: vm[vm.model].provisioning_dialog_id});
});
}

// list of repositories
API.get('/api/configuration_script_sources?collection_class=ManageIQ::Providers::EmbeddedAnsible::AutomationManager::ConfigurationScriptSource&expand=resources&attributes=id,name&filter[]=region_number=' + vm.currentRegion + sortOptions).then(function(data) {
vm.repositories = data.resources;
vm._retirement_repository = _.find(vm.repositories, {id: vm[vm.model].retirement_repository_id});
vm._provisioning_repository = _.find(vm.repositories, {id: vm[vm.model].provisioning_repository_id});
});

// list of machine credentials
API.get('/api/authentications?collection_class=ManageIQ::Providers::EmbeddedAnsible::AutomationManager::MachineCredential&expand=resources&attributes=id,name' + sortOptions).then(function(data) {
vm.machine_credentials = data.resources;
vm._retirement_machine_credential = _.find(vm.machine_credentials, {id: vm[vm.model].retirement_machine_credential_id});
vm._provisioning_machine_credential = _.find(vm.machine_credentials, {id: vm[vm.model].provisioning_machine_credential_id});
});

// list of network credentials
API.get('/api/authentications?collection_class=ManageIQ::Providers::EmbeddedAnsible::AutomationManager::NetworkCredential&expand=resources&attributes=id,name' + sortOptions).then(function(data) {
vm.network_credentials = data.resources;
vm._retirement_network_credential = _.find(vm.network_credentials, {id: vm[vm.model].retirement_network_credential_id});
vm._provisioning_network_credential = _.find(vm.network_credentials, {id: vm[vm.model].provisioning_network_credential_id});
});
};

// list of cloud credentials
var formCloudCredentials = function(vm, provisionCredentialId, retirementCredentialId) {
API.options('/api/authentications').then(function(data) {
var cloudTypes = {};
angular.forEach(data.data.credential_types.embedded_ansible_credential_types, function(credObject, credType) {
if (credObject.type === 'cloud') {
cloudTypes[credType] = credObject.label;
}
});
vm.cloudTypes = getSortedHash(cloudTypes);
cloudCredentialsList(vm, provisionCredentialId, retirementCredentialId);
});
};

// get playbooks for selected repository
var repositoryChanged = function(vm, prefix, id) {
API.get('/api/configuration_script_sources/' + id + '/configuration_script_payloads?expand=resources&filter[]=region_number=' + vm.currentRegion + sortOptions).then(function(data) {
vm[prefix + '_playbooks'] = data.resources;
// if repository has changed
if (id !== vm[vm.model][prefix + '_repository_id']) {
vm[vm.model][prefix + '_playbook_id'] = '';
vm[vm.model][prefix + '_repository_id'] = id;
if (vm[vm.model].retirement_remove_resources !== undefined) {
getRemoveResourcesTypes();
}
} else {
findObjectForDropDown(prefix, '_playbook', '_playbooks', vm);
}
});
};

var getRemoveResourcesTypes = function(vm) {
if (vm[vm.model].retirement_repository_id === undefined || vm[vm.model].retirement_repository_id === '') {
vm[vm.model].retirement_remove_resources = 'yes_without_playbook';
vm.remove_resources_types = {
'No': 'no_without_playbook',
'Yes': 'yes_without_playbook',
};
} else {
vm[vm.model].retirement_remove_resources = 'no_with_playbook';
vm.remove_resources_types = {
'No': 'no_with_playbook',
'Before Playbook runs': 'pre_with_playbook',
'After Playbook runs': 'post_with_playbook',
};
}
};

var cloudTypeChanged = function(vm, prefix, value) {
var valueChanged = (value !== vm.provisioning_cloud_type);
if (value) {
vm.provisioning_cloud_type = value;
} else {
vm.provisioning_cloud_type = '';
}
if (valueChanged) {
var typ = vm.provisioning_cloud_type;
vm[vm.model].provisioning_cloud_credential_id = '';
getCloudCredentialsforType(prefix, typ, vm);
}
};

return {
checkFormPristine: checkFormPristine,
cloudCredentialsList: cloudCredentialsList,
cloudTypeChanged: cloudTypeChanged,
getVerbosityTypes: getVerbosityTypes,
getSortedHash: getSortedHash,
getCloudCredentialsforType: getCloudCredentialsforType,
getRemoveResourcesTypes: getRemoveResourcesTypes,
findObjectForDropDown: findObjectForDropDown,
formOptions: formOptions,
formCloudCredentials: formCloudCredentials,
repositoryChanged: repositoryChanged,
setIfDefined: setIfDefined,
};
}]);
2 changes: 1 addition & 1 deletion app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1785,7 +1785,7 @@ def build_created_audit(rec, eh)
}
end

def build_saved_audit_hash(old_record_attributes, new_record, add)
def build_saved_audit_hash_angular(old_record_attributes, new_record, add)
name = new_record.respond_to?(:name) ? new_record.name : new_record.description
msg = if add
_("[%{name}] Record added (") % {:name => name}
Expand Down
29 changes: 0 additions & 29 deletions app/controllers/host_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -309,35 +309,6 @@ def update
end
end

def build_saved_audit_hash_angular(old_attributes, new_record, add)
name = new_record.respond_to?(:name) ? new_record.name : new_record.description
msg = if add
_("[%{name}] Record added (") % {:name => name}
else
_("[%{name}] Record updated (") % {:name => name}
end
event = "#{new_record.class.to_s.downcase}_record_#{add ? "add" : "update"}"

attribute_difference = new_record.attributes.to_a - old_attributes.to_a
attribute_difference = Hash[*attribute_difference.flatten]

difference_messages = []

attribute_difference.each do |key, value|
difference_messages << _("%{key} changed to %{value}") % {:key => key, :value => value}
end

msg = msg + difference_messages.join(", ") + ")"

{
:event => event,
:target_id => new_record.id,
:target_class => new_record.class.base_class.name,
:userid => session[:userid],
:message => msg
}
end

# handle buttons pressed on the button bar
def button
@edit = session[:edit] # Restore @edit for adv search box
Expand Down
3 changes: 2 additions & 1 deletion app/controllers/miq_ae_class_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -974,6 +974,7 @@ def form_method_field_changed
def method_form_fields
method = params[:id] == "new" ? MiqAeMethod.new : MiqAeMethod.find_by(:id => params[:id])
data = method.data ? JSON.parse(method.data) : {}
p "XXXXX #{data['cloud_credential_id']}"
method_hash = {
:name => method.name,
:display_name => method.display_name,
Expand Down Expand Up @@ -1168,7 +1169,7 @@ def add_update_method
else
old_method_attributes = method.attributes.clone
add_flash(_("%{model} \"%{name}\" was saved") % {:model => ui_lookup(:model => "MiqAeMethod"), :name => method.name})
AuditEvent.success(build_saved_audit_hash(old_method_attributes, method, params[:button] == "add"))
AuditEvent.success(build_saved_audit_hash_angular(old_method_attributes, method, params[:button] == "add"))
replace_right_cell(:replace_trees => [:ae])
return
end
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/ops_controller/ops_rbac.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def rbac_tenant_edit_save_add
return
end

AuditEvent.success(build_saved_audit_hash(old_tenant_attributes, tenant, params[:button] == "add"))
AuditEvent.success(build_saved_audit_hash_angular(old_tenant_attributes, tenant, params[:button] == "add"))
add_flash(_("%{model} \"%{name}\" was saved") %
{:model => tenant_type_title_string(params[:divisible] == "true"), :name => tenant.name})
if params[:button] == "add"
Expand Down
31 changes: 1 addition & 30 deletions app/controllers/ops_controller/settings/schedules.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def schedule_edit
add_flash(_("Error when adding a new schedule: %{message}") % {:message => bang.message}, :error)
javascript_flash
else
AuditEvent.success(build_saved_audit_hash(old_schedule_attributes, schedule, params[:button] == "add"))
AuditEvent.success(build_saved_audit_hash_angular(old_schedule_attributes, schedule, params[:button] == "add"))
add_flash(_("%{model} \"%{name}\" was saved") %
{:model => ui_lookup(:model => "MiqSchedule"), :name => schedule.name})
if params[:button] == "add"
Expand Down Expand Up @@ -257,35 +257,6 @@ def log_depot_validate

private

def build_saved_audit_hash(old_schedule_attributes, new_schedule, add)
name = new_schedule.respond_to?(:name) ? new_schedule.name : new_schedule.description
msg = if add
_("[%{name}] Record added (") % {:name => name}
else
_("[%{name}] Record updated (") % {:name => name}
end
event = "#{new_schedule.class.to_s.downcase}_record_#{add ? "add" : "update"}"

attribute_difference = new_schedule.attributes.to_a - old_schedule_attributes.to_a
attribute_difference = Hash[*attribute_difference.flatten]

difference_messages = []

attribute_difference.each do |key, value|
difference_messages << _("%{key} changed to %{value}") % {:key => key, :value => value}
end

msg = msg + difference_messages.join(", ") + ")"

{
:event => event,
:target_id => new_schedule.id,
:target_class => new_schedule.class.base_class.name,
:userid => session[:userid],
:message => msg
}
end

def schedule_check_compliance?(schedule)
schedule.sched_action && schedule.sched_action[:method] && schedule.sched_action[:method] == "check_compliance"
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
.col-md-9
%select{"ng-model" => "vm._#{prefix}_cloud_type",
"name" => "#{prefix}_cloud_type",
'ng-options' => "k as v for (k,v) in vm.cloud_types",
'ng-options' => "k as v for (k,v) in vm.cloudTypes",
"pf-select" => true}
%option{"value" => ""}
= "<#{_('Choose')}>"
Expand Down Expand Up @@ -299,7 +299,7 @@
%td{"ng-if" => "#{ng_model}.#{prefix}_key != ''"}
%button{:class => "btn btn-link",
:type => "button",
"ng-click" => "vm.addKeyValue('#{prefix}')",
"ng-click" => "vm.addKeyValue()",
"ng-if" => "#{ng_model}.#{prefix}_key != ''"}
%span{:class => "fa fa-plus tag-add"}

Expand Down Expand Up @@ -352,13 +352,13 @@
%td.table-view-pf-select
%div{"ng-if" => "#{ng_model}.#{prefix}_editMode && $index === #{ng_model}.s_index"}
%button{:class => "btn btn-link", :type => "button", "ng-disabled" => "(#{ng_model}.key === '' || #{ng_model}.key_value === '')", "ng-click" => "vm.saveKeyValue('#{prefix}', $index)"}
%button{:class => "btn btn-link", :type => "button", "ng-disabled" => "#{ng_model}.key === ''", "ng-click" => "vm.saveKeyValue($index)"}
%i.pficon.pficon-save
%button{:class => "btn btn-link", :type => "button", "ng-click" => "vm.cancelKeyValue($index)"}
%i.pficon.pficon-close

%div{"ng-if" => "!#{ng_model}.#{prefix}_editMode || (#{ng_model}.#{prefix}_editMode && $index !== #{ng_model}.s_index)", :class => "btn-container"}
%button{:class => "btn btn-link", :type => "button", "ng-click" => "vm.editKeyValue('#{prefix}', this.arr[0], this.arr[1], this.arr[2], $index)", "ng-disabled" => "#{ng_model}.#{prefix}_editMode"}
%button{:class => "btn btn-link", :type => "button", "ng-click" => "vm.editKeyValue(this.arr[0], this.arr[1], this.arr[2], $index)", "ng-disabled" => "#{ng_model}.#{prefix}_editMode"}
%span{:class => "pficon pficon-edit"}
%button{:class => "btn btn-link", :type => "button", "ng-click" => "vm.removeKeyValue('#{prefix}', this.arr[0], this.arr[1], this.arr[2], $index)", "ng-disabled" => "#{ng_model}.#{prefix}_editMode"}
%button{:class => "btn btn-link", :type => "button", "ng-click" => "vm.removeKeyValue($index)", "ng-disabled" => "#{ng_model}.#{prefix}_editMode"}
%span{:class => "pficon pficon-delete"}
2 changes: 1 addition & 1 deletion app/views/miq_ae_class/_angular_method_form.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
:id => @record.id,
:prefix => "provisioning",
:basic_info_needed => true}
= render :partial => "layouts/angular/x_edit_buttons_angular"
= render :partial => "layouts/angular/generic_form_buttons"
:javascript
ManageIQ.angular.app.value('aeMethodFormId', '#{@record.id || "new"}');
Expand Down

0 comments on commit b2c5e2d

Please sign in to comment.