-
Notifications
You must be signed in to change notification settings - Fork 8
/
resourceprovider.go
92 lines (76 loc) · 2.71 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
// 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 cuefrontendopaque
import (
"context"
"golang.org/x/exp/slices"
"namespacelabs.dev/foundation/framework/rpcerrors/multierr"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/internal/frontend/cuefrontend"
"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"
)
type cueResourceProvider struct {
ResourceInputs map[string]string `json:"inputs"` // Key: name, Value: serialized class ref
// TODO: parse prepare hook.
}
func parseResourceProvider(ctx context.Context, env *schema.Environment, pl parsing.EarlyPackageLoader, pkg *pkggraph.Package, key string, v *fncue.CueV) (*schema.ResourceProvider, error) {
var bits cueResourceProvider
if err := v.Val.Decode(&bits); err != nil {
return nil, err
}
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
}
rp := &schema.ResourceProvider{
ProvidesClass: classRef,
InitializedWith: initializedWithInvocation,
}
if resources := v.LookupPath("resources"); resources.Exists() {
resourceList, err := cuefrontend.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)
if err != nil {
errs = append(errs, err)
} else {
rp.ResourceInput = append(rp.ResourceInput, &schema.ResourceProvider_ResourceInput{
Name: schema.MakePackageRef(pkg.PackageName(), key),
Class: class,
})
}
}
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
}