-
Notifications
You must be signed in to change notification settings - Fork 9
/
resourceprovider.go
173 lines (141 loc) · 4.7 KB
/
resourceprovider.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
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package cuefrontend
import (
"bytes"
"context"
"encoding/json"
"fmt"
"golang.org/x/exp/slices"
"namespacelabs.dev/foundation/framework/rpcerrors/multierr"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/internal/frontend/cuefrontend/binary"
"namespacelabs.dev/foundation/internal/frontend/fncue"
"namespacelabs.dev/foundation/internal/parsing"
"namespacelabs.dev/foundation/schema"
"namespacelabs.dev/foundation/std/pkggraph"
)
var resourceProviderFields = []string{
// Needs to contain JSON names of cueResourceProvider fields.
"inputs", "intent", "availableClasses", "availablePackages",
"initializedWith", "resourcesFrom", "resources", "prepareWith",
}
type cueResourceProvider struct {
ResourceInputs map[string]cueResourceProviderInput `json:"inputs"`
Intent *cueResourceType `json:"intent,omitempty"`
AvailableClasses []string `json:"availableClasses"`
AvailablePackages []string `json:"availablePackages"`
// TODO: parse prepare hook.
}
type cueResourceProviderInput struct {
Class string
Default string
}
func parseResourceProvider(ctx context.Context, env *schema.Environment, pl parsing.EarlyPackageLoader, pkg *pkggraph.Package, key string, v *fncue.CueV) (*schema.ResourceProvider, error) {
if err := ValidateNoExtraFields(pkg.Location, fmt.Sprintf("resource provider %q:", key) /* messagePrefix */, v, resourceProviderFields); err != nil {
return nil, err
}
var bits cueResourceProvider
if err := v.Val.Decode(&bits); err != nil {
return nil, err
}
if bits.Intent == nil {
return nil, fnerrors.NewWithLocation(pkg.Location, "resource provider %q is missing an intent", key)
}
classRef, err := schema.ParsePackageRef(pkg.PackageName(), key)
if err != nil {
return nil, err
}
initializedWithInvocation, err := binary.ParseBinaryInvocationField(ctx, env, pl, pkg, "genb-res-init-"+key /* binaryName */, "initializedWith" /* cuePath */, v)
if err != nil {
return nil, err
}
resourcesFrom, err := binary.ParseBinaryInvocationField(ctx, env, pl, pkg, "genb-res-resfrom-"+key /* binaryName */, "resourcesFrom" /* cuePath */, v)
if err != nil {
return nil, err
}
rp := &schema.ResourceProvider{
PackageName: pkg.PackageName().String(),
ProvidesClass: classRef,
InitializedWith: initializedWithInvocation,
ResourcesFrom: resourcesFrom,
AvailablePackages: bits.AvailablePackages,
IntentType: parseResourceType(bits.Intent),
}
for _, x := range bits.AvailableClasses {
ref, err := schema.ParsePackageRef(pkg.PackageName(), x)
if err != nil {
return nil, err
}
rp.AvailableClasses = append(rp.AvailableClasses, ref)
}
if resources := v.LookupPath("resources"); resources.Exists() {
resourceList, err := ParseResourceList(resources)
if err != nil {
return nil, fnerrors.NewWithLocation(pkg.Location, "parsing resources failed: %w", err)
}
pack, err := resourceList.ToPack(ctx, env, pl, pkg)
if err != nil {
return nil, err
}
rp.ResourcePack = pack
}
var errs []error
for key, value := range bits.ResourceInputs {
class, err := schema.ParsePackageRef(pkg.PackageName(), value.Class)
if err != nil {
errs = append(errs, err)
} else {
input := &schema.ResourceProvider_ResourceInput{
Name: schema.MakePackageRef(pkg.PackageName(), key),
Class: class,
}
if value.Default != "" {
parsed, err := schema.ParsePackageRef(pkg.PackageName(), value.Default)
if err != nil {
errs = append(errs, err)
} else {
input.DefaultResource = parsed
}
}
rp.ResourceInput = append(rp.ResourceInput, input)
}
}
if err := multierr.New(errs...); err != nil {
return nil, err
}
slices.SortFunc(rp.ResourceInput, func(a, b *schema.ResourceProvider_ResourceInput) bool {
x := a.Name.Compare(b.Name)
if x == 0 {
return a.Class.Compare(b.Class) < 0
}
return x < 0
})
rp.PrepareWith, err = binary.ParseBinaryInvocationField(ctx, env, pl, pkg, "genb-res-prep-"+key /* binaryName */, "prepareWith" /* cuePath */, v)
if err != nil {
return nil, err
}
return rp, nil
}
func (input *cueResourceProviderInput) UnmarshalJSON(data []byte) error {
dec := json.NewDecoder(bytes.NewReader(data))
tok, err := dec.Token()
if err != nil {
return err
}
if v, ok := tok.(string); ok {
input.Class = v
return nil
}
var values struct {
Class string `json:"class"`
Default string `json:"default"`
}
if err := json.Unmarshal(data, &values); err != nil {
return err
}
input.Class = values.Class
input.Default = values.Default
return nil
}