forked from crossplane/function-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sdk_test.go
80 lines (61 loc) · 2.75 KB
/
sdk_test.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
/*
Copyright 2023 The Crossplane Authors.
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 function
import (
"fmt"
"google.golang.org/protobuf/encoding/protojson"
"github.com/crossplane/function-sdk-go/proto/v1beta1"
"github.com/crossplane/function-sdk-go/request"
"github.com/crossplane/function-sdk-go/resource"
"github.com/crossplane/function-sdk-go/resource/composed"
"github.com/crossplane/function-sdk-go/response"
)
var req = &v1beta1.RunFunctionRequest{
Observed: &v1beta1.State{
Composite: &v1beta1.Resource{
Resource: resource.MustStructJSON(`{"spec":{"widgets":9001}}`),
},
},
}
func Example() {
// Create a response to the request passed to your RunFunction method.
rsp := response.To(req, response.DefaultTTL)
// Get the observed composite resource (XR) from the request.
oxr, _ := request.GetObservedCompositeResource(req)
// Read the desired number of widgets from our observed XR.
widgets, _ := oxr.Resource.GetInteger("spec.widgets")
// Get any existing desired composed resources from the request.
// Desired composed resources would exist if a previous Function in the
// pipeline added them.
desired, _ := request.GetDesiredComposedResources(req)
// Create a desired composed resource using unstructured data.
desired["new"] = &resource.DesiredComposed{Resource: composed.New()}
desired["new"].Resource.SetAPIVersion("example.org/v1")
desired["new"].Resource.SetKind("CoolResource")
// Set the desired composed resource's widgets to the value extracted from
// the observed XR.
desired["new"].Resource.SetInteger("spec.widgets", widgets)
// Create a desired composed resource using structured data.
// db, _ := composed.From(&v1beta1.Instance{})
// desired["database"] = &resource.DesiredComposed{Resource: db}
// Add a label to our new desired resource, and any other.
for _, r := range desired {
r.Resource.SetLabels(map[string]string{"coolness": "high"})
}
// Set our updated desired composed resource in the response we'll return.
_ = response.SetDesiredComposedResources(rsp, desired)
j, _ := protojson.Marshal(rsp)
fmt.Println(string(j))
// Output:
// {"meta":{"ttl":"60s"},"desired":{"resources":{"new":{"resource":{"apiVersion":"example.org/v1","kind":"CoolResource","metadata":{"labels":{"coolness":"high"}},"spec":{"widgets":9001}}}}}}
}