This script is designed to restore deleted entities in Port using data from audit logs. It fetches logs for specific blueprints (e.g., sonarQubeProject
, pagerdutyService
, newRelicAlert
) within a specified timeframe and restores the entities that were deleted.
- Fetches audit logs from the PORT API for specific blueprints.
- Restores deleted entities based on the fetched logs.
- Logs the process for easy debugging.
- Python 3.11+
httpx
for making asynchronous HTTP requestsloguru
for loggingasyncio
for asynchronous programmingpython-decouple
for loading configuration variables
-
Clone the repository:
git clone https://github.com/port-labs/restore-port-data.git cd restore-port-data
-
Create a virtual environment (optional but recommended):
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
-
Install the dependencies:
pip install -r requirements.txt
If you don't have a
requirements.txt
, you can manually install the dependencies:pip install httpx loguru python-decouple
The script uses environment variables for data like API URLs and tokens. You can set up a .env
file or export the variables manually.
PORT_API_URL
: Base URL of Port API. Default is https://api.getport.io/v1PORT_CLIENT_ID
: Port client ID for authenticationPORT_CLIENT_SECRET
: Port client secret for authenticationPORT_API_TIMEOUT
: Timeout in seconds. Default is 300BLUEPRINT_IDENTIFIERS
: A comma separated list of blueprint identifiers to restore data forDAYS_TO_RECOVER
: Number of days to look back when fetching audit logs. Default is 1 day
To set these variables, you can create a .env
file in the project directory:
PORT_API_URL=https://api.getport.io/v1
PORT_CLIENT_ID=your_client_id
PORT_CLIENT_SECRET=your_client_secret
BLUEPRINT_IDENTIFIERS=your_blueprint_identifier_1,your_blueprint_identifier_2
Run the script with the following command:
python app.py
The script has basic error handling with logging for the following common issues:
- ReadTimeout: If the request to fetch audit logs times out, the error is logged. You may consider increasing the
PORT_API_TIMEOUT
timeout duration. - Authorization Error: Ensure your access token is valid. If it expires, the script will need to be restarted.
2024-11-15 15:11:42.671 | INFO | __main__:restore_deleted_entities:83 - Starting the process to restore deleted entities...
2024-11-15 15:11:43.325 | INFO | __main__:fetch_audit_logs:37 - Fetching audit logs for blueprint 'sonarQubeProject'...
2024-11-15 15:12:43.929 | ERROR | __main__:restore_deleted_entities:118 - An error occurred: Expecting value: line 1 column 1 (char 0)