/
resource_state_func.go
74 lines (62 loc) · 1.64 KB
/
resource_state_func.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
63
64
65
66
67
68
69
70
71
72
73
74
package test
import (
"crypto/sha1"
"encoding/hex"
"fmt"
"math/rand"
"github.com/hashicorp/terraform/helper/schema"
)
func testResourceStateFunc() *schema.Resource {
return &schema.Resource{
Create: testResourceStateFuncCreate,
Read: testResourceStateFuncRead,
Update: testResourceStateFuncUpdate,
Delete: testResourceStateFuncDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"optional": {
Type: schema.TypeString,
Optional: true,
},
"state_func": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
StateFunc: stateFuncHash,
},
"state_func_value": {
Type: schema.TypeString,
Optional: true,
},
},
}
}
func stateFuncHash(v interface{}) string {
hash := sha1.Sum([]byte(v.(string)))
return hex.EncodeToString(hash[:])
}
func testResourceStateFuncCreate(d *schema.ResourceData, meta interface{}) error {
d.SetId(fmt.Sprintf("%x", rand.Int63()))
// if we have a reference for the actual data in the state_func field,
// compare it
if data, ok := d.GetOk("state_func_value"); ok {
expected := data.(string)
got := d.Get("state_func").(string)
if expected != got {
return fmt.Errorf("expected state_func value:%q, got%q", expected, got)
}
}
return testResourceStateFuncRead(d, meta)
}
func testResourceStateFuncRead(d *schema.ResourceData, meta interface{}) error {
return nil
}
func testResourceStateFuncUpdate(d *schema.ResourceData, meta interface{}) error {
return nil
}
func testResourceStateFuncDelete(d *schema.ResourceData, meta interface{}) error {
d.SetId("")
return nil
}