Update project folder permission settings

Solved!
VickeyC
Level 3
Update project folder permission settings

Please share the Python code for updating project folder permission settings.


Operating system used: RHEL 7.9

0 Kudos
1 Solution
sergeyd
Dataiker

Ok, thanks. Sorry, I have missed that you are doing the direct permission assignment by replacing the existing variable. This will not work. You need to append new permissions to the array you got from get_permissions() function: 

 

pf_settings = new_project_folder.get_settings()
pf_permissions = pf_settings.get_permissions()

new_permission = {'group': MyProjectGroup, 'read': True, 'writeContents': True, 'admin': False}

pf_permissions.append(new_permission)
pf_settings.save()

 

 

View solution in original post

0 Kudos
8 Replies
sergeyd
Dataiker

Hi @VickeyC 

We have a good starting sample in our docs: 

https://doc.dataiku.com/dss/latest/python-api/project-folders.html#modifying-settings

You just need to get a project folder id:

prj_folder = client.get_project_folder(prj_folder_id)

and update the permissions either by modifying dictionaries containing existing permissions, or adding new/removing old ones. 

 

0 Kudos
VickeyC
Level 3
Author

@sergeyd  Yes, I'm very familiar with that page.   The missing link for me is the format of the new permissions.  What exactly would the code look like?   I've tried this on a folder with no permissions defined:

prj_folder = client.get_project_folder(new_project_folder.id)
pf_settings = prj_folder.get_settings()

pf_settings.permissions = [{'group=MyProjectGroup', 'read=True', 'writeContents=True'}]

 

The python code executes without errors, but the folder still doesn't have any permissions.   What am I doing wrong?  Note that MyProjectGroup is a variable

0 Kudos
sergeyd
Dataiker

Variables are fine. The issue here is that you are passing over not dictionary "key:values" pairs but just assigning variables.

Here is what you should have instead: 

pf_settings.permissions = [{'group': MyProjectGroup, 'read': True, 'writeContents': True, 'admin': True}]

 

0 Kudos
VickeyC
Level 3
Author

@sergeyd  Thanks for that info.  

Unfortunately, my folder permissions are still not being updated.   Here's my code:

root_folder = client.get_root_project_folder()
root_children = root_folder.list_child_folders()
new_project_folder_name = projectName

folder_exists = False
for i in root_children:
if i.name == new_project_folder_name:
new_project_folder_id = i.id
folder_exists = True

# Create the project folder if it doesn't already exist
if not folder_exists:
new_project_folder = root_folder.create_sub_folder(new_project_folder_name)

# Update the project folder settings and permissions
pf_settings = new_project_folder.get_settings()
pf_permissions = pf_settings.get_permissions()

pf_settings.permissions = [{'group': MyProjectGroup, 'read': True, 'writeContents': True, 'admin': False}]
pf_settings.save()

What can I do differently to make this work?  The save function doesn't seem to save anything.

0 Kudos
sergeyd
Dataiker

Hm...where do you define your MyProjectGroup variable in the code? 

0 Kudos
VickeyC
Level 3
Author

@sergeyd  It's there in an earlier part of the code.  Here's what it looks like:

MyProjectGroup = "my_project_group"

0 Kudos
sergeyd
Dataiker

Ok, thanks. Sorry, I have missed that you are doing the direct permission assignment by replacing the existing variable. This will not work. You need to append new permissions to the array you got from get_permissions() function: 

 

pf_settings = new_project_folder.get_settings()
pf_permissions = pf_settings.get_permissions()

new_permission = {'group': MyProjectGroup, 'read': True, 'writeContents': True, 'admin': False}

pf_permissions.append(new_permission)
pf_settings.save()

 

 

0 Kudos
VickeyC
Level 3
Author

I have it working now, thanks!

0 Kudos