Skip to content

Commit

Permalink
Merge pull request #56 from daveRendon/staging
Browse files Browse the repository at this point in the history
  • Loading branch information
daveRendon authored Oct 30, 2024
2 parents fb8db60 + 8e5ef7e commit 26a608d
Show file tree
Hide file tree
Showing 6 changed files with 466 additions and 2 deletions.
4 changes: 2 additions & 2 deletions docs/aix/code/get-inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

# Your Skytap "Login name" from the Skytap Portal and API token
login_name='your_login_name'
API_token='your_API_token'
API_token='your-api-token'

base_url = 'https://cloud.skytap.com/'
auth_sky = (login_name,API_token)
Expand All @@ -15,7 +15,7 @@

# Replace with your specific environment (configuration) ID
# Example: https://cloud.skytap.com/configurations/159726802?section=vms&sort=name&thumbnails=shown
environment_id = 'your_environment_id'
environment_id = '159726802'

# Skytap API endpoint to get the environment details
url = f'https://cloud.skytap.com/v2/configurations/{environment_id}'
Expand Down
100 changes: 100 additions & 0 deletions docs/aix/code/get-storage-details.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import requests
from requests.auth import HTTPBasicAuth

# Your Skytap "Login name" from the Skytap Portal and API token
login_name = 'your_login_name'
API_token = 'your-api-token'

base_url = 'https://cloud.skytap.com'
auth_sky = (login_name, API_token)
headers = {
'Accept': 'application/json',
'Content-Type': 'application/json'
}

# Replace with your specific environment (configuration) ID
ENVIRONMENT_ID = '159726802'

# Skytap API endpoint to get the environment details
url = f'{base_url}/v2/configurations/{ENVIRONMENT_ID}'

def get_environment(environment_id):
"""
Retrieve the details of the specified environment.
"""
response = requests.get(url, headers=headers, auth=auth_sky)
response.raise_for_status()
return response.json()

def get_vms_in_environment(environment):
"""
Retrieve VMs (LPARs) in the specified environment.
"""
return environment.get('vms', [])

def get_disks_for_vm(vm_data):
"""
Retrieve disks associated with the specified VM.
"""
return vm_data.get('hardware', {}).get('disks', [])

def get_multi_attach_storage(environment):
"""
Retrieve multi-attach storage configuration from the environment.
"""
return environment.get('multi_attach_storage_groups', [])

def main():
try:
# Retrieve environment details
environment = get_environment(ENVIRONMENT_ID)
print(f"Environment ID: {environment['id']}")
print(f"Environment Name: {environment['name']}")

# Retrieve VMs (LPARs) in the environment
vms = get_vms_in_environment(environment)
if not vms:
print("No VMs found in the environment.")
return

for vm in vms:
print(f"\nVM ID: {vm['id']}")
print(f"VM Name: {vm['name']}")

# Retrieve disks for each VM
disks = get_disks_for_vm(vm)
if disks:
for disk in disks:
print(f" Disk ID: {disk['id']}, Size: {disk['size']} GB, Type: {disk['type']}")
else:
print(" No disks found for this VM.")

# Display network interface details
for interface in vm.get('interfaces', []):
print(f" Interface ID: {interface['id']}, IP: {interface['ip']}, Hostname: {interface['hostname']}")

# Retrieve and print multi-attach storage configurations in the specified format
multi_attach_storage = get_multi_attach_storage(environment)
if multi_attach_storage:
print("\nMulti-Attach Storage:")
for storage in multi_attach_storage:
print(f" Storage Group ID: {storage['id']}, Name: {storage['name']}, Configuration ID: {storage['configuration_id']}")
for allocation in storage.get('storage_allocations', []):
print(f" Allocation ID: {allocation['id']}, Size: {allocation['size']} MB")
for attachment in allocation.get('disk_attachments', []):
# Check for 'vm_key' and other fields to avoid missing key errors
bus_type = attachment.get('bus_type', 'N/A')
bus_id = attachment.get('bus_id', 'N/A')
lun = attachment.get('lun', 'N/A')
vm_key = attachment.get('vm_key', 'N/A')
print(f" Bus Type: {bus_type}, Bus ID: {bus_id}, LUN: {lun}, VM Key: {vm_key}")
else:
print("No multi-attach storage configurations found.")

except requests.exceptions.HTTPError as http_err:
print(f"HTTP error occurred: {http_err}")
except Exception as err:
print(f"An error occurred: {err}")

if __name__ == "__main__":
main()
142 changes: 142 additions & 0 deletions modules/create-multi-attach-storage-copy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
import requests
from requests.auth import HTTPBasicAuth
import uuid

# Skytap API credentials
login_name = 'your-login-name'
API_token = 'your-api-token'

base_url = 'https://cloud.skytap.com'
auth_sky = HTTPBasicAuth(login_name, API_token)
headers = {
'Accept': 'application/json',
'Content-Type': 'application/json'
}

# Replace this with your Environment (configuration) ID
ENVIRONMENT_ID = '159726802'

def get_environment(environment_id):
url = f'{base_url}/v2/configurations/{environment_id}'
response = requests.get(url, headers=headers, auth=auth_sky)
if response.status_code == 404:
print("Error 404: Environment not found.")
return None
response.raise_for_status()
return response.json()

def get_multi_attach_storage(environment):
return environment.get('multi_attach_storage_groups', [])

def create_multi_attach_storage(storage):
unique_name = f"{storage['name']}-copy-{uuid.uuid4()}"

payload = {
'name': unique_name,
'configuration_id': storage['configuration_id'],
'hypervisor': storage['hypervisor'],
'vm_attachments': storage.get('vm_attachments', [])
}

