Commit b671e383 authored by Vitali Stupin's avatar Vitali Stupin
Browse files

Formating SQL

parent c0069e93
......@@ -9,17 +9,22 @@ MEMBER_NAME = 'XX test'
MEMBER_CLASS = 'GOV'
conf = {
'database': '',
'username': '',
'password': '',
'database': ''
'password': ''
}
# Getting database credentials from xroad configuration
with open('/etc/xroad/db.properties', 'r') as dbConf:
for line in dbConf:
# Example:
# username=centerui
# password=centerui
# database=centerui_production
m = re.match('^database\\s*=\\s*(.+)$', line)
if m:
conf['database'] = m.group(1)
m = re.match('^username\\s*=\\s*(.+)$', line)
if m:
conf['username'] = m.group(1)
......@@ -28,10 +33,6 @@ with open('/etc/xroad/db.properties', 'r') as dbConf:
if m:
conf['password'] = m.group(1)
m = re.match('^database\\s*=\\s*(.+)$', line)
if m:
conf['database'] = m.group(1)
if not conf['username'] or not conf['password'] or not conf['database']:
print('Cannot access password file')
exit(1)
......@@ -41,8 +42,13 @@ conn = psycopg2.connect(
'localhost', '5432', conf['database'], conf['username'], conf['password']))
cur = conn.cursor()
cur.execute("""select exists(select * from security_server_clients where type='XRoadMember' \
and member_code=%(str)s)""", {'str': MEMBER_CODE})
cur.execute(
"""
select exists(
select * from security_server_clients
where type='XRoadMember' and member_code=%(str)s
)
""", {'str': MEMBER_CODE})
rec = cur.fetchone()
if rec[0] is True:
print('Member exists')
......@@ -61,29 +67,39 @@ cur.execute("""select current_timestamp at time zone 'UTC'""")
utc_time = cur.fetchone()[0]
cur.execute(
"""insert into identifiers(object_type, xroad_instance, member_class, member_code, type, """
"""created_at, updated_at) """
"""values('MEMBER', (select value from system_parameters where key='instanceIdentifier'), """
"""%(class)s, %(code)s, 'ClientId', %(time)s, %(time)s) returning id""",
{'class': MEMBER_CLASS, 'code': MEMBER_CODE, 'time': utc_time}
"""
insert into identifiers (
object_type, xroad_instance, member_class, member_code, type, created_at, updated_at
) values (
'MEMBER', (select value from system_parameters where key='instanceIdentifier'),
%(class)s, %(code)s, 'ClientId', %(time)s, %(time)s
) returning id
""", {'class': MEMBER_CLASS, 'code': MEMBER_CODE, 'time': utc_time}
)
identifier_id = cur.fetchone()[0]
cur.execute(
"""insert into security_server_clients"""
"""(member_code, name, member_class_id, server_client_id, type, created_at, updated_at) """
"""values"""
"""(%(code)s, %(name)s, %(class_id)s, %(identifier_id)s, 'XRoadMember', %(time)s, %(time)s)""",
{
"""
insert into security_server_clients (
member_code, name, member_class_id, server_client_id, type, created_at, updated_at
) values (
%(code)s, %(name)s, %(class_id)s, %(identifier_id)s, 'XRoadMember', %(time)s,
%(time)s
)
""", {
'code': MEMBER_CODE, 'name': MEMBER_NAME, 'class_id': class_id,
'identifier_id': identifier_id, 'time': utc_time
}
)
cur.execute(
"""insert into security_server_client_names(name, client_identifier_id, created_at, """
"""updated_at) values(%(name)s, %(identifier_id)s, %(time)s, %(time)s)""",
{'name': MEMBER_NAME, 'identifier_id': identifier_id, 'time': utc_time}
"""
insert into security_server_client_names (
name, client_identifier_id, created_at, updated_at
) values (
%(name)s, %(identifier_id)s, %(time)s, %(time)s
)
""", {'name': MEMBER_NAME, 'identifier_id': identifier_id, 'time': utc_time}
)
cur.close()
......
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