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

[WIP] Image Builder support #13485

Closed
wants to merge 22 commits into from
Closed
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
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
106 changes: 106 additions & 0 deletions aws/data_source_aws_imagebuilder_component.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
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 dataSourceAwsImageBuilderComponent() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsImageBuilderComponentRead,

Schema: map[string]*schema.Schema{
"arn": {
Type: schema.TypeString,
Required: true,
},
"change_description": {
Type: schema.TypeString,
Computed: true,
},
"data": {
Type: schema.TypeString,
Computed: true,
},
"date_created": {
Type: schema.TypeString,
Computed: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
"encrypted": {
Type: schema.TypeBool,
Computed: true,
Optional: true,
},
"name": {
Type: schema.TypeString,
Computed: true,
Optional: true,
},
"owner": {
Type: schema.TypeString,
Computed: true,
Optional: true,
},
"platform": {
Type: schema.TypeString,
Computed: true,
Optional: true,
},
"tags": tagsSchemaComputed(),
"type": {
Type: schema.TypeString,
Computed: true,
Optional: true,
},
"semantic_version": {
Type: schema.TypeInt,
Computed: true,
Optional: true,
},
},
}
}

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

componentArn := d.Get("arn").(string)

params := &imagebuilder.GetComponentInput{
ComponentBuildVersionArn: aws.String(componentArn),
}

resp, err := conn.GetComponent(params)

if err != nil {
return fmt.Errorf("Error retrieving Component: %s", err)
}

d.SetId(*resp.Component.Arn)

if err := d.Set("tags", keyvaluetags.ImagebuilderKeyValueTags(resp.Component.Tags).IgnoreAws().IgnoreConfig(ignoreTagsConfig).Map()); err != nil {
return fmt.Errorf("error setting tags: %s", err)
}

d.Set("change_description", resp.Component.ChangeDescription)
d.Set("data", resp.Component.Data)
d.Set("date_created", resp.Component.DateCreated)
d.Set("description", resp.Component.Description)
d.Set("encrypted", resp.Component.Encrypted)
d.Set("kms_key_id", resp.Component.KmsKeyId)
d.Set("name", resp.Component.Name)
d.Set("owner", resp.Component.Owner)
d.Set("platform", resp.Component.Platform)
d.Set("type", resp.Component.Type)
d.Set("semantic_version", resp.Component.Version)

return nil
}
58 changes: 58 additions & 0 deletions aws/data_source_aws_imagebuilder_component_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package aws

import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"testing"
)

func TestAccDataSourceAwsImageBuilderComponent_basic(t *testing.T) {
rName := fmt.Sprintf("tf-ib-comp-test-%s", acctest.RandString(8))
resourceName := "aws_builder_component.test"
dataSourceName := "data.aws_builder_component.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAwsImageBuilderComponentConfig(rName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrPair(dataSourceName, "arn", resourceName, "arn"),
resource.TestCheckResourceAttrPair(dataSourceName, "name", resourceName, "name"),
resource.TestCheckResourceAttrPair(dataSourceName, "semantic_version", resourceName, "semantic_version"),
),
},
},
})
}

func testAccDataSourceAwsImageBuilderComponentConfig(name string) string {
return fmt.Sprintf(`
resource "aws_imagebuilder_component" "test" {
name = "%s"
platform = "Linux"
semantic_version = "1.0.1"

data = <<EOD
name: HelloWorldTestingDocument
description: This is hello world testing document.
schemaVersion: 1.0

phases:
- name: test
steps:
- name: HelloWorldStep
action: ExecuteBash
inputs:
commands:
- echo "Hello World! Test."
EOD
}

data "aws_builder_component" "test" {
arn = "arn:aws:imagebuilder:eu-west-1:116147290797:component/test/1.0.1/1"
}
`, name)
}
136 changes: 136 additions & 0 deletions aws/data_source_aws_imagebuilder_distribution_configuration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
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"
"log"
)

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

Schema: map[string]*schema.Schema{
"arn": {
Type: schema.TypeString,
Required: true,
},
"date_created": {
Type: schema.TypeString,
Computed: true,
},
"date_updated": {
Type: schema.TypeString,
Computed: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
"distributions": {
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": tagsSchema(),
"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,
},
},
},
},
"license_configuration_arn": {
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

resp, err := conn.GetDistributionConfiguration(&imagebuilder.GetDistributionConfigurationInput{
DistributionConfigurationArn: aws.String(d.Get("arn").(string)),
})

if isAWSErr(err, imagebuilder.ErrCodeResourceNotFoundException, "") {
log.Printf("[WARN] DistributionConfiguration (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}

if err != nil {
return fmt.Errorf("error reading DistributionConfiguration (%s): %s", d.Id(), err)
}

d.SetId(*resp.DistributionConfiguration.Arn)
d.Set("date_created", resp.DistributionConfiguration.DateCreated)
d.Set("date_updated", resp.DistributionConfiguration.DateUpdated)
d.Set("description", resp.DistributionConfiguration.Description)
d.Set("distributions", flattenAwsImageBuilderDistributions(resp.DistributionConfiguration.Distributions))
d.Set("name", resp.DistributionConfiguration.Name)

if err != nil {
return fmt.Errorf("error listing tags for DistributionConfiguration (%s): %s", d.Id(), err)
}
if err := d.Set("tags", keyvaluetags.ImagebuilderKeyValueTags(resp.DistributionConfiguration.Tags).IgnoreAws().IgnoreConfig(meta.(*AWSClient).IgnoreTagsConfig).Map()); err != nil {
return fmt.Errorf("error setting tags: %s", err)
}

return nil
}
Loading