Skip to content

Commit d69e7c6

Browse files
authored
Adds demo action service (#35)
* Adds demo action service * More actions and capabilities * Verion update of sdk * latest
1 parent 0aa210d commit d69e7c6

File tree

5 files changed

+379
-3
lines changed

5 files changed

+379
-3
lines changed

baton_capabilities.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@
6060
"CAPABILITY_ACCOUNT_PROVISIONING",
6161
"CAPABILITY_CREDENTIAL_ROTATION",
6262
"CAPABILITY_RESOURCE_CREATE",
63-
"CAPABILITY_RESOURCE_DELETE"
63+
"CAPABILITY_RESOURCE_DELETE",
64+
"CAPABILITY_ACTIONS"
6465
],
6566
"credentialDetails": {
6667
"capabilityAccountProvisioning": {

go.mod

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module github.com/conductorone/baton-demo
22

3-
go 1.23
3+
go 1.23.4
4+
45
toolchain go1.24.1
56

67
require (
@@ -10,6 +11,7 @@ require (
1011
github.com/segmentio/ksuid v1.0.4
1112
github.com/spf13/viper v1.19.0
1213
go.uber.org/zap v1.27.0
14+
google.golang.org/protobuf v1.36.5
1315
)
1416

1517
require (
@@ -103,7 +105,6 @@ require (
103105
google.golang.org/genproto/googleapis/api v0.0.0-20250127172529-29210b9bc287 // indirect
104106
google.golang.org/genproto/googleapis/rpc v0.0.0-20250219182151-9fdb1cabc7b2 // indirect
105107
google.golang.org/grpc v1.70.0 // indirect
106-
google.golang.org/protobuf v1.36.5 // indirect
107108
gopkg.in/ini.v1 v1.67.0 // indirect
108109
gopkg.in/yaml.v2 v2.4.0 // indirect
109110
gopkg.in/yaml.v3 v3.0.1 // indirect

pkg/connector/actions.go

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package connector
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"time"
7+
8+
config "github.com/conductorone/baton-sdk/pb/c1/config/v1"
9+
v2 "github.com/conductorone/baton-sdk/pb/c1/connector/v2"
10+
"github.com/conductorone/baton-sdk/pkg/actions"
11+
"github.com/conductorone/baton-sdk/pkg/annotations"
12+
"github.com/conductorone/baton-sdk/pkg/connectorbuilder"
13+
"google.golang.org/protobuf/types/known/structpb"
14+
)
15+
16+
var addNumbers = &v2.BatonActionSchema{
17+
Name: "addNumbers",
18+
Arguments: []*config.Field{
19+
{
20+
Name: "number1",
21+
DisplayName: "Number 1",
22+
Field: &config.Field_IntField{},
23+
IsRequired: true,
24+
},
25+
{
26+
Name: "number2",
27+
DisplayName: "Number 2",
28+
Field: &config.Field_IntField{},
29+
IsRequired: true,
30+
},
31+
},
32+
ReturnTypes: []*config.Field{
33+
{
34+
Name: "sum",
35+
DisplayName: "Sum",
36+
Field: &config.Field_IntField{},
37+
},
38+
},
39+
}
40+
41+
var helloIn1Minute = &v2.BatonActionSchema{
42+
Name: "helloIn1Minute",
43+
Arguments: []*config.Field{
44+
{
45+
Name: "name",
46+
DisplayName: "Name",
47+
Field: &config.Field_StringField{},
48+
IsRequired: true,
49+
},
50+
},
51+
ReturnTypes: []*config.Field{
52+
{
53+
Name: "hello",
54+
DisplayName: "Hello",
55+
Field: &config.Field_StringField{},
56+
},
57+
},
58+
}
59+
60+
func (d *Demo) RegisterActionManager(ctx context.Context) (connectorbuilder.CustomActionManager, error) {
61+
actionManager := actions.NewActionManager(ctx)
62+
63+
// addNumbers action
64+
err := actionManager.RegisterAction(ctx, addNumbers.Name, addNumbers, d.addNumbers)
65+
if err != nil {
66+
return nil, err
67+
}
68+
69+
// helloIn1Minute action
70+
err = actionManager.RegisterAction(ctx, helloIn1Minute.Name, helloIn1Minute, d.helloIn1Minute)
71+
if err != nil {
72+
return nil, err
73+
}
74+
75+
return actionManager, nil
76+
}
77+
78+
func (d *Demo) addNumbers(ctx context.Context, args *structpb.Struct) (*structpb.Struct, annotations.Annotations, error) {
79+
number1, ok := args.Fields["number1"]
80+
if !ok {
81+
return nil, nil, fmt.Errorf("missing required argument number1")
82+
}
83+
number2, ok := args.Fields["number2"]
84+
if !ok {
85+
return nil, nil, fmt.Errorf("missing required argument number2")
86+
}
87+
88+
if number1.GetNumberValue() > 100 || number2.GetNumberValue() > 100 {
89+
return nil, nil, fmt.Errorf("numbers must be less than 100")
90+
}
91+
92+
sum := number1.GetNumberValue() + number2.GetNumberValue()
93+
response := &structpb.Struct{
94+
Fields: map[string]*structpb.Value{
95+
"sum": structpb.NewNumberValue(sum),
96+
},
97+
}
98+
return response, nil, nil
99+
}
100+
101+
func (d *Demo) helloIn1Minute(ctx context.Context, args *structpb.Struct) (*structpb.Struct, annotations.Annotations, error) {
102+
name, ok := args.Fields["name"]
103+
if !ok {
104+
return nil, nil, fmt.Errorf("missing required argument name")
105+
}
106+
107+
// Sleep for 1 minute
108+
time.Sleep(1 * time.Minute)
109+
110+
response := &structpb.Struct{
111+
Fields: map[string]*structpb.Value{
112+
"hello": structpb.NewStringValue("Hello, " + name.GetStringValue() + "!"),
113+
},
114+
}
115+
return response, nil, nil
116+
}

0 commit comments

Comments
 (0)