post_url = f"{base_url}/v2/configurations/{ENVIRONMENT_ID}/multi_attach_storage_groups"
print(f"Creating multi-attach storage group with payload:\n{payload}")
response = requests.post(post_url, headers=headers, auth=auth_sky, json=payload)
if response.status_code == 404:
print("Error 404: Multi-attach storage group creation endpoint not found.")
return None
response.raise_for_status()
return response.json()

def add_storage_allocations(new_storage_id, storage_allocations):
allocation_url = f"{base_url}/v2/multi_attach_storage_groups/{new_storage_id}/storage_allocations"
allocation_ids = []

for allocation in storage_allocations:
allocation_payload = {
"spec": {
"volume": [allocation['size']]
}
}

print(f"\nSending storage allocation payload for {new_storage_id}:\n{allocation_payload}")

response = requests.post(allocation_url, headers=headers, auth=auth_sky, json=allocation_payload)
if response.status_code == 404:
print("Error 404: Storage allocation endpoint not found.")
continue
response.raise_for_status()

added_allocation = response.json()
allocation_id = added_allocation.get('id')
allocation_size = added_allocation.get('size', 'Unknown size')
if allocation_id:
print(f" Added Allocation ID: {allocation_id}, Size: {allocation_size} MB")
allocation_ids.append(allocation_id)
else:
print(" Warning: Allocation ID missing in response.")

for attachment in allocation.get('disk_attachments', []):
add_disk_attachment(new_storage_id, allocation_id, attachment)

return allocation_ids

def add_disk_attachment(storage_id, allocation_id, attachment):
attachment_url = f"{base_url}/v2/multi_attach_storage_groups/{storage_id}/storage_allocations/{allocation_id}/disk_attachments"
attachment_payload = {
'controller': attachment['controller'],
'bus_type': attachment['bus_type'],
'bus_id': attachment['bus_id'],
'lun': attachment['lun']
}
print(f"Adding disk attachment with payload:\n{attachment_payload}")

response = requests.post(attachment_url, headers=headers, auth=auth_sky, json=attachment_payload)
if response.status_code == 404:
print("Error 404: Disk attachment endpoint not found.")
return None
response.raise_for_status()

added_attachment = response.json()
print(f" Added Disk Attachment ID: {added_attachment['id']}")
return added_attachment

def main():
try:
environment = get_environment(ENVIRONMENT_ID)
if environment is None:
print("Environment could not be retrieved.")
return

print(f"Environment ID: {environment['id']}")
print(f"Environment Name: {environment['name']}")

multi_attach_storage = get_multi_attach_storage(environment)
if multi_attach_storage:
print("\nMulti-Attach Storage:")
for storage in multi_attach_storage:
print(f" Original Storage Group ID: {storage['id']}, Name: {storage['name']}, Configuration ID: {storage['configuration_id']}")

copied_storage = create_multi_attach_storage(storage)
if copied_storage is None:
print("Copy of multi-attach storage group could not be created.")
continue

print(f" Copied Storage Group ID: {copied_storage['id']}, Name: {copied_storage['name']}")

# Add storage allocations and disk attachments, and print results
copied_allocation_ids = add_storage_allocations(copied_storage['id'], storage.get('storage_allocations', []))
print(f" Completed copying storage allocations for group ID {copied_storage['id']}. Allocations copied: {copied_allocation_ids}")

else:
print("No multi-attach storage configurations found.")

except requests.exceptions.HTTPError as http_err:
if http_err.response.status_code == 404:
print("HTTP 404 Error: Resource not found.")
else:
print(f"HTTP error occurred: {http_err}")
except Exception as err:
print(f"An error occurred: {err}")

if __name__ == "__main__":
main()
88 changes: 88 additions & 0 deletions modules/get-inventory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import requests
from requests.auth import HTTPBasicAuth
import json

# Your Skytap "Login name" from the Skytap Portal and API token
login_name='your_login_name'
API_token='your-api-token'

base_url = 'https://cloud.skytap.com/'
auth_sky = (login_name,API_token)
headers = {
'Accept': 'application/json',
'Content-Type': 'application/json'
}

# Replace with your specific environment (configuration) ID
# Example: https://cloud.skytap.com/configurations/159726802?section=vms&sort=name&thumbnails=shown
environment_id = '159726802'

# Skytap API endpoint to get the environment details
url = f'https://cloud.skytap.com/v2/configurations/{environment_id}'

response = requests.get(url, headers=headers,auth=auth_sky)

# Check if the request was successful
if response.status_code == 200:
environment_data = response.json()
print('Environment Details:')
# Pretty print the entire environment data for debugging
# print(json.dumps(environment_data, indent=2))

# Extract the VMs (LPARs) from the environment data
vms = environment_data.get('vms', [])

# Initialize markdown table with EC column
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')

# Convert memory from MB to GB
memory_gb = memory_mb / 1024 if memory_mb != 'N/A' else 'N/A'

# Calculate total storage in GB
storage_gb = 0
for disk in hardware.get('disks', []):
storage_gb += disk.get('size', 0) / 1024

# Extract Entitled Capacity (EC)
entitled_capacity = settings.get('entitled_capacity', {}).get('current', 0)

# Update totals
total_vcpus += vcpus
total_memory_gb += memory_gb
total_storage_gb += storage_gb
total_entitled_capacity += entitled_capacity

# Append row to markdown table
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"

# Convert total storage from GB to TB
total_storage_tb = total_storage_gb / 1024

# Append totals to the markdown table
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 the markdown table
print(markdown_table)
else:
print('Failed to retrieve environment details')
print('Status Code:', response.status_code)
print('Response:', response.text)
Loading

0 comments on commit 26a608d

Please sign in to comment.