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

Add import support for random id and random integer #20

Merged
merged 4 commits into from
Mar 21, 2018
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
24 changes: 24 additions & 0 deletions random/resource_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/hex"
"errors"
"math/big"
"strings"

"github.com/hashicorp/errwrap"
"github.com/hashicorp/terraform/helper/schema"
Expand All @@ -16,6 +17,9 @@ func resourceId() *schema.Resource {
Create: CreateID,
Read: RepopulateEncodings,
Delete: schema.RemoveFromState,
Importer: &schema.ResourceImporter{
State: ImportID,
},

Schema: map[string]*schema.Schema{
"keepers": {
Expand Down Expand Up @@ -108,3 +112,23 @@ func RepopulateEncodings(d *schema.ResourceData, _ interface{}) error {

return nil
}

func ImportID(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
id := d.Id()

sep := strings.LastIndex(id, ",")
if sep != -1 {
d.Set("prefix", id[:sep])
id = id[sep+1:]
}

bytes, err := base64.RawURLEncoding.DecodeString(id)
if err != nil {
return nil, errwrap.Wrapf("Error decoding ID: {{err}}", err)
}

d.Set("byte_length", len(bytes))
d.SetId(id)

return []*schema.ResourceData{d}, nil
}
13 changes: 12 additions & 1 deletion random/resource_id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,17 @@ func TestAccResourceID(t *testing.T) {
}),
),
},
{
ResourceName: "random_id.foo",
ImportState: true,
ImportStateVerify: true,
},
{
ResourceName: "random_id.bar",
ImportState: true,
ImportStateIdPrefix: "cloud-,",
ImportStateVerify: true,
},
},
})
}
Expand Down Expand Up @@ -85,7 +96,7 @@ resource "random_id" "foo" {

resource "random_id" "bar" {
byte_length = 4
prefix = "cloud-"
prefix = "cloud-"
}
`
)
37 changes: 37 additions & 0 deletions random/resource_integer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package random
import (
"fmt"
"strconv"
"strings"

"github.com/hashicorp/errwrap"
"github.com/hashicorp/terraform/helper/schema"
)

Expand All @@ -12,6 +14,9 @@ func resourceInteger() *schema.Resource {
Create: CreateInteger,
Read: RepopulateInteger,
Delete: schema.RemoveFromState,
Importer: &schema.ResourceImporter{
State: ImportInteger,
},

Schema: map[string]*schema.Schema{
"keepers": {
Expand Down Expand Up @@ -66,3 +71,35 @@ func CreateInteger(d *schema.ResourceData, meta interface{}) error {
func RepopulateInteger(d *schema.ResourceData, _ interface{}) error {
return nil
}

func ImportInteger(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
parts := strings.Split(d.Id(), ",")
if len(parts) != 3 && len(parts) != 4 {
return nil, fmt.Errorf("Invalid import usage: expecting {result},{min},{max} or {result},{min},{max},{seed}")
}

result, err := strconv.Atoi(parts[0])
if err != nil {
return nil, errwrap.Wrapf("Error parsing \"result\": {{err}}", err)
}
d.Set("result", result)

min, err := strconv.Atoi(parts[1])
if err != nil {
return nil, errwrap.Wrapf("Error parsing \"min\": {{err}}", err)
}
d.Set("min", min)

max, err := strconv.Atoi(parts[2])
if err != nil {
return nil, errwrap.Wrapf("Error parsing \"max\": {{err}}", err)
}
d.Set("max", max)

if len(parts) == 4 {
d.Set("seed", parts[3])
}
d.SetId(parts[0])

return []*schema.ResourceData{d}, nil
}
6 changes: 6 additions & 0 deletions random/resource_integer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ func TestAccResourceIntegerBasic(t *testing.T) {
testAccResourceIntegerBasic("random_integer.integer_1"),
),
},
{
ResourceName: "random_integer.integer_1",
ImportState: true,
ImportStateId: "3,1,3,12345",
ImportStateVerify: true,
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally, we'd see a version of this that didn't set the Seed, just to exercise that code path, too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a difficult test to write: the import tests create a resource and then import it and check that the result matches. How would you go about guaranteeing that the resource will be created in that first step with a particular result without the seed?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a fair question! Request rescinded.

},
})
}
Expand Down
15 changes: 15 additions & 0 deletions website/docs/r/id.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,18 @@ The following attributes are exported:
* `b64_std` - The generated id presented in base64 without additional transformations.
* `hex` - The generated id presented in padded hexadecimal digits. This result will always be twice as long as the requested byte length.
* `dec` - The generated id presented in non-padded decimal digits.

## Import

Random Ids can be imported using the `b64_url` with an optional `prefix`. This can be used to replace a config value with a value
interpolated from the random provider without experiencing diffs.

Example with no prefix:
```
$ terraform import random_id.server p-9hUg
```

Example with prefix (prefix is separated by a `,`):
```
$ terraform import random_id.server my-prefix-,p-9hUg
```
10 changes: 10 additions & 0 deletions website/docs/r/integer.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,13 @@ The following attributes are supported:

* `id` - (string) An internal id.
* `result` - (int) The random Integer result.

## Import

Random integers can be imported using the `result`, `min`, and `max`, with an optional `seed`.
This can be used to replace a config value with a value interpolated from the random provider without experiencing diffs.

Example (values are separated by a `,`):
```
$ terraform import random_integer.priority 15390,1,99999
```