From 84ca91cdd1d3760a8ba806d86c6fc38b0a3fac80 Mon Sep 17 00:00:00 2001 From: Jim Rosser Date: Mon, 26 Dec 2016 11:53:13 -0600 Subject: [PATCH] Added CodeBuild Project resource and a CodeBuild example (#624) --- examples/CodeBuild.py | 32 +++++++ tests/examples_output/CodeBuild.template | 30 +++++++ troposphere/codebuild.py | 106 +++++++++++++++++++++++ 3 files changed, 168 insertions(+) create mode 100644 examples/CodeBuild.py create mode 100644 tests/examples_output/CodeBuild.template create mode 100644 troposphere/codebuild.py diff --git a/examples/CodeBuild.py b/examples/CodeBuild.py new file mode 100644 index 000000000..02a351a12 --- /dev/null +++ b/examples/CodeBuild.py @@ -0,0 +1,32 @@ +from troposphere import Template +from troposphere.codebuild import Artifacts, Environment, Source, Project + + +template = Template() +template.add_version('2010-09-09') + +artifacts = Artifacts(Type='NO_ARTIFACTS') + +environment = Environment( + ComputeType='BUILD_GENERAL1_SMALL', + Image='aws/codebuild/java:openjdk-8', + Type='LINUX_CONTAINER', + EnvironmentVariables=[{'Name': 'APP_NAME', 'Value': 'demo'}], +) + +source = Source( + Location='codebuild-demo-test/0123ab9a371ebf0187b0fe5614fbb72c', + Type='S3' +) + +project = Project( + "DemoProject", + Artifacts=artifacts, + Environment=environment, + Name='DemoProject', + ServiceRole='arn:aws:iam::0123456789:role/codebuild-role', + Source=source, +) +template.add_resource(project) + +print(template.to_json()) diff --git a/tests/examples_output/CodeBuild.template b/tests/examples_output/CodeBuild.template new file mode 100644 index 000000000..47094771d --- /dev/null +++ b/tests/examples_output/CodeBuild.template @@ -0,0 +1,30 @@ +{ + "AWSTemplateFormatVersion": "2010-09-09", + "Resources": { + "DemoProject": { + "Properties": { + "Artifacts": { + "Type": "NO_ARTIFACTS" + }, + "Environment": { + "ComputeType": "BUILD_GENERAL1_SMALL", + "EnvironmentVariables": [ + { + "Name": "APP_NAME", + "Value": "demo" + } + ], + "Image": "aws/codebuild/java:openjdk-8", + "Type": "LINUX_CONTAINER" + }, + "Name": "DemoProject", + "ServiceRole": "arn:aws:iam::0123456789:role/codebuild-role", + "Source": { + "Location": "codebuild-demo-test/0123ab9a371ebf0187b0fe5614fbb72c", + "Type": "S3" + } + }, + "Type": "AWS::CodeBuild::Project" + } + } +} diff --git a/troposphere/codebuild.py b/troposphere/codebuild.py new file mode 100644 index 000000000..7c7370ed3 --- /dev/null +++ b/troposphere/codebuild.py @@ -0,0 +1,106 @@ +# Copyright (c) 2016, Mark Peek +# All rights reserved. +# +# See LICENSE file for full license. + +from . import AWSObject, AWSProperty, Tags +from .validators import integer + + +class Artifacts(AWSProperty): + props = { + 'Location': (basestring, False), + 'Name': (basestring, False), + 'NameSpaceType': (basestring, False), + 'Packaging': (basestring, False), + 'Path': (basestring, False), + 'Type': (basestring, True), + } + + def validate(self): + valid_types = [ + 'CODEPIPELINE', + 'NO_ARTIFACTS', + 'S3', + ] + artifact_type = self.properties.get('Type') + if artifact_type not in valid_types: + raise ValueError('Artifacts Type: must be one of %s' % + ','.join(valid_types)) + + if artifact_type == 'S3': + for required_property in ['Name', 'Location']: + if not self.properties.get(required_property): + raise ValueError( + 'Artifacts Type S3: requires %s to be set' % + required_property + ) + + +class EnvironmentVariable(AWSProperty): + props = { + 'Name': (basestring, True), + 'Value': (basestring, True), + } + + +class Environment(AWSProperty): + props = { + 'ComputeType': (basestring, True), + 'EnvironmentVariables': ((list, [EnvironmentVariable]), False), + 'Image': (basestring, True), + 'Type': (basestring, True), + } + + def validate(self): + valid_types = [ + 'LINUX_CONTAINER', + ] + env_type = self.properties.get('Type') + if env_type not in valid_types: + raise ValueError('Environment Type: must be one of %s' % + ','.join(valid_types)) + + +class Source(AWSProperty): + props = { + 'BuildSpec': (basestring, False), + 'Location': (basestring, False), + 'Type': (basestring, True), + } + + def validate(self): + valid_types = [ + 'CODECOMMIT', + 'CODEPIPELINE', + 'GITHUB', + 'S3', + ] + + source_type = self.properties.get('Type') + if source_type not in valid_types: + raise ValueError('Source Type: must be one of %s' % + ','.join(valid_types)) + + location = self.properties.get('Location') + if source_type is not 'CODEPIPELINE' and not location: + raise ValueError( + 'Source Location: must be defined when type is %s' % + source_type + ) + + +class Project(AWSObject): + resource_type = "AWS::CodeBuild::Project" + + props = { + 'Artifacts': (Artifacts, True), + 'Description': (basestring, False), + 'EncryptionKey': (basestring, False), + 'Environment': (Environment, True), + 'Name': (basestring, True), + 'ServiceRole': (basestring, True), + 'Source': (Source, True), + 'Tags': (Tags, False), + 'TimeoutInMinutes': (integer, False), + }