__cdb_credentials_to_user_mapping function & test

This commit is contained in:
Rafa de la Torre 2019-10-08 16:09:30 +02:00
parent 49efd939f6
commit 101d276f91
2 changed files with 32 additions and 4 deletions

View File

@ -2,6 +2,25 @@
-- Federated Tables management functions -- Federated Tables management functions
---------------------------------------------------------------------- ----------------------------------------------------------------------
-- Take a server_config jsonb and transform it to an input suitable
-- for _CDB_SetUp_User_PG_FDW_Server
CREATE OR REPLACE FUNCTION @extschema@.__cdb_credentials_to_user_mapping(input_config jsonb)
RETURNS jsonb
AS $$
DECLARE
user_mapping jsonb;
BEGIN
user_mapping := json_build_object('user_mapping',
jsonb_build_object(
'user', input_config->'credentials'->'username',
'password', input_config->'credentials'->'password'
)
);
RETURN (input_config - 'credentials')::jsonb || user_mapping;
END
$$
LANGUAGE PLPGSQL IMMUTABLE PARALLEL SAFE;
-- --
-- Set up a federated server for later connection of tables/views -- Set up a federated server for later connection of tables/views
@ -17,11 +36,14 @@
-- "password": "secret" -- "password": "secret"
-- } -- }
-- }'); -- }');
CREATE OR REPLACE FUNCTION @extschema@.CDB_SetUp_PG_Federated_Server(server_alias text, server_config json) CREATE OR REPLACE FUNCTION @extschema@.CDB_SetUp_PG_Federated_Server(server_alias text, server_config jsonb)
RETURNS void RETURNS void
AS $$ AS $$
DECLARE
final_config jsonb;
BEGIN BEGIN
PERFORM cartodb._CDB_SetUp_User_PG_FDW_Server(server_alias, server_config); final_config := @extschema@.__cdb_credentials_to_user_mapping(server_config);
PERFORM cartodb._CDB_SetUp_User_PG_FDW_Server(server_alias, final_config::json);
END END
$$ $$
LANGUAGE PLPGSQL VOLATILE PARALLEL UNSAFE; LANGUAGE PLPGSQL VOLATILE PARALLEL UNSAFE;

View File

@ -683,12 +683,17 @@ function test_federated_tables() {
"host": "localhost", "host": "localhost",
"port": ${PGPORT:-5432} "port": ${PGPORT:-5432}
}, },
"user_mapping": { "credentials": {
"user": "fdw_user", "username": "fdw_user",
"password": "foobarino" "password": "foobarino"
} }
} }
EOF EOF
# Unit-test this helper method
sql postgres "SELECT cartodb.__cdb_credentials_to_user_mapping('$federated_server_config')" \
should '{"server": {"host": "localhost", "port": 5432, "dbname": "fdw_target"}, "user_mapping": {"user": "fdw_user", "password": "foobarino"}}'
# There must be a function with the expected interface # There must be a function with the expected interface
sql postgres "SELECT cartodb.CDB_SetUp_PG_Federated_Server('my_server', '$federated_server_config');" sql postgres "SELECT cartodb.CDB_SetUp_PG_Federated_Server('my_server', '$federated_server_config');"
@ -699,6 +704,7 @@ EOF
sql cdb_testmember_1 'SELECT * from "cdb_fdw_my_server".foo;' sql cdb_testmember_1 'SELECT * from "cdb_fdw_my_server".foo;'
sql cdb_testmember_1 'SELECT a from "cdb_fdw_my_server".foo LIMIT 1;' should 42 sql cdb_testmember_1 'SELECT a from "cdb_fdw_my_server".foo LIMIT 1;' should 42
# Tear down
sql postgres "SELECT cartodb._CDB_Drop_User_PG_FDW_Server('my_server', /* force = */ true)" sql postgres "SELECT cartodb._CDB_Drop_User_PG_FDW_Server('my_server', /* force = */ true)"
tear_down_fdw_target tear_down_fdw_target
} }