Skip to content

Commit

Permalink
feat(validate): Add profile and region options (aws#582)
Browse files Browse the repository at this point in the history
Currently, `sam validate` requires AWS Creds (due to the SAM Translator).
This commits adds the ability to pass in the credientials through a profile
that is configured through `aws configure`.
  • Loading branch information
jfuss authored Aug 2, 2018
1 parent f5c297f commit d4af44b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
8 changes: 6 additions & 2 deletions samcli/commands/validate/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
import os

import boto3
from botocore.exceptions import NoCredentialsError
import click
from samtranslator.translator.managed_policy_translator import ManagedPolicyLoader


from samcli.cli.main import pass_context, common_options as cli_framework_options
from samcli.commands.exceptions import UserException
from samcli.cli.main import pass_context, common_options as cli_framework_options, aws_creds_options
from samcli.commands.local.cli_common.options import template_common_option as template_option
from samcli.commands.local.cli_common.user_exceptions import InvalidSamTemplateException, SamTemplateNotFoundException
from samcli.yamlhelper import yaml_parse
Expand All @@ -19,6 +20,7 @@
@click.command("validate",
short_help="Validate an AWS SAM template.")
@template_option
@aws_creds_options
@cli_framework_options
@pass_context
def cli(ctx, template):
Expand All @@ -43,6 +45,8 @@ def do_cli(ctx, template):
except InvalidSamDocumentException as e:
click.secho("Template provided at '{}' was invalid SAM Template.".format(template), bg='red')
raise InvalidSamTemplateException(str(e))
except NoCredentialsError as e:
raise UserException("AWS Credentials are required. Please configure your credentials.")

click.secho("{} is a valid SAM Template".format(template), fg='green')

Expand Down
18 changes: 18 additions & 0 deletions tests/unit/commands/validate/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from unittest import TestCase
from mock import Mock, patch

from botocore.exceptions import NoCredentialsError

from samcli.commands.exceptions import UserException
from samcli.commands.local.cli_common.user_exceptions import SamTemplateNotFoundException, InvalidSamTemplateException
from samcli.commands.validate.lib.exceptions import InvalidSamDocumentException
from samcli.commands.validate.validate import do_cli, _read_sam_file
Expand Down Expand Up @@ -47,6 +50,21 @@ def test_template_fails_validation(self, read_sam_file_patch, click_patch, templ
do_cli(ctx=None,
template=template_path)

@patch('samcli.commands.validate.validate.SamTemplateValidator')
@patch('samcli.commands.validate.validate.click')
@patch('samcli.commands.validate.validate._read_sam_file')
def test_no_credentials_provided(self, read_sam_file_patch, click_patch, template_valiadator):
template_path = 'path_to_template'
read_sam_file_patch.return_value = {"a": "b"}

is_valid_mock = Mock()
is_valid_mock.is_valid.side_effect = NoCredentialsError
template_valiadator.return_value = is_valid_mock

with self.assertRaises(UserException):
do_cli(ctx=None,
template=template_path)

@patch('samcli.commands.validate.validate.SamTemplateValidator')
@patch('samcli.commands.validate.validate.click')
@patch('samcli.commands.validate.validate._read_sam_file')
Expand Down

0 comments on commit d4af44b

Please sign in to comment.