-
Notifications
You must be signed in to change notification settings - Fork 43
/
resource.go
161 lines (137 loc) · 3.86 KB
/
resource.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package sdkv2
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
shim "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfshim"
)
var (
_ = shim.Resource(v2Resource{})
_ = shim.ResourceMap(v2ResourceMap{})
)
type v2Resource struct {
tf *schema.Resource
}
func NewResource(r *schema.Resource) shim.Resource {
return v2Resource{r}
}
func (r v2Resource) Schema() shim.SchemaMap {
return v2SchemaMap(r.tf.SchemaMap())
}
func (r v2Resource) SchemaVersion() int {
return r.tf.SchemaVersion
}
//nolint:staticcheck
func (r v2Resource) Importer() shim.ImportFunc {
if r.tf.Importer == nil {
return nil
}
return func(t, id string, meta interface{}) ([]shim.InstanceState, error) {
data := r.tf.Data(nil)
data.SetId(id)
data.SetType(t)
var v2Results []*schema.ResourceData
var err error
switch {
case r.tf.Importer.State != nil:
v2Results, err = r.tf.Importer.State(data, meta)
case r.tf.Importer.StateContext != nil:
v2Results, err = r.tf.Importer.StateContext(context.TODO(), data, meta)
}
if err != nil {
return nil, err
}
results := make([]shim.InstanceState, len(v2Results))
for i, v := range v2Results {
s := v.State()
if s == nil {
return nil, fmt.Errorf("importer for %s returned a empty resource state. This is always "+
"the result of a bug in the resource provider - please report this "+
"as a bug in the Pulumi provider repository.", id)
}
if s.Attributes != nil {
results[i] = v2InstanceState{r.tf, s, nil}
}
}
return results, nil
}
}
func (r v2Resource) DeprecationMessage() string {
return r.tf.DeprecationMessage
}
func (r v2Resource) Timeouts() *shim.ResourceTimeout {
if r.tf.Timeouts == nil {
return nil
}
return &shim.ResourceTimeout{
Create: r.tf.Timeouts.Create,
Read: r.tf.Timeouts.Read,
Update: r.tf.Timeouts.Update,
Delete: r.tf.Timeouts.Delete,
Default: r.tf.Timeouts.Default,
}
}
func (r v2Resource) InstanceState(id string, object, meta map[string]interface{}) (shim.InstanceState, error) {
// Read each top-level value out of the object using a ConfigFieldReader and recursively flatten
// them into their TF attribute form. The result is our set of TF attributes.
config := &terraform.ResourceConfig{Raw: object, Config: object}
attributes := map[string]string{}
reader := &schema.ConfigFieldReader{Config: config, Schema: r.tf.SchemaMap()}
for k := range r.tf.SchemaMap() {
// Elide nil values.
if v, ok := object[k]; ok && v == nil {
continue
}
f, err := reader.ReadField([]string{k})
if err != nil {
return nil, fmt.Errorf("could not read field %v: %w", k, err)
}
flattenValue(attributes, k, f.Value)
}
return v2InstanceState{
r.tf,
&terraform.InstanceState{
ID: id,
Attributes: attributes,
Meta: meta,
}, nil,
}, nil
}
func (r v2Resource) DecodeTimeouts(config shim.ResourceConfig) (*shim.ResourceTimeout, error) {
v2Timeouts := &schema.ResourceTimeout{}
if err := v2Timeouts.ConfigDecode(r.tf, configFromShim(config)); err != nil {
return nil, err
}
return &shim.ResourceTimeout{
Create: v2Timeouts.Create,
Read: v2Timeouts.Read,
Update: v2Timeouts.Update,
Delete: v2Timeouts.Delete,
Default: v2Timeouts.Default,
}, nil
}
type v2ResourceMap map[string]*schema.Resource
func (m v2ResourceMap) Len() int {
return len(m)
}
func (m v2ResourceMap) Get(key string) shim.Resource {
r, _ := m.GetOk(key)
return r
}
func (m v2ResourceMap) GetOk(key string) (shim.Resource, bool) {
if r, ok := m[key]; ok {
return v2Resource{r}, true
}
return nil, false
}
func (m v2ResourceMap) Range(each func(key string, value shim.Resource) bool) {
for key, value := range m {
if !each(key, v2Resource{value}) {
return
}
}
}
func (m v2ResourceMap) Set(key string, value shim.Resource) {
m[key] = value.(v2Resource).tf
}