Skip to content

Instantly share code, notes, and snippets.

@kaugm
Last active January 10, 2023 21:25
Show Gist options
  • Save kaugm/01e698463c4c187d2af2033fccba1f0c to your computer and use it in GitHub Desktop.
Save kaugm/01e698463c4c187d2af2033fccba1f0c to your computer and use it in GitHub Desktop.
Spot.io Ocean - Get Cluster Costs - Python SDK Example
#!/opt/homebrew/bin/python3
import os
from datetime import datetime
try:
from spotinst_sdk2 import SpotinstSession
from spotinst_sdk2.models.ocean.aws import *
except ModuleNotFoundError:
print('Spotinst SDK Library not installed. Exiting..')
os._exit(1)
# Get Required Variables for Authentication
TOKEN = os.environ.get('SPOTINST_TOKEN')
ACCOUNT = os.environ.get('SPOTINST_ACCOUNT')
# Variables
NOW = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
START_OF_MONTH = datetime.now().replace(day=1, hour=0, minute=0, second=0).strftime('%Y-%m-%d %H:%M:%S')
if __name__ == "__main__":
# Initialize Client
session = SpotinstSession()
OceanAwsClient = session.client("ocean_aws")
# Set Variables for AggregatedClusterCosts Object
ocean_id = 'o-656f9955'
start = START_OF_MONTH
end = NOW
grouping_type = 'namespace' # DEFAULTS TO 'None'
# aggregated_filter = '' # DEFAULTS TO 'None'
filter_object = AggregatedClusterCosts(start_time=start, end_time=end, group_by=grouping_type, aggregated_filter=None)
try:
# Data of Type '<class 'dict'>
data = OceanAwsClient.get_aggregated_cluster_costs(ocean_id, filter_object)
except spotinst_sdk2.client.SpotinstClientException as error:
print(f"Error: Please check to make sure the Ocean Cluster in question is in the in the account: {ACCOUNT}.\n\n{error}")
os._exit(1)
# Output
print(data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment