Created
March 2, 2023 20:44
-
-
Save kaugm/e8a70c73a6b7247846b5c05b5766f128 to your computer and use it in GitHub Desktop.
Export Spot.io Ocean Rightsizing Recommendations to CSV
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/opt/homebrew/bin/python3 | |
try: | |
import requests | |
import os | |
import json | |
import re | |
import csv | |
from datetime import datetime | |
except ModuleNotFoundError: | |
print("Please ensure proper modules are installed.\npip install requests") | |
os._exit(1) | |
# Get Required Variables for Authentication | |
TOKEN = os.environ.get('SPOTINST_TOKEN') | |
ACCOUNT = os.environ.get('SPOTINST_ACCOUNT_AWS') | |
if not (TOKEN and ACCOUNT): | |
print(f"Please set environment variables for token and account id.\n") | |
os._exit(1) | |
class Endpoint: | |
def __init__(self, url, body=None): | |
'''API Endpoint. Account and Token set by default''' | |
self.url = url | |
self.body = body | |
self.query_params = dict({TOKEN: ACCOUNT}) | |
self.headers = dict({ | |
'User-Agent': '_Karl_Reverse_Engineered_SDK_Agent', | |
'Content-Type': 'application/json', | |
'Authorization': 'Bearer ' + TOKEN | |
}) | |
def _POST(self): | |
'''Send a POST request''' | |
print(f"Sending POST request to {self.url}...\n") | |
result = requests.post(self.url, params=self.query_params, data=self.body, headers=self.headers) | |
if result.status_code == requests.codes.ok: | |
data = json.loads(result.content.decode('utf-8')) | |
return data['response']['items'] | |
else: | |
print(f"Error: {result.reason}\n\n{result.text}\n") | |
return 0 | |
if __name__ == "__main__": | |
# Variables | |
FILE_SUFFIX = datetime.now().strftime('%Y-%m-%d') | |
oceanClusterId = '' # Example: o-12345678 | |
# Export: Add headers to CSV columns | |
with open(f"{oceanClusterId}_rightsizing_recommendations_{FILE_SUFFIX}.csv", 'a') as file: | |
writer = csv.writer(file) | |
writer.writerow(['Deployment Name', 'Resource Name', 'Resource Type', 'Namespace', 'Suggested CPU', 'Suggested Memory', 'Requested CPU', 'Requested Memory', 'Containers']) | |
# Get Rightsizing Suggestions - POST | |
__base_rightsizing_url = f'https://api.spotinst.io/ocean/aws/k8s/cluster/{oceanClusterId}/rightSizing/suggestion' | |
# body = {} # Filtering is optional, see https://docs.spot.io/api/ | |
spot_api_call = Endpoint(f"{__base_rightsizing_url}") | |
rightsizing_suggestions = spot_api_call._POST() | |
# Export: Recommendations to rows | |
for rec in rightsizing_suggestions: | |
_deployment_name = rec['deploymentName'] | |
_resource_name = rec['resourceName'] | |
_resource_type = rec['resourceType'] | |
_namespace = rec['namespace'] | |
_suggested_cpu = rec['suggestedCPU'] | |
_suggested_memory = rec['suggestedMemory'] | |
_requested_cpu = rec['requestedCPU'] | |
_requested_memory = rec['requestedMemory'] | |
_containers = rec['containers'] | |
with open(f"{oceanClusterId}_rightsizing_recommendations_{FILE_SUFFIX}.csv", 'a') as file: | |
writer = csv.writer(file) | |
writer.writerow([_deployment_name, _resource_name, _resource_type, _namespace, _suggested_cpu, _suggested_memory, _requested_cpu, _requested_memory, _containers]) | |
print(f"Rightsizing recommendations for {oceanClusterId} have been exported to file: {oceanClusterId}_rightsizing_recommendations_{FILE_SUFFIX}.csv\n\n") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment