Creating PDP Policy using Python

mpoudel
mpoudel Contributor

Hi All,

I am having some issue creating PDP policy through Python. The script runs without error and creates the policy but it does not add users into that policy. I wrote this based on the domo github repository. The goal is basically to assign the access to the data based on the office column in master dataset and some users have access to the data of multiple office. I have attached the screenshot of sample Permission dataset id, user dataset id and masterdataset id and sample code in python 3.5.

 

Dataset Descriptions: These all datasets are in domo. 

user dataset has User ID , Name of the user and and their Employee Number [Specific to our company]

Master Dataset is the dataset where PDP Policy is to be created based on the office filter

Permission Dataset has User ID, Name of user and office they should have access to.

PDP policy is the screenshot of the policy that is created on the master dataset. 

 

Any help will be greatly appreciated!

import requests
from requests.auth import HTTPBasicAuth
import json # json is a Python standard package
from pydomo import Domo


# SCRIPT PARAMETERS

apiRoot = "https://api.domo.com"
clientId = "<your client id here>"
clientSecret = "<your client secret here>"

permissionsDatasetId = "<your dataset id here>"
masterDatasetId = "<your dataset id here>"
user_dataset_id = "<your user dataset id>"
offices = ["Austin", "Tempe", "Boston", "New York","West Palm Beach","San Diego","Chicago","Philadelphia"]


# GENERATE ACCESS TOKEN
tokenResponse = requests.get(apiRoot+"/oauth/token?grant_type=client_credentials&scope=user data", auth=HTTPBasicAuth(clientId, clientSecret))
accessToken = tokenResponse.json()["access_token"]
#print (accessToken)


# GET USER LIST (WITH IDS)
#x[1] is username and x[0] is user id

User_dataset = requests.get(apiRoot+"/v1/datasets/"+user_dataset_id+"/data?includeHeader=false", headers={"Authorization": "bearer "+accessToken})
user_permissions = [row.split(",") for row in User_dataset.text.split("\n")][:-1]
users = [[x[1],x[0]] for x in user_permissions]


# GET PERMISSIONS DATASET CONTENT FROM INSTANCE
permissionsResponse = requests.get(apiRoot+"/v1/datasets/"+permissionsDatasetId+"/data?includeHeader=false", headers={"Authorization": "bearer "+accessToken})
permission_list = [row.split(",") for row in permissionsResponse.text.split("\n")][:-1]
permissions = [[x[2],x[0],x[1]] for x in permission_list]

# for permissionRow in permissions:
# print(permissionRow)


# CREATE PDP FILTER CONFIGURATION OBJECTS FOR UPLOAD


def getIdForUser(userName):
return filter(lambda user: user[0] == userName, users)[0][1]

def makeFilterForPDP(region):
return {
"name": region,
"filters": [{
"column": "Office",
"values": [region],
"operator": "EQUALS"
}],
"users": [getIdForUser(user[0]) for user in filter(lambda permissionRow: permissionRow[1] == region, permissions)]
}

filtersForPDP = map(makeFilterForPDP, offices)




# CREATE THE PDP POLICIES
def createPDPPolicy(filter):
creationResponse = requests.post(apiRoot+"/v1/datasets/"+masterDatasetId+"/policies", data=json.dumps(filter), headers={"Authorization": "bearer "+accessToken, "Content-Type": "application/json"})
return creationResponse.text

PDPPolicies = [createPDPPolicy(filter) for filter in filtersForPDP]
# print(PDPPolicies)

Comments

This discussion has been closed.