Skip to content

Commit

Permalink
Fixes for editing a playbook service template
Browse files Browse the repository at this point in the history
  • Loading branch information
lgalis committed Mar 27, 2017
1 parent e809b39 commit d074607
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 55 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
ManageIQ.angular.app.controller('catalogItemFormController', ['$scope', 'catalogItemFormId', 'miqService', 'postService', 'API', 'catalogItemDataFactory', function($scope, catalogItemFormId, miqService, postService, API, catalogItemDataFactory) {
var vm = this;
var sort_options = "&sort_by=name&sort_order=ascending"
var init = function() {
var init = function () {
vm.catalogItemModel = {
name: '',
description: '',
Expand All @@ -23,7 +23,6 @@ ManageIQ.angular.app.controller('catalogItemFormController', ['$scope', 'catalog
provisioning_value: '',
provisioning_variables: {},
provisioning_editMode: false,
provisioning_cloud_type: '',
retirement_repository_id: '',
retirement_playbook_id: '',
retirement_machine_credential_id: '',
Expand All @@ -37,20 +36,22 @@ ManageIQ.angular.app.controller('catalogItemFormController', ['$scope', 'catalog
retirement_value: '',
retirement_variables: {},
retirement_editMode: false,
retirement_cloud_type: ''
};
getRemoveResourcesTypes();
vm.provisioning_cloud_type = '';
vm.retirement_cloud_type = '';
vm.formId = catalogItemFormId;
vm.afterGet = false;
vm.model = "catalogItemModel";

ManageIQ.angular.scope = $scope;
$scope.saveable = miqService.saveable;

if (catalogItemFormId == 'new') {
$scope.newRecord = true;
vm.formOptions();
vm.afterGet = true;
vm.modelCopy = angular.copy( vm.catalogItemModel );
vm.afterGet = true;
vm.modelCopy = angular.copy(vm.catalogItemModel);
} else {
vm.newRecord = false;
catalogItemDataFactory.getCatalogItemData(catalogItemFormId).then(function (catalogItemData) {
Expand All @@ -59,34 +60,46 @@ ManageIQ.angular.app.controller('catalogItemFormController', ['$scope', 'catalog
vm.catalogItemModel.description = catalogItemData.description;
vm.catalogItemModel.display = catalogItemData.display;
vm.catalogItemModel.catalog_id = catalogItemData.service_template_catalog_id;
vm.catalogItemModel.provisioning_dialog_id = catalogItemData.provisioning_dialog_id;
vm.catalogItemModel.retirement_dialog_id = catalogItemData.retirement_dialog_id;
vm.formOptions();
vm.catalogItemModel.provisioning_dialog_id = catalogItemData.provisioning_dialog_id;
vm.catalogItemModel.retirement_dialog_id = catalogItemData.retirement_dialog_id;
vm.formOptions(catalogItemData.config_info);
getConfigInfo(catalogItemData.config_info);
vm.afterGet = true;
vm.modelCopy = angular.copy(vm.catalogItemModel);
});
}
};

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

var getConfigInfo = function(configData) {
var getCredentialType = function (prefix, credentialId) {
var url = '/api/authentications/' + credentialId;
API.get(url).then(function (data) {
vm[prefix + '_cloud_type'] = data.type;

if (vm.cloud_types[vm[prefix + '_cloud_type']] != 'undefined') {
vm['_' + prefix + '_cloud_type'] = data.type;
getCloudCredentialsforType(prefix, data.type);
}
})
};

var getConfigInfo = function (configData) {
vm.catalogItemModel.provisioning_repository_id = configData.provision.repository_id;
vm.catalogItemModel.provisioning_playbook_id = configData.provision.playbook_id;
vm.catalogItemModel.provisioning_machine_credential_id = configData.provision.credential_id;
Expand All @@ -100,10 +113,11 @@ ManageIQ.angular.app.controller('catalogItemFormController', ['$scope', 'catalog
vm.catalogItemModel.provisioning_value = '';
setExtraVars('provisioning_variables', configData.provision.extra_vars);

vm.catalogItemModel.retirement_remove_resources = configData.retirement.remove_resources;
if (typeof configData.retirement.repository_id !== 'undefined') {
vm.catalogItemModel.retirement_repository_id = configData.retirement.repository_id;
vm.catalogItemModel.retirement_playbook_id = configData.retirement.playbook_id;
getRemoveResourcesTypes();
vm.catalogItemModel.retirement_remove_resources = configData.retirement.remove_resources;
vm.catalogItemModel.retirement_machine_credential_id = configData.retirement.credential_id;
vm.catalogItemModel.retirement_dialog_existing = configData.retirement.dialog_id ? "existing" : "create";
vm.catalogItemModel.retirement_dialog_id = configData.retirement.dialog_id;
Expand All @@ -117,14 +131,15 @@ ManageIQ.angular.app.controller('catalogItemFormController', ['$scope', 'catalog
setExtraVars('retirement_variables', configData.retirement.extra_vars);
};

var setExtraVars = function(variableName, extraVars) {
var setExtraVars = function (variableName, extraVars) {
if (typeof extraVars !== 'undefined')
vm.catalogItemModel[variableName] = extraVars;
$scope.checkFormPristine();
}

var redirectUrl = '/catalog/explorer/' + catalogItemFormId;

$scope.cancelClicked = function() {
$scope.cancelClicked = function () {
if ($scope.newRecord)
var msg = sprintf(__("Add of Catalog Item was cancelled by the user"));
else
Expand All @@ -133,15 +148,15 @@ ManageIQ.angular.app.controller('catalogItemFormController', ['$scope', 'catalog
$scope.angularForm.$setPristine(true);
};

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

$scope.saveClicked = function() {
$scope.saveClicked = function () {
var successMsg = sprintf(__("Catalog Item %s was saved"), vm.catalogItemModel.name);
postService.saveRecord('/api/service_templates/' + catalogItemFormId,
redirectUrl + '?button=save',
Expand All @@ -150,7 +165,7 @@ ManageIQ.angular.app.controller('catalogItemFormController', ['$scope', 'catalog
$scope.angularForm.$setPristine(true);
};

$scope.addClicked = function($event, formSubmit) {
$scope.addClicked = function ($event, formSubmit) {
var successMsg = sprintf(__("Catalog Item %s was added"), vm.catalogItemModel.name);
postService.createRecord('/api/service_templates',
redirectUrl + '?button=add',
Expand All @@ -159,10 +174,10 @@ ManageIQ.angular.app.controller('catalogItemFormController', ['$scope', 'catalog
$scope.angularForm.$setPristine(true);
};

var setConfigInfo = function(configData) {
var setConfigInfo = function (configData) {
catalog_item = {
name: configData.name,
description: configData.description,
description: configData.description,
display: configData.display,
service_template_catalog_id: configData.catalog_id,
prov_type: "generic_ansible_playbook",
Expand Down Expand Up @@ -218,8 +233,8 @@ ManageIQ.angular.app.controller('catalogItemFormController', ['$scope', 'catalog


// list of service catalogs
vm.formOptions = function() {
API.get("/api/service_catalogs/?expand=resources&attributes=id,name" + sort_options).then(function(data) {
vm.formOptions = function (configData = null) {
API.get("/api/service_catalogs/?expand=resources&attributes=id,name" + sort_options).then(function (data) {
vm.catalogs = data.resources;
vm._catalog = _.find(vm.catalogs, {id: vm.catalogItemModel.catalog_id});
})
Expand Down Expand Up @@ -252,35 +267,41 @@ ManageIQ.angular.app.controller('catalogItemFormController', ['$scope', 'catalog
vm._provisioning_network_credential = _.find(vm.network_credentials, {id: vm.catalogItemModel.provisioning_network_credential_id});
})

API.options('/api/authentications').then(function(data) {
API.options('/api/authentications').then(function (data) {
var cloud_types = {};
angular.forEach(data.data.credential_types.embedded_ansible_credential_types, function(cred_object, cred_type) {
angular.forEach(data.data.credential_types.embedded_ansible_credential_types, function (cred_object, cred_type) {
if (cred_object.type == 'cloud')
cloud_types[cred_type] = cred_object.label;
});
vm.cloud_types = getSortedHash(cloud_types);
if (configData && configData.provision.cloud_credential_id) {
getCredentialType('provisioning', configData.provision.cloud_credential_id);
}
if (configData && configData.retirement.cloud_credential_id) {
getCredentialType('retirement', configData.retirement.cloud_credential_id);
}
});
};

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

// get playbooks for selected repository
vm.repositoryChanged = function(prefix, id) {
API.get("/api/configuration_script_sources/" + id + "?attributes=configuration_script_payloads" + sort_options).then(function(data) {
vm[prefix + '_playbooks'] = data.configuration_script_payloads;
vm.repositoryChanged = function (prefix, id) {
API.get("/api/configuration_script_sources/" + id + "?attributes=configuration_script_payloads" + sort_options).then(function (data) {
vm[prefix + '_playbooks'] = data.configuration_script_payloads;
// if repository has changed
if (id !== vm.catalogItemModel[prefix + '_repository_id']) {
vm.catalogItemModel[prefix + '_playbook_id'] = '';
Expand All @@ -291,55 +312,93 @@ ManageIQ.angular.app.controller('catalogItemFormController', ['$scope', 'catalog
})
};

$scope.$watch('vm._provisioning_repository', function(value) {
$scope.$watch('vm._provisioning_repository', function (value) {
if (value) {
vm.repositoryChanged("provisioning", value.id)
} else
} else {
vm.catalogItemModel['provisioning_repository_id'] = '';
}
$scope.checkFormPristine();
});

$scope.$watch('vm._retirement_repository', function(value) {
$scope.$watch('vm._retirement_repository', function (value) {
if (value) {
vm.repositoryChanged("retirement", value.id)
} else {
vm.catalogItemModel['retirement_playbook_id'] = '';
vm.catalogItemModel['retirement_repository_id'] = '';
getRemoveResourcesTypes();
}
$scope.checkFormPristine();
});

$scope.checkFormPristine = function (){
if (angular.equals(vm.catalogItemModel, vm.modelCopy)) {
$scope.angularForm.$setPristine();
}
else{
$scope.angularForm.$setDirty();
}
};

$scope.cloudTypeChanged = function(prefix) {
var typ = vm.catalogItemModel[prefix + "_cloud_type"];
var typ = vm[prefix + "_cloud_type"];
getCloudCredentialsforType(prefix, typ);
};

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

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

vm.playbookTypeChanged = function(prefix) {
if (prefix === "retirement")
getRemoveResourcesTypes();
$scope.checkFormPristine();
};

$scope.$watch('vm._retirement_playbook', function(value) {
if (value) {
vm.catalogItemModel['retirement_playbook_id'] = value.id;
} else
} else {
vm.catalogItemModel['retirement_playbook_id'] = '';
vm.playbookTypeChanged("retirement");
}
$scope.checkFormPristine();
});

$scope.$watch('vm.catalogItemModel.display', function(value) {
vm.catalogItemModel.display = value;
return vm.catalogItemModel.display;
})

$scope.$watch('vm._provisioning_cloud_type', function(value) {
if (value) {
vm['provisioning_cloud_type'] = value;
}
else{
vm['provisioning_cloud_type'] = '';
}
$scope.cloudTypeChanged("provisioning");
$scope.checkFormPristine();
})


$scope.$watch('vm._retirement_cloud_type', function(value) {
var valueChanged = (value != vm['retirement_cloud_type']);
if (value){
vm['retirement_cloud_type'] = value;
}
else{
vm['retirement_cloud_type'] = '';
}
if(valueChanged)
$scope.cloudTypeChanged("retirement");
$scope.checkFormPristine();
})

vm.addKeyValue = function(prefix) {
vm.catalogItemModel[prefix + "_variables"][vm.catalogItemModel[prefix + "_key"]] = vm.catalogItemModel[prefix + "_value"];
vm.catalogItemModel[prefix + "_key"] = '';
Expand Down Expand Up @@ -404,6 +463,19 @@ ManageIQ.angular.app.controller('catalogItemFormController', ['$scope', 'catalog
$scope.$watch(field_name, function(value) {
if (value)
vm.catalogItemModel[name + '_id'] = value.id;
$scope.checkFormPristine();
});
};

// watch for all the drop downs on screen
"retirement_remove_resources".split(" ").forEach(typeWatch)

function typeWatch(name) {
field_name = "vm.catalogItemModel." + name;
$scope.$watch(field_name, function(value) {
if (value)
vm.catalogItemModel[name] = value;
$scope.checkFormPristine();
});
}

Expand All @@ -418,9 +490,9 @@ ManageIQ.angular.app.controller('catalogItemFormController', ['$scope', 'catalog
vm.catalogItemModel.retirement_playbook_id = vm.catalogItemModel.provisioning_playbook_id;
vm.catalogItemModel.retirement_machine_credential_id = vm.catalogItemModel.provisioning_machine_credential_id;
vm.catalogItemModel.retirement_network_credential_id = vm.catalogItemModel.provisioning_network_credential_id;
vm.catalogItemModel.retirement_cloud_type = vm.catalogItemModel.provisioning_cloud_type;
vm.retirement_cloud_type = vm.provisioning_cloud_type;
vm.catalogItemModel.retirement_cloud_credential_id = vm.catalogItemModel.provisioning_cloud_credential_id;
if (vm.catalogItemModel.retirement_cloud_type !== '')
if (vm.retirement_cloud_type !== '')
$scope.cloudTypeChanged('retirement');
vm.catalogItemModel.retirement_inventory = vm.catalogItemModel.provisioning_inventory;
vm.catalogItemModel.retirement_dialog_existing = vm.catalogItemModel.provisioning_dialog_existing;
Expand All @@ -429,6 +501,7 @@ ManageIQ.angular.app.controller('catalogItemFormController', ['$scope', 'catalog
vm.catalogItemModel.retirement_key = '';
vm.catalogItemModel.retirement_value = '';
vm.catalogItemModel.retirement_variables = angular.copy(vm.catalogItemModel.provisioning_variables);
$scope.checkFormPristine();
}

var closeConfirmationModal = function() {
Expand Down
1 change: 1 addition & 0 deletions app/views/catalog/_st_angular_form.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"ng-controller" => "catalogItemFormController as vm",
"miq-form" => 'true',
"ng-show" => "vm.afterGet",
"model" => "vm.catalogItemModel",
"model-copy" => 'vm.modelCopy'}
= render :partial => "layouts/flash_msg"
%div
Expand Down
Loading

0 comments on commit d074607

Please sign in to comment.