Skip to content

Commit

Permalink
add import to random_pet resource hashicorp#184
Browse files Browse the repository at this point in the history
  • Loading branch information
wakeful committed Jun 8, 2022
1 parent b7b4a99 commit 5c9c363
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
8 changes: 8 additions & 0 deletions docs/resources/pet.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,12 @@ resource "aws_instance" "server" {

- `id` (String) The random pet name

## Import

Import is supported using the following syntax:

```shell
# Random Pet can be imported by just specifying the value:

terraform import random_pet.example pet_name
```
3 changes: 3 additions & 0 deletions examples/resources/random_pet/import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Random Pet can be imported by just specifying the value:

terraform import random_pet.example pet_name
19 changes: 19 additions & 0 deletions internal/provider/resource_pet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down Expand Up @@ -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
}

0 comments on commit 5c9c363

Please sign in to comment.