-
Notifications
You must be signed in to change notification settings - Fork 9
/
resourceclass.go
88 lines (71 loc) · 2.63 KB
/
resourceclass.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
// 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 (
"context"
"fmt"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/internal/frontend/fncue"
"namespacelabs.dev/foundation/internal/parsing"
"namespacelabs.dev/foundation/internal/parsing/invariants"
"namespacelabs.dev/foundation/schema"
"namespacelabs.dev/foundation/std/pkggraph"
)
// Needs to be consistent with JSON names of cueResourceClass fields.
var resourceClassFields = []string{"intent", "produces", "defaultProvider", "description"}
type cueResourceClass struct {
Intent *cueResourceType `json:"intent"`
Produces *cueResourceType `json:"produces"`
DefaultProvider string `json:"defaultProvider"`
Description string `json:"description"`
}
type cueResourceType struct {
Type string `json:"type"`
Source string `json:"source"`
Package string `json:"package"`
}
func parseResourceClass(ctx context.Context, pl parsing.EarlyPackageLoader, loc pkggraph.Location, name string, v *fncue.CueV) (*schema.ResourceClass, error) {
if err := ValidateNoExtraFields(loc, fmt.Sprintf("resource class %q:", name) /* messagePrefix */, v, resourceClassFields); err != nil {
return nil, err
}
var bits cueResourceClass
if err := v.Val.Decode(&bits); err != nil {
return nil, err
}
if bits.Produces == nil {
return nil, fnerrors.NewWithLocation(loc, "resource class %q must specify the provided type", name)
}
if bits.Intent != nil {
return nil, fnerrors.NewWithLocation(loc, "Resource class %q may not specify an intent. Please specify the intent in the resource provider instead.", name)
}
instanceType, err := parseResourceType(ctx, pl, loc, bits.Produces)
if err != nil {
return nil, fnerrors.AttachLocation(loc, err)
}
return &schema.ResourceClass{
Name: name,
InstanceType: instanceType,
DefaultProvider: bits.DefaultProvider,
Description: bits.Description,
}, nil
}
func parseResourceType(ctx context.Context, pl parsing.EarlyPackageLoader, loc pkggraph.Location, t *cueResourceType) (*schema.ResourceType, error) {
if t == nil {
return nil, nil
}
rt := &schema.ResourceType{
ProtoType: t.Type,
ProtoSource: t.Source,
}
if t.Package == "" {
rt.ProtoPackage = loc.PackageName.String()
} else {
rt.ProtoPackage = t.Package
target := schema.PackageName(t.Package)
if err := invariants.EnsurePackageLoaded(ctx, pl, loc.PackageName, target); err != nil {
return nil, err
}
}
return rt, nil
}