Skip to content

Commit

Permalink
Merge pull request #138 from jmcs/lb_scheme
Browse files Browse the repository at this point in the history
Allow setting loadbalancer_scheme as a command line argument
  • Loading branch information
hjacobs committed Nov 12, 2015
2 parents f44768b + 019a6b9 commit 6802fb3
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 13 deletions.
11 changes: 11 additions & 0 deletions senza/templates/_helper.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import boto3
import click
import clickclick
import json
import re
from clickclick import Action
Expand All @@ -17,6 +18,15 @@ def prompt(variables: dict, var_name, *args, **kwargs):
variables[var_name] = click.prompt(*args, **kwargs)


def choice(variables: dict, var_name, *args, **kwargs):
if var_name not in variables:
if callable(kwargs.get('default')):
# evaluate callable
kwargs['default'] = kwargs['default']()

variables[var_name] = clickclick.choice(*args, **kwargs)


def confirm(*args, **kwargs):
return click.confirm(*args, **kwargs)

Expand All @@ -30,6 +40,7 @@ def _value_checker(value: str):
raise click.UsageError('did not match regex {}.'.format(match_regex))
else:
raise click.UsageError('Value is too long! {} > {} chars'.format(len(value), max_length))

return _value_checker


Expand Down
19 changes: 9 additions & 10 deletions senza/templates/webapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@
HTTP app with auto scaling, ELB and DNS
'''

from clickclick import warning, error, choice
from clickclick import warning, error
from senza.utils import pystache_render

from ._helper import prompt, confirm, check_security_group, check_iam_role, get_mint_bucket_name, check_value

from ._helper import prompt, choice, confirm, check_security_group, check_iam_role, get_mint_bucket_name, check_value

TEMPLATE = '''
# basic information for generating and executing this definition
Expand Down Expand Up @@ -68,12 +66,13 @@ def gather_user_variables(variables, region, account_info):
prompt(variables, 'mint_bucket', 'Mint S3 bucket name', default=lambda: get_mint_bucket_name(region))
else:
variables['mint_bucket'] = None
variables['loadbalancer_scheme'] = choice(prompt='Please select the load balancer scheme',
options=[('internal',
'internal: only accessible from the own VPC'),
('internet-facing',
'internet-facing: accessible from the public internet')],
default='internal')
choice(variables, 'loadbalancer_scheme',
prompt='Please select the load balancer scheme',
options=[('internal',
'internal: only accessible from the own VPC'),
('internet-facing',
'internet-facing: accessible from the public internet')],
default='internal')
http_port = variables['http_port']

sg_name = 'app-{}'.format(variables['application_id'])
Expand Down
22 changes: 19 additions & 3 deletions tests/test_templates.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import click
from unittest.mock import MagicMock
from senza.templates._helper import get_iam_role_policy, get_mint_bucket_name, check_value
from senza.templates._helper import get_iam_role_policy, get_mint_bucket_name, check_value, prompt, choice


def test_template_helper_get_mint_bucket_name(monkeypatch):
Expand All @@ -15,8 +15,8 @@ def test_template_helper_get_mint_bucket_name(monkeypatch):
s3 = MagicMock()
s3.return_value.Bucket.return_value.load.side_effect = Exception()
monkeypatch.setattr('boto3.resource', s3)
assert('myorg-stups-mint-123-otherregion' == get_mint_bucket_name('otherregion'),
'Return Name of Bucket, if no other Bucket found')
assert ('myorg-stups-mint-123-otherregion' == get_mint_bucket_name('otherregion'),
'Return Name of Bucket, if no other Bucket found')

exist_bucket = MagicMock()
exist_bucket.name = 'myorg-stups-mint-123-myregion'
Expand Down Expand Up @@ -67,3 +67,19 @@ def test_template_helper_check_value():
assert False, 'check_value raise with a unkown exception'
else:
assert False, 'check_value doesnot return with a raise'


def test_choice_callable_default(monkeypatch):
mock = MagicMock()
monkeypatch.setattr('clickclick.choice', mock)
variables = {}
choice(variables, 'test', default=lambda: 'default')
mock.assert_called_once_with(default='default')


def test_prompt_callable_default(monkeypatch):
mock = MagicMock()
monkeypatch.setattr('click.prompt', mock)
variables = {}
prompt(variables, 'test', default=lambda: 'default')
mock.assert_called_once_with(default='default')

0 comments on commit 6802fb3

Please sign in to comment.