Skip to content

Instantly share code, notes, and snippets.

@stevenfeltner
Last active September 24, 2021 17:33
Show Gist options
  • Save stevenfeltner/ae228cf863c404a08db8e36f61a6dfa6 to your computer and use it in GitHub Desktop.
Save stevenfeltner/ae228cf863c404a08db8e36f61a6dfa6 to your computer and use it in GitHub Desktop.
Spot.io Account Connect CLI

Readme

The following CLI script can be used to do the following:

  • Create new Account within Spot.io Platform
  • Delete a Spot Account within Spot.io Platform
  • Retrieve and generate an External ID for an AWS connection
  • Set the cloud credentials (ARN) for an AWS linked account to the Spot.io Account
  • Get the Spot Account ID

Pre-req

Make sure to install the packages from requirements.txt $ pip install -r requirements.txt

Spot Admin Token needs to be stored as an environment variable under a variable called SPOTINST_TOKEN

Example Usage

Help

python3 spot_aws_connect.py --help
Usage: spot_aws_connect.py [OPTIONS] COMMAND [ARGS]...

Options:
  --help  Show this message and exit.

Commands:
  create                 Create a new Spot Account
  create-external-id     Generate the Spot External ID for Spot Account...
  delete                 Delete a Spot Account
  get                    Retrieve the Spot Account ID.
  set-cloud-credentials  Set AWS ROLE ARN to Spot Account

Get AccountID:

python3 spot_aws_connect.py get --filter=name='AccountName' --attr-account_id
###### Requirements without Version Specifiers ######`
click
###### Requirements with Version Specifiers ######`
spotinst-sdk2 >= 2.1.10
from setuptools import setup, find_packages
setup(
name='spot-account-aws',
version='0.1',
packages=find_packages(),
include_package_data=True,
install_requires=[
'Click',
'spotinst-sdk2>=2.1.10'
],
entry_points='''
[console_scripts]
spot-account-aws=spot_account_aws:cli
''',
)
#!/bin/bash
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
cd "${DIR}"
pip3 install -e . > /dev/null
spot-account-aws "$@"
import click
import json
from spotinst_sdk2 import SpotinstSession
@click.group()
@click.pass_context
def cli(ctx, *args, **kwargs):
ctx.obj = {}
session = SpotinstSession()
ctx.obj['client'] = session.client("admin")
ctx.obj['client2'] = session.client("setup_aws")
@cli.command()
@click.argument('name',)
@click.pass_context
def create(ctx, *args, **kwargs):
"""Create a new Spot Account"""
result = ctx.obj['client'].create_account(kwargs.get('name'))
click.echo(json.dumps(result))
@cli.command()
@click.argument('account-id')
@click.pass_context
def delete(ctx, *args, **kwargs):
"""Delete a Spot Account"""
ctx.obj['client'].delete_account(kwargs.get('account_id'))
@cli.command()
@click.argument('account-id')
@click.pass_context
def create_external_id(ctx, *args, **kwargs):
"""Generate the Spot External ID for Spot Account connection"""
ctx.obj['client2'].account_id = kwargs.get('account_id')
result = ctx.obj['client2'].create_external_id()
click.echo(json.dumps(result))
@cli.command()
@click.argument('account-id')
@click.argument('role-arn')
@click.pass_context
def set_cloud_credentials(ctx, *args, **kwargs):
"""Set AWS ROLE ARN to Spot Account"""
ctx.obj['client2'].account_id = kwargs.get('account_id')
result = ctx.obj['client2'].set_credentials(iam_role=kwargs.get('role_arn'))
click.echo(json.dumps(result))
@cli.command()
@click.option(
'--filter',
required=False,
help='Return matching records. Syntax: key=value'
)
@click.option(
'--attr',
required=False,
help='Return only the raw value of a single attribute'
)
@click.pass_context
def get(ctx, *args, **kwargs):
"""Retrieve the Spot Account ID. Returns ONLY the first match. Syntax "get --filter=name=<account name>"""
ctx.obj['client'].account_id = kwargs.get('account_id')
result = ctx.obj['client'].get_accounts()
if kwargs.get('filter'):
k, v = kwargs.get('filter').split('=')
result = [x for x in result if x[k] == v]
if kwargs.get('attr'):
if result:
result = result[0].get(kwargs.get('attr'))
click.echo(result)
else:
if result:
click.echo(json.dumps(result[0]))
if __name__ == "__main__":
cli()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment