Letâs create a new user and group, and add the user to the group. Then weâll list all users in the tenancy, and finally clean up the user and group we created.
First, weâll need to create a valid config object and service client. If you havenât set up a config file, head over to the Configuration section to create one. Weâll use the default location ~/.oci/config
and default profile name DEFAULT
to create an Identity client. Since weâll be using the root compartment (or tenancy) for most operations, letâs also extract that from the config object:
>>> import oci >>> config = oci.config.from_file() >>> identity = oci.identity.IdentityClient(config) >>> compartment_id = config["tenancy"]
Next weâll need to populate an instance of the CreateGroupDetails
model with our request, and then send it:
>>> from oci.identity.models import CreateGroupDetails >>> request = CreateGroupDetails() >>> request.compartment_id = compartment_id >>> request.name = "my-test-group" >>> request.description = "Created with the Python SDK" >>> group = identity.create_group(request) >>> print(group.data.id) "id": "ocid1.group.oc1..aaaaaaaaikib..."
Creating a user is very similar:
>>> from oci.identity.models import CreateUserDetails >>> request = CreateUserDetails() >>> request.compartment_id = compartment_id >>> request.name = "my-test-user" >>> request.description = "Created with the Python SDK" >>> user = identity.create_user(request) >>> print(user.data.id) "ocid1.user.oc1..aaaaaaaamkym..."
Using the ids from the group
and user
above, we can add the user to the group:
>>> from oci.identity.models import AddUserToGroupDetails >>> request = AddUserToGroupDetails() >>> request.group_id = group.data.id >>> request.user_id = user.data.id >>> response = identity.add_user_to_group(request) >>> print(response.status) 200Deleting entities¶
Now to clean up the entities we created. Users canât be deleted if theyâre still part of a group, and groups canât be deleted if they still have users. So we need to use identity.remove_user_from_group
, which takes a user_group_membership_id
. Because users and groups can have any number of relationships, weâll use list_user_group_memberships
and provide both optional parameters user_id
and group_id
to constrain the result set:
>>> memberships = identity.list_user_group_memberships( ... compartment_id=compartment_id, ... user_id=user.data.id, ... group_id=group.data.id) # There can never be more than one membership for a unique user/group combination >>> assert len(memberships.data) == 1 >>> membership_id = memberships.data[0].id
Finally, we can remove the user from the group, and delete both resources. Here weâre using response.status
to make sure the delete responded with 204:
>>> identity.remove_user_from_group( ... user_group_membership_id=membership_id).status 204 >>> identity.delete_user(user_id=user.data.id).status 204 >>> identity.delete_group(group_id=group.data.id).status 204Working with Bytes¶
When using object storage, youâll need to provide a namespace, in addition to your compartment id:
>>> object_storage = oci.object_storage.ObjectStorageClient(config) >>> namespace = object_storage.get_namespace().data
To upload an object, weâll create a bucket:
>>> from oci.object_storage.models import CreateBucketDetails >>> request = CreateBucketDetails() >>> request.compartment_id = compartment_id >>> request.name = "MyTestBucket" >>> bucket = object_storage.create_bucket(namespace, request) >>> bucket.data.etag '5281759f-60bb-4b93-8676-f8d141b5f211'
Now we can upload arbitrary bytes:
>>> my_data = b"Hello, World!" >>> obj = object_storage.put_object( ... namespace, ... bucket.data.name, ... "my-object-name", ... my_data)
And to get it back:
>>> same_obj = object_storage.get_object( ... namespace, ... bucket.data.name, ... "my-object-name") ... same_obj.data <Response [200]> ... same_obj.data.content b'Hello, World!'Next Steps¶
Next, head to the User Guides or jump right into the API Reference to explore the available operations for each service, and their parameters. Additional Python examples can be found on GitHub.
Note
The Python SDK uses lowercase_with_underscores
for operations and parameters. For example, the ListApiKeys operation is called with IdentityClient.list_api_keys
and its parameter userId
is translated to user_id
.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4