-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
rpc.go
299 lines (272 loc) 路 9.07 KB
/
rpc.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
// Copyright 2016-2018, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package pulumi
import (
"reflect"
structpb "github.com/golang/protobuf/ptypes/struct"
"github.com/pkg/errors"
"github.com/spf13/cast"
"github.com/pulumi/pulumi/pkg/resource"
"github.com/pulumi/pulumi/pkg/resource/plugin"
"github.com/pulumi/pulumi/sdk/go/pulumi/asset"
)
// marshalInputs turns resource property inputs into a gRPC struct suitable for marshaling.
func marshalInputs(props map[string]interface{}) (*structpb.Struct, map[string][]URN, []URN, error) {
var depURNs []URN
pmap, pdeps := make(map[string]interface{}), make(map[string][]URN)
for key := range props {
// Get the underlying value, possibly waiting for an output to arrive.
v, resourceDeps, err := marshalInput(props[key])
if err != nil {
return nil, nil, nil, errors.Wrapf(err, "awaiting input property %s", key)
}
pmap[key] = v
// Record all dependencies accumulated from reading this property.
deps := make([]URN, 0, len(resourceDeps))
for _, dep := range resourceDeps {
depURN, err := dep.URN().Value()
if err != nil {
return nil, nil, nil, err
}
deps = append(deps, depURN)
}
pdeps[key] = deps
depURNs = append(depURNs, deps...)
}
// Marshal all properties for the RPC call.
m, err := plugin.MarshalProperties(
resource.NewPropertyMapFromMap(pmap),
plugin.MarshalOptions{KeepUnknowns: true},
)
return m, pdeps, depURNs, err
}
// `gosec` thinks these are credentials, but they are not.
// nolint: gosec
const (
rpcTokenSpecialSigKey = "4dabf18193072939515e22adb298388d"
rpcTokenSpecialAssetSig = "c44067f5952c0a294b673a41bacd8c17"
rpcTokenSpecialArchiveSig = "0def7320c3a5731c473e5ecbe6d01bc7"
rpcTokenSpecialSecretSig = "1b47061264138c4ac30d75fd1eb44270"
rpcTokenUnknownValue = "04da6b54-80e4-46f7-96ec-b56ff0331ba9"
)
// marshalInput marshals an input value, returning its raw serializable value along with any dependencies.
func marshalInput(v interface{}) (interface{}, []Resource, error) {
// If nil, just return that.
if v == nil {
return nil, nil, nil
}
// Next, look for some well known types.
switch t := v.(type) {
case bool, int, uint, int8, uint8, int16, uint16, int32, uint32, int64, uint64, float32, float64, string:
return t, nil, nil
case asset.Asset:
return map[string]interface{}{
rpcTokenSpecialSigKey: rpcTokenSpecialAssetSig,
"path": t.Path(),
"text": t.Text(),
"uri": t.URI(),
}, nil, nil
case asset.Archive:
var assets map[string]interface{}
if as := t.Assets(); as != nil {
assets = make(map[string]interface{})
for k, a := range as {
aa, _, err := marshalInput(a)
if err != nil {
return nil, nil, err
}
assets[k] = aa
}
}
return map[string]interface{}{
rpcTokenSpecialSigKey: rpcTokenSpecialAssetSig,
"assets": assets,
"path": t.Path(),
"uri": t.URI(),
}, nil, nil
case Output:
return marshalInputOutput(&t)
case *Output:
return marshalInputOutput(t)
case CustomResource:
// Resources aren't serializable; instead, serialize a reference to ID, tracking as a dependency.a
e, d, err := marshalInput(t.ID())
if err != nil {
return nil, nil, err
}
return e, append([]Resource{t}, d...), nil
}
// Finally, handle the usual primitives (numbers, strings, arrays, maps, ...)
rv := reflect.ValueOf(v)
switch rk := rv.Type().Kind(); rk {
case reflect.Array, reflect.Slice:
// If an array or a slice, create a new array by recursing into elements.
var arr []interface{}
var deps []Resource
for i := 0; i < rv.Len(); i++ {
elem := rv.Index(i)
e, d, err := marshalInput(elem.Interface())
if err != nil {
return nil, nil, err
}
arr = append(arr, e)
deps = append(deps, d...)
}
return arr, deps, nil
case reflect.Map:
// For maps, only support string-based keys, and recurse into the values.
obj := make(map[string]interface{})
var deps []Resource
for _, key := range rv.MapKeys() {
k, ok := key.Interface().(string)
if !ok {
return nil, nil,
errors.Errorf("expected map keys to be strings; got %v", reflect.TypeOf(key.Interface()))
}
value := rv.MapIndex(key)
mv, d, err := marshalInput(value.Interface())
if err != nil {
return nil, nil, err
}
obj[k] = mv
deps = append(deps, d...)
}
return obj, deps, nil
case reflect.Ptr:
// See if this is an alias for *Output. If so, convert to an *Output, and recurse.
ot := reflect.TypeOf(&Output{})
if rv.Type().ConvertibleTo(ot) {
oo := rv.Convert(ot)
return marshalInput(oo.Interface())
}
// For all other pointers, recurse into the underlying value.
if rv.IsNil() {
return nil, nil, nil
}
return marshalInput(rv.Elem().Interface())
case reflect.String:
return marshalInput(rv.String())
}
return nil, nil, errors.Errorf("unrecognized input property type: %v (%v)", v, reflect.TypeOf(v))
}
func marshalInputOutput(out *Output) (interface{}, []Resource, error) {
// Await the value and return its raw value.
ov, known, err := out.Value()
if err != nil {
return nil, nil, err
}
// If the value is known, marshal it.
if known {
e, d, merr := marshalInput(ov)
if merr != nil {
return nil, nil, merr
}
return e, append(out.Deps(), d...), nil
}
// Otherwise, simply return the unknown value sentinel.
return rpcTokenUnknownValue, out.Deps(), nil
}
// unmarshalOutputs unmarshals all the outputs into a simple map.
func unmarshalOutputs(outs *structpb.Struct) (map[string]interface{}, error) {
outprops, err := plugin.UnmarshalProperties(outs, plugin.MarshalOptions{})
if err != nil {
return nil, err
}
result := make(map[string]interface{})
for k, v := range outprops.Mappable() {
result[k], err = unmarshalOutput(v)
if err != nil {
return nil, err
}
}
return result, nil
}
// unmarshalOutput unmarshals a single output variable into its runtime representation. For the most part, this just
// returns the raw value. In a small number of cases, we need to change a type.
func unmarshalOutput(v interface{}) (interface{}, error) {
// Check for nils and unknowns.
if v == nil || v == rpcTokenUnknownValue {
return nil, nil
}
// In the case of assets and archives, turn these into real asset and archive structures.
if m, ok := v.(map[string]interface{}); ok {
if sig, hasSig := m[rpcTokenSpecialSigKey]; hasSig {
switch sig {
case rpcTokenSpecialAssetSig:
if path := m["path"]; path != nil {
return asset.NewFileAsset(cast.ToString(path)), nil
} else if text := m["text"]; text != nil {
return asset.NewStringAsset(cast.ToString(text)), nil
} else if uri := m["uri"]; uri != nil {
return asset.NewRemoteAsset(cast.ToString(uri)), nil
}
return nil, errors.New("expected asset to be one of File, String, or Remote; got none")
case rpcTokenSpecialArchiveSig:
if assets := m["assets"]; assets != nil {
as := make(map[string]interface{})
for k, v := range assets.(map[string]interface{}) {
a, err := unmarshalOutput(v)
if err != nil {
return nil, err
}
as[k] = a
}
return asset.NewAssetArchive(as), nil
} else if path := m["path"]; path != nil {
return asset.NewFileArchive(cast.ToString(path)), nil
} else if uri := m["uri"]; uri != nil {
return asset.NewRemoteArchive(cast.ToString(uri)), nil
}
return nil, errors.New("expected asset to be one of File, String, or Remote; got none")
case rpcTokenSpecialSecretSig:
return nil, errors.New("this version of the Pulumi SDK does not support first-class secrets")
default:
return nil, errors.Errorf("unrecognized signature '%v' in output value", sig)
}
}
}
// For arrays and maps, just make sure to transform them deeply.
rv := reflect.ValueOf(v)
switch rk := rv.Type().Kind(); rk {
case reflect.Array, reflect.Slice:
// If an array or a slice, create a new array by recursing into elements.
var arr []interface{}
for i := 0; i < rv.Len(); i++ {
elem := rv.Index(i)
e, err := unmarshalOutput(elem.Interface())
if err != nil {
return nil, err
}
arr = append(arr, e)
}
return arr, nil
case reflect.Map:
// For maps, only support string-based keys, and recurse into the values.
obj := make(map[string]interface{})
for _, key := range rv.MapKeys() {
k, ok := key.Interface().(string)
if !ok {
return nil, errors.Errorf("expected map keys to be strings; got %v", reflect.TypeOf(key.Interface()))
}
value := rv.MapIndex(key)
mv, err := unmarshalOutput(value)
if err != nil {
return nil, err
}
obj[k] = mv
}
return obj, nil
}
return v, nil
}