Skip to content

Instantly share code, notes, and snippets.

@kaugm
Created March 23, 2023 21:26
Show Gist options
  • Save kaugm/514b3c15b444ea2eb74bd76afc769ecf to your computer and use it in GitHub Desktop.
Save kaugm/514b3c15b444ea2eb74bd76afc769ecf to your computer and use it in GitHub Desktop.
Call the Spot.io API with a shell script
#!/usr/bin/env bash
# This script shows how to call the Spot.io API via a shell script and contains 4 examples for HTTP methods: GET, POST, PUT, and DELETE
# Requires jq
# Written by Karl Martin
# [Optional] Set Spot token and account manually. Currently set as environment variables
# SPOTINST_TOKEN=''
# SPOTINST_ACCOUNT_AWS=''
# Parsing the response with jq
# For more options, visit https://stedolan.github.io/jq/manual/#Basicfilters
# echo $response | jq -r '.request' # Request metadata
# echo $response | jq -r '.response.status.code' # Success -> 200
# echo $response | jq -r '.response.status.message' # Success -> "OK"
# echo $launch_vng_nodes | jq '.response.items' # -> Array of valid response items
# Get Ocean Cluster (GET)
CLUSTER_ID='o-d109430a'
ocean_cluster=$(curl -s -X GET -H "Authorization: Bearer ${SPOTINST_TOKEN}" "https://api.spotinst.io/ocean/aws/k8s/cluster/${CLUSTER_ID}")
echo $ocean_cluster | jq -r '.response.items[0]' # Returns config
# Get Rightsizing Suggestions (POST)
# CLUSTER_ID='o-d109430a'
# rightsizing_recommendations=$(curl -L -s -X POST "https://api.spotinst.io/ocean/aws/k8s/cluster/${CLUSTER_ID}/rightSizing/suggestion?accountId=${SPOTINST_ACCOUNT_AWS}" --header 'Content-Type: application/json' --header 'Accept: application/json' --header "Authorization: Bearer ${SPOTINST_TOKEN}" --data-raw '{
# "filter": {
# "namespaces": [
# "default",
# "kube-system"
# ]
# }
# }')
# echo $rightsizing_recommendations | jq -r '.response.items'
# Launch Nodes in a VNG (PUT)
# VNG_ID='ols-d4b23fde'
# launch_vng_nodes=$(curl -L -s -X PUT "https://api.spotinst.io/ocean/aws/k8s/launchSpec/${VNG_ID}/launchNodes?accountId=act-61e1c107" -H 'Content-Type: application/json' -H 'Accept: application/json' -H "Authorization: Bearer ${SPOTINST_TOKEN}" --data-raw '{
# "launchRequest": {
# "amount": 2
# }
# }')
# echo $launch_vng_nodes | jq '.response.items'
# Delete a User (DELETE)
# USER_ID='u-c7244d1c'
# curl -L -s -X DELETE "https://api.spotinst.io/setup/user/${USER_ID}" -H 'Accept: application/json' -H "Authorization: Bearer ${SPOTINST_TOKEN}"
# echo $response | jq -r '.response.status.message'
# echo $launch_vng_nodes | jq '.response.items'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment