Skip to content

Instantly share code, notes, and snippets.

@csjx
Last active February 21, 2020 18:18
Show Gist options
  • Save csjx/ebf30bd5a6c6cdd46b2524a6ab463f07 to your computer and use it in GitHub Desktop.
Save csjx/ebf30bd5a6c6cdd46b2524a6ab463f07 to your computer and use it in GitHub Desktop.
Example of listing all objects in a repository and updating the system metadata, in this case updating an AccessRule Subject string
#!/usr/bin/env python3
from d1_client.mnclient_2_0 import MemberNodeClient_2_0
import os
# Log in and get a token from https://search.dataone.org
# (or https://search-stage.test.dataone.org if using a test server)
# and set the token as a shell variable
token = os.getenv("token")
base_url = "https://data.piscoweb.org/catalog/d1/mn"
# Append the JWT token as an HTTP header
headers = {
"Authorization": "Bearer " + token
}
try:
# Create a Member Node Client to interact with PISCO
mn = MemberNodeClient_2_0(base_url, headers=headers)
# You can get up to 7000 objects per listObjects() call, I used 100 as an example.
# TODO: use a for loop for processing greater than 7000
count = 100
objects = mn.listObjects(count=count, start=0)
for index, objectinfo in enumerate(objects.content()):
pid = objectinfo.identifier.value()
print("Processing {}) {}".format(index, pid))
try:
sysmeta = mn.getSystemMetadata(pid)
# Get the array of allow rules in the access policy
access_rules = sysmeta.accessPolicy.allow
for rule in access_rules:
# Multiple subjects are supported per allow rule
subjects = rule.subject
for i, subject in enumerate(subjects):
# Update the group subject
if subject.value().lower() == "cn=data-managers,o=piscogroups,dc=ecoinformatics,dc=org":
subjects[i].reset().append("CN=PISCO-data-managers,DC=dataone,DC=org")
# Update the repository with the modified system metadata
response_id = mn.updateSystemMetadata(pid, sysmeta)
print("Updated {}".format(response_id))
except AttributeError as ae:
print(ae)
except BaseException as e:
print(e)
except BaseException as be:
print(be)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment