-
Notifications
You must be signed in to change notification settings - Fork 9.3k
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
Conversation
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.
Hi @iliazlobin 👋 Thanks so much for this contribution, its off to a great start. A few minor things below and we should be able to get this in. Please reach out if you have any questions or do not have time to implement the feedback items. 👍
@@ -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, |
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 to TypeList
instead. 👍
Type: schema.TypeSet, | |
Type: schema.TypeList, |
@@ -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 comment
The 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!
if v := envConfig["registry_credential"]; v != nil { | |
if v, ok := envConfig["registry_credential"]; ok && len(v.([]interface{})) > 0 { |
@@ -659,6 +679,22 @@ func expandProjectEnvironment(d *schema.ResourceData) *codebuild.ProjectEnvironm | |||
projectEnv.ImagePullCredentialsType = aws.String(v.(string)) | |||
} | |||
|
|||
if v := envConfig["registry_credential"]; v != nil { | |||
config := v.(*schema.Set).List()[0].(map[string]interface{}) |
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.
With the switch to TypeList
and the length check added above for safety, this simplifies to:
config := v.(*schema.Set).List()[0].(map[string]interface{}) | |
config := v.([]interface{}).[0].(map[string]interface{}) |
|
||
projectRegistryCredential := &codebuild.RegistryCredential{} | ||
|
||
if v := config["credential"].(string); v != "" { |
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.
For additional safety we should use existence checking here as well. 👍
if v := config["credential"].(string); v != "" { | |
if v, ok := config["credential"]; ok && v.(string) != "" { |
projectRegistryCredential := &codebuild.RegistryCredential{} | ||
|
||
if v := config["credential"].(string); v != "" { | ||
projectRegistryCredential.Credential = &v |
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.
Generally when setting AWS Go SDK parameters, we prefer (by convention) to use the provided conversion functions (e.g. aws.String()
) instead of raw Go pointer referencing
projectRegistryCredential.Credential = &v | |
projectRegistryCredential.Credential = aws.String(v.(string)) |
projectRegistryCredential.Credential = &v | ||
} | ||
|
||
if v := config["credential_provider"].(string); v != "" { |
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.
Similar change here:
if v := config["credential_provider"].(string); v != "" { | |
if v, ok := config["credential_provider"]; ok && v.(string) != "" { |
} | ||
|
||
if v := config["credential_provider"].(string); v != "" { | ||
projectRegistryCredential.CredentialProvider = &v |
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.
Similar change here:
projectRegistryCredential.CredentialProvider = &v | |
projectRegistryCredential.CredentialProvider = aws.String(v.(string)) |
environment { | ||
compute_type = "BUILD_GENERAL1_SMALL" | ||
image = "2" | ||
type = "LINUX_CONTAINER" |
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.
Can you please fix the formatting here? Looks like spaces versus tabs. 😄
type = "LINUX_CONTAINER" | |
type = "LINUX_CONTAINER" |
compute_type = "BUILD_GENERAL1_SMALL" | ||
image = "2" | ||
type = "LINUX_CONTAINER" | ||
image_pull_credentials_type = "SERVICE_ROLE" |
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.
image_pull_credentials_type = "SERVICE_ROLE" | |
image_pull_credentials_type = "SERVICE_ROLE" |
Hi @bflad! Thank you for reviewing my code and putting clear notes of what needs to be tuned. |
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.
Thanks so much for the updates here, @iliazlobin! Your changes looked good! 🚀
Output from acceptance testing:
--- PASS: TestAccAWSCodeBuildProject_Source_Type_Bitbucket (24.02s)
--- PASS: TestAccAWSCodeBuildProject_basic (24.13s)
--- PASS: TestAccAWSCodeBuildProject_Source_Auth (24.35s)
--- PASS: TestAccAWSCodeBuildProject_SecondarySources_CodeCommit (24.40s)
--- PASS: TestAccAWSCodeBuildProject_importBasic (25.59s)
--- PASS: TestAccAWSCodeBuildProject_Source_ReportBuildStatus_Bitbucket (32.31s)
--- PASS: TestAccAWSCodeBuildProject_Source_ReportBuildStatus_GitHubEnterprise (32.63s)
--- PASS: TestAccAWSCodeBuildProject_Source_GitCloneDepth (32.90s)
--- PASS: TestAccAWSCodeBuildProject_BuildTimeout (33.12s)
--- PASS: TestAccAWSCodeBuildProject_Environment_EnvironmentVariable_Type (33.13s)
--- PASS: TestAccAWSCodeBuildProject_Description (33.84s)
--- PASS: TestAccAWSCodeBuildProject_BadgeEnabled (33.93s)
--- PASS: TestAccAWSCodeBuildProject_SecondaryArtifacts (35.97s)
--- PASS: TestAccAWSCodeBuildProject_Artifacts_EncryptionDisabled (36.12s)
--- PASS: TestAccAWSCodeBuildProject_Environment_Certificate (39.08s)
--- PASS: TestAccAWSCodeBuildProject_Source_Type_NoSourceInvalid (7.32s)
--- PASS: TestAccAWSCodeBuildProject_Source_InsecureSSL (40.11s)
--- PASS: TestAccAWSCodeBuildProject_Environment_RegistryCredential (41.34s)
--- PASS: TestAccAWSCodeBuildProject_Environment_EnvironmentVariable (42.96s)
--- PASS: TestAccAWSCodeBuildProject_WindowsContainer (20.95s)
--- PASS: TestAccAWSCodeBuildProject_Source_Type_GitHubEnterprise (20.79s)
--- PASS: TestAccAWSCodeBuildProject_Source_Type_CodePipeline (21.18s)
--- PASS: TestAccAWSCodeBuildProject_EncryptionKey (52.34s)
--- PASS: TestAccAWSCodeBuildProject_Source_Type_NoSource (20.67s)
--- PASS: TestAccAWSCodeBuildProject_Source_Type_S3 (32.59s)
--- PASS: TestAccAWSCodeBuildProject_Source_Type_CodeCommit (32.69s)
--- PASS: TestAccAWSCodeBuildProject_Tags (28.28s)
--- PASS: TestAccAWSCodeBuildProject_Source_ReportBuildStatus_GitHub (29.05s)
--- PASS: TestAccAWSCodeBuildProject_Cache (62.92s)
There is one very subtle detail that even our acceptance testing cannot properly catch in this scenario and its only related to some technical debt we have planned to clean up in #6427 I'm leaving full details for this below, but this is not something we would typically expect outside contributors to pick up on at all.
For updates to properly occur with TypeSet
attributes that have a custom Set
function declared, new attributes must be added to the hashing function. The Terraform Provider SDK uses the hash value to determine if the parent attribute needs to be updated, otherwise configuration updates will not result in API updates and Terraform will show no updates.
In general, proper TypeSet
attribute testing is very problematic in the current Terraform Provider SDK framework. Usually ImportStateVerify
can uncover issues with the state values, but unfortunately it cannot easily pick up on these particular hashing issues since we cannot:
- Easily use
resource.TestCheckResourceAttr()
(or in this particular caseresource.TestCheckResourceAttrPair()
) since theenvironment
hash value in the flatmap would be dynamic due to the dynamic value of the underlyingcredential
ARN, we'd have to rely on checking the API response value in&project
fromtestAccCheckAWSCodeBuildProjectExists
- Determine the missing change with
ImportStateVerify
testing
To fix this particular issue, it was just a matter of adding this to resourceAwsCodeBuildProjectEnvironmentHash
in a followup commit (5d5dcbb):
if v, ok := m["registry_credential"]; ok && len(v.([]interface{})) > 0 && v.([]interface{})[0] != nil {
m := v.([]interface{})[0].(map[string]interface{})
if v, ok := m["credential"]; ok && v.(string) != "" {
buf.WriteString(fmt.Sprintf("%s-", v.(string)))
}
if v, ok := m["credential_provider"]; ok && v.(string) != "" {
buf.WriteString(fmt.Sprintf("%s-", v.(string)))
}
}
In the future we'll be replacing these TypeSet
and MaxItems: 1
attributes with TypeList
, so this particular issue cannot occur.
This has been released in version 2.19.0 of the Terraform AWS provider. Please see the Terraform documentation on provider versioning or reach out if you need any assistance upgrading. For further feature requests or bug reports with this functionality, please create a new GitHub issue following the template for triage. Thanks! |
I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. Thanks! |
Community Note
Closes #9147 #8028
Release note for CHANGELOG:
Output from acceptance testing: