From 5549716fe16ad6fd1623bff9d154b17a934190d0 Mon Sep 17 00:00:00 2001 From: AJ Date: Thu, 13 Jun 2024 06:59:23 +0100 Subject: [PATCH] add import to random_pet resource #184 --- docs/resources/pet.md | 10 ++++++ examples/resources/random_pet/import.sh | 3 ++ internal/provider/resource_pet.go | 42 ++++++++++++++++++++++++- 3 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 examples/resources/random_pet/import.sh diff --git a/docs/resources/pet.md b/docs/resources/pet.md index e740eb55..a1cbbff6 100644 --- a/docs/resources/pet.md +++ b/docs/resources/pet.md @@ -53,3 +53,13 @@ resource "aws_instance" "server" { ### Read-Only - `id` (String) The random pet name. + +## Import + +Import is supported using the following syntax: + +```shell +# Random Pet can be imported with format: pet_name,separator,prefix + +terraform import random_pet.example pet_name,separator,prefix +``` diff --git a/examples/resources/random_pet/import.sh b/examples/resources/random_pet/import.sh new file mode 100644 index 00000000..eddd75d2 --- /dev/null +++ b/examples/resources/random_pet/import.sh @@ -0,0 +1,3 @@ +# Random Pet can be imported with format: pet_name,separator,prefix + +terraform import random_pet.example pet_name,separator,prefix \ No newline at end of file diff --git a/internal/provider/resource_pet.go b/internal/provider/resource_pet.go index e6bf758e..dd89278d 100644 --- a/internal/provider/resource_pet.go +++ b/internal/provider/resource_pet.go @@ -21,7 +21,10 @@ import ( mapplanmodifiers "github.com/terraform-providers/terraform-provider-random/internal/planmodifiers/map" ) -var _ resource.Resource = (*petResource)(nil) +var ( + _ resource.Resource = (*petResource)(nil) + _ resource.ResourceWithImportState = (*petResource)(nil) +) func NewPetResource() resource.Resource { return &petResource{} @@ -151,6 +154,43 @@ func (r *petResource) Update(ctx context.Context, req resource.UpdateRequest, re func (r *petResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { } +func (r *petResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { + parts := strings.Split(req.ID, ",") + if len(parts) != 3 { + resp.Diagnostics.AddError( + "Unexpected Import Identifier", + fmt.Sprintf("Expected import identifier with format: pet_name,separator,prefix. Got: %q", req.ID), + ) + return + } + + id, separator, prefix := parts[0], parts[1], parts[2] + if separator == "" { + separator = "-" + } + + nameLength := len(strings.Split(id, separator)) + + prefixVal := types.StringNull() + if prefix != "" { + prefixVal = types.StringValue(prefix) + } + + state := petModelV0{ + ID: types.StringValue(id), + Length: types.Int64Value(int64(nameLength)), + Prefix: prefixVal, + Separator: types.StringValue(separator), + Keepers: types.MapNull(types.StringType), + } + + diags := resp.State.Set(ctx, &state) + resp.Diagnostics.Append(diags...) + if resp.Diagnostics.HasError() { + return + } +} + type petModelV0 struct { ID types.String `tfsdk:"id"` Keepers types.Map `tfsdk:"keepers"`