Skip to content

Commit

Permalink
Merge pull request #20 from danawillow/import
Browse files Browse the repository at this point in the history
Add import support for random id and random integer
  • Loading branch information
paddycarver authored Mar 21, 2018
2 parents b45ba5a + 64f0203 commit 2f0b6b1
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 1 deletion.
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,
},
},
})
}
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
```

0 comments on commit 2f0b6b1

Please sign in to comment.