Skip to content

Commit

Permalink
Add logs_config schema to aws_codebuild_project
Browse files Browse the repository at this point in the history
Fix test fixture
  • Loading branch information
srhaber committed Jul 9, 2019
1 parent ece3969 commit 12d5b5c
Show file tree
Hide file tree
Showing 3 changed files with 332 additions and 0 deletions.
240 changes: 240 additions & 0 deletions aws/resource_aws_codebuild_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"log"
"regexp"
"strconv"
"strings"
"time"

"github.com/aws/aws-sdk-go/aws"
Expand Down Expand Up @@ -221,6 +222,95 @@ func resourceAwsCodeBuildProject() *schema.Resource {
},
Set: resourceAwsCodeBuildProjectEnvironmentHash,
},
"logs_config": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"cloudwatch_logs": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"status": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{
codebuild.LogsConfigStatusTypeDisabled,
codebuild.LogsConfigStatusTypeEnabled,
}, false),
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
if old == codebuild.LogsConfigStatusTypeEnabled && new == "" {
return true
}
return false
},
},
"group_name": {
Type: schema.TypeString,
Optional: true,
},
"stream_name": {
Type: schema.TypeString,
Optional: true,
},
},
},
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
if old == "1" && new == "0" {
return true
}
return false
},
},
"s3_logs": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"status": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{
codebuild.LogsConfigStatusTypeDisabled,
codebuild.LogsConfigStatusTypeEnabled,
}, false),
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
if old == codebuild.LogsConfigStatusTypeDisabled && new == "" {
return true
}
return false
},
},
"location": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validateAwsCodeBuildProjectS3LogsLocation,
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
return strings.TrimPrefix(new, "arn:aws:s3:::") == old
},
},
},
},
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
if old == "1" && new == "0" {
return true
}
return false
},
},
},
},
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
if old == "1" && new == "0" {
return true
}
return false
},
},
"name": {
Type: schema.TypeString,
Required: true,
Expand Down Expand Up @@ -482,6 +572,7 @@ func resourceAwsCodeBuildProjectCreate(d *schema.ResourceData, meta interface{})
projectArtifacts := expandProjectArtifacts(d)
projectSecondaryArtifacts := expandProjectSecondaryArtifacts(d)
projectSecondarySources := expandProjectSecondarySources(d)
projectLogsConfig := expandProjectLogsConfig(d)

if aws.StringValue(projectSource.Type) == codebuild.SourceTypeNoSource {
if aws.StringValue(projectSource.Buildspec) == "" {
Expand All @@ -500,6 +591,7 @@ func resourceAwsCodeBuildProjectCreate(d *schema.ResourceData, meta interface{})
Artifacts: &projectArtifacts,
SecondaryArtifacts: projectSecondaryArtifacts,
SecondarySources: projectSecondarySources,
LogsConfig: projectLogsConfig,
}

if v, ok := d.GetOk("cache"); ok {
Expand Down Expand Up @@ -727,6 +819,82 @@ func expandProjectEnvironment(d *schema.ResourceData) *codebuild.ProjectEnvironm
return projectEnv
}

func expandProjectLogsConfig(d *schema.ResourceData) *codebuild.LogsConfig {
logsConfig := &codebuild.LogsConfig{}

if v, ok := d.GetOk("logs_config"); ok {
configList := v.([]interface{})
data := configList[0].(map[string]interface{})

if v, ok := data["cloudwatch_logs"]; ok {
logsConfig.CloudWatchLogs = expandCodeBuildCloudWatchLogsConfig(v.([]interface{}))
}

if v, ok := data["s3_logs"]; ok {
logsConfig.S3Logs = expandCodeBuildS3LogsConfig(v.([]interface{}))
}
}

if logsConfig.CloudWatchLogs == nil {
logsConfig.CloudWatchLogs = &codebuild.CloudWatchLogsConfig{
Status: aws.String(codebuild.LogsConfigStatusTypeEnabled),
}
}

if logsConfig.S3Logs == nil {
logsConfig.S3Logs = &codebuild.S3LogsConfig{
Status: aws.String(codebuild.LogsConfigStatusTypeDisabled),
}
}

return logsConfig
}

func expandCodeBuildCloudWatchLogsConfig(configList []interface{}) *codebuild.CloudWatchLogsConfig {
data := configList[0].(map[string]interface{})

status := data["status"].(string)

cloudWatchLogsConfig := &codebuild.CloudWatchLogsConfig{
Status: aws.String(status),
}

if v, ok := data["group_name"]; ok {
groupName := v.(string)
if len(groupName) > 0 {
cloudWatchLogsConfig.GroupName = aws.String(groupName)
}
}

if v, ok := data["stream_name"]; ok {
streamName := v.(string)
if len(streamName) > 0 {
cloudWatchLogsConfig.StreamName = aws.String(streamName)
}
}

return cloudWatchLogsConfig
}

func expandCodeBuildS3LogsConfig(configList []interface{}) *codebuild.S3LogsConfig {
data := configList[0].(map[string]interface{})

status := data["status"].(string)

s3LogsConfig := &codebuild.S3LogsConfig{
Status: aws.String(status),
}

if v, ok := data["location"]; ok {
location := strings.TrimPrefix(v.(string), "arn:aws:s3:::")
if len(location) > 0 {
s3LogsConfig.Location = aws.String(location)
}
}

return s3LogsConfig
}

func expandCodeBuildVpcConfig(rawVpcConfig []interface{}) *codebuild.VpcConfig {
vpcConfig := codebuild.VpcConfig{}
if len(rawVpcConfig) == 0 || rawVpcConfig[0] == nil {
Expand Down Expand Up @@ -837,6 +1005,10 @@ func resourceAwsCodeBuildProjectRead(d *schema.ResourceData, meta interface{}) e
return err
}

if err := d.Set("logs_config", flattenAwsCodeBuildLogsConfig(project.LogsConfig)); err != nil {
return err
}

if err := d.Set("secondary_artifacts", flattenAwsCodeBuildProjectSecondaryArtifacts(project.SecondaryArtifacts)); err != nil {
return err
}
Expand Down Expand Up @@ -910,6 +1082,11 @@ func resourceAwsCodeBuildProjectUpdate(d *schema.ResourceData, meta interface{})
params.VpcConfig = expandCodeBuildVpcConfig(d.Get("vpc_config").([]interface{}))
}

if d.HasChange("logs_config") {
logsConfig := expandProjectLogsConfig(d)
params.LogsConfig = logsConfig
}

if d.HasChange("cache") {
if v, ok := d.GetOk("cache"); ok {
params.Cache = expandProjectCache(v.([]interface{}))
Expand Down Expand Up @@ -981,6 +1158,51 @@ func resourceAwsCodeBuildProjectDelete(d *schema.ResourceData, meta interface{})
return err
}

func flattenAwsCodeBuildLogsConfig(logsConfig *codebuild.LogsConfig) []interface{} {
if logsConfig == nil {
return []interface{}{}
}

values := map[string]interface{}{}

if v := logsConfig.CloudWatchLogs; v != nil {
values["cloudwatch_logs"] = flattenAwsCodeBuildCloudWatchLogs(v)
}

if v := logsConfig.S3Logs; v != nil {
values["s3_logs"] = flattenAwsCodeBuildS3Logs(v)
}

return []interface{}{values}
}

func flattenAwsCodeBuildCloudWatchLogs(cloudWatchLogsConfig *codebuild.CloudWatchLogsConfig) []interface{} {
if cloudWatchLogsConfig == nil {
return []interface{}{}
}

values := map[string]interface{}{
"status": aws.StringValue(cloudWatchLogsConfig.Status),
"group_name": aws.StringValue(cloudWatchLogsConfig.GroupName),
"stream_name": aws.StringValue(cloudWatchLogsConfig.StreamName),
}

return []interface{}{values}
}

func flattenAwsCodeBuildS3Logs(s3LogsConfig *codebuild.S3LogsConfig) []interface{} {
if s3LogsConfig == nil {
return []interface{}{}
}

values := map[string]interface{}{
"status": aws.StringValue(s3LogsConfig.Status),
"location": aws.StringValue(s3LogsConfig.Location),
}

return []interface{}{values}
}

func flattenAwsCodeBuildProjectSecondaryArtifacts(artifactsList []*codebuild.ProjectArtifacts) *schema.Set {
artifactSet := schema.Set{
F: resourceAwsCodeBuildProjectArtifactsHash,
Expand Down Expand Up @@ -1282,3 +1504,21 @@ func validateAwsCodeBuildProjectName(v interface{}, k string) (ws []string, erro

return
}

func validateAwsCodeBuildProjectS3LogsLocation(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)

if _, errs := validateArn(v, k); len(errs) == 0 {
errors = append(errors, errs...)
return
}

simplePattern := `^[a-z0-9][^/]*\/(.+)$`
if !regexp.MustCompile(simplePattern).MatchString(value) {
errors = append(errors, fmt.Errorf(
"%q does not match pattern (%q): %q",
k, simplePattern, value))
}

return
}
62 changes: 62 additions & 0 deletions aws/resource_aws_codebuild_project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,32 @@ func TestAccAWSCodeBuildProject_Environment_Certificate(t *testing.T) {
})
}

func TestAccAWSCodeBuildProject_LogsConfig(t *testing.T) {
var project codebuild.Project
rName := acctest.RandomWithPrefix("tf-acc-test")
bName := acctest.RandomWithPrefix("tf-acc-test-bucket")
resourceName := "aws_codebuild_project.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccPreCheckAWSCodeBuild(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSCodeBuildProjectDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSCodeBuildProjectConfig_LogsConfig(rName, bName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSCodeBuildProjectExists(resourceName, &project),
resource.TestCheckResourceAttr(resourceName, "logs_config.0.cloudwatch_logs.0.status", "ENABLED"),
resource.TestCheckResourceAttr(resourceName, "logs_config.0.cloudwatch_logs.0.group_name", "build-log"),
resource.TestCheckResourceAttr(resourceName, "logs_config.0.cloudwatch_logs.0.stream_name", "build-log"),
resource.TestCheckResourceAttr(resourceName, "logs_config.0.s3_logs.0.status", "ENABLED"),
resource.TestMatchResourceAttr(resourceName, "logs_config.0.s3_logs.0.location", regexp.MustCompile(`tf-acc-test-bucket-[0-9]+/build-log$`)),
),
},
},
})
}

