2016-05-14 23:00:58 +02:00
|
|
|
package random
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/hex"
|
2016-11-06 16:58:24 +01:00
|
|
|
"errors"
|
2016-05-14 23:00:58 +02:00
|
|
|
"math/big"
|
|
|
|
|
2016-11-06 16:58:24 +01:00
|
|
|
"github.com/hashicorp/errwrap"
|
2016-05-14 23:00:58 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
)
|
|
|
|
|
|
|
|
func resourceId() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: CreateID,
|
2016-11-06 23:57:15 +01:00
|
|
|
Read: RepopulateEncodings,
|
2016-11-06 16:58:24 +01:00
|
|
|
Delete: schema.RemoveFromState,
|
2016-05-14 23:00:58 +02:00
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
2016-11-06 16:58:24 +01:00
|
|
|
"keepers": {
|
2016-05-14 23:00:58 +02:00
|
|
|
Type: schema.TypeMap,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2016-11-06 16:58:24 +01:00
|
|
|
"byte_length": {
|
2016-05-14 23:00:58 +02:00
|
|
|
Type: schema.TypeInt,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2016-11-06 16:58:24 +01:00
|
|
|
"b64": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
Deprecated: "Use b64_url for old behavior, or b64_std for standard base64 encoding",
|
|
|
|
},
|
|
|
|
|
|
|
|
"b64_url": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"b64_std": {
|
2016-05-14 23:00:58 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
2016-11-06 16:58:24 +01:00
|
|
|
"hex": {
|
2016-05-14 23:00:58 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
2016-11-06 16:58:24 +01:00
|
|
|
"dec": {
|
2016-05-14 23:00:58 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-06 23:57:15 +01:00
|
|
|
func CreateID(d *schema.ResourceData, meta interface{}) error {
|
2016-05-14 23:00:58 +02:00
|
|
|
byteLength := d.Get("byte_length").(int)
|
|
|
|
bytes := make([]byte, byteLength)
|
|
|
|
|
|
|
|
n, err := rand.Reader.Read(bytes)
|
|
|
|
if n != byteLength {
|
2016-11-06 16:58:24 +01:00
|
|
|
return errors.New("generated insufficient random bytes")
|
2016-05-14 23:00:58 +02:00
|
|
|
}
|
|
|
|
if err != nil {
|
2016-11-06 16:58:24 +01:00
|
|
|
return errwrap.Wrapf("error generating random bytes: {{err}}", err)
|
2016-05-14 23:00:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
b64Str := base64.RawURLEncoding.EncodeToString(bytes)
|
2016-11-06 23:57:15 +01:00
|
|
|
d.SetId(b64Str)
|
|
|
|
|
|
|
|
return RepopulateEncodings(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func RepopulateEncodings(d *schema.ResourceData, _ interface{}) error {
|
|
|
|
base64Str := d.Id()
|
|
|
|
|
|
|
|
bytes, err := base64.RawURLEncoding.DecodeString(base64Str)
|
|
|
|
if err != nil {
|
|
|
|
return errwrap.Wrapf("Error decoding ID: {{err}}", err)
|
|
|
|
}
|
|
|
|
|
2016-11-06 16:58:24 +01:00
|
|
|
b64StdStr := base64.StdEncoding.EncodeToString(bytes)
|
2016-05-14 23:00:58 +02:00
|
|
|
hexStr := hex.EncodeToString(bytes)
|
|
|
|
|
2016-11-06 16:58:24 +01:00
|
|
|
bigInt := big.Int{}
|
|
|
|
bigInt.SetBytes(bytes)
|
|
|
|
decStr := bigInt.String()
|
2016-05-14 23:00:58 +02:00
|
|
|
|
2016-11-06 23:57:15 +01:00
|
|
|
d.Set("b64", base64Str)
|
|
|
|
d.Set("b64_url", base64Str)
|
2016-11-06 16:58:24 +01:00
|
|
|
d.Set("b64_std", b64StdStr)
|
|
|
|
|
2016-05-14 23:00:58 +02:00
|
|
|
d.Set("hex", hexStr)
|
|
|
|
d.Set("dec", decStr)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|