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

New Resource: aws_imagebuilder_distribution_configuration #16180

Merged
merged 27 commits into from
Nov 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
96b4232
Add aws_imagebuilder_component
Dogers Feb 21, 2020
2d588c2
Add test for data_source_aws_imagebuilder_component
Dogers Feb 22, 2020
a5d9f8f
Add aws_imagebuilder_infrastructureconfiguration
Dogers Feb 22, 2020
6414616
Add aws_imagebuilder_recipe
Dogers Mar 3, 2020
c29b5da
Rename to aws_imagebuilder_infrastructure_configuration
Dogers May 6, 2020
8b53767
Fix issue with infraconfig logging
Dogers May 25, 2020
8ce7b6b
Fix incorrect MaxItems on res
Dogers May 25, 2020
fbe7510
Fix S024
Dogers May 25, 2020
28dfb17
Fix AWSR002
Dogers May 25, 2020
507fe42
Fix linting
Dogers May 25, 2020
ca89e2b
[#11084] Fix the imagebuilder resource, add test and docs
blckct Jul 12, 2020
195ba8a
Merge remote-tracking branch 'source/master' into image-builder
Dogers Aug 2, 2020
0d050cd
Merge branch 'master' into image-builder
Dogers Aug 19, 2020
26e1bb9
Fix incorrect import on error handling
Dogers Aug 19, 2020
df7016f
Update to v2 SDK
Dogers Aug 19, 2020
89e7bdd
Fix R004 lint issue
Dogers Aug 19, 2020
18dd1b0
Remove changes to aws.erb
Dogers Aug 19, 2020
1b85e94
Add imagebuilder_distribution_configuration
Dogers Aug 2, 2020
eb8e1ed
Add imagebuilder_image_pipeline
Dogers Aug 25, 2020
0402fe7
fixed some lint issues and fixed terminate_instance_on_failure not be…
wrschneider Aug 28, 2020
a20d120
description can be updated in place
wrschneider Aug 28, 2020
10c55b6
Merge pull request #1 from wrschneider/image-builder
Dogers Aug 28, 2020
7cfe977
Merge branch 'image-builder' of ssh://github.com/Dogers/terraform-pro…
bflad Nov 12, 2020
09e672e
service/imagebuilder: Remove non-aws_imagebuilder_distribution_config…
bflad Nov 12, 2020
c21b59c
New Resource: aws_imagebuilder_distribution_configuration
bflad Nov 13, 2020
8649bc0
docs/resource/aws_imagebuilder_distribution_configuration: Fix typo
bflad Nov 13, 2020
30b5f3a
Merge branch 'master' into f-aws_imagebuilder_distribution_configuration
bflad Nov 17, 2020
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
144 changes: 144 additions & 0 deletions aws/data_source_aws_imagebuilder_distribution_configuration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package aws

import (
"fmt"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/imagebuilder"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags"
)

func datasourceAwsImageBuilderDistributionConfiguration() *schema.Resource {
return &schema.Resource{
Read: datasourceAwsImageBuilderDistributionConfigurationRead,

Schema: map[string]*schema.Schema{
"arn": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validateArn,
},
"date_created": {
Type: schema.TypeString,
Computed: true,
},
"date_updated": {
Type: schema.TypeString,
Computed: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
"distribution": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"ami_distribution_configuration": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"ami_tags": tagsSchemaComputed(),
"description": {
Type: schema.TypeString,
Computed: true,
},
"kms_key_id": {
Type: schema.TypeString,
Computed: true,
},
"launch_permission": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"user_groups": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"user_ids": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
},
},
"name": {
Type: schema.TypeString,
Computed: true,
},
"target_account_ids": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
},
},
"license_configuration_arns": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"region": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"name": {
Type: schema.TypeString,
Computed: true,
},
"tags": tagsSchemaComputed(),
},
}
}

func datasourceAwsImageBuilderDistributionConfigurationRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).imagebuilderconn
ignoreTagsConfig := meta.(*AWSClient).IgnoreTagsConfig

input := &imagebuilder.GetDistributionConfigurationInput{}

if v, ok := d.GetOk("arn"); ok {
input.DistributionConfigurationArn = aws.String(v.(string))
}
Comment on lines +118 to +120
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this check, since arn is required?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Treating all the fields as potentially "optional" in the Create/Update function logic will allow us to reduce code churn in the future should the APIs support other fields and gets us closer towards code generation since it can be agnostic to how the data is coming in. It is just a little awkward right now because we do (or at least should) treat everything as "optional" in the expand functions but the root attribute handling is different with ResourceData instead of a raw state map.

