diff --git a/magnum_ui/static/dashboard/container-infra/cluster-templates/cluster-templates.module.js b/magnum_ui/static/dashboard/container-infra/cluster-templates/cluster-templates.module.js index 075b52ed..17226ad9 100644 --- a/magnum_ui/static/dashboard/container-infra/cluster-templates/cluster-templates.module.js +++ b/magnum_ui/static/dashboard/container-infra/cluster-templates/cluster-templates.module.js @@ -54,12 +54,12 @@ run.$inject = [ 'horizon.framework.conf.resource-type-registry.service', - 'horizon.app.core.openstack-service-api.magnum', + 'horizon.dashboard.container-infra.cluster-templates.service', 'horizon.dashboard.container-infra.cluster-templates.basePath', 'horizon.dashboard.container-infra.cluster-templates.resourceType' ]; - function run(registry, magnum, basePath, resourceType) { + function run(registry, clusterTemplatesService, basePath, resourceType) { registry.getResourceType(resourceType) .setNames(gettext('Cluster Template'), gettext('Cluster Templates')) @@ -78,14 +78,14 @@ .setProperty('network_driver', { label: gettext('Network Driver') }) - .setListFunction(listFunction) + .setListFunction(clusterTemplatesService.getClusterTemplatesPromise) .tableColumns .append({ id: 'name', priority: 1, sortDefault: true, filters: ['noName'], - urlFunction: urlFunction + urlFunction: clusterTemplatesService.urlFunction }) .append({ id: 'id', @@ -122,23 +122,6 @@ {label: gettext('Mesos'), key: 'mesos'} ] }); - - function listFunction(params) { - return magnum.getClusterTemplates(params).then(modifyResponse); - - function modifyResponse(response) { - return {data: {items: response.data.items.map(addTrackBy)}}; - - function addTrackBy(clusterTemplate) { - clusterTemplate.trackBy = clusterTemplate.id; - return clusterTemplate; - } - } - } - - function urlFunction(item) { - return 'project/ngdetails/OS::Magnum::ClusterTemplate/' + item.id; - } } config.$inject = [ diff --git a/magnum_ui/static/dashboard/container-infra/cluster-templates/cluster-templates.service.js b/magnum_ui/static/dashboard/container-infra/cluster-templates/cluster-templates.service.js new file mode 100644 index 00000000..b270ee65 --- /dev/null +++ b/magnum_ui/static/dashboard/container-infra/cluster-templates/cluster-templates.service.js @@ -0,0 +1,60 @@ +/** + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. You may obtain + * a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ + +(function() { + "use strict"; + + angular.module('horizon.dashboard.container-infra.cluster-templates') + .factory('horizon.dashboard.container-infra.cluster-templates.service', + clusterTemplatesService); + + clusterTemplatesService.$inject = [ + '$filter', + 'horizon.app.core.openstack-service-api.magnum' + ]; + + /* + * @ngdoc factory + * @name horizon.dashboard.container-infra.cluster-templates.service + * + * @description + * This service provides functions that are used through the Cluster Templates + * features. These are primarily used in the module registrations + * but do not need to be restricted to such use. Each exposed function + * is documented below. + */ + function clusterTemplatesService($filter, magnum) { + return { + getClusterTemplatesPromise: getClusterTemplatesPromise, + urlFunction: urlFunction + }; + + function getClusterTemplatesPromise(params) { + return magnum.getClusterTemplates(params).then(modifyResponse); + + function modifyResponse(response) { + return {data: {items: response.data.items.map(addTrackBy)}}; + + function addTrackBy(clusterTemplate) { + clusterTemplate.trackBy = clusterTemplate.id; + return clusterTemplate; + } + } + } + + function urlFunction(item) { + return 'project/ngdetails/OS::Magnum::ClusterTemplate/' + item.id; + } + } +})(); diff --git a/magnum_ui/static/dashboard/container-infra/cluster-templates/cluster-templates.service.spec.js b/magnum_ui/static/dashboard/container-infra/cluster-templates/cluster-templates.service.spec.js new file mode 100644 index 00000000..96a5e1a3 --- /dev/null +++ b/magnum_ui/static/dashboard/container-infra/cluster-templates/cluster-templates.service.spec.js @@ -0,0 +1,51 @@ +/* + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +(function() { + "use strict"; + + describe('cluster templates service', function() { + var service; + beforeEach(module('horizon.dashboard.container-infra.cluster-templates')); + beforeEach(inject(function($injector) { + service = $injector.get('horizon.dashboard.container-infra.cluster-templates.service'); + })); + + describe('getClusterTemplatesPromise', function() { + it("provides a promise", inject(function($q, $injector, $timeout) { + var magnum = $injector.get('horizon.app.core.openstack-service-api.magnum'); + var deferred = $q.defer(); + spyOn(magnum, 'getClusterTemplates').and.returnValue(deferred.promise); + var result = service.getClusterTemplatesPromise({}); + deferred.resolve({ + data:{ + items: [{id: 123, name: 'template1'}] + } + }); + $timeout.flush(); + expect(magnum.getClusterTemplates).toHaveBeenCalled(); + expect(result.$$state.value.data.items[0].name).toBe('template1'); + })); + }); + + describe('urlFunction', function() { + it("get url", inject(function() { + var result = service.urlFunction({id:"123abc"}); + expect(result).toBe("project/ngdetails/OS::Magnum::ClusterTemplate/123abc"); + })); + }); + + }); + +})(); diff --git a/magnum_ui/static/dashboard/container-infra/clusters/clusters.module.js b/magnum_ui/static/dashboard/container-infra/clusters/clusters.module.js index d278997b..2ae8c271 100644 --- a/magnum_ui/static/dashboard/container-infra/clusters/clusters.module.js +++ b/magnum_ui/static/dashboard/container-infra/clusters/clusters.module.js @@ -52,12 +52,12 @@ run.$inject = [ 'horizon.framework.conf.resource-type-registry.service', - 'horizon.app.core.openstack-service-api.magnum', + 'horizon.dashboard.container-infra.clusters.service', 'horizon.dashboard.container-infra.clusters.basePath', 'horizon.dashboard.container-infra.clusters.resourceType' ]; - function run(registry, magnum, basePath, resourceType) { + function run(registry, clustersService, basePath, resourceType) { registry.getResourceType(resourceType) .setNames(gettext('Cluster'), gettext('Clusters')) @@ -79,14 +79,14 @@ .setProperty('node_count', { label: gettext('Node Count') }) - .setListFunction(listFunction) + .setListFunction(clustersService.getClustersPromise) .tableColumns .append({ id: 'name', priority: 1, sortDefault: true, filters: ['noName'], - urlFunction: urlFunction + urlFunction: clustersService.urlFunction }) .append({ id: 'id', @@ -149,23 +149,6 @@ 'name': 'node_count', 'singleton': true }); - - function listFunction(params) { - return magnum.getClusters(params).then(modifyResponse); - - function modifyResponse(response) { - return {data: {items: response.data.items.map(addTrackBy)}}; - - function addTrackBy(cluster) { - cluster.trackBy = cluster.id; - return cluster; - } - } - } - - function urlFunction(item) { - return 'project/ngdetails/OS::Magnum::Cluster/' + item.id; - } } config.$inject = [ diff --git a/magnum_ui/static/dashboard/container-infra/clusters/clusters.service.js b/magnum_ui/static/dashboard/container-infra/clusters/clusters.service.js new file mode 100644 index 00000000..d8475219 --- /dev/null +++ b/magnum_ui/static/dashboard/container-infra/clusters/clusters.service.js @@ -0,0 +1,60 @@ +/** + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. You may obtain + * a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ + +(function() { + "use strict"; + + angular.module('horizon.dashboard.container-infra.clusters') + .factory('horizon.dashboard.container-infra.clusters.service', + clustersService); + + clustersService.$inject = [ + '$filter', + 'horizon.app.core.openstack-service-api.magnum' + ]; + + /* + * @ngdoc factory + * @name horizon.dashboard.container-infra.clusters.service + * + * @description + * This service provides functions that are used through the Clusters + * features. These are primarily used in the module registrations + * but do not need to be restricted to such use. Each exposed function + * is documented below. + */ + function clustersService($filter, magnum) { + return { + getClustersPromise: getClustersPromise, + urlFunction: urlFunction + }; + + function getClustersPromise(params) { + return magnum.getClusters(params).then(modifyResponse); + + function modifyResponse(response) { + return {data: {items: response.data.items.map(addTrackBy)}}; + + function addTrackBy(cluster) { + cluster.trackBy = cluster.id; + return cluster; + } + } + } + + function urlFunction(item) { + return 'project/ngdetails/OS::Magnum::Cluster/' + item.id; + } + } +})(); diff --git a/magnum_ui/static/dashboard/container-infra/clusters/clusters.service.spec.js b/magnum_ui/static/dashboard/container-infra/clusters/clusters.service.spec.js new file mode 100644 index 00000000..f8e9871a --- /dev/null +++ b/magnum_ui/static/dashboard/container-infra/clusters/clusters.service.spec.js @@ -0,0 +1,51 @@ +/* + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +(function() { + "use strict"; + + describe('cluster templates service', function() { + var service; + beforeEach(module('horizon.dashboard.container-infra.cluster-templates')); + beforeEach(inject(function($injector) { + service = $injector.get('horizon.dashboard.container-infra.clusters.service'); + })); + + describe('getClustersPromise', function() { + it("provides a promise", inject(function($q, $injector, $timeout) { + var magnum = $injector.get('horizon.app.core.openstack-service-api.magnum'); + var deferred = $q.defer(); + spyOn(magnum, 'getClusters').and.returnValue(deferred.promise); + var result = service.getClustersPromise({}); + deferred.resolve({ + data:{ + items: [{id: 123, name: 'cluster1'}] + } + }); + $timeout.flush(); + expect(magnum.getClusters).toHaveBeenCalled(); + expect(result.$$state.value.data.items[0].name).toBe('cluster1'); + })); + }); + + describe('urlFunction', function() { + it("get url", inject(function() { + var result = service.urlFunction({id:"123abc"}); + expect(result).toBe("project/ngdetails/OS::Magnum::Cluster/123abc"); + })); + }); + + }); + +})();