Add option to specify surveil api url

Change-Id: Ia3abf43f1dfc0b076520437741b00837b8249d1e
This commit is contained in:
Vincent Fournier 2015-07-29 12:52:51 -04:00
parent c7c793b699
commit 6d40b87c7d
5 changed files with 130 additions and 101 deletions

View File

@ -31,12 +31,9 @@ angular.module('bansho.authentication', [])
login($scope.credentials); login($scope.credentials);
}; };
if (authService.isAuthenticated()) {
login($scope.credentials);
}
configManager.loadDevelopmentConfig().then(function () { configManager.loadDevelopmentConfig().then(function () {
var devConfig = configManager.getDevelopmentConfig(); var devConfig = configManager.getDevelopmentConfig();
if (devConfig.env === 'development') { if (devConfig.env === 'development') {
login({ login({
'auth': { 'auth': {
@ -47,6 +44,8 @@ angular.module('bansho.authentication', [])
} }
} }
}); });
} else if (authService.isAuthenticated()) {
login($scope.credentials);
} }
}, function () { }, function () {
@ -55,13 +54,13 @@ angular.module('bansho.authentication', [])
}]) }])
.factory('authService', [ '$http', '$location', '$rootScope', 'session', 'configManager', 'themeManager', .factory('authService', [ '$http', '$location', '$rootScope', 'session', 'configManager', 'themeManager', 'surveilConfig',
function ($http, $location, $rootScope, session, configManager, themeManager) { function ($http, $location, $rootScope, session, configManager, themeManager, surveilConfig) {
var authService = {}; var authService = {};
authService.login = function (credentials) { authService.login = function (credentials) {
return $http return $http
.post('surveil/v2/auth/tokens/', credentials) .post(surveilConfig.getAuthUrl() + '/tokens/', credentials)
.success(function (data) { .success(function (data) {
$rootScope.isAuthenticated = true; $rootScope.isAuthenticated = true;

View File

@ -87,103 +87,106 @@ angular.module('bansho.config', [])
}; };
}]) }])
.service('configManager', ['$http', '$q', 'componentsConfig', function ($http, $q, componentsConfig) { .service('configManager', ['$http', '$q', 'componentsConfig', 'surveilConfig',
var config = {}, function ($http, $q, componentsConfig, surveilConfig) {
developmentConfig = {}; var config = {},
developmentConfig = {};
this.loadDevelopmentConfig = function() { this.loadDevelopmentConfig = function() {
var promise = $q.defer(); var promise = $q.defer();
$http.get('components/config/developmentConfig.json') $http.get('components/config/developmentConfig.json')
.success(function (config) { .success(function (config) {
developmentConfig = config; developmentConfig = config;
promise.resolve(); surveilConfig.setSurveilApiUrl(config.surveilApiUrl);
}) surveilConfig.setAuthUrl(config.surveilAuthUrl);
.error(function() { promise.resolve();
promise.reject(); })
}); .error(function() {
promise.reject();
});
return promise.promise; return promise.promise;
}; };
this.getDevelopmentConfig = function () { this.getDevelopmentConfig = function () {
return developmentConfig; return developmentConfig;
}; };
this.getConfigData = function (templateName) { this.getConfigData = function (templateName) {
return config.data[templateName]; return config.data[templateName];
}; };
this.readConfig = function () { this.readConfig = function () {
return config.data; return config.data;
}; };
this.saveConfig = function(configuration) { this.saveConfig = function(configuration) {
config.data = configuration; config.data = configuration;
saveConfig(); saveConfig();
}; };
this.setThemeAndSave = function (theme) { this.setThemeAndSave = function (theme) {
config.data.banshoConfig.theme = theme; config.data.banshoConfig.theme = theme;
saveConfig(); saveConfig();
}; };
this.getTheme = function () { this.getTheme = function () {
var theme; var theme;
if (config.data) { if (config.data) {
theme = config.data.banshoConfig.theme; theme = config.data.banshoConfig.theme;
} }
return theme; return theme;
}; };
var saveConfig = function () { var saveConfig = function () {
var responsePromise = $q.defer(); var responsePromise = $q.defer();
$http.post('surveil/v2/bansho/config', JSON.stringify(config.data)) $http.post(surveilConfig.endpoint('config'), JSON.stringify(config.data))
.success(function () { .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;
responsePromise.resolve(); responsePromise.resolve();
} })
}) .error(function () {
.error(function () { responsePromise.reject('Failed to send config to server');
responsePromise.reject('Failed to fetch config'); });
});
return responsePromise.promise; 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;
};
}]);

View File

@ -2,5 +2,7 @@
"env": "production", "env": "production",
"username":"", "username":"",
"password":"", "password":"",
"useStoredConfig": true "useStoredConfig": true,
"surveilApiUrl": "surveil/v2",
"surveilAuthUrl": "surveil/v2/auth"
} }

View File

@ -3,10 +3,10 @@
'use strict'; 'use strict';
angular.module('bansho.surveil') angular.module('bansho.surveil')
.service('surveilStatus', ['$http', '$q', 'surveilQuery', 'componentsConfig', .service('surveilStatus', ['$http', '$q', 'surveilQuery', 'componentsConfig', 'surveilConfig',
function ($http, $q, surveilQuery, componentsConfig) { function ($http, $q, surveilQuery, componentsConfig, surveilConfig) {
var getMetric = function (host, service, metric) { var getMetric = function (host, service, metric) {
var url = 'surveil/v2/status/hosts/' + host, var url = surveilConfig.endpoint('status') + '/hosts/' + host,
responsePromise = $q.defer(); responsePromise = $q.defer();
if (service !== undefined) { if (service !== undefined) {
@ -25,7 +25,7 @@ angular.module('bansho.surveil')
}; };
var getMetricNames = function (host, service) { var getMetricNames = function (host, service) {
var url = '/surveil/v2/status/hosts/' + host, var url = surveilConfig.endpoint('status') + '/hosts/' + host,
responsePromise = $q.defer(); responsePromise = $q.defer();
if (service !== undefined) { if (service !== undefined) {
@ -44,7 +44,7 @@ angular.module('bansho.surveil')
responsePromise.resolve(result); responsePromise.resolve(result);
}).error(function () { }).error(function () {
throw new Error('getMetricNames: GET Request failed'); throw new Error('getMetricNames: GET Request failed');
}); });
return responsePromise.promise; return responsePromise.promise;
@ -78,9 +78,9 @@ angular.module('bansho.surveil')
var hostQuery = surveilQuery(fields, filters.hosts), var hostQuery = surveilQuery(fields, filters.hosts),
serviceQuery = surveilQuery(fields, filters.services); serviceQuery = surveilQuery(fields, filters.services);
executeQuery('surveil/v2/status/hosts/', 'POST', hostQuery) executeQuery(surveilConfig.endpoint('status') + '/hosts/', 'POST', hostQuery)
.success(function (hosts) { .success(function (hosts) {
executeQuery('surveil/v2/status/services/', 'POST', serviceQuery) executeQuery(surveilConfig.endpoint('status') + '/services/', 'POST', serviceQuery)
.success(function (services) { .success(function (services) {
callback(hosts, services); callback(hosts, services);
}); });
@ -129,7 +129,7 @@ angular.module('bansho.surveil')
"hosts": function (fields, filters, callback) { "hosts": function (fields, filters, callback) {
var hostQuery = surveilQuery(fields, filters.hosts), var hostQuery = surveilQuery(fields, filters.hosts),
method = 'POST', method = 'POST',
hostUrl = 'surveil/v2/status/hosts/'; hostUrl = surveilConfig.endpoint('status') + '/hosts/';
if (filters.hosts && filters.hosts.is && filters.hosts.is.host_name) { if (filters.hosts && filters.hosts.is && filters.hosts.is.host_name) {
hostUrl += filters.hosts.is.host_name; hostUrl += filters.hosts.is.host_name;
@ -151,7 +151,7 @@ angular.module('bansho.surveil')
"events": function (fields, filters, callback) { "events": function (fields, filters, callback) {
var query = surveilQuery(fields, filters.events); var query = surveilQuery(fields, filters.events);
executeQuery('surveil/v2/status/events/', 'POST', query) executeQuery(surveilConfig.endpoint('status') + '/events/', 'POST', query)
.success(function (events) { .success(function (events) {
angular.forEach(events, function (event) { angular.forEach(events, function (event) {
angular.forEach(event, function (value, attr) { angular.forEach(event, function (value, attr) {

View File

@ -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;
}
};
});