-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
resource_state_func.go
118 lines (103 loc) · 2.87 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package test
import (
"crypto/sha1"
"encoding/hex"
"fmt"
"math/rand"
"github.com/hashicorp/terraform/helper/hashcode"
"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,
},
// set block with computed elements
"set_block": {
Type: schema.TypeSet,
Optional: true,
Set: setBlockHash,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"required": {
Type: schema.TypeString,
Required: true,
},
"optional": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
},
},
},
},
}
}
func stateFuncHash(v interface{}) string {
hash := sha1.Sum([]byte(v.(string)))
return hex.EncodeToString(hash[:])
}
func setBlockHash(v interface{}) int {
m := v.(map[string]interface{})
required, _ := m["required"].(string)
optional, _ := m["optional"].(string)
return hashcode.String(fmt.Sprintf("%s|%s", required, optional))
}
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)
}
}
// Check that we can lookup set elements by our computed hash.
// This is not advised, but we can use this to make sure the final diff was
// prepared with the correct values.
setBlock, ok := d.GetOk("set_block")
if ok {
set := setBlock.(*schema.Set)
for _, obj := range set.List() {
idx := setBlockHash(obj)
requiredAddr := fmt.Sprintf("%s.%d.%s", "set_block", idx, "required")
_, ok := d.GetOkExists(requiredAddr)
if !ok {
return fmt.Errorf("failed to get attr %q from %#v", fmt.Sprintf(requiredAddr), d.State().Attributes)
}
}
}
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
}