I'm hoping later this week to submit more definitive documentation on ideal object/"structure" handling in the provider codebase, with an eye towards that sort of code generation in the future, so we can have clear standards where possible. 😄


output, err := conn.GetDistributionConfiguration(input)

if err != nil {
return fmt.Errorf("error getting Image Builder Distribution Configuration (%s): %w", d.Id(), err)
}

if output == nil || output.DistributionConfiguration == nil {
return fmt.Errorf("error getting Image Builder Distribution Configuration (%s): empty response", d.Id())
}

distributionConfiguration := output.DistributionConfiguration

d.SetId(aws.StringValue(distributionConfiguration.Arn))
d.Set("arn", distributionConfiguration.Arn)
d.Set("date_created", distributionConfiguration.DateCreated)
d.Set("date_updated", distributionConfiguration.DateUpdated)
d.Set("description", distributionConfiguration.Description)
d.Set("distribution", flattenImageBuilderDistributions(distributionConfiguration.Distributions))
d.Set("name", distributionConfiguration.Name)
d.Set("tags", keyvaluetags.ImagebuilderKeyValueTags(distributionConfiguration.Tags).IgnoreAws().IgnoreConfig(ignoreTagsConfig).Map())

return nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package aws

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccAwsImageBuilderDistributionConfigurationDataSource_Arn(t *testing.T) {
rName := acctest.RandomWithPrefix("tf-acc-test")
dataSourceName := "data.aws_imagebuilder_distribution_configuration.test"
resourceName := "aws_imagebuilder_distribution_configuration.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviderFactories,
CheckDestroy: testAccCheckAwsImageBuilderDistributionConfigurationDestroy,
Steps: []resource.TestStep{
{
Config: testAccAwsImageBuilderDistributionConfigurationDataSourceConfigArn(rName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrPair(dataSourceName, "arn", resourceName, "arn"),
resource.TestCheckResourceAttrPair(dataSourceName, "date_created", resourceName, "date_created"),
resource.TestCheckResourceAttrPair(dataSourceName, "date_updated", resourceName, "date_updated"),
resource.TestCheckResourceAttrPair(dataSourceName, "description", resourceName, "description"),
resource.TestCheckResourceAttrPair(dataSourceName, "distribution.#", resourceName, "distribution.#"),
resource.TestCheckResourceAttrPair(dataSourceName, "name", resourceName, "name"),
resource.TestCheckResourceAttrPair(dataSourceName, "tags.%", resourceName, "tags.%"),
),
},
},
})
}

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

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

distribution {
region = data.aws_region.current.name
}
}

data "aws_imagebuilder_distribution_configuration" "test" {
arn = aws_imagebuilder_distribution_configuration.test.arn
}
`, rName)
}
2 changes: 2 additions & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ func Provider() *schema.Provider {
"aws_iam_server_certificate": dataSourceAwsIAMServerCertificate(),
"aws_iam_user": dataSourceAwsIAMUser(),
"aws_imagebuilder_component": dataSourceAwsImageBuilderComponent(),
"aws_imagebuilder_distribution_configuration": datasourceAwsImageBuilderDistributionConfiguration(),
"aws_internet_gateway": dataSourceAwsInternetGateway(),
"aws_iot_endpoint": dataSourceAwsIotEndpoint(),
"aws_inspector_rules_packages": dataSourceAwsInspectorRulesPackages(),
Expand Down Expand Up @@ -703,6 +704,7 @@ func Provider() *schema.Provider {
"aws_iam_user": resourceAwsIamUser(),
"aws_iam_user_login_profile": resourceAwsIamUserLoginProfile(),
"aws_imagebuilder_component": resourceAwsImageBuilderComponent(),
"aws_imagebuilder_distribution_configuration": resourceAwsImageBuilderDistributionConfiguration(),
"aws_inspector_assessment_target": resourceAWSInspectorAssessmentTarget(),
"aws_inspector_assessment_template": resourceAWSInspectorAssessmentTemplate(),
"aws_inspector_resource_group": resourceAWSInspectorResourceGroup(),
Expand Down
Loading