OnDemand SQL update using pyspark for Role, user management

86 Views Asked by At

I am trying to create ROLE based on the Groups coming in from Azure AAD groups and if they do not exist in DB. I am using the code below but it fails saying "ParseException: Operation not allowed: CREATE ROLE(line 1, pos 0)

== SQL == CREATE ROLE [ROLE_AZURE_AAD_GROUP] AUTHORIZATION [dbo]; ^^^" " What is the workaround?


for row in df_AADGroup.rdd.collect():
    #print('ROLE_' + row['Azure_AAD_Group_name'] + row['Azure_AAD_Group_UsageType'])
    if row['Azure_AAD_Group_UsageType'] == 'Functional':
        query = "select * from database_principals where name = 'ROLE_" + row['Azure_AAD_Group_name'] + "'"
        if spark.sql(query).count() == 0 :
            role_query = "CREATE ROLE [ROLE_" + row['Azure_AAD_Group_name'] + "]  AUTHORIZATION [dbo];"
            spark.sql(role_query)
        print('ROLE_' + row['Azure_AAD_Group_name'] + " does not exists")

0

There are 0 best solutions below