Skip to content

Commit

Permalink
Merge pull request #4 from waterbear-cloud/cli-error
Browse files Browse the repository at this point in the history
Cli error
  • Loading branch information
kteague committed Jul 8, 2019
2 parents 37613d8 + 0636fd5 commit 4852c0b
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 11 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ Changelog for aim
1.0.1 (unreleased)
------------------

- Nothing changed yet.
### Added

- CLI reports human readable validation errors from AIM project configuration files


1.0.0 (2019-07-06)
Expand Down
1 change: 0 additions & 1 deletion docs/quickstart101.rst
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,6 @@ this file will describe your network and looks like this:
title: "My AIM Network"
availability_zones: 2
enabled: true
region: eu-central-1
vpc:
enable_dns_hostnames: true
enable_dns_support: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,4 @@ name: Master
title: Master AWS Account
account_type: AWS
account_id: '{{cookiecutter.master_account_id}}'
aws_profile: ''
admin_delegate_role_name: 'AIM-Admin-Delegate-Role'
mfa_account_id: '{{cookiecutter.master_account_id}}'
mfa_iam_user_name: '{{cookiecutter.master_admin_iam_username}}'
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ network:
title: "{{cookiecutter.network_environment_title}}"
availability_zones: 2
enabled: true
region: {{cookiecutter.aws_default_region}}
vpc:
enable_dns_hostnames: true
enable_dns_support: true
Expand Down
3 changes: 2 additions & 1 deletion src/aim/commands/cmd_delete.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import aim.models
import click
import sys
from aim.commands.helpers import pass_aim_context, controller_args, aim_home_option, init_aim_home_option
from aim.commands.helpers import pass_aim_context, controller_args, aim_home_option, init_aim_home_option, handle_exceptions
from aim.core.exception import StackException


@click.command('delete', short_help='Delete AIM managed resources')
@controller_args
@aim_home_option
@pass_aim_context
@handle_exceptions
def delete_command(aim_ctx, controller_type, component_name=None, config_name=None, config_region=None, home='.'):
"""Deletes provisioned AWS Resources"""
init_aim_home_option(aim_ctx, home)
Expand Down
5 changes: 3 additions & 2 deletions src/aim/commands/cmd_describe.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import click
import sys
import aim.models
from aim.commands.helpers import pass_aim_context, aim_home_option, init_aim_home_option
from aim.commands.helpers import pass_aim_context, aim_home_option, init_aim_home_option, handle_exceptions

@click.command('describe', short_help='Describe an AIM project')
@aim_home_option
@pass_aim_context
@handle_exceptions
def describe_command(ctx, home='.'):
"""Describe an AIM project"""
init_aim_home_option(ctx, home)
if not ctx.home:
print('AIM configuration directory needs to be specified with either --home or AIM_HOME environment variable.')
sys.exit()
project = aim.models.load_project_from_yaml(ctx, ctx.home)
project = aim.models.load_project_from_yaml(ctx.aim_ref, ctx.home)

print('Project: {} - {}'.format(project.name, project.title))
print('Location: {}'.format(ctx.home))
Expand Down
3 changes: 2 additions & 1 deletion src/aim/commands/cmd_provision.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import aim.models
import click
import sys
from aim.commands.helpers import controller_args, aim_home_option, init_aim_home_option, pass_aim_context
from aim.commands.helpers import controller_args, aim_home_option, init_aim_home_option, pass_aim_context, handle_exceptions
from aim.core.exception import StackException

@click.command(name='provision', short_help='Provision an AIM project or a specific environment.')
@controller_args
@aim_home_option
@pass_aim_context
@handle_exceptions
def provision_command(aim_ctx, controller_type, component_name=None, config_name=None, config_region=None, home='.'):
"""Provision AWS Resources"""
init_aim_home_option(aim_ctx, home)
Expand Down
3 changes: 2 additions & 1 deletion src/aim/commands/cmd_validate.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import click
import sys
from aim.core.exception import StackException
from aim.commands.helpers import pass_aim_context, controller_args, aim_home_option, init_aim_home_option
from aim.commands.helpers import pass_aim_context, controller_args, aim_home_option, init_aim_home_option, handle_exceptions

@click.command('validate', short_help='Validate an AIM project')
@controller_args
@aim_home_option
@pass_aim_context
@handle_exceptions
def validate_command(aim_ctx, controller_type, component_name=None, config_name=None, config_region=None, home='.'):
"""Validates a Config CloudFormation"""

Expand Down
26 changes: 26 additions & 0 deletions src/aim/commands/helpers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import click
import os
import sys
from aim.config.aim_context import AimContext, AccountContext
from aim.core.exception import AimException, StackException
from aim.models.exceptions import InvalidAimProjectFile, UnusedAimProjectField, InvalidAimReference
from boto3.exceptions import Boto3Error
from botocore.exceptions import BotoCoreError, ClientError
from functools import wraps

pass_aim_context = click.make_pass_decorator(AimContext, ensure=True)

Expand Down Expand Up @@ -32,3 +38,23 @@ def init_aim_home_option(ctx, home):
home = os.environ.get('AIM_HOME')
if home is not None:
ctx.home = home

def handle_exceptions(func):
"""
Catches exceptions and displays errors in a human readable format
"""
@wraps(func)
def decorated(*args, **kwargs):
try:
return func(*args, **kwargs)
except (InvalidAimReference, UnusedAimProjectField, InvalidAimProjectFile, AimException, StackException, BotoCoreError,
ClientError, Boto3Error) as error:
#import pdb; pdb.set_trace();
click.echo("\nERROR!\n")
error_name = error.__class__.__name__
if error_name in ('InvalidAimProjectFile', 'UnusedAimProjectField', 'InvalidAimReference'):
click.echo("Invalid AIM project configuration files at {}".format(args[0].home))
click.echo(error)
sys.exit(1)

return decorated

0 comments on commit 4852c0b

Please sign in to comment.