snowflake - Purge User - Alert Role Owner that user is not active for long time

Viewed 96

Agenda - to notify role owner that user is inactive for long time.

I am creating a procedure where i need to capture user inactive for more than 90 days and to capture his role and role owners and need to compile and send list to role owner and role owner will take action later.

below is the procedure I developed

    CREATE OR REPLACE PROCEDURE "Notify_Owner_Inactive_Users"()
    RETURNS VARCHAR(16777216)
    LANGUAGE JAVASCRIPT
    EXECUTE AS CALLER
    AS '
    var users = "SHOW USERS;" ;
    var users_drtemp = snowflake.createStatement({ sqlText: users });
    users_drtemp.execute();
    
    
    var inactive_users = snowflake.createStatement(
      
      { sqlText: 
        `
        SELECT "login_name",DATEDIFF("Day","last_success_login",CURRENT_DATE) as not_login_since,owner FROM TABLE(RESULT_SCAN(LAST_QUERY_ID()))
         where DATEDIFF("Day","last_success_login",CURRENT_DATE) > 90 or last_success_login is null 
         and "disabled"='false'
         and "snowflake_lock"='false'
         ;
    `
    }
    );
    var rs_inactive_users= inactive_users.execute();
    
    while (rs_inactive_users.next()) 
    
    {
    try{
           var user_name = rs_inactive_users.getColumnValue(1);
           var owner_name = rs_inactive_users.getColumnValue(3);
           
    var roles = "SHOW ROLES;" ;
    var users_Roles = snowflake.createStatement({ sqlText: roles });
    users_Roles.execute();
    
    var owner_email_id = snowflake.createStatement(
      
      { sqlText: 
        `
        
        select distinct split_part(b.data,':',2) as Owner_Email from (
        select a.value::string as data from TABLE(RESULT_SCAN(LAST_QUERY_ID())),
        lateral flatten(input=>split(comment,',')) a) b where b.data like '%Owner%';
        
         ;
    `
    }
    );
    var rs_owner_email= owner_email_id.execute();
    
    while (rs_owner_email.next()) 
    
    {
    try{
           var email_address = rs_owner_email.getColumnValue(1);
           
    catch(err)
          {
                 return("Job Failure");   
          }
         
    }
    
    return("Job Completed")
    ';

i need to take user list and map to role owners and send an email to owner Can someone develop on top of it

0 Answers
Related