Skip to content

Commit

Permalink
feat(added new scripts): added a script for resources
Browse files Browse the repository at this point in the history
  • Loading branch information
grantcurell committed Dec 11, 2024
1 parent eff049b commit e55bfd4
Show file tree
Hide file tree
Showing 13 changed files with 495 additions and 63 deletions.
26 changes: 24 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,32 @@
# DTIAS Examples
# Dell TELCO Infrastructure Automation Suite (DTIAS) Examples

- [Dell TELCO Infrastructure Automation Suite (DTIAS) Examples](#dell-telco-infrastructure-automation-suite-dtias-examples)
- [What is DTIAS?](#what-is-dtias)
- [About This Repository](#about-this-repository)
- [Useful Links](#useful-links)
- [Contributing](#contributing)
- [DTIAS Installation](#dtias-installation)
- [Install DTIAS on Ubuntu 20.04](#install-dtias-on-ubuntu-2004)
- [Install DTIAS on RHEL](#install-dtias-on-rhel)

## What is DTIAS?

DTIAS is designed to handle environments with large numbers of heterogenous servers. You don't need to be a TELCO to use it, but this is a core problem TELCOs face. DTIAS supports both HPE and Dell and has the ability to perform full lifecycle management of the servers. It has built in functionality to take a server from bare metal to with a few clicks standardized BIOS settings, up to date firmware, and a deployed and configured operating system.

## About This Repository

This repository is dedicated to demonstrating the capabilities of Dell DTIAS. It has [API usage examples](./dtias_api_examples/) which show off how to control DTIAS through the API and a [blueprint repository](./blueprints/) with a host of useful, prebuilt, blueprints you can deploy.

## Useful Links

- [DTIAS REST API Documentation](https://developer.dell.com/apis/7ce7d11a-ff9c-47cc-b958-bd30dc9770f0/versions/2.1.0/docs/introduction.md)
- Note: DTIAS has recently merged with Cloudify. If you notice discrepancies in the API documentation feel free to open a ticket on this repository.

## Contributing

For required coding style and structure guidelines see [CONTRIBUTING.md](./CONTRIBUTING.md)

## Installation
## DTIAS Installation

```bash
sudo dnf install epel-release -y
Expand Down
2 changes: 1 addition & 1 deletion dtias-examples/readme.md → blueprints/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ The playbook_source_url should be the URL to your uploaded playbook package and
## Step 3: Create Deployment

Create a deployment using the uploaded blueprint via DTIAS REST API (refer to [DTIAS API usage guide](https://stoplight.dell.com/docs/fulcrum-rest-api-guide/d6a8ba3f3c186-introduction-dell-telecom-infrastructure-automation-foundation-1-1-rest-ap-is) on syntax):
POST https://dtiaf_ip/v1/tenants/default/deployments. Include your input values from `bp-inputs.yaml` inside the request body. After the deployment succeeds, you should see the desired effect of your Ansible playbook on your specified remote host.
POST https://dtiaf_ip/v1/tenants/default/deployments. Include your input values from `bp-inputs.yaml` inside the request body. After the deployment succeeds, you should see the desired effect of your Ansible playbook on your specified remote host.
55 changes: 0 additions & 55 deletions dtias-examples/api_examples/get_ditias_token.py

This file was deleted.

3 changes: 0 additions & 3 deletions dtias-examples/api_examples/notes.md

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,3 +1,32 @@
"""
#### Synopsis
Script to retrieve and apply hardware profiles on servers using DTIAS
#### Description
This script uses the DTIAS REST API to:
1. Authenticate with the server and generate an authentication token.
2. Retrieve the list of available hardware profiles.
3. Apply a specified hardware profile to a given list of servers.
The script disables SSL warnings for ease of use in environments with self-signed certificates. It requires the user
to specify the target hardware profile and the servers on which it should be applied.
#### Python Example
```bash
python apply_hardware_profile.py --server_ip <ip addr> --tenant_id <tenant> --username <username> \
--password <password> --profile_name <profile name> --servers <server1,server2>
```
where:
- server_ip is the IP address of the DTIAS server.
- tenant_id is the tenant name (default: Fulcrum).
- username and password are the credentials for authentication.
- profile_name specifies the name of the hardware profile to apply.
servers is a comma-separated list of server names to apply the profile to.
"""

import argparse
import requests
import urllib3
Expand Down Expand Up @@ -124,10 +153,10 @@ def apply_hardware_profile(server_ip, id_token, profile, servers):
if __name__ == "__main__":
# Set up argparse for command-line argument parsing
parser = argparse.ArgumentParser(description="Retrieve and apply hardware profiles.")
parser.add_argument("--server_ip", default="172.21.0.215", required=True,
parser.add_argument("--server_ip", required=True,
help="The IP address of the DTIAS server.")
parser.add_argument("--tenant_id", default="Fulcrum", help="The tenant ID (default: Fulcrum).")
parser.add_argument("--username", default="admin", required=True, help="The username to authenticate with.")
parser.add_argument("--username", required=True, help="The username to authenticate with.")
parser.add_argument("--password", required=True, help="The password for the username.")
parser.add_argument("--profile_name", required=True,
help="The name of the hardware profile.")
Expand Down
140 changes: 140 additions & 0 deletions dtias_api_examples/get_ditias_token.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
"""
#### Synopsis
Script to authenticate with DTIAS and retrieve resources.
#### Description
This script performs the following actions:
1. Authenticates with the DTIAS REST API using provided credentials.
2. Retrieves a list of resources based on optional filters and pagination parameters.
The script allows flexible filtering and pagination for resource retrieval, enabling efficient querying.
#### Python Example
```bash
python get_resources.py --server_ip <ip addr> --tenant_id <tenant> --username <username> \
--password <password> --filters '{"Key": "Value"}' --pagination '{"offset": 0, "limit": 10}'
where:
- server_ip is the IP address of the DTIAS server.
- tenant_id is the tenant name (default: Fulcrum).
- username and password are the credentials for authentication.
- filters specifies query filters as a JSON string (optional).
- pagination specifies pagination parameters as a JSON string (optional).
"""

import argparse
import requests
import urllib3

# Disable SSL warnings globally
urllib3.disable_warnings()

def create_token(server_ip, tenant_id, username, password):
"""
Creates a token for the DTIAS server.
Parameters:
server_ip (str): The IP address of the DTIAS server.
tenant_id (str): The tenant ID (e.g., "Fulcrum").
username (str): The username to authenticate with.
password (str): The password for the username.
Returns:
dict: A dictionary containing the access token, id token, and refresh token.
"""
url = f"https://{server_ip}/identity/v1/tenant/{tenant_id}/token/create"
headers = {"Content-Type": "application/json"}
data = {
"grant_type": "password",
"client_id": "ccpapi",
"username": username,
"password": password
}

try:
response = requests.post(url, headers=headers, json=data, verify=False) # Set verify=False for self-signed certs.
response.raise_for_status() # Raise an error for HTTP codes 4xx/5xx.
return response.json() # Return the JSON response with tokens.
except requests.exceptions.RequestException as e:
print(f"Error: {e}")
return None

def get_resources(server_ip, tenant_id, id_token, filters=None, pagination=None):
"""
Retrieves resources from the DTIAS server.
Parameters:
server_ip (str): The IP address of the DTIAS server.
tenant_id (str): The tenant ID (e.g., "Fulcrum").
id_token (str): The ID token for authentication.
filters (list): List of filters for querying resources (optional).
pagination (dict): Pagination parameters such as offset and limit (optional).
Returns:
dict: Resources data.
"""
url = f"https://{server_ip}/v1/tenants/{tenant_id}/search/resources"
headers = {
"accept": "application/json, text/plain, */*",
"authorization": f"Bearer {id_token}",
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36"
}
data = {}
if filters:
data["Filters"] = filters
if pagination:
data.update(pagination)

try:
response = requests.post(url, headers=headers, json=data, verify=False)
response.raise_for_status()
return response.json()
except requests.exceptions.RequestException as e:
print(f"Error retrieving resources: {e}")
return None

if __name__ == "__main__":
# Set up argparse
parser = argparse.ArgumentParser(description="Retrieve resources from the DTIAS server.")
parser.add_argument("--server_ip", required=True, help="The IP address of the DTIAS server.")
parser.add_argument("--tenant_id", default="Fulcrum", help="The tenant ID (default: Fulcrum).")
parser.add_argument("--username", required=True, help="The username to authenticate with.")
parser.add_argument("--password", required=True, help="The password for the username.")
parser.add_argument("--filters", help="Filters for querying resources as JSON string (optional).", default=None)
parser.add_argument("--pagination", help="Pagination parameters as JSON string (optional).", default=None)

# Parse arguments
args = parser.parse_args()

# Generate the token
tokens = create_token(args.server_ip, args.tenant_id, args.username, args.password)
if not tokens:
print("Failed to generate token. Exiting...")
exit(1)

id_token = tokens.get("id_token")

# Step 2: Retrieve resources
filters = None
if args.filters:
try:
filters = eval(args.filters) # Safely parse JSON-like string
except Exception as e:
print(f"Error parsing filters: {e}")
exit(1)

pagination = None
if args.pagination:
try:
pagination = eval(args.pagination) # Safely parse JSON-like string
except Exception as e:
print(f"Error parsing pagination: {e}")
exit(1)

resources_data = get_resources(args.server_ip, args.tenant_id, id_token, filters, pagination)
if resources_data:
print("Resources Retrieved Successfully:")
print(resources_data)
else:
print("Failed to retrieve resources.")
Original file line number Diff line number Diff line change
@@ -1,6 +1,29 @@
"""
#### Synopsis
Script to retrieve hardware profiles from DTIAS
#### Description
This script uses the DTIAS REST API to retrieve a list of hardware profiles. It authenticates using a token obtained
via the API and retrieves the profiles associated with the specified tenant.
#### Python Example
```bash
python get_hardware_profiles.py --server_ip <ip addr> --tenant_id <tenant> --username <username> --password <password>
```
where:
- server_ip is the IP address of the DTIAS server.
- tenant_id is the tenant name (default: metalweaver).
- username and password are the credentials for authentication.
"""

import argparse
import requests
import urllib3

# Disable SSL warnings globally
urllib3.disable_warnings()

def create_token(server_ip, tenant_id, username, password):
"""
Expand Down
Loading

0 comments on commit e55bfd4

Please sign in to comment.