Commit 53b6059f authored by Patricio Bruna's avatar Patricio Bruna

Muchos cambios. De ahora en adelante mas prolijo los commits

parent 33cabdb0
This diff is collapsed.
This diff is collapsed.
...@@ -10,7 +10,7 @@ var http = require("http"), ...@@ -10,7 +10,7 @@ var http = require("http"),
http.createServer(function(request, response) { http.createServer(function(request, response) {
console.log(request); console.log(request.url);
if (request.url.indexOf(prefix) === 0) { if (request.url.indexOf(prefix) === 0) {
proxy.web(request, response, { target: 'https://127.0.0.1:7071', secure: false }); proxy.web(request, response, { target: 'https://127.0.0.1:7071', secure: false });
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
// See LICENSE.txt for license information. // See LICENSE.txt for license information.
var jszimbra = require('js-zimbra'); var jszimbra = require('js-zimbra');
import Dictionary from './utils/dictionary.js';
class Error { class Error {
constructor(err) { constructor(err) {
...@@ -30,6 +31,8 @@ class ZimbraAdminApi { ...@@ -30,6 +31,8 @@ class ZimbraAdminApi {
this.password = auth_object.password; this.password = auth_object.password;
this._client = new jszimbra.Communication({url: auth_object.url}); this._client = new jszimbra.Communication({url: auth_object.url});
this.default_params = { namespace: 'zimbraAdmin', params: { } }; this.default_params = { namespace: 'zimbraAdmin', params: { } };
this.parseAllResponse = this.parseAllResponse.bind(this);
this.dictionary = new Dictionary();
} }
...@@ -78,7 +81,7 @@ class ZimbraAdminApi { ...@@ -78,7 +81,7 @@ class ZimbraAdminApi {
return request; return request;
} }
makeRequest(request, resource, parse_response, success, error) { makeRequest(request, resource, parse_response, callback) {
let that = this; let that = this;
this.default_params.name = `${request}Request`; this.default_params.name = `${request}Request`;
let request_object = that.buildRequest(); let request_object = that.buildRequest();
...@@ -87,11 +90,12 @@ class ZimbraAdminApi { ...@@ -87,11 +90,12 @@ class ZimbraAdminApi {
return that.handleError(err); return that.handleError(err);
} }
let obj = new Object(self); let obj = new Object(self);
obj.success = success;
obj.error = error;
obj.response_name = `${request}Response`; obj.response_name = `${request}Response`;
obj.param_object_name = resource.toLocaleLowerCase(); obj.param_object_name = resource.toLocaleLowerCase();
that.client.send(request_object, parse_response.bind(obj)); that.client.send(request_object, function(err, data){
if (err) return callback(err);
parse_response(data, obj, callback);
});
}); });
} }
...@@ -104,33 +108,43 @@ class ZimbraAdminApi { ...@@ -104,33 +108,43 @@ class ZimbraAdminApi {
return this.success(response_object); return this.success(response_object);
} }
parseAllResponse(err, data){
if (err) { parseAllResponse(data, obj, callback){
return this.error(err); const resource = obj.param_object_name.toLowerCase();
} const that = this;
let response_object = data.get()[this.response_name][this.param_object_name]; const response_name = that.dictionary.resourceResponseName(resource)
return this.success(response_object); const response_object = data.get()[obj.response_name][response_name];
const response_array = [];
response_object.forEach((r) => {
let element = that.dictionary.classFactory(resource, r);
response_array.push(element);
});
return callback(null, response_array);
} }
getAll(resource, success, error) { getAll(resource, callback) {
if (this.client.token) { if (this.client.token) {
this.makeRequest(`GetAll${resource}s`, resource, this.parseAllResponse, success, error); this.makeRequest(`GetAll${resource}s`, resource, this.parseAllResponse, callback);
} else { } else {
var that = this; var that = this;
let callback = function(err, response){ let getAllCallback = function(err, response){
if (err) return this.handleError(err); if (err) return this.handleError(err);
that.makeRequest(`GetAll${resource}s`, resource, that.parseAllResponse, success, error); that.makeRequest(`GetAll${resource}s`, resource, that.parseAllResponse, callback);
} }
this.login(callback); this.login(getAllCallback);
} }
} }
getAllDomains(success, error) { getAllDomains(callback) {
this.getAll('Domain', success, error); this.getAll('Domain', callback);
}
getAllAccounts(callback) {
this.getAll('Account', callback);
} }
getAllAccounts(success, error) { getAllDistributionLists(callback) {
this.getAll('Account', success, error); this.getAll('DistributionList', callback);
} }
} }
......
// Copyright (c) 2016 ZBox, Spa. All Rights Reserved.
// See LICENSE.txt for license information.
import $ from 'jquery';
import jszimbra from 'js-zimbra';
import Domain from '../zimbra/domain.jsx';
import Account from '../zimbra/account.jsx';
let domain;
let account;
// función que maneja el error como corresponde
function handleError(methodName, err) {
var e = null;
try {
e = JSON.parse(err);
} catch (parseError) {
e = null;
}
console.error(methodName, e); //eslint-disable-line no-console
// Aqui deberiamos revisar si falta hacer login nuevamente
return e;
}
function error(err) {
console.error(err);
}
function success(data) {
console.log("SUCCESS");
return data;
}
export function getClientConfig(success, error) {
return $.ajax({
url: 'config/config.json',
dataType: 'json',
success,
error: function onError(xhr, status, err) {
var e = handleError('getClientConfig', err);
error(e);
}
});
}
export function login(username, secret, success, error) {
const zimbra = new jszimbra.Communication({
url: global.window.manager_config.zimbraUrl
});
zimbra.auth({
username,
secret,
isPassword: true,
isAdmin: true
}, (err) => {
if (err) {
var e = handleError('login', err);
return error(e);
}
// aqui deberiamos inicializar todas las apis
domain = new Domain(zimbra);
account = new Account(zimbra);
return success();
});
}
export function getDomain(name, by, attrs, success, error) {
console.log(domain);
if (domain) {
return domain.get(name, by, attrs,
(data) => {
// aqui formateamos la data que viene en "data"
const nuevoObj = {
dominio: 'el nombre del dominio'
}
return success(nuevoObj);
},
(err) => {
var e = handleError('getDomain', err);
error(e);
});
}
// probablemente esto lo que deba hacer es forzar un login
return error({message: 'Domain not initialized'});
}
export function getAllDomains(success, error) {
if (domain) {
return domain.getAll(
(data) => {
return success(data);
},
(err) => {
var e = handleError('getDomain', err);
error(e);
});
}
// probablemente esto lo que deba hacer es forzar un login
return error({message: 'Domain not initialized'});
}
export function getAllAccounts(success, error) {
if (account) {
return account.getAll(
(data) => {
return success(data);
},
(err) => {
var e = handleError('getDomain', err);
error(e);
});
}
// probablemente esto lo que deba hacer es forzar un login
return error({message: 'Domain not initialized'});
}
// Copyright (c) 2016 ZBox, Spa. All Rights Reserved.
// See License.txt for license information.
import keyMirror from 'keymirror';
export default {
ActionTypes: keyMirror({
RECEIVED_ERROR: null
}),
PayloadSources: keyMirror({
SERVER_ACTION: null,
VIEW_ACTION: null
}),
RESERVED_TEAM_NAMES: [
'www',
'web',
'admin',
'support',
'notify',
'test',
'demo',
'mail',
'team',
'channel',
'internal',
'localhost',
'dockerhost',
'stag',
'post',
'cluster',
'api'
],
RESERVED_USERNAMES: [
'admin',
'root'
],
MONTHS: ['Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio', 'Juio', 'Agosto', 'Septiembre', 'Octubre', 'Noviembre', 'Diciembre'],
KeyCodes: {
UP: 38,
DOWN: 40,
LEFT: 37,
RIGHT: 39,
BACKSPACE: 8,
ENTER: 13,
ESCAPE: 27,
SPACE: 32,
TAB: 9
}
};
// Copyright (c) 2016 ZBox, Spa. All Rights Reserved.
// See LICENSE.txt for license information.
import Domain from './../zimbra/domain.js';
import Account from './../zimbra/account.js';
import DistributionList from './../zimbra/distribution_list.js';
export default class Dictionary {
constructor() {
this.zimbra_resources = this.ZimbraResources();
}
resourceToClass (resource) {
return this.zimbra_resources[resource].class_name;
}
classFactory (resource, object) {
const class_name = this.resourceToClass(resource);
return new class_name(object);
}
resourceResponseName (resource) {
return this.zimbra_resources[resource].response_name;
}
ZimbraResources() {
return {
domain: {
class_name: Domain,
response_name: 'domain'
},
account: {
class_name: Account,
response_name: 'account'
},
distributionlist: {
class_name: DistributionList,
response_name: 'dl'
}
};
}
}
// Copyright (c) 2016 ZBox, Spa. All Rights Reserved. // Copyright (c) 2016 ZBox, Spa. All Rights Reserved.
// See LICENSE.txt for license information. // See LICENSE.txt for license information.
import ZimbraAdmin from './zimbra_admin.jsx'; import Zimbra from './zimbra.js';
export default class Domain extends ZimbraAdmin { export default class Account extends Zimbra {
constructor(connection) { constructor(account_obj) {
super(connection, 'Domain'); super(account_obj);
} }
} }
// Copyright (c) 2016 ZBox, Spa. All Rights Reserved.
// See LICENSE.txt for license information.
import ZimbraAdmin from './zimbra_admin.jsx';
export default class Account extends ZimbraAdmin {
constructor(connection, name, id, attrs) {
super(connection, 'Account');
this.name = name;
this.id = id;
this.attrs = attrs;
}
}
// Copyright (c) 2016 ZBox, Spa. All Rights Reserved.
// See LICENSE.txt for license information.
export default class DistributionList {
constructor(dl_obj) {
this.name = dl_obj.name;
this.id = dl_obj.id;
this.attrs = dl_obj.a;
}
}
// Copyright (c) 2016 ZBox, Spa. All Rights Reserved.
// See LICENSE.txt for license information.
import ZimbraAdmin from './zimbra_admin.jsx';
export default class DistributionList extends ZimbraAdmin {
constructor(connection, name, id, attrs) {
super(connection, 'DistributionList');
this.param_object_name = 'dl';
this.name = name;
this.id = id;
this.attrs = attrs;
}
}
// Copyright (c) 2016 ZBox, Spa. All Rights Reserved.
// See LICENSE.txt for license information.
export default class Domain {
constructor(domain_obj) {
this.name = domain_obj.name;
this.id = domain_obj.id;
this.attrs = domain_obj.a;
}
}
// Copyright (c) 2016 ZBox, Spa. All Rights Reserved.
// See LICENSE.txt for license information.
export default class Zimbra {
constructor(resource_obj) {
this.name = resource_obj.name;
this.id = resource_obj.id;
this.attrs = resource_obj.a;
}
}
// Copyright (c) 2016 ZBox, Spa. All Rights Reserved.
// See LICENSE.txt for license information.
export default class zimbraAdmin {
constructor(connection, object_name) {
this.zimbra = connection;
this.object_name = object_name;
this.param_object_name = object_name.toLocaleLowerCase();
this.request = this.buildRequest();
this.params = this.buildParams();
}
return_error(err){
if (err) return error(err);
}
buildParams() {
return { namespace: 'zimbraAdmin', params: { [this.param_object_name]: {} } };
}
buildRequest() {
request = null;
this.zimbra.getRequest({}, (err, req) => {
if (err) return error(err);
request = req;
});
return request;
}
makeRequest(request_name, parse_response, success, error) {
const self = this;
this.params.name = `${request_name}Request`;
this.response_name = `${request_name}Response`;
this.request.addRequest(this.params, function(err){
if (err) {
return error(err);
}
const obj = Object.create(self);
obj.success = success;
obj.error = error;
self.zimbra.send(self.request, parse_response.bind(obj));
});
}
parseResponse(err, data) {
if (err) {
return this.error(err);
}
let response_object = data.get()[this.response_name][this.param_object_name];
return this.success(response_object);
}
parseAllResponse(err, data){
if (err) {
return this.error(err);
}
let response_object = data.get()[this.response_name][this.param_object_name];
return this.success(response_object);
}
attributesHandler() {
return {
get(target, key) {
if (target[key]) return target[key];
if (target.a[key]) return target.a[key];
return null;
}
};
}
requestParams() {
return this.params.params[this.param_object_name];
}
getAll(success, error) {
this.makeRequest(`GetAll${this.object_name}s`, this.parseAllResponse, success, error);
}
get(name, by, attrs, success, error) {
var params = this.requestParams();
params.attrs = attrs;
params.by = by;
params._content = name;
this.makeRequest(`Get${this.object_name}`, this.parseResponse, success, error);
}
}
This diff is collapsed.
...@@ -30,7 +30,7 @@ ...@@ -30,7 +30,7 @@
'url': 'http://localhost', 'url': 'http://localhost',
'user': 'user', 'user': 'user',
'password':'pass'}); 'password':'pass'});
api.client.options.timeout = 1000; api.client.options.timeout = 10000;
api.login(null, function(err){ api.login(null, function(err){
let error = api.handleError(err); let error = api.handleError(err);
expect(error.constructor.name).to.equal('Error'); expect(error.constructor.name).to.equal('Error');
...@@ -61,24 +61,51 @@ ...@@ -61,24 +61,51 @@
it('should get all domains', function() { it('should get all domains', function() {
var api = new ZimbraAdminApi(auth_data); var api = new ZimbraAdminApi(auth_data);
var success = function(d){ api.getAllDomains(function(err, data){
d.forEach(function(v){ if (err) console.log(err);
console.log(v.name); expect(data[0].constructor.name).to.equal('Domain');
}) });
};
var error = function(d){console.log(d);};
// sucess, err (Why)?
api.getAllAccounts(function(data, err){
if (err) return console.log(err);
data.forEach(function(v){
console.log(v.id + ' ' + v.name);
})
}); });
it('should get all accounts', function() {
var api = new ZimbraAdminApi(auth_data);
api.getAllAccounts(function(err, data){
if (err) console.log(err);
expect(data[0].constructor.name).to.equal('Account');
});
}); });
it('should get all distribution_lists', function() {
var api = new ZimbraAdminApi(auth_data);
api.getAllDistributionLists(function(err, data){
if (err) console.log(err);
expect(data[0].constructor.name).to.equal('DistributionList');
});
});
});
})();
describe('Using callbacks', function() { // it('should get all domains', function() {
// var api = new ZimbraAdminApi(auth_data);
// var success = function(d){
// d.forEach(function(v){
// console.log(v.name);
// })
// };
// var error = function(d){console.log(d);};
// // sucess, err (Why)?
// api.getAllAccounts(function(data, err){
// if (err) return console.log(err);
// data.forEach(function(v){
// console.log(v.id + ' ' + v.name);
// })
// });
//
// });
//
//
// describe('Using callbacks', function() {
// it('should get a track', function() { // it('should get a track', function() {
// var callback = sinon.spy(); // var callback = sinon.spy();
...@@ -131,6 +158,3 @@ ...@@ -131,6 +158,3 @@
// expect(that.requests).to.have.length(1); // expect(that.requests).to.have.length(1);
// expect(that.requests[0].url).to.equal('https://api.spotify.com/v1/albums/0sNOF9WDwhWunNAHPD3Baj/tracks'); // expect(that.requests[0].url).to.equal('https://api.spotify.com/v1/albums/0sNOF9WDwhWunNAHPD3Baj/tracks');
// }); // });
});
});
})();
...@@ -28,11 +28,11 @@ var config = { ...@@ -28,11 +28,11 @@ var config = {
module: { module: {
loaders: [ loaders: [
{ {
test: /\.jsx?$/, test: /\.(js|jsx)?$/,
loader: 'babel', loader: 'babel',
exclude: /(node_modules)/, exclude: /(node_modules)/,
query: { query: {
presets: ['es2015'],
plugins: ['transform-runtime'], plugins: ['transform-runtime'],
cacheDirectory: DEV cacheDirectory: DEV
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment