2015-08-14 20:03:53 +08:00
|
|
|
-- Requires configuration parameter. Example: SELECT cartodb.CDB_Conf_SetConf('groups_api', '{ "host": "127.0.0.1", "port": 3000, "timeout": 10, "username": "superadmin", "password": "monkey" }');
|
|
|
|
|
2015-08-14 19:55:52 +08:00
|
|
|
-- Sends the create group request
|
|
|
|
CREATE OR REPLACE
|
|
|
|
FUNCTION cartodb._CDB_Group_CreateGroup_API(database_name text, group_name text, group_role text)
|
|
|
|
RETURNS VOID AS
|
|
|
|
$$
|
|
|
|
import httplib
|
|
|
|
import string
|
|
|
|
|
|
|
|
try:
|
2015-08-17 18:26:38 +08:00
|
|
|
params = plpy.execute("select c.host, c.port, c.timeout, c.auth from cartodb._CDB_Group_API_Conf() c;")[0]
|
2015-08-17 17:49:31 +08:00
|
|
|
if params['host'] is None:
|
2015-08-14 19:55:52 +08:00
|
|
|
return
|
2015-08-17 17:49:31 +08:00
|
|
|
|
2015-08-14 19:55:52 +08:00
|
|
|
client = httplib.HTTPConnection(params['host'], params['port'], False, params['timeout'])
|
|
|
|
body = '{ "name": "%s", "database_role": "%s" }' % (group_name, group_role)
|
2015-08-17 18:55:42 +08:00
|
|
|
headers = { 'Authorization': params['auth'], 'Content-Type': 'application/json' }
|
2015-08-17 17:49:31 +08:00
|
|
|
client.request('POST', '/api/v1/databases/%s/groups' % database_name, body, headers)
|
2015-08-14 19:55:52 +08:00
|
|
|
response = client.getresponse()
|
|
|
|
assert response.status == 200
|
|
|
|
except Exception as err:
|
|
|
|
plpy.warning('group creation error: ' + str(err))
|
|
|
|
raise err
|
|
|
|
$$ LANGUAGE 'plpythonu' VOLATILE;
|
|
|
|
|
|
|
|
CREATE OR REPLACE
|
|
|
|
FUNCTION cartodb._CDB_Group_DropGroup_API(database_name text, group_name text)
|
|
|
|
RETURNS VOID AS
|
|
|
|
$$
|
|
|
|
import httplib
|
|
|
|
import string
|
|
|
|
|
|
|
|
try:
|
2015-08-17 18:26:38 +08:00
|
|
|
params = plpy.execute("select c.host, c.port, c.timeout, c.auth from cartodb._CDB_Group_API_Conf() c;")[0]
|
2015-08-17 17:49:31 +08:00
|
|
|
if params['host'] is None:
|
2015-08-14 19:55:52 +08:00
|
|
|
return
|
2015-08-17 17:49:31 +08:00
|
|
|
|
2015-08-14 19:55:52 +08:00
|
|
|
client = httplib.HTTPConnection(params['host'], params['port'], False, params['timeout'])
|
2015-08-17 18:55:42 +08:00
|
|
|
headers = { 'Authorization': params['auth'], 'Content-Type': 'application/json' }
|
2015-08-17 17:49:31 +08:00
|
|
|
client.request('DELETE', '/api/v1/databases/%s/groups/%s' % (database_name, group_name), '', headers)
|
2015-08-14 19:55:52 +08:00
|
|
|
response = client.getresponse()
|
|
|
|
assert response.status == 200
|
|
|
|
except Exception as err:
|
|
|
|
plpy.warning('group creation error: ' + str(err))
|
|
|
|
raise err
|
|
|
|
$$ LANGUAGE 'plpythonu' VOLATILE;
|
2015-08-17 17:49:31 +08:00
|
|
|
|
|
|
|
DO LANGUAGE 'plpgsql' $$
|
|
|
|
BEGIN
|
|
|
|
DROP FUNCTION IF EXISTS cartodb._CDB_Group_API_Conf();
|
|
|
|
DROP TYPE IF EXISTS _CDB_Group_API_Params;
|
|
|
|
END
|
|
|
|
$$;
|
|
|
|
|
|
|
|
CREATE TYPE _CDB_Group_API_Params AS (
|
|
|
|
host text,
|
|
|
|
port int,
|
|
|
|
timeout int,
|
2015-08-17 18:26:38 +08:00
|
|
|
auth text
|
2015-08-17 17:49:31 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
-- This must be explicitally extracted because "composite types are currently not supported".
|
|
|
|
-- See http://www.postgresql.org/docs/9.3/static/plpython-database.html.
|
|
|
|
CREATE OR REPLACE
|
|
|
|
FUNCTION cartodb._CDB_Group_API_Conf()
|
|
|
|
RETURNS _CDB_Group_API_Params AS
|
|
|
|
$$
|
|
|
|
conf = plpy.execute("SELECT cartodb.CDB_Conf_GetConf('groups_api') conf")[0]['conf']
|
|
|
|
if conf is None:
|
|
|
|
return None
|
|
|
|
else:
|
|
|
|
import json
|
|
|
|
params = json.loads(conf)
|
2015-08-17 18:55:42 +08:00
|
|
|
auth = 'Basic %s' % plpy.execute("SELECT cartodb._CDB_Group_API_Auth('%s', '%s') as auth" % (params['username'], params['password']))[0]['auth']
|
2015-08-17 18:26:38 +08:00
|
|
|
return { "host": params['host'], "port": params['port'], 'timeout': params['timeout'], 'auth': auth }
|
2015-08-17 17:49:31 +08:00
|
|
|
# return params
|
|
|
|
$$ LANGUAGE 'plpythonu' VOLATILE;
|
|
|
|
|
|
|
|
CREATE OR REPLACE
|
|
|
|
FUNCTION cartodb._CDB_Group_API_Auth(username text, password text)
|
|
|
|
RETURNS TEXT AS
|
|
|
|
$$
|
|
|
|
import base64
|
|
|
|
base64.encodestring('%s:%s' % (username, password)).replace('\n', '')
|
|
|
|
$$ LANGUAGE 'plpythonu' IMMUTABLE;
|