diff --git a/docs/resources/pet.md b/docs/resources/pet.md index 536681a5..7e1f0253 100644 --- a/docs/resources/pet.md +++ b/docs/resources/pet.md @@ -54,4 +54,11 @@ resource "aws_instance" "server" { - `id` (String) The random pet name +## Import +Random Pet can be imported by just specifying the value: + +Example: +``` +$ terraform import random_pet.example pet_name +``` diff --git a/internal/provider/resource_pet.go b/internal/provider/resource_pet.go index 0d29f33d..7e84fd71 100644 --- a/internal/provider/resource_pet.go +++ b/internal/provider/resource_pet.go @@ -26,6 +26,9 @@ func resourcePet() *schema.Resource { CreateContext: CreatePet, ReadContext: schema.NoopContext, DeleteContext: RemoveResourceFromState, + Importer: &schema.ResourceImporter{ + StateContext: importPet, + }, Schema: map[string]*schema.Schema{ "keepers": { @@ -83,3 +86,19 @@ func CreatePet(_ context.Context, d *schema.ResourceData, meta interface{}) diag return nil } + +func importPet(ctx context.Context, d *schema.ResourceData, _ interface{}) ([]*schema.ResourceData, error) { + + val := d.Id() + d.SetId(val) + + if err := d.Set("length", len(val)); err != nil { + return nil, fmt.Errorf("error setting result: %w", err) + } + + if err := d.Set("separator", "-"); err != nil { + return nil, fmt.Errorf("error setting result: %w", err) + } + + return []*schema.ResourceData{d}, nil +}