Add python sdk docs

This commit is contained in:
Tyler Mairose
2024-03-21 12:00:08 -04:00
parent ccf691e50a
commit b99d242da0
9 changed files with 699 additions and 0 deletions

View File

@@ -0,0 +1,74 @@
---
id: python-sdk-create
title: Creating resources with The Python SDK
pagination_label: Create a resource
sidebar_label: Create a resource
sidebar_position: 2
sidebar_class_name: pythonsdk
keywords: ['python', 'sdk', 'create']
description: Learn how to use the Python SDK to create new resources.
slug: /tools/sdk/python/create
tags: ['SDK']
---
You can use the SDK to create new resources.
For example, you can run a script to create a work group, also known as a [governance group](https://documentation.sailpoint.com/saas/help/common/users/governance_groups.html).
Copy this 'create WorkGroup' script from the beta APIs into your Python project to try it out:
```python showLineNumbers
import sailpoint
import sailpoint.v3
import sailpoint.beta
from sailpoint.beta.models.workgroup_dto import WorkgroupDto
from sailpoint.beta.models.owner_dto import OwnerDto
from sailpoint.configuration import Configuration
configuration = Configuration()
api_client = sailpoint.v3.ApiClient(configuration)
api_client_beta = sailpoint.beta.ApiClient(configuration)
identities_api_instance = sailpoint.v3.PublicIdentitiesApi(api_client)
workgroups_api_instance = sailpoint.beta.GovernanceGroupsApi(api_client_beta)
identity = identities_api_instance.get_public_identities(limit=1)[0]
workgroup = WorkgroupDto(name='DB Access Governance Group',
description='Description of the Governance Group',
owner=OwnerDto(id=identity.id,
name=identity.name,
type='IDENTITY'))
try:
workgroupResponse = workgroups_api_instance.create_workgroup(workgroup)
print("The response of GovernanceGroupsApi->create_workgroup:\n")
print(workgroupResponse)
except Exception as e:
print("Exception when calling GovernanceGroupsApi->create_workgroup: %s\n" % e)
```
Run this command to run the code:
```bash
python sdk.py
```
The example uses the `getPublicIdentities` method from the `PublicIdentitiesApi` to pull an identity needed to be the owner of the work group.
The `create_workgroup` request is initialized on lines 18-22, using the identity's `name` and `id` in the owner object.
The SDK will return the created work group:
```bash
id='d287a1e2-81fc-474e-bc0c-155bd8ab0899'
name='DB Access Governance Group'
description='Description of the Governance Group'
member_count=0
connection_count=0
owner=OwnerDto(type='IDENTITY',
id='0003c25c365e492381d4e557b6159f9b',
name='Brian Mendoza')
```

View File

@@ -0,0 +1,62 @@
---
id: python-sdk-delete
title: Deleting resources with The Python SDK
pagination_label: Delete a resource
sidebar_label: Delete a resource
sidebar_position: 4
sidebar_class_name: pythonsdk
keywords: ['python', 'sdk', 'delete']
description: Learn how to use the Python SDK to delete resources.
slug: /tools/sdk/python/delete
tags: ['SDK']
---
You can use the SDK to delete resources.
For example, you can run a script that searches by name for a created work group, also known as a [governance group](https://documentation.sailpoint.com/saas/help/common/users/governance_groups.html), and calls the delete method to remove it from your environment.
This script searches by name for the work group created in the example in [Creating resources](./creating-resources.md) and calls the delete method to remove it from your environment. Copy the script into your Python project to try it out:
```python
import sailpoint
import sailpoint.beta
from sailpoint.beta.models.workgroup_dto import WorkgroupDto
from sailpoint.beta.models.usage_type import UsageType
from sailpoint.beta.models.owner_dto import OwnerDto
from sailpoint.beta.models.json_patch_operation import JsonPatchOperation
from sailpoint.beta.models.json_patch_operation_value import JsonPatchOperationValue
from sailpoint.configuration import Configuration
configuration = Configuration()
api_client_beta = sailpoint.beta.ApiClient(configuration)
workgroups_api_instance = sailpoint.beta.GovernanceGroupsApi(api_client_beta)
workgroup = workgroups_api_instance.list_workgroups(filters='name eq "DB Access Governance Group"')[0]
try:
workgroupResponse = workgroups_api_instance.delete_workgroup_with_http_info(workgroup.id)
print("The response of GovernanceGroupsApi->delete_workgroup:\n")
print(workgroupResponse)
except Exception as e:
print("Exception when calling GovernanceGroupsApi->delete_workgroup: %s\n" % e)
```
Run this command to run the code:
```bash
python sdk.go
```
The SDK returns the `deletionStatus` with a value of 204.
```python
The response of GovernanceGroupsApi->patch_workgroup:
status_code=204
headers={'Date': 'Wed, 31 Jan 2024 18:37:33 GMT', 'Connection': 'keep-alive', 'Server': 'nginx', 'Vary': 'Access-Control-Request-Headers', 'Cache-Control': 'no-cache, no-store, must-revalidate', 'SLPT-Request-ID': 'acdbe637fc044befbfe0ce16ad2224ad', 'Access-Control-Expose-Headers': 'Retry-After,Connection,SLPT-Request-ID,Date,X-Zuul-ServiceId', 'X-Robots-Tag': 'noindex'}
data=None
raw_data=b''s
```

View File

@@ -0,0 +1,59 @@
---
id: python-sdk-error-handling
title: Error Handling with The Python SDK
pagination_label: Error Handling
sidebar_label: Error Handling
sidebar_position: 8
sidebar_class_name: pythonsdk
keywords: ['py', 'python', 'sdk', 'error']
description: Learn how to configure error handling when using the Python SDK.
slug: /tools/sdk/python/error-handling
tags: ['SDK']
---
The Python SDK returns key exceptions if the request fails. You can find these exceptions at `sailpoint.v3.exceptions`.
Here is an example of the error handling process. In this example, `name rt "Test"` isn't a valid filter, which causes the function to return a 400 'bad request' exception message. Line 11 will catch that 'bad request' response, and you can log the appropriate message. This example will also catch 401 'unauthorized' and 500 'service exception' messages:
```python showLineNumbers
import sailpoint
import sailpoint.v3
from sailpoint.configuration import Configuration
from sailpoint.v3.exceptions import BadRequestException, UnauthorizedException, ServiceException
configuration = Configuration()
api_client = sailpoint.v3.ApiClient(configuration)
try:
transforms = sailpoint.v3.TransformsApi(api_client).list_transforms(filters='name rt "Test"')
except BadRequestException as e:
print("Exception when calling TransformsApi->list_transforms: %s\n" % e)
except UnauthorizedException as e:
print("Unauthorized exception when calling TransformsApi->list_transforms: %s\n" % e)
except ServiceException as e:
print("Service exception when calling TransformsApi->list_transforms: %s\n" % e)
```
If you don't want the program to exit for any error response, you can include `pass` in each of your `except` blocks after printing the error message, as you can see in this example:
```python
import sailpoint
import sailpoint.v3
from sailpoint.configuration import Configuration
from sailpoint.v3.exceptions import BadRequestException, UnauthorizedException, ServiceException
configuration = Configuration()
api_client = sailpoint.v3.ApiClient(configuration)
try:
transforms = sailpoint.v3.TransformsApi(api_client).list_transforms(filters='name rt "Test"')
except BadRequestException as e:
print("Exception when calling TransformsApi->list_transforms: %s\n" % e)
pass
except UnauthorizedException as e:
print("Unauthorized exception when calling TransformsApi->list_transforms: %s\n" % e)
pass
except ServiceException as e:
print("Service exception when calling TransformsApi->list_transforms: %s\n" % e)
pass
```

View File

@@ -0,0 +1,98 @@
---
id: python-sdk-getting-started
title: Getting started with the Python SDK
pagination_label: Python SDK
sidebar_label: Getting Started
sidebar_position: 1
sidebar_class_name: pythonsdk
keywords: ['python', 'sdk', 'start']
description: Learn how to use the Python SDK in this guide.
slug: /tools/sdk/python/getting-started
tags: ['SDK']
---
Once your SDK is installed and configured, you can start accessing the SDK's different functionalities. To learn how to install and configure the Python SDK, refer to [Installation and Configuration](./index.mdx).
## List Transforms
One of the most useful functionalities of the Python SDK is the ability to easily access all the [V3 APIs](/idn/api/v3) and [Beta APIs](/idn/api/beta) and implement them in your project.
Here is an example of how to use the SDK to get a list of available [transforms](/idn/docs/transforms). This example leverages the [List Transforms endpoint](/idn/api/v3/list-transforms).
Create a file in your project called "sdk.py" and copy this content into it:
```python
import sailpoint
import sailpoint.v3
from sailpoint.configuration import Configuration
import urllib3
configuration = Configuration()
with sailpoint.v3.ApiClient(configuration) as api_client:
# Create an instance of the API class
api_instance = sailpoint.v3.TransformsApi(api_client)
# List transforms
try:
api_response = api_instance.list_transforms()
print("The response of TransformsApi->list_transforms:\n")
print(api_response)
except Exception as e:
print("Exception when calling TransformsApi->list_transforms: %s\n" % e)
```
This example imports the V3 APIs, which allows you to call the List Transforms V3 endpoint in your code.
To run the code, run this command:
```go
python sdk.py
```
The SDK will return a list of available transforms.
You can use this example as a guide for how to access all the V3 and Beta APIs (you would use `import sailpoint.beta` to import the Beta APIs).
### Use query parameters to filter your tenant's transform list
With the same SDK function, you can use query parameters to limit the results of your transforms list to only the results you want.
Refer to the [List Transforms endpoint specification](/idn/api/v3/list-transforms) to view all its query parameters.
Here is an example that uses query parameters to limit the list to no more than 10 transforms that all start with the name "Test":
```python
import sailpoint
import sailpoint.v3
from sailpoint.configuration import Configuration
import urllib3
configuration = Configuration()
with sailpoint.v3.ApiClient(configuration) as api_client:
# Create an instance of the API class
api_instance = sailpoint.v3.TransformsApi(api_client)
# List transforms
try:
api_response = api_instance.list_transforms(filters='name sw "Test"', limit=10)
print("The response of TransformsApi->list_transforms:\n")
print(api_response)
except Exception as e:
print("Exception when calling TransformsApi->list_transforms: %s\n" % e)
```
To run the code, run this command:
```go
python sdk.py
```
The SDK will return a list of no more than 10 transforms that all start with the name "Test".
## Use methods that return HTTP Info
Each method has two versions - one returns only the response sent back from the endpoint, and the other returns the status and other HTTP info along with the response.
An example of the first method version is the `list_transforms()` method from earlier example. This method returns the response from the endpoint but no status or headers.
This is what the second method version would be: `list_transforms_with_http_info()`. This method will return the response as well as status, headers and raw data from the endpoint.

View File

@@ -0,0 +1,193 @@
---
id: python-sdk
title: Python SDK
pagination_label: Python SDK
sidebar_label: Python
sidebar_position: 4
sidebar_class_name: pythonsdk
keywords: ['python', 'sdk']
description: Learn how to use the Python SDK in this guide.
slug: /tools/sdk/python
tags: ['SDK', 'Software Development Kit']
---
Read this guide to learn how to use the Python SDK. The Python SDK has some pre-built code examples you can use to learn how to build tools that can interact with IdentityNow.
## Requirements
You need the following to use the Python SDK:
- Python version 3.7 or above. You can download it [here](https://www.python.org/downloads/). You can use `python --version` to check your version.
- Your tenant name in IdentityNow. To learn how to find it, refer to [Getting Started](/idn/api/getting-started#find-your-tenant-name). The SDK will use this tenant name to connect to your IdentityNow instance.
- A PAT with a client secret and ID. To learn how to create one in IdentityNow, refer to [Personal Access Tokens](https://documentation.sailpoint.com/saas/help/common/api_keys.html#generating-a-personal-access-token). The SDK will use this PAT to authenticate with the SailPoint APIs.
## Setup
<details>
<summary>CLI Assisted <em>(Recommended)</em></summary>
The SailPoint CLI offers a few commands that will allow you to quickly get started with the Python SDK. To learn how to install and use the SailPoint CLI, refer to [SailPoint CLI](https://developer.sailpoint.com/idn/tools/cli#get-the-cli).
Once the CLI is installed and configured, run this command to create a new Python project with the Python SDK:
```bash
sail sdk init python python-example
```
Running this command will create the structure for your project:
```text
|-- python-example
| |-- requirements.txt
| |-- sdk.py
```
Navigate into your project folder and run this command to install the required dependencies:
```bash
pip install -r requirements.txt
```
The SDK is now installed. To learn how to configure the SDK, refer to the [Configure section](#configure).
</details>
<details>
<summary>Manual Installation</summary>
To begin your Python project, you will need to create a directory for your project.
To create a project directory, run this command:
```bash
mkdir python-example
```
Then run this command to change into your project directory:
```bash
cd python-example
```
To initialize your project and install the SDK, create the "requirements.txt" file with the following line in your project directory:
```text
sailpoint >= 1.0.0
```
To install the SDK and its dependencies, run this command:
```bash
pip install -r requirements.txt
```
The SDK is now installed. To learn how to configure the SDK, refer to the [Configure section](#configure).
</details>
## Configure
You must provide configuration to the SDK so it can authenticate to your SailPoint tenant and make API calls. To do so, you can either use a configuration file, "config.json", or environment variables.
### Configuration File
The SDK requires a configuration file to be named "config.json". Within the file, provide these key/value pairs: `ClientID`, `ClientSecret`, `BaseUrl`.
<details>
<summary>CLI Assisted <em>(Recommended)</em></summary>
The SailPoint CLI offers a command to generate the "config.json" file with your currently configured CLI credentials.
```bash
sail sdk init config
```
If you have multiple environments configured with the CLI, you can pass an additional parameter to state the environment you wish to create a "config.json" for.
To pass an additional parameter that states the environment you want to configure, run this command:
```bash
sail sdk init config --env devrel
```
#### Example "config.json"
```json
{
"ClientID": "g0567b766b413b22c05c66e75d532f1b",
"ClientSecret": "cabd0e950a7230b63c1ff45be33fb22065b382b6251a73c61177a8bb5482fcc7",
"BaseUrl": "https://[tenant].api.identitynow.com"
}
```
</details>
<details>
<summary>Manual Configuration</summary>
Create a file named "config.json", and provide these key/value pairs: `ClientID`, `ClientSecret`, `BaseUrl`.
#### Example "config.json"
```json
{
"ClientID": "g0567b766b413b22c05c66e75d532f1b",
"ClientSecret": "cabd0e950a7230b63c1ff45be33fb22065b382b6251a73c61177a8bb5482fcc7",
"BaseUrl": "https://[tenant].api.identitynow.com"
}
```
</details>
:::warning
Please ensure this file is ignored in your version control system (ex. .gitignore for git)
:::
### Environment variable configuration
You can also store your configuration in environment variables.
To get your environment variables to persist across terminal sessions, add these exports to your shell profile, something like `~/.bash_profile`.
import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';
<Tabs>
<TabItem value="linux_mac" label="Linux/Mac" default>
```bash
export SAIL_BASE_URL=https://[tenant].api.identitynow.com
export SAIL_CLIENT_ID=[clientID]
export SAIL_CLIENT_SECRET=[clientSecret]
```
</TabItem>
<TabItem value="windows" label="Windows PowerShell">
```bash
$env:SAIL_BASE_URL=https://[tenant].api.identitynow.com
$env:SAIL_CLIENT_ID=[clientID]
$env:SAIL_CLIENT_SECRET=[clientSecret]
```
To get your environment variables to persist across PowerShell sessions, run these commands instead:
```powershell
[System.Environment]::SetEnvironmentVariable('SAIL_BASE_URL','https://[tenant].api.identitynow.com')
[System.Environment]::SetEnvironmentVariable('SAIL_CLIENT_ID','[clientID]')
[System.Environment]::SetEnvironmentVariable('SAIL_CLIENT_SECRET','[clientSecret]')
```
</TabItem>
</Tabs>
## Discuss
You can use this SDK to build new tools that extend your IdentityNow platform and improve experiences across your organization. Use this guide to get started, and if you have questions, don't hesitate to reach out on the SailPoint Developer Community forum at https://developer.sailpoint.com/discuss!
## Getting Started
To get started using the SDK, refer to the [Getting Started Guide](./getting-started.md).

View File

@@ -0,0 +1,47 @@
---
id: python-sdk-pagination
title: Paginate Results with The Python SDK
pagination_label: Paginate Results
sidebar_label: Paginate Results
sidebar_position: 5
sidebar_class_name: pythonsdk
keywords: ['python', 'sdk', 'paginate']
description: Learn how to use the Python SDK to paginate results.
slug: /tools/sdk/python/paginate
tags: ['SDK']
---
By default, your requests will return a maximum of 250 records. To return more, you must implement pagination. To learn more about pagination, refer to [Paginating Results](/idn/api/standard-collection-parameters/#paginating-results).
Here is an example of how to implement pagination with the SDK on line 10:
```python showLineNumbers
import sailpoint
import sailpoint.v3
from sailpoint.configuration import Configuration
from sailpoint.paginator import Paginator
configuration = Configuration()
api_client = sailpoint.v3.ApiClient(configuration)
accounts = Paginator.paginate(sailpoint.v3.AccountsApi(api_client).list_accounts, result_limit=1000, limit=250)
print(accounts)
```
Run this command to run the code:
```bash
python sdk.py
```
This example returns 1000 accounts, 250 accounts per page.
The function takes a few parameters: the first is the function you want to invoke the pagination against. The earlier example calls `sailpoint.v3.AccountsApi(api_client).list_accounts` to list accounts.
The `result_limit` specifies the total number of results you can return, 1000. The following limit number, 250, refers to the `increment`, the number of records per page. For example, changing the `result_limit` to 50 and the following "250" to 5 would change the request to return a total of 50 records, 5 per page.
You can also provide an `offset` value to specify which record number to start the request on. For example, you can add `offset=11` to start getting accounts from the 12th record, 11, instead of the first, 0.
To find out whether an endpoint supports pagination, refer to its documentation. Any API supporting pagination lists the optional query parameters detailed in [Paginating Results](/idn/api/standard-collection-parameters/#paginating-results).

View File

@@ -0,0 +1,39 @@
---
id: python-sdk-retries
title: Retries with The Python SDK
pagination_label: Retries
sidebar_label: Retries
sidebar_position: 7
sidebar_class_name: pythonsdk
keywords: ['py', 'python', 'sdk', 'retry']
description: Learn how to configure retries when using the Python SDK.
slug: /tools/sdk/python/retries
tags: ['SDK']
---
The Python SDK uses the [urllib3 retry](https://urllib3.readthedocs.io/en/stable/reference/urllib3.util.html) module to support retry logic.
Here is an example of the retry logic, implemented on line 8. With this configuration, if the returned status code from the API is equal to 502, 503, or 504, the SDK will retry the call up to 5 times:
```python showLineNumbers
import sailpoint
import sailpoint.v3
from sailpoint.configuration import Configuration
from sailpoint.paginator import Paginator
import urllib3
configuration = Configuration()
configuration.retries = urllib3.Retry(total=5, status_forcelist=[ 502, 503, 504 ])
api_client = sailpoint.v3.ApiClient(configuration)
accounts = Paginator.paginate(sailpoint.v3.AccountsApi(api_client).list_accounts, result_limit=1000, limit=250)
print(accounts)
```
Run this command to run the code:
```bash
python sdk.go
```

View File

@@ -0,0 +1,54 @@
---
id: python-sdk-search
title: Search with The Python SDK
pagination_label: Search
sidebar_label: Search
sidebar_position: 6
sidebar_class_name: pythonsdk
keywords: ['py', 'python', 'sdk', 'search']
description: Learn how to use the Python SDK to search.
slug: /tools/sdk/python/search
tags: ['SDK']
---
One of the most useful functionalities you can access with the Python SDK is IdentityNow's [search functionality](/idn/api/v3/search-post).
Here is an example of how you can implement Search, along with pagination. Copy this code into your "sdk.py" file to try it out:
```python
import sailpoint
import sailpoint.v3
from sailpoint.configuration import Configuration
from sailpoint.paginator import Paginator
configuration = Configuration()
api_client = sailpoint.v3.ApiClient(configuration)
search = sailpoint.v3.Search(
indices=['identities'],
query=sailpoint.v3.Query(query='*'),
sort=['-name'])
identities = Paginator.paginate_search(sailpoint.v3.SearchApi(api_client), search, increment=100, limit=1000)
print(identities.count)
```
Run this command to run the code:
```bash
python sdk.py
```
This example returns 1000 identities, 100 per page, and sorts them in descending order by name (`'-name'`). You can also change the search pagination by changing "100" and "1000", respectively.
There are two main ways you can manipulate this example to search for the results you want:
The first way is to change the `indices`, the document types you want to limit your search to. For example, if you add `"access profiles"` to the indices, the SDK will search access profiles too. To see all the indices you can search, refer to the [Search endpoint specification](/idn/api/v3/search-post).
The second way is to change the `query`, the value you're searching for. For example, if you change the query to "a*", the search will return all records starting with the letter "a". To learn more about how to build search queries, refer to [Building a Search Query](https://documentation.sailpoint.com/saas/help/search/building-query.html).
You can also change the sorting logic in the brackets next to `sort`. For more information about sorting results, refer to [Sorting Results](/idn/api/standard-collection-parameters/#sorting-results).

View File

@@ -0,0 +1,73 @@
---
id: python-sdk-patch
title: Updating resources with The Python SDK
pagination_label: Update a resource
sidebar_label: Update a resource
sidebar_position: 3
sidebar_class_name: pythonsdk
keywords: ['python', 'sdk', 'update']
description: Learn how to use the Python SDK to update resources.
slug: /tools/sdk/python/update
tags: ['SDK']
---
You can use the SDK to update resources.
For example, you can run a script to update a work group, also known as a [governance group](https://documentation.sailpoint.com/saas/help/common/users/governance_groups.html).
This example 'update WorkGroup' script updates the description for the work group created in [Create a Resource](./creating-resources.md). Copy it into your Python project to try it out:
```python
import sailpoint
import sailpoint.v3
import sailpoint.beta
from sailpoint.beta.models.workgroup_dto import WorkgroupDto
from sailpoint.beta.models.usage_type import UsageType
from sailpoint.beta.models.owner_dto import OwnerDto
from sailpoint.beta.models.json_patch_operation import JsonPatchOperation
from sailpoint.beta.models.json_patch_operation_value import JsonPatchOperationValue
from sailpoint.configuration import Configuration
configuration = Configuration()
api_client = sailpoint.v3.ApiClient(configuration)
api_client_beta = sailpoint.beta.ApiClient(configuration)
identities_api_instance = sailpoint.v3.PublicIdentitiesApi(api_client)
workgroups_api_instance = sailpoint.beta.GovernanceGroupsApi(api_client_beta)
workgroup = workgroups_api_instance.list_workgroups(filters='name eq "DB Access Governance Group"')[0]
json_patch_operation = [JsonPatchOperation(op='replace', path='/description', value=JsonPatchOperationValue('This is an updated description for the workgroup.'))]
try:
workgroupResponse = workgroups_api_instance.patch_workgroup(workgroup.id,json_patch_operation=json_patch_operation)
print("The response of GovernanceGroupsApi->patch_workgroup:\n")
print(workgroupResponse)
except Exception as e:
print("Exception when calling GovernanceGroupsApi->patch_workgroup: %s\n" % e)
```
Run this command to run the code:
```bash
python sdk.go
```
The example uses a PATCH `replace` operation to update the value in the `/description` path to "This is an updated description for the workgroup."
The SDK will return the updated work group with its new description:
```python
The response of GovernanceGroupsApi->patch_workgroup:
id='d287a1e2-81fc-474e-bc0c-155bd8ab0899'
name='DB Access Governance Group'
description='This is an updated description for the workgroup.'
member_count=0
connection_count=0
owner=OwnerDto(
type='IDENTITY',
id='0003c25c365e492381d4e557b6159f9b',
name='Brian Mendoza')
```