diff --git a/app/components/authentication/authentication.js b/app/components/authentication/authentication.js index d3316e8..4472a90 100644 --- a/app/components/authentication/authentication.js +++ b/app/components/authentication/authentication.js @@ -31,12 +31,9 @@ angular.module('bansho.authentication', []) login($scope.credentials); }; - if (authService.isAuthenticated()) { - login($scope.credentials); - } - configManager.loadDevelopmentConfig().then(function () { var devConfig = configManager.getDevelopmentConfig(); + if (devConfig.env === 'development') { login({ 'auth': { @@ -47,6 +44,8 @@ angular.module('bansho.authentication', []) } } }); + } else if (authService.isAuthenticated()) { + login($scope.credentials); } }, function () { @@ -55,13 +54,13 @@ angular.module('bansho.authentication', []) }]) - .factory('authService', [ '$http', '$location', '$rootScope', 'session', 'configManager', 'themeManager', - function ($http, $location, $rootScope, session, configManager, themeManager) { + .factory('authService', [ '$http', '$location', '$rootScope', 'session', 'configManager', 'themeManager', 'surveilConfig', + function ($http, $location, $rootScope, session, configManager, themeManager, surveilConfig) { var authService = {}; authService.login = function (credentials) { return $http - .post('surveil/v2/auth/tokens/', credentials) + .post(surveilConfig.getAuthUrl() + '/tokens/', credentials) .success(function (data) { $rootScope.isAuthenticated = true; diff --git a/app/components/config/config.js b/app/components/config/config.js index 8ac4472..c9d480d 100644 --- a/app/components/config/config.js +++ b/app/components/config/config.js @@ -87,103 +87,106 @@ angular.module('bansho.config', []) }; }]) - .service('configManager', ['$http', '$q', 'componentsConfig', function ($http, $q, componentsConfig) { - var config = {}, - developmentConfig = {}; + .service('configManager', ['$http', '$q', 'componentsConfig', 'surveilConfig', + function ($http, $q, componentsConfig, surveilConfig) { + var config = {}, + developmentConfig = {}; - this.loadDevelopmentConfig = function() { - var promise = $q.defer(); + this.loadDevelopmentConfig = function() { + var promise = $q.defer(); - $http.get('components/config/developmentConfig.json') - .success(function (config) { - developmentConfig = config; - promise.resolve(); - }) - .error(function() { - promise.reject(); - }); + $http.get('components/config/developmentConfig.json') + .success(function (config) { + developmentConfig = config; + surveilConfig.setSurveilApiUrl(config.surveilApiUrl); + surveilConfig.setAuthUrl(config.surveilAuthUrl); + promise.resolve(); + }) + .error(function() { + promise.reject(); + }); - return promise.promise; - }; + return promise.promise; + }; - this.getDevelopmentConfig = function () { - return developmentConfig; - }; + this.getDevelopmentConfig = function () { + return developmentConfig; + }; - this.getConfigData = function (templateName) { - return config.data[templateName]; - }; + this.getConfigData = function (templateName) { + return config.data[templateName]; + }; - this.readConfig = function () { - return config.data; - }; + this.readConfig = function () { + return config.data; + }; - this.saveConfig = function(configuration) { - config.data = configuration; - saveConfig(); - }; + this.saveConfig = function(configuration) { + config.data = configuration; + saveConfig(); + }; - this.setThemeAndSave = function (theme) { - config.data.banshoConfig.theme = theme; - saveConfig(); - }; + this.setThemeAndSave = function (theme) { + config.data.banshoConfig.theme = theme; + saveConfig(); + }; - this.getTheme = function () { - var theme; + this.getTheme = function () { + var theme; - if (config.data) { - theme = config.data.banshoConfig.theme; - } + if (config.data) { + theme = config.data.banshoConfig.theme; + } - return theme; - }; + return theme; + }; - var saveConfig = function () { - var responsePromise = $q.defer(); + var saveConfig = function () { + var responsePromise = $q.defer(); - $http.post('surveil/v2/bansho/config', JSON.stringify(config.data)) - .success(function () { - responsePromise.resolve(); - }) - .error(function () { - responsePromise.reject('Failed to send config to server'); - }); - - return responsePromise.promise; - }; - - this.fetchConfig = function (useStoredConfig) { - var responsePromise = $q.defer(); - - componentsConfig.load(); - - $http.get('surveil/v2/bansho/config') - .success(function (conf) { - if (!useStoredConfig || jQuery.isEmptyObject(conf)) { - $http.get('components/config/config.json') - .success(function (conf) { - config.data = conf; - - $http.post('surveil/v2/bansho/config', JSON.stringify(conf)) - .success(function () { - responsePromise.resolve(); - }) - .error(function () { - responsePromise.reject('Failed to send config to server'); - }); - }) - .error(function () { - responsePromise.reject('Failed to fetch default config'); - }); - } else { - config.data = conf; + $http.post(surveilConfig.endpoint('config'), JSON.stringify(config.data)) + .success(function () { responsePromise.resolve(); - } - }) - .error(function () { - responsePromise.reject('Failed to fetch config'); - }); + }) + .error(function () { + responsePromise.reject('Failed to send config to server'); + }); return responsePromise.promise; }; - }]); + + this.fetchConfig = function (useStoredConfig) { + var responsePromise = $q.defer(); + + componentsConfig.load(); + + $http.get(surveilConfig.endpoint('config')) + .success(function (conf) { + if (!useStoredConfig || jQuery.isEmptyObject(conf)) { + $http.get('components/config/config.json') + .success(function (conf) { + config.data = conf; + + $http.post(surveilConfig.endpoint('config'), JSON.stringify(conf)) + .success(function () { + responsePromise.resolve(); + }) + .error(function () { + responsePromise.reject('Failed to send config to server'); + }); + }) + .error(function () { + responsePromise.reject('Failed to fetch default config'); + }); + } else { + config.data = conf; + responsePromise.resolve(); + } + }) + .error(function () { + responsePromise.reject('Failed to fetch config'); + }); + + return responsePromise.promise; + }; + }]); diff --git a/app/components/config/developmentConfig.json b/app/components/config/developmentConfig.json index bf1a652..242e452 100644 --- a/app/components/config/developmentConfig.json +++ b/app/components/config/developmentConfig.json @@ -2,5 +2,7 @@ "env": "production", "username":"", "password":"", - "useStoredConfig": true + "useStoredConfig": true, + "surveilApiUrl": "surveil/v2", + "surveilAuthUrl": "surveil/v2/auth" } diff --git a/app/components/surveil/status.js b/app/components/surveil/status.js index 9418d07..316b2f5 100644 --- a/app/components/surveil/status.js +++ b/app/components/surveil/status.js @@ -3,10 +3,10 @@ 'use strict'; angular.module('bansho.surveil') - .service('surveilStatus', ['$http', '$q', 'surveilQuery', 'componentsConfig', - function ($http, $q, surveilQuery, componentsConfig) { + .service('surveilStatus', ['$http', '$q', 'surveilQuery', 'componentsConfig', 'surveilConfig', + function ($http, $q, surveilQuery, componentsConfig, surveilConfig) { var getMetric = function (host, service, metric) { - var url = 'surveil/v2/status/hosts/' + host, + var url = surveilConfig.endpoint('status') + '/hosts/' + host, responsePromise = $q.defer(); if (service !== undefined) { @@ -25,7 +25,7 @@ angular.module('bansho.surveil') }; var getMetricNames = function (host, service) { - var url = '/surveil/v2/status/hosts/' + host, + var url = surveilConfig.endpoint('status') + '/hosts/' + host, responsePromise = $q.defer(); if (service !== undefined) { @@ -44,7 +44,7 @@ angular.module('bansho.surveil') responsePromise.resolve(result); }).error(function () { - throw new Error('getMetricNames: GET Request failed'); + throw new Error('getMetricNames: GET Request failed'); }); return responsePromise.promise; @@ -78,9 +78,9 @@ angular.module('bansho.surveil') var hostQuery = surveilQuery(fields, filters.hosts), serviceQuery = surveilQuery(fields, filters.services); - executeQuery('surveil/v2/status/hosts/', 'POST', hostQuery) + executeQuery(surveilConfig.endpoint('status') + '/hosts/', 'POST', hostQuery) .success(function (hosts) { - executeQuery('surveil/v2/status/services/', 'POST', serviceQuery) + executeQuery(surveilConfig.endpoint('status') + '/services/', 'POST', serviceQuery) .success(function (services) { callback(hosts, services); }); @@ -129,7 +129,7 @@ angular.module('bansho.surveil') "hosts": function (fields, filters, callback) { var hostQuery = surveilQuery(fields, filters.hosts), method = 'POST', - hostUrl = 'surveil/v2/status/hosts/'; + hostUrl = surveilConfig.endpoint('status') + '/hosts/'; if (filters.hosts && filters.hosts.is && filters.hosts.is.host_name) { hostUrl += filters.hosts.is.host_name; @@ -151,7 +151,7 @@ angular.module('bansho.surveil') "events": function (fields, filters, callback) { var query = surveilQuery(fields, filters.events); - executeQuery('surveil/v2/status/events/', 'POST', query) + executeQuery(surveilConfig.endpoint('status') + '/events/', 'POST', query) .success(function (events) { angular.forEach(events, function (event) { angular.forEach(event, function (value, attr) { diff --git a/app/components/surveil/surveil.js b/app/components/surveil/surveil.js index 20c99bf..b8cd106 100644 --- a/app/components/surveil/surveil.js +++ b/app/components/surveil/surveil.js @@ -1 +1,26 @@ -angular.module('bansho.surveil', []); +angular.module('bansho.surveil', []) + .service('surveilConfig', function () { + var apiUrl, + authUrl, + surveilEndpoints = {}; + + return { + setSurveilApiUrl: function (surveilApiUrl) { + apiUrl = surveilApiUrl; + surveilEndpoints = { + status: apiUrl + '/status', + actions: apiUrl + '/actions', + config: apiUrl + '/bansho/config' + }; + }, + setAuthUrl: function (url) { + authUrl = url; + }, + endpoint: function (endpoint) { + return surveilEndpoints[endpoint]; + }, + getAuthUrl: function () { + return authUrl; + } + }; + });