Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add repository CRUD #346

Merged
merged 22 commits into from
Mar 27, 2017
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
ManageIQ.angular.app.controller('repositoryFormController', ['$scope', 'repositoryId', 'miqService', 'API', function($scope, repositoryId, miqService, API) {
var vm = this;

var init = function() {
vm.afterGet = false;

vm.repositoryModel = {
name: '',
description: '',
scm_type: 'git',
manager_resource: {},
scm_url: '',
scm_credentials: null,
Copy link
Contributor

@jameswnl jameswnl Mar 24, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Potentially a silly question as I am not familiar with UI code)
There can be 0 or 1 scm_credential associated with a repo/project. Here the pluralized form, does it mean multiple values expected? Or it is the list of values to be selected from?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jameswnl there are no stupid questions :) I must have missed some change because scm_credentials no longer exists and there's authentication_id instead. Thanks :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, feel good to be useful in UI land 😄

scm_branch: '',
scm_clean: false,
scm_delete_on_update: false,
scm_update_on_launch: false,
};

API.get('/api/providers?collection_class=ManageIQ::Providers::EmbeddedAutomationManager')
.then(getManagerResource)
.catch(miqService.handleFailure);

vm.model = 'repositoryModel';

ManageIQ.angular.scope = vm;

$scope.newRecord = repositoryId === 'new';

vm.scm_credentials = [{name: __('Select credentials'), value: null}];
API.get('/api/authentications?collection_class=ManageIQ::Providers::EmbeddedAnsible::AutomationManager::ScmCredential&expand=resources')
.then(getCredentials)
.catch(miqService.handleFailure);

if (repositoryId !== 'new') {
API.get('/api/configuration_script_sources/' + repositoryId)
.then(getRepositoryFormData)
.catch(miqService.handleFailure);
} else {
vm.afterGet = true;
vm.modelCopy = angular.copy( vm.repositoryModel );
miqService.sparkleOff();
}
};

$scope.cancelClicked = function() {
var message = $scope.newRecord ? __('Add of Repository cancelled by user.') : sprintf(__('Edit of Repository \"%s\" cancelled by user.'), vm.repositoryModel.name);
var url = '/ansible_repository/show_list' + '?flash_msg=' + message + '&escape=true&flash_warning=true&flash_error=false';
window.location.href = url;
};

$scope.resetClicked = function() {
vm.repositoryModel = angular.copy( vm.modelCopy );
$scope.angularForm.$setPristine(true);
miqService.miqFlash('warn', __('All changes have been reset'));
};

$scope.saveClicked = function() {
API.put('/api/configuration_script_sources/' + repositoryId, vm.repositoryModel)
.then(getBack)
.catch(miqService.handleFailure);
};

$scope.addClicked = function() {
API.post('/api/configuration_script_sources/', vm.repositoryModel)
.then(getBack)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 spaces, not three please

.catch(miqService.handleFailure);
};

var getRepositoryFormData = function(response) {
var data = response;
Object.assign(vm.repositoryModel, data);
vm.modelCopy = angular.copy( vm.repositoryModel );
vm.afterGet = true;
miqService.sparkleOff();
};

var getBack = function(response) {
var message = '';
var error = false;
if (response.hasOwnProperty('results')) {
error = ! response.results[0].success;
if (error) {
message = __('Unable to add Repository ') + vm.repositoryModel.name + ' .' + response.results[0].message;
} else {
message = sprintf(__('Add of Repository \"%s\" was successfully initialized.'), vm.repositoryModel.name);
}
} else {
error = ! response.success;
if (error) {
message = __('Unable to edit Repository') + vm.repositoryModel.name + ' .' + response.message;
} else {
message = sprintf(__('Edit of Repository \"%s\" was successfully initialized.'), vm.repositoryModel.name);
}
}
var url = '/ansible_repository/show_list' + '?flash_msg=' + message + '&escape=true';
if (error) {
url += '&flash_warning=false&flash_error=true';
}
window.location.href = url;
};

var getCredentials = function(response) {
response.resources.forEach( function(resource) {
vm.scm_credentials.push({name: resource.name, value: resource.href});
});
};

var getManagerResource = function(response) {
vm.repositoryModel.manager_resource = {'href': response.resources[0].href};
};
init();
}]);