func TestAccAWSCodeBuildProject_Source_Auth(t *testing.T) {
var project codebuild.Project
rName := acctest.RandomWithPrefix("tf-acc-test")
Expand Down Expand Up @@ -1510,6 +1536,42 @@ resource "aws_secretsmanager_secret_version" "test" {
`, rName)
}

func testAccAWSCodeBuildProjectConfig_LogsConfig(rName, bName string) string {
return testAccAWSCodeBuildProjectConfig_Base_ServiceRole(rName) + testAccAWSCodeBuildProjectConfig_Base_Bucket(bName) + fmt.Sprintf(`
resource "aws_codebuild_project" "test" {
name = "%s"
service_role = "${aws_iam_role.test.arn}"
artifacts {
type = "NO_ARTIFACTS"
}
environment {
compute_type = "BUILD_GENERAL1_SMALL"
image = "2"
type = "LINUX_CONTAINER"
}
source {
location = "https://github.com/hashicorp/packer.git"
type = "GITHUB"
}
logs_config {
cloudwatch_logs {
status = "ENABLED"
group_name = "build-log"
stream_name = "build-log"
}
s3_logs {
status = "ENABLED"
location = "${aws_s3_bucket.test.arn}/build-log"
}
}
}
`, rName)
}

func testAccAWSCodeBuildProjectConfig_Source_Auth(rName, authResource, authType string) string {
return testAccAWSCodeBuildProjectConfig_Base_ServiceRole(rName) + fmt.Sprintf(`
resource "aws_codebuild_project" "test" {
Expand Down
Loading

0 comments on commit 12d5b5c

Please sign in to comment.