Skip to content

Update skytap-get-inventory.yml #4

Update skytap-get-inventory.yml

Update skytap-get-inventory.yml #4

name: SkytapApiGetInventory
# Trigger the workflow on push to the main branch
on:
push:
branches:
- main
jobs:
run-python-script:
runs-on: ubuntu-latest
steps:
# Step 1: Checkout the repository
- name: Checkout repository
uses: actions/checkout@v3
# Step 2: Set up Python environment
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.x' # Specify the Python version you need
# Step 3: Install required dependencies
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install requests
# Step 4: Run the Python script
- name: Run Skytap API Script
env:
LOGIN_NAME: ${{ secrets.SKYTAP_LOGIN_NAME }}
API_TOKEN: ${{ secrets.SKYTAP_API_TOKEN }}
ENVIRONMENT_ID: ${{ secrets.SKYTAP_ENVIRONMENT_ID }}
run: |
python <<EOF
import os
import requests
import json
login_name = os.getenv('LOGIN_NAME')
API_token = os.getenv('API_TOKEN')
environment_id = os.getenv('ENVIRONMENT_ID')
base_url = 'https://cloud.skytap.com/'
auth_sky = (login_name, API_token)
headers = {
'Accept': 'application/json',
'Content-Type': 'application/json'
}
url = f'https://cloud.skytap.com/v2/configurations/{environment_id}'
response = requests.get(url, headers=headers, auth=auth_sky)
if response.status_code == 200:
environment_data = response.json()
print('Environment Details:')
vms = environment_data.get('vms', [])
markdown_table = '| VM ID | Name | Status | OS | vCPUs | Memory (GB) | Storage (GB) | EC |\n'
markdown_table += '|------------------|--------------------|------------|---------------------|-------|-------------|--------------|--------|\n'
total_vcpus = 0
total_memory_gb = 0
total_storage_gb = 0
total_entitled_capacity = 0
for vm in vms:
vm_id = vm.get('id', 'N/A')
name = vm.get('name', 'N/A')
status = vm.get('runstate', 'N/A')
hardware = vm.get('hardware', {})
settings = hardware.get('settings', {})
os = hardware.get('guestOS', 'N/A')
vcpus = settings.get('cpus', {}).get('current', 'N/A')
memory_mb = settings.get('ram', {}).get('current', 'N/A')
memory_gb = memory_mb / 1024 if memory_mb != 'N/A' else 'N/A'
storage_gb = sum(disk.get('size', 0) / 1024 for disk in hardware.get('disks', []))
entitled_capacity = settings.get('entitled_capacity', {}).get('current', 0)
total_vcpus += vcpus
total_memory_gb += memory_gb
total_storage_gb += storage_gb
total_entitled_capacity += entitled_capacity
markdown_table += f'| {vm_id:<16} | {name:<18} | {status:<10} | {os:<19} | {vcpus:<5} | {memory_gb:<11.2f} | {storage_gb:<12.2f} | {entitled_capacity:<6.2f} |\n'
total_storage_tb = total_storage_gb / 1024
markdown_table += '|------------------|--------------------|------------|---------------------|-------|-------------|--------------|--------|\n'
markdown_table += f'| {"TOTAL":<16} | {"":<18} | {"":<10} | {"":<19} | {total_vcpus:<5} | {total_memory_gb:<11.2f} | {total_storage_tb:<9.2f} TB | {total_entitled_capacity:<6.2f} |\n'
print(markdown_table)
else:
print('Failed to retrieve environment details')
print('Status Code:', response.status_code)
print('Response:', response.text)
EOF