|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +/** |
| 4 | + * @ngdoc function |
| 5 | + * @name openshiftConsole.controller:ProvisionedServicesController |
| 6 | + * @description |
| 7 | + * # ProjectController |
| 8 | + * Controller of the openshiftConsole |
| 9 | + */ |
| 10 | +angular.module('openshiftConsole') |
| 11 | + .controller('ProvisionedServicesController', function ($routeParams, |
| 12 | + $scope, |
| 13 | + APIService, |
| 14 | + BindingService, |
| 15 | + Constants, |
| 16 | + DataService, |
| 17 | + ProjectsService, |
| 18 | + ResourceAlertsService, |
| 19 | + $filter, |
| 20 | + LabelFilter) { |
| 21 | + //Logger) { |
| 22 | + |
| 23 | + var overview = this; |
| 24 | + var limitWatches = $filter('isIE')() || $filter('isEdge')(); |
| 25 | + var DEFAULT_POLL_INTERVAL = 60 * 1000; // milliseconds |
| 26 | + |
| 27 | + // Enable service catalog features if the new experience is enabled and the |
| 28 | + // servicecatalog.k8s.io resources are available. |
| 29 | + var SERVICE_CATALOG_ENABLED = |
| 30 | + _.get(Constants, 'ENABLE_TECH_PREVIEW_FEATURE.service_catalog_landing_page') && |
| 31 | + APIService.apiInfo({ group: 'servicecatalog.k8s.io', resource: 'serviceclasses' }) && |
| 32 | + APIService.apiInfo({ group: 'servicecatalog.k8s.io', resource: 'instances' }) && |
| 33 | + APIService.apiInfo({ group: 'servicecatalog.k8s.io', resource: 'bindings' }); |
| 34 | + |
| 35 | + $scope.projectName = $routeParams.project; |
| 36 | + $scope.services = {}; |
| 37 | + $scope.unfilteredServices = {}; |
| 38 | + $scope.routesByService = {}; |
| 39 | + $scope.routes = {}; |
| 40 | + $scope.labelSuggestions = {}; |
| 41 | + $scope.alerts = $scope.alerts || {}; |
| 42 | + $scope.emptyMessage = "Loading..."; |
| 43 | + $scope.emptyMessageRoutes = "Loading..."; |
| 44 | + |
| 45 | + var state = overview.state = { |
| 46 | + notificationsByObjectUID: {}, |
| 47 | + serviceInstances: {} |
| 48 | + }; |
| 49 | + |
| 50 | + var getUID = function(apiObject) { |
| 51 | + return _.get(apiObject, 'metadata.uid'); |
| 52 | + }; |
| 53 | + |
| 54 | + var setNotifications = function(apiObject, notifications) { |
| 55 | + var uid = getUID(apiObject); |
| 56 | + state.notificationsByObjectUID[uid] = notifications || {}; |
| 57 | + }; |
| 58 | + // TODO: code duplicated from directives/bindService.js |
| 59 | + // extract & share |
| 60 | + var sortServiceInstances = function() { |
| 61 | + if(!state.serviceInstances && !state.serviceClasses) { |
| 62 | + state.bindableServiceInstances = null; |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + state.bindableServiceInstances = _.filter(state.serviceInstances, function(serviceInstance) { |
| 67 | + return BindingService.isServiceBindable(serviceInstance, state.serviceClasses); |
| 68 | + }); |
| 69 | + |
| 70 | + // |
| 71 | + //state.orderedServiceInstances = _.sortBy(state.serviceInstances, |
| 72 | + $scope.services = _.sortBy(state.serviceInstances, |
| 73 | + function(item) { |
| 74 | + return _.get(state.serviceClasses, [item.spec.serviceClassName, 'externalMetadata', 'displayName']) || item.spec.serviceClassName; |
| 75 | + }, |
| 76 | + function(item) { |
| 77 | + return _.get(item, 'metadata.name', ''); |
| 78 | + } |
| 79 | + ); |
| 80 | + }; |
| 81 | + |
| 82 | + // Update the label filter suggestions for a list of objects. This should |
| 83 | + // only be called for filterable top-level items to avoid polluting the list. |
| 84 | + /*var updateLabelSuggestions = function(objects) { |
| 85 | + if (_.isEmpty(objects)) { |
| 86 | + return; |
| 87 | + } |
| 88 | +
|
| 89 | + LabelFilter.addLabelSuggestionsFromResources(objects, labelSuggestions); |
| 90 | + if (overview.viewBy !== 'pipeline') { |
| 91 | + LabelFilter.setLabelSuggestions(labelSuggestions); |
| 92 | + } |
| 93 | + };*/ |
| 94 | + |
| 95 | + |
| 96 | + var watches = []; |
| 97 | + |
| 98 | + ProjectsService |
| 99 | + .get($routeParams.project) |
| 100 | + .then(_.spread(function(project, context) { |
| 101 | + $scope.project = project; |
| 102 | + |
| 103 | + /*watches.push(DataService.watch("services", context, function(services) { |
| 104 | + $scope.unfilteredServices = services.by("metadata.name"); |
| 105 | + LabelFilter.addLabelSuggestionsFromResources($scope.unfilteredServices, $scope.labelSuggestions); |
| 106 | + LabelFilter.setLabelSuggestions($scope.labelSuggestions); |
| 107 | + $scope.services = LabelFilter.getLabelSelector().select($scope.unfilteredServices); |
| 108 | + $scope.emptyMessage = "No services to show"; |
| 109 | + updateFilterWarning(); |
| 110 | +
|
| 111 | + Logger.log("services (subscribe)", $scope.unfilteredServices); |
| 112 | + }));*/ |
| 113 | + |
| 114 | + var canI = $filter('canI'); |
| 115 | + |
| 116 | + $scope.emptyMessage = "No provisioned services to show"; |
| 117 | + |
| 118 | + if (SERVICE_CATALOG_ENABLED && canI({resource: 'instances', group: 'servicecatalog.k8s.io'}, 'watch')) { |
| 119 | + watches.push(DataService.watch({ |
| 120 | + group: 'servicecatalog.k8s.io', |
| 121 | + resource: 'instances' |
| 122 | + }, context, function(serviceInstances) { |
| 123 | + state.serviceInstances = serviceInstances.by('metadata.name'); |
| 124 | + _.each(state.serviceInstances, function(instance) { |
| 125 | + var notifications = ResourceAlertsService.getServiceInstanceAlerts(instance); |
| 126 | + setNotifications(instance, notifications); |
| 127 | + }); |
| 128 | + sortServiceInstances(); |
| 129 | + //updateLabelSuggestions(state.serviceInstances); |
| 130 | + //updateFilter(); |
| 131 | + //$scope.emptyMessage = "No provisioned services to show"; |
| 132 | + updateFilterWarning(); |
| 133 | + |
| 134 | + //Logger.log("provisioned services (subscribe)", $scope.unfilteredServices); |
| 135 | + |
| 136 | + }, {poll: limitWatches, pollInterval: DEFAULT_POLL_INTERVAL})); |
| 137 | + } |
| 138 | + |
| 139 | + function updateFilterWarning() { |
| 140 | + if (!LabelFilter.getLabelSelector().isEmpty() && $.isEmptyObject($scope.services) && !$.isEmptyObject($scope.unfilteredServices)) { |
| 141 | + $scope.alerts["provisioned-services"] = { |
| 142 | + type: "warning", |
| 143 | + details: "The active filters are hiding all services." |
| 144 | + }; |
| 145 | + } |
| 146 | + else { |
| 147 | + delete $scope.alerts["provisioned-services"]; |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + LabelFilter.onActiveFiltersChanged(function(labelSelector) { |
| 152 | + // trigger a digest loop |
| 153 | + $scope.$apply(function() { |
| 154 | + $scope.services = labelSelector.select($scope.unfilteredServices); |
| 155 | + updateFilterWarning(); |
| 156 | + }); |
| 157 | + }); |
| 158 | + |
| 159 | + $scope.$on('$destroy', function(){ |
| 160 | + DataService.unwatchAll(watches); |
| 161 | + }); |
| 162 | + |
| 163 | + })); |
| 164 | + }); |
0 commit comments