-
Notifications
You must be signed in to change notification settings - Fork 9.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35097 from kamilturek/f-aws-imagebuilder-workflow
r/aws_imagebuilder_workflow: new resource
- Loading branch information
Showing
5 changed files
with
922 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```release-note:new-resource | ||
aws_imagebuilder_workflow | ||
``` |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.