Skip to content

Commit

Permalink
feat: codebuild template generator (#36)
Browse files Browse the repository at this point in the history
* feat: add parameters output to support cfn deploys

* fix: add missing param permissions

* fix: add correct exception handling and response

* fix: update to new product structure

* fix: change default type to new format
  • Loading branch information
kalleeh authored and shendriksen committed Dec 19, 2019
1 parent d8dc5d0 commit 6d7535c
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 31 deletions.
23 changes: 1 addition & 22 deletions lambda_layers/dependencies/python/managers/stack_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,6 @@ def list_stacks(self, keys):

def create_stack(self, stack_name, payload):
""" Creates a new stack.
Args:
type: (app|pipeline|service)
stack_name:
payload:
Basic Usage:
Returns:
List: List of ..
"""

params = self._generate_params(payload)
Expand Down Expand Up @@ -177,14 +169,7 @@ def describe_stack(self):
return stacks

def update_stack(self, payload):
"""
Args:
type: (app|pipeline|service)
stack_name:
payload:
Basic Usage:
Returns:
List: List of ..
""" Updates a CloudFormation stack.
"""
stack_name = transform_utils.add_prefix(self._params['name'])
LOGGER.debug(
Expand Down Expand Up @@ -249,12 +234,6 @@ def update_stack(self, payload):

def delete_stack(self):
""" Deletes a CloudFormation stack.
Args:
name (string): Name of the CloudFormation Stack
Basic Usage:
Returns:
List: List of JSON objects containing stack information
"""
stack_name = transform_utils.add_prefix(self._params['name'])
LOGGER.debug(
Expand Down
3 changes: 1 addition & 2 deletions lambda_layers/dependencies/python/template_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,9 @@ def generate_template_url(stack_type, payload):
elif stack_type == 'service':
prefix_path = 'services'

template_url = 'https://s3.amazonaws.com/{}/{}/{}-{}-{}.yaml'.format(
template_url = 'https://s3.amazonaws.com/{}/{}/{}/{}.yaml'.format(
platform_config.PLATFORM_BUCKET,
prefix_path,
stack_type,
payload['subtype'],
payload['version']
)
Expand Down
2 changes: 1 addition & 1 deletion src/apps/create_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def post(event, _context):
name = transform_utils.add_prefix(payload['name'])

if 'subtype' not in payload:
payload['subtype'] = 'shared-lb'
payload['subtype'] = 'ecs-fargate'
if 'version' not in payload:
payload['version'] = 'latest'

Expand Down
2 changes: 1 addition & 1 deletion src/pipelines/create_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def post(event, _context):

# Configure default values if not present
if 'subtype' not in payload:
payload['subtype'] = 'github'
payload['subtype'] = 'github/cfn'
if 'version' not in payload:
payload['version'] = 'latest'

Expand Down
10 changes: 10 additions & 0 deletions src/services/create_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import json

from exceptions import AlreadyExists
from aws_xray_sdk.core import patch_all
from logger import configure_logger

Expand All @@ -36,6 +37,9 @@ def post(event, _context):
data['services'] = []

payload = json.loads(event['body-json'][0])
LOGGER.debug(
'Received payload: %s',
payload)

# Configure default values if not present

Expand All @@ -51,7 +55,13 @@ def post(event, _context):
name,
payload
)
except AlreadyExists:
return response_builder.error('A service with that name already exists.', 409)
except Exception as ex:
LOGGER.debug(
'Exception: %s',
ex,
exc_info=True)
return response_builder.error('Unknown Error: {}'.format(ex))
else:
data['services'] = resp
Expand Down
16 changes: 11 additions & 5 deletions src/services/name/update_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

import json

from exceptions import NoSuchObject, PermissionDenied
from exceptions import NoSuchObject, PermissionDenied, UnknownParameter
from aws_xray_sdk.core import patch_all
from logger import configure_logger

Expand All @@ -34,15 +34,19 @@ def patch(event, _context):

payload = json.loads(event['body-json'][0])

# Configure default values if not present
if 'subtype' not in payload:
payload['subtype'] = 's3'
if 'version' not in payload:
payload['version'] = 'latest'

bindings = payload['service_bindings'].split(',')
for binding in bindings:
if not sm.has_permissions(binding):
return response_builder.error('{} doesn\'t exist or not enough permissions.'.format(binding), 400)
try:
bindings = payload['ServiceBindings'].split(',')
for binding in bindings:
if not sm.has_permissions(binding):
return response_builder.error('{} doesn\'t exist or not enough permissions.'.format(binding), 400)
except KeyError:
return response_builder.error('ServiceBindings not provided in payload.', 400)

try:
resp = sm.update_stack(
Expand All @@ -52,6 +56,8 @@ def patch(event, _context):
return response_builder.error('No such service.', 400)
except PermissionDenied:
return response_builder.error('Permission denied.', 401)
except UnknownParameter as ex:
return response_builder.error('{}'.format(ex), 400)
except Exception as ex:
return response_builder.error('Unknown Error: {}'.format(ex))
else:
Expand Down
17 changes: 17 additions & 0 deletions src/template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,7 @@ Resources:
- 'cloudformation:CreateStack'
- 'cloudformation:ListExports'
- 'iam:PassRole'
- 'ssm:GetParametersByPath'
Resource:
- '*'

Expand Down Expand Up @@ -799,3 +800,19 @@ Resources:
Type: "String"
Value: !Sub "https://${ApiGatewayApi}.execute-api.${AWS::Region}.amazonaws.com/v1/"
Description: "API Endpoint for the API."

APIDeploymentRoleArn:
Type: "AWS::SSM::Parameter"
Properties:
Name: "/gurum/api/deployment-role-arn"
Type: "String"
Value: !GetAtt DeploymentRole.Arn
Description: "IAM ARN to the Deployment Role."

GurumProductsBucket:
Type: "AWS::SSM::Parameter"
Properties:
Name: "/gurum/products/bucket"
Type: "String"
Value: !Ref ProductsBucket
Description: "S3 Bucket holding the Gurum Products."

0 comments on commit 6d7535c

Please sign in to comment.