Skip to content
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

r/aws_imagebuilder_distribution_configuration - add container_distribution_configuration attribute #22758

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/22758.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/aws_imagebuilder_distribution_configuration: Add `container_distribution_configuration` argument
```
129 changes: 129 additions & 0 deletions internal/service/imagebuilder/distribution_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,47 @@ func ResourceDistributionConfiguration() *schema.Resource {
},
},
},
"container_distribution_configuration": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"container_tags": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.StringLenBetween(1, 1024),
},
},
"description": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringLenBetween(1, 1024),
},
"target_repository": {
Type: schema.TypeList,
Required: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"repository_name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringLenBetween(1, 1024),
},
"service": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{"ECR"}, false),
},
},
},
},
},
},
},
"license_configuration_arns": {
Type: schema.TypeSet,
Optional: true,
Expand Down Expand Up @@ -317,6 +358,28 @@ func expandAMIDistributionConfiguration(tfMap map[string]interface{}) *imagebuil
return apiObject
}

func expandContainerDistributionConfiguration(tfMap map[string]interface{}) *imagebuilder.ContainerDistributionConfiguration {
if tfMap == nil {
return nil
}

apiObject := &imagebuilder.ContainerDistributionConfiguration{}

if v, ok := tfMap["container_tags"].(*schema.Set); ok && v.Len() > 0 {
apiObject.ContainerTags = flex.ExpandStringSet(v)
}

if v, ok := tfMap["description"].(string); ok && v != "" {
apiObject.Description = aws.String(v)
}

if v, ok := tfMap["target_repository"].([]interface{}); ok && len(v) > 0 && v[0] != nil {
apiObject.TargetRepository = expandTargetContainerRepository(v[0].(map[string]interface{}))
}

return apiObject
}

func expandDistribution(tfMap map[string]interface{}) *imagebuilder.Distribution {
if tfMap == nil {
return nil
Expand All @@ -328,6 +391,10 @@ func expandDistribution(tfMap map[string]interface{}) *imagebuilder.Distribution
apiObject.AmiDistributionConfiguration = expandAMIDistributionConfiguration(v[0].(map[string]interface{}))
}

if v, ok := tfMap["container_distribution_configuration"].([]interface{}); ok && len(v) > 0 && v[0] != nil {
apiObject.ContainerDistributionConfiguration = expandContainerDistributionConfiguration(v[0].(map[string]interface{}))
}

if v, ok := tfMap["license_configuration_arns"].(*schema.Set); ok && v.Len() > 0 {
apiObject.LicenseConfigurationArns = flex.ExpandStringSet(v)
}
Expand Down Expand Up @@ -390,6 +457,24 @@ func expandLaunchPermissionConfiguration(tfMap map[string]interface{}) *imagebui
return apiObject
}

func expandTargetContainerRepository(tfMap map[string]interface{}) *imagebuilder.TargetContainerRepository {
if tfMap == nil {
return nil
}

apiObject := &imagebuilder.TargetContainerRepository{}

if v, ok := tfMap["repository_name"].(string); ok && v != "" {
apiObject.RepositoryName = aws.String(v)
}

if v, ok := tfMap["service"].(string); ok && v != "" {
apiObject.Service = aws.String(v)
}

return apiObject
}

func flattenAMIDistributionConfiguration(apiObject *imagebuilder.AmiDistributionConfiguration) map[string]interface{} {
if apiObject == nil {
return nil
Expand Down Expand Up @@ -424,6 +509,28 @@ func flattenAMIDistributionConfiguration(apiObject *imagebuilder.AmiDistribution
return tfMap
}

func flattenContainerDistributionConfiguration(apiObject *imagebuilder.ContainerDistributionConfiguration) map[string]interface{} {
if apiObject == nil {
return nil
}

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

if v := apiObject.ContainerTags; v != nil {
tfMap["container_tags"] = aws.StringValueSlice(v)
}

if v := apiObject.Description; v != nil {
tfMap["description"] = aws.StringValue(v)
}

if v := apiObject.TargetRepository; v != nil {
tfMap["target_repository"] = []interface{}{flattenTargetContainerRepository(v)}
}

return tfMap
}

func flattenDistribution(apiObject *imagebuilder.Distribution) map[string]interface{} {
if apiObject == nil {
return nil
Expand All @@ -435,6 +542,10 @@ func flattenDistribution(apiObject *imagebuilder.Distribution) map[string]interf
tfMap["ami_distribution_configuration"] = []interface{}{flattenAMIDistributionConfiguration(v)}
}

if v := apiObject.ContainerDistributionConfiguration; v != nil {
tfMap["container_distribution_configuration"] = []interface{}{flattenContainerDistributionConfiguration(v)}
}

if v := apiObject.LicenseConfigurationArns; v != nil {
tfMap["license_configuration_arns"] = aws.StringValueSlice(v)
}
Expand Down Expand Up @@ -481,3 +592,21 @@ func flattenLaunchPermissionConfiguration(apiObject *imagebuilder.LaunchPermissi

return tfMap
}

func flattenTargetContainerRepository(apiObject *imagebuilder.TargetContainerRepository) map[string]interface{} {
if apiObject == nil {
return nil
}

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

if v := apiObject.RepositoryName; v != nil {
tfMap["repository_name"] = aws.StringValue(v)
}

if v := apiObject.Service; v != nil {
tfMap["service"] = aws.StringValue(v)
}

return tfMap
}
185 changes: 185 additions & 0 deletions internal/service/imagebuilder/distribution_configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,124 @@ func TestAccImageBuilderDistributionConfiguration_DistributionAMIDistribution_ta
})
}

func TestAccImageBuilderDistributionConfiguration_DistributionContainerDistribution_containerTags(t *testing.T) {
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_imagebuilder_distribution_configuration.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ErrorCheck: acctest.ErrorCheck(t, imagebuilder.EndpointsID),
ProviderFactories: acctest.ProviderFactories,
CheckDestroy: testAccCheckDistributionConfigurationDestroy,
Steps: []resource.TestStep{
{
Config: testAccDistributionConfigurationDistributionContainerDistributionConfigurationContainerTagsConfig(rName, "tag1"),
Check: resource.ComposeTestCheckFunc(
testAccCheckDistributionConfigurationExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "distribution.#", "1"),
resource.TestCheckResourceAttr(resourceName, "distribution.0.container_distribution_configuration.#", "1"),
resource.TestCheckTypeSetElemAttr(resourceName, "distribution.0.container_distribution_configuration.0.container_tags.*", "tag1"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccDistributionConfigurationDistributionContainerDistributionConfigurationContainerTagsConfig(rName, "tag2"),
Check: resource.ComposeTestCheckFunc(
testAccCheckDistributionConfigurationExists(resourceName),
acctest.CheckResourceAttrRFC3339(resourceName, "date_updated"),
resource.TestCheckResourceAttr(resourceName, "distribution.#", "1"),
resource.TestCheckResourceAttr(resourceName, "distribution.0.container_distribution_configuration.#", "1"),
resource.TestCheckTypeSetElemAttr(resourceName, "distribution.0.container_distribution_configuration.0.container_tags.*", "tag2"),
),
},
},
})
}

func TestAccImageBuilderDistributionConfiguration_DistributionContainerDistribution_description(t *testing.T) {
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_imagebuilder_distribution_configuration.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ErrorCheck: acctest.ErrorCheck(t, imagebuilder.EndpointsID),
ProviderFactories: acctest.ProviderFactories,
CheckDestroy: testAccCheckDistributionConfigurationDestroy,
Steps: []resource.TestStep{
{
Config: testAccDistributionConfigurationDistributionContainerDistributionConfigurationDescriptionConfig(rName, "description1"),
Check: resource.ComposeTestCheckFunc(
testAccCheckDistributionConfigurationExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "distribution.#", "1"),
resource.TestCheckResourceAttr(resourceName, "distribution.0.container_distribution_configuration.#", "1"),
resource.TestCheckResourceAttr(resourceName, "distribution.0.container_distribution_configuration.0.description", "description1"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccDistributionConfigurationDistributionContainerDistributionConfigurationDescriptionConfig(rName, "description2"),
Check: resource.ComposeTestCheckFunc(
testAccCheckDistributionConfigurationExists(resourceName),
acctest.CheckResourceAttrRFC3339(resourceName, "date_updated"),
resource.TestCheckResourceAttr(resourceName, "distribution.#", "1"),
resource.TestCheckResourceAttr(resourceName, "distribution.0.container_distribution_configuration.#", "1"),
resource.TestCheckResourceAttr(resourceName, "distribution.0.container_distribution_configuration.0.description", "description2"),
),
},
},
})
}

func TestAccImageBuilderDistributionConfiguration_DistributionContainerDistribution_targetRepository(t *testing.T) {
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_imagebuilder_distribution_configuration.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ErrorCheck: acctest.ErrorCheck(t, imagebuilder.EndpointsID),
ProviderFactories: acctest.ProviderFactories,
CheckDestroy: testAccCheckDistributionConfigurationDestroy,
Steps: []resource.TestStep{
{
Config: testAccDistributionConfigurationDistributionContainerDistributionConfigurationTargetRepositoryConfig(rName, "repository1"),
Check: resource.ComposeTestCheckFunc(
testAccCheckDistributionConfigurationExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "distribution.#", "1"),
resource.TestCheckResourceAttr(resourceName, "distribution.0.container_distribution_configuration.#", "1"),
resource.TestCheckResourceAttr(resourceName, "distribution.0.container_distribution_configuration.0.target_repository.#", "1"),
resource.TestCheckResourceAttr(resourceName, "distribution.0.container_distribution_configuration.0.target_repository.0.repository_name", "repository1"),
resource.TestCheckResourceAttr(resourceName, "distribution.0.container_distribution_configuration.0.target_repository.0.service", "ECR"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccDistributionConfigurationDistributionContainerDistributionConfigurationTargetRepositoryConfig(rName, "repository2"),
Check: resource.ComposeTestCheckFunc(
testAccCheckDistributionConfigurationExists(resourceName),
acctest.CheckResourceAttrRFC3339(resourceName, "date_updated"),
resource.TestCheckResourceAttr(resourceName, "distribution.#", "1"),
resource.TestCheckResourceAttr(resourceName, "distribution.0.container_distribution_configuration.#", "1"),
resource.TestCheckResourceAttr(resourceName, "distribution.0.container_distribution_configuration.0.target_repository.#", "1"),
resource.TestCheckResourceAttr(resourceName, "distribution.0.container_distribution_configuration.0.target_repository.0.repository_name", "repository2"),
resource.TestCheckResourceAttr(resourceName, "distribution.0.container_distribution_configuration.0.target_repository.0.service", "ECR"),
),
},
},
})
}

func TestAccImageBuilderDistributionConfiguration_Distribution_licenseARNs(t *testing.T) {
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
licenseConfigurationResourceName := "aws_licensemanager_license_configuration.test"
Expand Down Expand Up @@ -740,6 +858,73 @@ resource "aws_imagebuilder_distribution_configuration" "test" {
`, rName, targetAccountId)
}

