forked from jcarrothers-sap/terraform-provider-random
/
resource_pet.go
62 lines (49 loc) · 1.06 KB
/
resource_pet.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package random
import (
"fmt"
"strings"
"github.com/dustinkirkland/golang-petname"
"github.com/hashicorp/terraform/helper/schema"
)
func resourcePet() *schema.Resource {
return &schema.Resource{
Create: CreatePet,
Read: schema.Noop,
Delete: schema.RemoveFromState,
Schema: map[string]*schema.Schema{
"keepers": {
Type: schema.TypeMap,
Optional: true,
ForceNew: true,
},
"length": {
Type: schema.TypeInt,
Optional: true,
Default: 2,
ForceNew: true,
},
"prefix": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"separator": {
Type: schema.TypeString,
Optional: true,
Default: "-",
ForceNew: true,
},
},
}
}
func CreatePet(d *schema.ResourceData, meta interface{}) error {
length := d.Get("length").(int)
separator := d.Get("separator").(string)
prefix := d.Get("prefix").(string)
pet := strings.ToLower(petname.Generate(length, separator))
if prefix != "" {
pet = fmt.Sprintf("%s%s%s", prefix, separator, pet)
}
d.SetId(pet)
return nil
}