Skip to content

Commit

Permalink
Merge pull request #35097 from kamilturek/f-aws-imagebuilder-workflow
Browse files Browse the repository at this point in the history
r/aws_imagebuilder_workflow: new resource
  • Loading branch information
ewbankkit authored Jan 3, 2024
2 parents 5100b05 + ef23e3b commit b7fe731
Show file tree
Hide file tree
Showing 5 changed files with 922 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/35097.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-resource
aws_imagebuilder_workflow
```
8 changes: 8 additions & 0 deletions internal/service/imagebuilder/service_package_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

221 changes: 221 additions & 0 deletions internal/service/imagebuilder/workflow.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package imagebuilder

import (
"context"
"log"

"github.com/YakDriver/regexache"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/imagebuilder"
"github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/id"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
"github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag"
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
"github.com/hashicorp/terraform-provider-aws/internal/verify"
"github.com/hashicorp/terraform-provider-aws/names"
)

// @SDKResource("aws_imagebuilder_workflow", name="Workflow")
// @Tags(identifierAttribute="id")
func ResourceWorkflow() *schema.Resource {
return &schema.Resource{
CreateWithoutTimeout: resourceWorkflowCreate,
ReadWithoutTimeout: resourceWorkflowRead,
UpdateWithoutTimeout: resourceWorkflowUpdate,
DeleteWithoutTimeout: resourceWorkflowDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},

Schema: map[string]*schema.Schema{
"arn": {
Type: schema.TypeString,
Computed: true,
},
"change_description": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validation.StringLenBetween(1, 1024),
},
"data": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
ExactlyOneOf: []string{"data", "uri"},
ValidateFunc: validation.StringLenBetween(1, 16000),
},
"date_created": {
Type: schema.TypeString,
Computed: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validation.StringLenBetween(1, 1024),
},
"kms_key_id": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validation.StringLenBetween(1, 1024),
},
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringLenBetween(1, 128),
},
"owner": {
Type: schema.TypeString,
Computed: true,
},
names.AttrTags: tftags.TagsSchema(),
names.AttrTagsAll: tftags.TagsSchemaComputed(),
"type": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice(imagebuilder.WorkflowType_Values(), false),
},
"uri": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ExactlyOneOf: []string{"data", "uri"},
},
"version": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringMatch(regexache.MustCompile(`^[0-9]+\.[0-9]+\.[0-9]+$`), "valid semantic version must be provided"),
},
},

CustomizeDiff: verify.SetTagsDiff,
}
}

func resourceWorkflowCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).ImageBuilderConn(ctx)

input := &imagebuilder.CreateWorkflowInput{
ClientToken: aws.String(id.UniqueId()),
Name: aws.String(d.Get("name").(string)),
SemanticVersion: aws.String(d.Get("version").(string)),
Type: aws.String(d.Get("type").(string)),
Tags: getTagsIn(ctx),
}

if v, ok := d.GetOk("change_description"); ok {
input.ChangeDescription = aws.String(v.(string))
}

if v, ok := d.GetOk("data"); ok {
input.Data = aws.String(v.(string))
}

if v, ok := d.GetOk("description"); ok {
input.Description = aws.String(v.(string))
}

if v, ok := d.GetOk("kms_key_id"); ok {
input.KmsKeyId = aws.String(v.(string))
}

if v, ok := d.GetOk("uri"); ok {
input.Uri = aws.String(v.(string))
}

output, err := conn.CreateWorkflowWithContext(ctx, input)

if err != nil {
return sdkdiag.AppendErrorf(diags, "creating Image Builder Workflow: %s", err)
}

if output == nil {
return sdkdiag.AppendErrorf(diags, "creating Image Builder Workflow: empty response")
}

d.SetId(aws.StringValue(output.WorkflowBuildVersionArn))

return append(diags, resourceWorkflowRead(ctx, d, meta)...)
}

func resourceWorkflowRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).ImageBuilderConn(ctx)

input := &imagebuilder.GetWorkflowInput{
WorkflowBuildVersionArn: aws.String(d.Id()),
}

output, err := conn.GetWorkflowWithContext(ctx, input)

if !d.IsNewResource() && tfawserr.ErrCodeEquals(err, imagebuilder.ErrCodeResourceNotFoundException) {
log.Printf("[WARN] Image Builder Workflow (%s) not found, removing from state", d.Id())
d.SetId("")
return diags
}

if output == nil || output.Workflow == nil {
return sdkdiag.AppendErrorf(diags, "getting Image Builder Workflow (%s): empty response", d.Id())
}

workflow := output.Workflow

d.Set("arn", workflow.Arn)
d.Set("change_description", workflow.ChangeDescription)
d.Set("data", workflow.Data)
d.Set("date_created", workflow.DateCreated)
d.Set("description", workflow.Description)
d.Set("name", workflow.Name)
d.Set("kms_key_id", workflow.KmsKeyId)
d.Set("owner", workflow.Owner)

setTagsOut(ctx, workflow.Tags)

d.Set("type", workflow.Type)
d.Set("version", workflow.Version)

return diags
}

func resourceWorkflowUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics

// Tags only.

return append(diags, resourceWorkflowRead(ctx, d, meta)...)
}

func resourceWorkflowDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).ImageBuilderConn(ctx)

input := &imagebuilder.DeleteWorkflowInput{
WorkflowBuildVersionArn: aws.String(d.Id()),
}

_, err := conn.DeleteWorkflowWithContext(ctx, input)

if tfawserr.ErrCodeEquals(err, imagebuilder.ErrCodeResourceNotFoundException) {
return diags
}

if err != nil {
return sdkdiag.AppendErrorf(diags, "deleting Image Builder Workflow (%s): %s", d.Id(), err)
}

return diags
}
Loading

0 comments on commit b7fe731

Please sign in to comment.