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_workflow: new resource #35097

Merged
merged 9 commits into from
Jan 3, 2024
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/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
Loading