func testAccDistributionConfigurationDistributionContainerDistributionConfigurationTargetRepositoryConfig(rName string, repositoryName string) string {
return fmt.Sprintf(`
data "aws_region" "current" {}

resource "aws_imagebuilder_distribution_configuration" "test" {
name = %[1]q

distribution {
container_distribution_configuration {
target_repository {
repository_name = %[2]q
service = "ECR"
}
}

region = data.aws_region.current.name
}
}
`, rName, repositoryName)
}

func testAccDistributionConfigurationDistributionContainerDistributionConfigurationDescriptionConfig(rName string, description string) string {
return fmt.Sprintf(`
data "aws_region" "current" {}

resource "aws_imagebuilder_distribution_configuration" "test" {
name = %[1]q

distribution {
container_distribution_configuration {
target_repository {
repository_name = "repository-name"
service = "ECR"
}

description = %[2]q
}

region = data.aws_region.current.name
}
}
`, rName, description)
}

func testAccDistributionConfigurationDistributionContainerDistributionConfigurationContainerTagsConfig(rName string, containerTag string) string {
return fmt.Sprintf(`
data "aws_region" "current" {}

resource "aws_imagebuilder_distribution_configuration" "test" {
name = %[1]q

distribution {
container_distribution_configuration {
target_repository {
repository_name = "repository-name"
service = "ECR"
}

container_tags = [%[2]q]
}

region = data.aws_region.current.name
}
}
`, rName, containerTag)
}

func testAccDistributionConfigurationDistributionLicenseConfigurationARNs1Config(rName string) string {
return fmt.Sprintf(`
data "aws_region" "current" {}
Expand Down
Loading