Skip to content

Commit

Permalink
DT-1217: Python script to remove a user from a list of snapshots (#1901)
Browse files Browse the repository at this point in the history
* first pass

* working example

* Code review: Remove other api definitions; add better logging
  • Loading branch information
snf2ye authored Feb 12, 2025
1 parent c083614 commit d8c36d4
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
5 changes: 5 additions & 0 deletions tools/removeSnapshotMember/example_steward.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"email": "[email protected]",
"role": "steward",
"snapshotIds": ["733dcfa4-1bd6-4c58-a9e5-8dd224d3ce9f"]
}
70 changes: 70 additions & 0 deletions tools/removeSnapshotMember/remove_member_from_snapshot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import argparse
import json
import subprocess

from data_repo_client import (
Configuration,
ApiClient,
ProfilesApi,
DatasetsApi,
SnapshotsApi,
JobsApi,
SnapshotAccessRequestApi,
)


class Clients:
def __init__(self, host):
config = Configuration()
config.host = host
token_output = subprocess.run(
["gcloud", "auth", "print-access-token"], capture_output=True
)
config.access_token = token_output.stdout.decode("UTF-8").strip()
self.api_client = ApiClient(configuration=config)
self.snapshots_api = SnapshotsApi(api_client=self.api_client)


def main():
parser = argparse.ArgumentParser()
parser.add_argument(
"--host",
required=True,
help="The data repo root URL to point to. This is required flag. Examples include `http://localhost:8080` or `https://jade.datarepo-dev.broadinstitute.org`",
)
parser.add_argument(
"--request",
required=True,
help="file name for request containing member email, role and list of snapshots",
)

args = parser.parse_args()
clients = Clients(args.host)

request = args.request

with open(request) as remove_member_json:
removal_request = json.load(remove_member_json)
email = removal_request["email"]
role = removal_request["role"]
snapshot_ids = removal_request["snapshotIds"]

print(f"Removing {email} as {role} from {len(snapshot_ids)} snapshots")
success_count = 0
failure_count = 0
for snapshot_id in snapshot_ids:
try:
clients.snapshots_api.delete_snapshot_policy_member(snapshot_id, role,
email)
success_count += 1
except Exception as e:
print(
f"Error: Could not remove {email} from {snapshot_id}; Exception: {e}")
failure_count += 1
continue
print(
f"DONE. Successfully removed {success_count} members from {len(snapshot_ids)} snapshots. {failure_count} failed.")


if __name__ == "__main__":
main()

0 comments on commit d8c36d4

Please sign in to comment.