18 changes: 18 additions & 0 deletions app/assets/javascripts/directives/url_validation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
ManageIQ.angular.app.directive('urlValidation', function() {
return {
require: 'ngModel',
link: function (_scope, _elem, _attrs, ctrl) {
ctrl.$validators.urlValidation = function (modelValue, viewValue) {
if (!viewValue) {
return true;
}
return validUrl(viewValue);
};

var validUrl = function(s) {
debugger;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ZitaNemeckova same here

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wink wink, nudge nudge

return s.substring(0, 8) === 'https://' || s.substring(0, 7) === 'http://';
};
}
}
});
51 changes: 51 additions & 0 deletions app/controllers/ansible_repository_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class AnsibleRepositoryController < ApplicationController
include Mixins::GenericShowMixin

menu_section :ansible
toolbar :ansible_repository

def self.display_methods
%w(playbooks)
Expand All @@ -19,6 +20,56 @@ def self.model
ManageIQ::Providers::EmbeddedAutomationManager::ConfigurationScriptSource
end

def title
_("Repository")
end

def button
if params[:pressed] == "embedded_configuration_script_source_edit"
id = from_cid(params[:miq_grid_checks])
javascript_redirect :action => 'edit', :id => id
elsif params[:pressed] == "embedded_configuration_script_source_add"
javascript_redirect :action => 'new'
elsif params[:pressed] == "embedded_configuration_script_source_delete"
delete_repositories
end
end

def delete_repositories
checked = find_checked_items
checked[0] = params[:id] if checked.blank? && params[:id]
AnsibleRepositoryController.model.where(:id => checked).each do |repo|
begin
repo.delete_in_provider_queue
add_flash(_("Delete of Repository \"%{name}\" was successfully initiated.") % {:name => repo.name})
rescue => ex
add_flash(_("Unable to delete Repository \"%{name}\": %{details}") % {:name => repo.name,
:details => ex},
:error)
end
end
session[:flash_msgs] = @flash_array
javascript_redirect :action => 'show_list'
end

def edit
assert_privileges('embedded_configuration_script_source_edit')
@record = AnsibleRepositoryController.model.find(params[:id])
drop_breadcrumb(:name => _("Edit a Repository \"%{name}\"") % {:name => @record.name},
:url => "/ansible_repository/edit/#{@record.id}")
@title = _("Edit Repository \"%{name}\"") % {:name => @record.name}
@id = @record.id
@in_a_form = true
end

def new
assert_privileges('embedded_configuration_script_source_add')
drop_breadcrumb(:name => _("Add a new Repository"), :url => "/ansible_repository/new")
@title = _("Add new Repository")
@id = 'new'
@in_a_form = true
end

def display_playbooks
nested_list("ansible_playbook", ManageIQ::Providers::EmbeddedAnsible::AutomationManager::Playbook)
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
class ApplicationHelper::Toolbar::AnsibleRepositoriesCenter < ApplicationHelper::Toolbar::Basic
button_group('ansible_repositories', [
select(
:ansible_repositories_configuration,
'fa fa-cog fa-lg',
t = N_('Configuration'),
t,
:items => [
button(
:embedded_configuration_script_source_add,
'pficon pficon-edit fa-lg',
t = N_('Add New Repository'),
t,
:url_parms => "new_div"),
button(
:embedded_configuration_script_source_edit,
'pficon pficon-edit fa-lg',
t = N_('Edit this Repository'),
t,
:enabled => false,
:onwhen => "1",
:url_parms => "edit_div"),
button(
:embedded_configuration_script_source_delete,
'pficon pficon-delete fa-lg',
t = N_('Remove selected Repositories'),
t,
:url_parms => "delete_div",
:enabled => false,
:onwhen => "1+",
:confirm => N_("Warning: The selected Repository will be permanently removed!")),
]
)
])
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class ApplicationHelper::Toolbar::AnsibleRepositoryCenter < ApplicationHelper::Toolbar::Basic
button_group('ansible_repository', [
select(
:ansible_repository_configuration,
'fa fa-cog fa-lg',
t = N_('Configuration'),
t,
:items => [
button(
:embedded_configuration_script_source_edit,
'pficon pficon-edit fa-lg',
t = N_('Edit this Repository'),
t,
:url => "/edit"),
button(
:embedded_configuration_script_source_delete,
'pficon pficon-delete fa-lg',
t = N_('Remove this Repository'),
t,
:url_parms => "&refresh=y",
:confirm => N_("Warning: The selected Repository will be permanently removed!")),
]
),
])
end
95 changes: 95 additions & 0 deletions app/views/ansible_repository/_repository_form.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
%br
%br
.form-horizontal
%form#form_div{:name => "angularForm",
'ng-controller' => "repositoryFormController as vm",
'ng-show' => "vm.afterGet",
'ng-cloak' => '',
"miq-form" => true,
"model" => "vm.repositoryModel",
"model-copy" => 'vm.modelCopy',
:novalidate => true}
= render :partial => "layouts/flash_msg"
.form-group{"ng-class" => "{'has-error': angularForm.name.$invalid}"}
%label.col-md-2.control-label
= _('Name')
.col-md-8
%input.form-control{:type => "text",
:name => "name",
"id" => "name",
'ng-model' => "vm.repositoryModel.name",
:maxlength => MAX_NAME_LEN,
:required => "",
:checkchange => true}
%span.help-block{"ng-show" => "angularForm.name.$error.required"}
= _("Required")
.form-group
%label.col-md-2.control-label
= _('Description')
.col-md-8
%input.form-control{:type => "text",
:name => "description",
"id" => "description",
'ng-model' => "vm.repositoryModel.description",
:checkchange => true}
.form-group
%label.col-md-2.control-label
= _('SCM type')
.col-md-8
= select_tag('scm_type',
options_for_select([["#{_('GIT')}", "git"]], 'git'),
"ng-model" => "vm.repositoryModel.scm_type",
:disabled => true)
.form-group{"ng-class" => "{'has-error': angularForm.scm_url.$error.required || angularForm.scm_url.$error.urlValidation}"}
%label.col-md-2.control-label
= _('URL')
.col-md-8
%input.form-control{:type => "text",
:name => "scm_url",
:id => "scm_url",
'ng-model' => "vm.repositoryModel.scm_url",
:required => "",
:checkchange => true,
'url-validation' => true}
%span.help-block{"ng-show" => "angularForm.scm_url.$error.required"}
= _("Required")
%span.help-block{"ng-show" => "angularForm.scm_url.$error.urlValidation"}
= _("URL must include a protocol (http:// or https://)")
.form-group
%label.col-md-2.control-label
= _('SCM credentials')
.col-md-8
%select{'ng-model' => 'vm.repositoryModel.scm_credentials',
'ng-options' => 'scm_credential.value as scm_credential.name for scm_credential in vm.scm_credentials'}
.form-group
%label.col-md-2.control-label
= _('SCM Branch')
.col-md-8
%input.form-control{:type => "text",
:name => "scm_branch",
"id" => "scm_branch",
'ng-model' => "vm.repositoryModel.scm_branch",
:checkchange => true}
.form-group
%label.col-md-2.control-label
= _('SCM Update Options')
.col-md-8
%div
%label
= check_box_tag("clean", "1", false, 'ng-model' => "vm.repositoryModel.scm_clean")
= _('Clean')
%div
%label
= check_box_tag("scm_delete_on_update", "1", false, 'ng-model' => 'vm.repositoryModel.scm_delete_on_update')
= _('Delete on Update')
%div
%label
= check_box_tag("scm_update_on_launch", "1", false, 'ng-model' => 'vm.repositoryModel.scm_update_on_launch')
= _('Update on Launch')
= render :partial => "layouts/angular/x_edit_buttons_angular"
:javascript
ManageIQ.angular.app.value('repositoryId', '#{@id}');
miq_bootstrap('#form_div');
$("#form-div").submit(function (e) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is broken since you should never be submitting the form (old-style) .. Better to just put miqSparkleOn() in addClicked and saveClicked.

miqSparkleOn();
});
2 changes: 2 additions & 0 deletions app/views/ansible_repository/edit.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#main_div
= render :partial => 'repository_form', :locals => {:newRecord => false}
2 changes: 2 additions & 0 deletions app/views/ansible_repository/new.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#main_div
= render :partial => 'repository_form', :locals => {:newRecord => true}
8 changes: 7 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2033,11 +2033,17 @@
:get => %w(
download_data
download_summary_pdf
edit
new
show
show_list
),
:post => %w(
show_list)
button
edit
new
show_list
)
},

:miq_ae_class => {
Expand Down