Skip to content

Commit

Permalink
Added CodeBuild Project resource and a CodeBuild example (#624)
Browse files Browse the repository at this point in the history
  • Loading branch information
jarosser06 authored and markpeek committed Dec 26, 2016
1 parent c0088fd commit 84ca91c
Show file tree
Hide file tree
Showing 3 changed files with 168 additions and 0 deletions.
32 changes: 32 additions & 0 deletions 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())
30 changes: 30 additions & 0 deletions 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"
}
}
}
106 changes: 106 additions & 0 deletions troposphere/codebuild.py
@@ -0,0 +1,106 @@
# Copyright (c) 2016, Mark Peek <mark@peek.org>
# 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),
}

0 comments on commit 84ca91c

Please sign in to comment.