|
| 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