Debian Codegraph @

docs/examples/guest_collection_creation.rst

raw
1 .. _example_guest_collection_creation:
2
3 Guest Collection Creation Script
4 --------------------------------
5
6 The following is a script for a Globus client identity to create a GCSv5 guest
7 collection on an existing mapped collection that it has a valid mapping for.
8 The constants defined do not refer to a real GCSv5 installation, or client
9 identity, so the script cannot be run as is.
10
11 This script can be tweaked to use a human user identity instead of a client
12 by changing the authorizer from a ClientCredentialsAuthorizer to an
13 AccessTokenAuthorizer or RefreshTokenAuthorizer using a user token.
14
15 The script assumes the mapped collection is on a storage gateway using
16 the POSIX connector. Other connectors may need to have connector specific
17 policy documents passed to create the user credential.
18
19 .. code-block:: python
20
21 import globus_sdk
22 from globus_sdk import scopes
23
24 # constants
25 endpoint_hostname = "abc.xyz.data.globus.org"
26 endpoint_id = "59544bb0-8aa3-4c73-9ce4-06d66887bc89"
27 mapped_collection_id = "a1c2f515-254a-48a1-a5de-3ea51d783638"
28 storage_gateway_id = "1b949deb-d608-403c-a226-a533892789c6"
29
30 # client credentials
31 # This client identity must have the needed permissions to create a guest
32 # collection on the mapped collection, and a valid mapping to a local account
33 # on the storage gateway that matches the local_username
34 # If using user tokens, the user must be the one with the correct permissions
35 # and identity mapping.
36 client_id = "4de65cd7-4363-4510-b652-f8d15a43a0af"
37 client_secret = "*redacted*"
38 local_username = "local-username"
39
40 # The scope the client will need, note that primary scope is for the endpoint,
41 # but it has a dependency on the mapped collection's data_access scope
42 scope = scopes.Scope(scopes.GCSEndpointScopeBuilder(endpoint_id).manage_collections)
43 scope = scope.with_dependency(
44 scopes.GCSCollectionScopeBuilder(mapped_collection_id).data_access
45 )
46
47 # Build a GCSClient to act as the client by using a ClientCredentialsAuthorizor
48 confidential_client = globus_sdk.ConfidentialAppAuthClient(
49 client_id=client_id, client_secret=client_secret
50 )
51 authorizer = globus_sdk.ClientCredentialsAuthorizer(confidential_client, scopes=scope)
52 client = globus_sdk.GCSClient(endpoint_hostname, authorizer=authorizer)
53
54 # The identity creating the guest collection must have a user credential on
55 # the mapped collection.
56 # Note that this call is connector specific. Most connectors will require
57 # connector specific policies to be passed here, but POSIX does not.
58 credential_document = globus_sdk.UserCredentialDocument(
59 storage_gateway_id=storage_gateway_id,
60 identity_id=client_id,
61 username=local_username,
62 )
63 client.create_user_credential(credential_document)
64
65 # Create the collection
66 collection_document = globus_sdk.GuestCollectionDocument(
67 public="True",
68 collection_base_path="/",
69 display_name="guest_collection",
70 mapped_collection_id=mapped_collection_id,
71 )
72 response = client.create_collection(collection_document)
73 guest_collection_id = response["id"]
74 print(f"guest collection {guest_collection_id} created")