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