-
Notifications
You must be signed in to change notification settings - Fork 9.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
resource/aws_codebuild_project: Add registry_credential argument for environment #9168
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -197,6 +197,26 @@ func resourceAwsCodeBuildProject() *schema.Resource { | |||||
Optional: true, | ||||||
ValidateFunc: validation.StringMatch(regexp.MustCompile(`\.(pem|zip)$`), "must end in .pem or .zip"), | ||||||
}, | ||||||
"registry_credential": { | ||||||
Type: schema.TypeSet, | ||||||
Optional: true, | ||||||
MaxItems: 1, | ||||||
Elem: &schema.Resource{ | ||||||
Schema: map[string]*schema.Schema{ | ||||||
"credential": { | ||||||
Type: schema.TypeString, | ||||||
Required: true, | ||||||
}, | ||||||
"credential_provider": { | ||||||
Type: schema.TypeString, | ||||||
Required: true, | ||||||
ValidateFunc: validation.StringInSlice([]string{ | ||||||
codebuild.CredentialProviderTypeSecretsManager, | ||||||
}, false), | ||||||
}, | ||||||
}, | ||||||
}, | ||||||
}, | ||||||
}, | ||||||
}, | ||||||
Set: resourceAwsCodeBuildProjectEnvironmentHash, | ||||||
|
@@ -659,6 +679,22 @@ func expandProjectEnvironment(d *schema.ResourceData) *codebuild.ProjectEnvironm | |||||
projectEnv.ImagePullCredentialsType = aws.String(v.(string)) | ||||||
} | ||||||
|
||||||
if v := envConfig["registry_credential"]; v != nil { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like the code around this might not be performing the safer check, but could you please update this to something like the following to perform the existence checking? Thanks!
Suggested change
|
||||||
config := v.(*schema.Set).List()[0].(map[string]interface{}) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With the switch to
Suggested change
|
||||||
|
||||||
projectRegistryCredential := &codebuild.RegistryCredential{} | ||||||
|
||||||
if v := config["credential"].(string); v != "" { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For additional safety we should use existence checking here as well. 👍
Suggested change
|
||||||
projectRegistryCredential.Credential = &v | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Generally when setting AWS Go SDK parameters, we prefer (by convention) to use the provided conversion functions (e.g.
Suggested change
|
||||||
} | ||||||
|
||||||
if v := config["credential_provider"].(string); v != "" { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar change here:
Suggested change
|
||||||
projectRegistryCredential.CredentialProvider = &v | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar change here:
Suggested change
|
||||||
} | ||||||
|
||||||
projectEnv.RegistryCredential = projectRegistryCredential | ||||||
} | ||||||
|
||||||
if v := envConfig["environment_variable"]; v != nil { | ||||||
envVariables := v.([]interface{}) | ||||||
if len(envVariables) > 0 { | ||||||
|
@@ -1027,12 +1063,26 @@ func flattenAwsCodeBuildProjectEnvironment(environment *codebuild.ProjectEnviron | |||||
envConfig["privileged_mode"] = *environment.PrivilegedMode | ||||||
envConfig["image_pull_credentials_type"] = *environment.ImagePullCredentialsType | ||||||
|
||||||
envConfig["registry_credential"] = flattenAwsCodebuildRegistryCredential(environment.RegistryCredential) | ||||||
|
||||||
if environment.EnvironmentVariables != nil { | ||||||
envConfig["environment_variable"] = environmentVariablesToMap(environment.EnvironmentVariables) | ||||||
} | ||||||
|
||||||
return []interface{}{envConfig} | ||||||
} | ||||||
|
||||||
func flattenAwsCodebuildRegistryCredential(registryCredential *codebuild.RegistryCredential) []interface{} { | ||||||
if registryCredential == nil { | ||||||
return []interface{}{} | ||||||
} | ||||||
|
||||||
values := map[string]interface{}{ | ||||||
"credential": aws.StringValue(registryCredential.Credential), | ||||||
"credential_provider": aws.StringValue(registryCredential.CredentialProvider), | ||||||
} | ||||||
|
||||||
return []interface{}{values} | ||||||
} | ||||||
|
||||||
func flattenAwsCodeBuildProjectSecondarySources(sourceList []*codebuild.ProjectSource) []interface{} { | ||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -896,6 +896,26 @@ func TestAWSCodeBuildProject_nameValidation(t *testing.T) { | |||||
} | ||||||
} | ||||||
|
||||||
func TestAccAWSCodeBuildProject_Environment_RegistryCredential(t *testing.T) { | ||||||
var project codebuild.Project | ||||||
rName := acctest.RandomWithPrefix("tf-acc-test") | ||||||
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_Environment_RegistryCredential(rName), | ||||||
Check: resource.ComposeTestCheckFunc( | ||||||
testAccCheckAWSCodeBuildProjectExists(resourceName, &project), | ||||||
), | ||||||
}, | ||||||
bflad marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
}, | ||||||
}) | ||||||
} | ||||||
|
||||||
func testAccCheckAWSCodeBuildProjectExists(n string, project *codebuild.Project) resource.TestCheckFunc { | ||||||
return func(s *terraform.State) error { | ||||||
rs, ok := s.RootModule().Resources[n] | ||||||
|
@@ -1399,6 +1419,46 @@ resource "aws_codebuild_project" "test" { | |||||
`, oName, rName) | ||||||
} | ||||||
|
||||||
func testAccAWSCodeBuildProjectConfig_Environment_RegistryCredential(rName string) string { | ||||||
return testAccAWSCodeBuildProjectConfig_Base_ServiceRole(rName) + fmt.Sprintf(` | ||||||
resource "aws_codebuild_project" "test" { | ||||||
name = %[1]q | ||||||
service_role = "${aws_iam_role.test.arn}" | ||||||
|
||||||
artifacts { | ||||||
type = "NO_ARTIFACTS" | ||||||
} | ||||||
|
||||||
environment { | ||||||
compute_type = "BUILD_GENERAL1_SMALL" | ||||||
image = "2" | ||||||
type = "LINUX_CONTAINER" | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you please fix the formatting here? Looks like spaces versus tabs. 😄
Suggested change
|
||||||
image_pull_credentials_type = "SERVICE_ROLE" | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
registry_credential { | ||||||
credential = "${aws_secretsmanager_secret_version.test.arn}" | ||||||
credential_provider = "SECRETS_MANAGER" | ||||||
} | ||||||
} | ||||||
|
||||||
source { | ||||||
type = "GITHUB" | ||||||
location = "https://github.com/hashicorp/packer.git" | ||||||
} | ||||||
} | ||||||
|
||||||
resource "aws_secretsmanager_secret" "test" { | ||||||
name = "test" | ||||||
recovery_window_in_days = 0 | ||||||
} | ||||||
|
||||||
resource "aws_secretsmanager_secret_version" "test" { | ||||||
secret_id = "${aws_secretsmanager_secret.test.id}" | ||||||
secret_string = "${jsonencode(map("username", "user", "password", "pass"))}" | ||||||
} | ||||||
`, rName) | ||||||
} | ||||||
|
||||||
func testAccAWSCodeBuildProjectConfig_Source_Auth(rName, authResource, authType string) string { | ||||||
return testAccAWSCodeBuildProjectConfig_Base_ServiceRole(rName) + fmt.Sprintf(` | ||||||
resource "aws_codebuild_project" "test" { | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When using
MaxItems: 1
for configuration block attributes, we prefer to simply the attribute type toTypeList
instead. 👍