|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +angular.module('openshiftConsole').component('bindService', { |
| 4 | + controller: [ |
| 5 | + '$filter', |
| 6 | + 'DataService', |
| 7 | + 'DNS1123_SUBDOMAIN_VALIDATION', |
| 8 | + BindService |
| 9 | + ], |
| 10 | + controllerAs: 'ctrl', |
| 11 | + bindings: { |
| 12 | + target: '<', |
| 13 | + serviceInstances: '<', |
| 14 | + serviceClasses: '<', |
| 15 | + onClose: '<' |
| 16 | + }, |
| 17 | + templateUrl: 'views/directives/bind-service.html' |
| 18 | +}); |
| 19 | + |
| 20 | +function BindService($filter, |
| 21 | + DataService, |
| 22 | + DNS1123_SUBDOMAIN_VALIDATION) { |
| 23 | + var ctrl = this; |
| 24 | + |
| 25 | + ctrl.steps = [{ |
| 26 | + id: 'services', |
| 27 | + label: 'Services', |
| 28 | + view: 'views/directives/bind-service/select-service.html' |
| 29 | + }, { |
| 30 | + label: 'Results', |
| 31 | + id: 'results', |
| 32 | + view: 'views/directives/bind-service/results.html' |
| 33 | + }]; |
| 34 | + |
| 35 | + ctrl.$onInit = function() { |
| 36 | + // TODO: handle not having any service instances, or handle path where coming into binding from svc instance |
| 37 | + ctrl.gotoStep(ctrl.steps[0]); |
| 38 | + }; |
| 39 | + |
| 40 | + var statusCondition = $filter('statusCondition'); |
| 41 | + ctrl.$onChanges = function(changes) { |
| 42 | + if (changes.serviceInstances && !ctrl.serviceToBind) { |
| 43 | + var newestReady; |
| 44 | + var newestNotReady; |
| 45 | + _.each(ctrl.serviceInstances, function(instance) { |
| 46 | + var ready = _.get(statusCondition(instance, 'Ready'), 'status') === 'True'; |
| 47 | + if (ready && (!newestReady || instance.metadata.creationTimestamp > newestReady.metadata.creationTimestamp)) { |
| 48 | + newestReady = instance; |
| 49 | + } |
| 50 | + if (!ready && (!newestNotReady || instance.metadata.creationTimestamp > newestNotReady.metadata.creationTimestamp)) { |
| 51 | + newestNotReady = instance; |
| 52 | + } |
| 53 | + }); |
| 54 | + ctrl.serviceToBind = _.get(newestReady, 'metadata.name') || _.get(newestNotReady, 'metadata.name'); |
| 55 | + } |
| 56 | + |
| 57 | + // wait till both service instances and service classes are available so that the sort is stable and items dont jump around |
| 58 | + if ((changes.serviceInstances || changes.serviceClasses) && ctrl.serviceClasses && ctrl.serviceInstances) { |
| 59 | + var instances = _.toArray(ctrl.serviceInstances); |
| 60 | + instances.sort(function(left, right) { |
| 61 | + var leftName = _.get(ctrl.serviceClasses, [left.spec.serviceClassName, 'osbMetadata', 'displayName']) || left.spec.serviceClassName; |
| 62 | + var rightName = _.get(ctrl.serviceClasses, [left.spec.serviceClassName, 'osbMetadata', 'displayName']) || right.spec.serviceClassName; |
| 63 | + |
| 64 | + // Fall back to sorting by `metadata.name` if the display names are the |
| 65 | + // same so that the sort is stable. |
| 66 | + if (leftName === rightName) { |
| 67 | + leftName = _.get(left, 'metadata.name', ''); |
| 68 | + rightName = _.get(right, 'metadata.name', ''); |
| 69 | + } |
| 70 | + |
| 71 | + return leftName.localeCompare(rightName); |
| 72 | + }); |
| 73 | + ctrl.orderedServiceInstances = instances; |
| 74 | + } |
| 75 | + }; |
| 76 | + |
| 77 | + var gotoStepID = function(id) { |
| 78 | + var step = _.find(ctrl.steps, { id: id }); |
| 79 | + ctrl.gotoStep(step); |
| 80 | + }; |
| 81 | + |
| 82 | + ctrl.gotoStep = function(step) { |
| 83 | + _.each(ctrl.steps, function(st) { |
| 84 | + st.selected = false; |
| 85 | + }); |
| 86 | + if (ctrl.currentStep) { |
| 87 | + ctrl.currentStep.visited = true; |
| 88 | + } |
| 89 | + ctrl.currentStep = step; |
| 90 | + ctrl.currentStep.selected = true; |
| 91 | + }; |
| 92 | + |
| 93 | + ctrl.stepClick = function(step) { |
| 94 | + // Prevent returning to previous steps if the order is complete. |
| 95 | + if (ctrl.wizardComplete) { |
| 96 | + return; |
| 97 | + } |
| 98 | + |
| 99 | + if (!step.visited) { |
| 100 | + return; |
| 101 | + } |
| 102 | + |
| 103 | + ctrl.gotoStep(step); |
| 104 | + }; |
| 105 | + |
| 106 | + var generateName = $filter('generateName'); |
| 107 | + var makeBinding = function() { |
| 108 | + var serviceInstanceName = _.get(ctrl.serviceInstances[ctrl.serviceToBind], 'metadata.name'); |
| 109 | + // TODO - would be better if generateName could take in an optional maxlength |
| 110 | + var truncatedSvcInstanceName = _.trunc(serviceInstanceName, DNS1123_SUBDOMAIN_VALIDATION.maxlength - 6); |
| 111 | + ctrl.generatedSecretName = generateName(truncatedSvcInstanceName + '-'); |
| 112 | + var binding = { |
| 113 | + kind: 'Binding', |
| 114 | + apiVersion: 'servicecatalog.k8s.io/v1alpha1', |
| 115 | + metadata: { |
| 116 | + generateName: serviceInstanceName + '-' |
| 117 | + }, |
| 118 | + spec: { |
| 119 | + instanceRef: { |
| 120 | + name: serviceInstanceName |
| 121 | + }, |
| 122 | + secretName: ctrl.generatedSecretName |
| 123 | + } |
| 124 | + }; |
| 125 | + |
| 126 | + return binding; |
| 127 | + }; |
| 128 | + |
| 129 | + ctrl.bindService = function() { |
| 130 | + var context = { |
| 131 | + namespace: _.get(ctrl.serviceInstances[ctrl.serviceToBind], 'metadata.namespace') |
| 132 | + }; |
| 133 | + DataService.create({ |
| 134 | + group: 'servicecatalog.k8s.io', |
| 135 | + resource: 'bindings' |
| 136 | + }, null, makeBinding(), context).then(function(binding) { |
| 137 | + ctrl.binding = binding; |
| 138 | + |
| 139 | + DataService.watchObject({ |
| 140 | + group: 'servicecatalog.k8s.io', |
| 141 | + resource: 'bindings' |
| 142 | + }, _.get(ctrl.binding, 'metadata.name'), context, function(binding) { |
| 143 | + ctrl.binding = binding; |
| 144 | + }); |
| 145 | + |
| 146 | + ctrl.wizardComplete = true; |
| 147 | + ctrl.error = null; |
| 148 | + gotoStepID('results'); |
| 149 | + }, function(e) { |
| 150 | + ctrl.error = e; |
| 151 | + }); |
| 152 | + }; |
| 153 | + |
| 154 | + ctrl.closeWizard = function() { |
| 155 | + if (_.isFunction(ctrl.onClose)) { |
| 156 | + ctrl.onClose(); |
| 157 | + } |
| 158 | + }; |
| 159 | + |
| 160 | +} |
0 commit comments