Script to modify project settings

Srkanoje
Srkanoje Registered Posts: 32 ✭✭✭✭

Hi All,

I have created a script to modify the project seeting, and have scheduled it as a scenario to run daily but the list keeps on getting append by the same group name on daily basis. can you please suggest how to break this. -- please find the image attached for reference.

below is the script which i am using ---
import dataiku
client = dataiku.api_client()
for project_key in PROJECT_KEYS:
project = client.get_project(project_key)
project_permissions = project.get_permissions()
project_permissions['permissions'].append({'group':'Test1','admin': True})
project_permissions['permissions'].append({'group':'Test2','readProjectContent': True})
project_permissions['permissions'].append({'group':'Test3','readProjectContent': True})
project.set_permissions(project_permissions)


Operating system used: linux


Operating system used: linux

Answers

  • Alexandru
    Alexandru Dataiker, Dataiku DSS Core Designer, Dataiku DSS ML Practitioner, Dataiku DSS Adv Designer, Registered Posts: 1,226 Dataiker
    edited July 17

    Hi @Srkanoje
    ,

    The API call will not check if the values already exist you will need to do this yourself something like this:

    import dataiku
    
    client = dataiku.api_client()
    for project_key in PROJECT_KEYS:
        project = client.get_project(project_key)
        project_permissions = project.get_permissions()
        
        # Define permissions to add
        permissions_to_add = [
            {'group': 'Test1', 'admin': True},
            {'group': 'Test2', 'readProjectContent': True},
            {'group': 'Test3', 'readProjectContent': True}
        ]
        
        # Check if permissions already exist, if not append
        for permission in permissions_to_add:
            if permission not in project_permissions['permissions']:
                project_permissions['permissions'].append(permission)
        
        project.set_permissions(project_permissions)

  • Srkanoje
    Srkanoje Registered Posts: 32 ✭✭✭✭

    @AlexT
    thank you for helping this works

  • Srkanoje
    Srkanoje Registered Posts: 32 ✭✭✭✭

    @AlexT
    is there a way to delete the same permission

  • Alexandru
    Alexandru Dataiker, Dataiku DSS Core Designer, Dataiku DSS ML Practitioner, Dataiku DSS Adv Designer, Registered Posts: 1,226 Dataiker
    edited July 17

    yes, you can same approach to delete a list of permissions:

    import dataiku
    
    client = dataiku.api_client()
    for project_key in PROJECT_KEYS:
        project = client.get_project(project_key)
        project_permissions = project.get_permissions()
        
        # Define permissions to delete
        permissions_to_delete = [
            {'group': 'Test1', 'admin': True},
            {'group': 'Test2', 'readProjectContent': True},
            {'group': 'Test3', 'readProjectContent': True}
        ]
        
        # Check if permissions exist, if yes, remove them
        updated_permissions = []
        for permission in project_permissions['permissions']:
            if permission not in permissions_to_delete:
                updated_permissions.append(permission)
        
        project_permissions['permissions'] = updated_permissions
        
        project.set_permissions(project_permissions)

Setup Info
    Tags
      Help me…