Creating and Managing DataHub Policies Programmatically in Python SDK and REST API

Original Slack Thread

Hi, Is there any way to create a policy (either metadata or platform) via python SDK or rest API in DataHub? Thanks!

Hi Jason,

yes it is. Via the Python SDK you can make use of DataHubPolicySnapshotClass
This allows you to define custom policies programmatically.
Happy to provide some more details, if needed :slightly_smiling_face:

Hi <@U049WUH4155> - thanks for sharing this! Would you be able to share how you implemented the creation/update of a policy using DataHubPolicySnapshotClass? Did you use the rest emitter to create a metadata change event?

Sure, here is the creation of the MCE:

                proposedSnapshot=DataHubPolicySnapshotClass(
                    urn=f"urn:li:dataHubPolicy:{policy.name}",
                    aspects=[
                        DataHubPolicyInfoClass(
                            actors=DataHubActorFilterClass(
                                allGroups=policy.all_groups,
                                allUsers=policy.all_users,
                                resourceOwners=policy.resource_owners,
                                users=policy.users,
                                groups=policy.groups,
                            ),
                            description=policy.description,
                            displayName=policy.display_name,
                            editable=policy.editable,
                            privileges=policy.privileges,
                            state=policy.state,
                            type=policy.type,
                        )
                    ],
                )
            )```
The creation of the request is then done via: `emitter.emit(item=mcp)` where `emitter` is an instance of `DatahubRestEmitter`

Thank you!

Is there any link that can view this API’s (DataHubPolicySnapshotClass) detail infomation? <@U049WUH4155>

The only one I am aware of is the following: https://github.com/datahub-project/datahub/blob/ccb09a69669e24624afbeca486220aa106dabfb7/metadata-models/src/main/pegasus/com/linkedin/metadata/snapshot/DataHubPolicySnapshot.pdl|https://github.com/datahub-project/datahub/blob/ccb09a69669e24624afbeca486220aa106[…]egasus/com/linkedin/metadata/snapshot/DataHubPolicySnapshot.pdl