Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions cbt.go
Original file line number Diff line number Diff line change
Expand Up @@ -872,6 +872,15 @@ var commands = []struct {
" Example: cbt updatecluster my-instance-c1 num-nodes=5",
Required: ProjectAndInstanceRequired,
},
{
Name: "updatefunction",
Desc: "Update a user-defined function in the configured instance",
do: doUpdateFunction,
Usage: "cbt updatefunction <function-id> [wasm-file=<path>] [args=<name>:<type>,...] [return-type=<type>] [deletion-protection=<true|false>] [etag=<etag>]\n\n" +
" Example:\n" +
" cbt updatefunction doc_has_keyword wasm-file=doc_has_keyword.wasm args=payload:BYTES,keyword:STRING return-type=BOOL",
Required: ProjectAndInstanceRequired,
},
{
Name: "version",
Desc: "Print the current cbt version",
Expand Down
10 changes: 10 additions & 0 deletions cbtdoc.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ The commands are:
setgcpolicy Set the garbage-collection policy (age, versions) for a column family
updateappprofile Update app profile for an instance
updatecluster Update a cluster in the configured instance
updatefunction Update a user-defined function in the configured instance
version Print the current cbt version
waitforreplication Block until all the completed writes have been replicated to all the clusters

Expand Down Expand Up @@ -486,6 +487,15 @@ Usage:

Example: cbt updatecluster my-instance-c1 num-nodes=5

# Update a user-defined function in the configured instance

Usage:

cbt updatefunction <function-id> [wasm-file=<path>] [args=<name>:<type>,...] [return-type=<type>] [deletion-protection=<true|false>] [etag=<etag>]

Example:
cbt updatefunction doc_has_keyword wasm-file=doc_has_keyword.wasm args=payload:BYTES,keyword:STRING return-type=BOOL

# Print the current cbt version

Usage:
Expand Down
117 changes: 117 additions & 0 deletions functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import (
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/emptypb"
"google.golang.org/protobuf/types/known/fieldmaskpb"
)

const (
Expand All @@ -65,6 +66,7 @@ var (
// polling so CLI commands can be tested hermetically without package-level globals.
type functionAdminAPI interface {
CreateFunction(ctx context.Context, req *btapb.CreateFunctionRequest) (*longrunningpb.Operation, error)
UpdateFunction(ctx context.Context, req *btapb.UpdateFunctionRequest) (*longrunningpb.Operation, error)
GetFunction(ctx context.Context, req *btapb.GetFunctionRequest) (*btapb.Function, error)
ListFunctions(ctx context.Context, req *btapb.ListFunctionsRequest) (*btapb.ListFunctionsResponse, error)
DeleteFunction(ctx context.Context, req *btapb.DeleteFunctionRequest) (*emptypb.Empty, error)
Expand Down Expand Up @@ -139,6 +141,10 @@ func (c *grpcFunctionAdminClient) CreateFunction(ctx context.Context, req *btapb
return c.admin.CreateFunction(withRequestParams(ctx, "parent", req.GetParent()), req)
}

func (c *grpcFunctionAdminClient) UpdateFunction(ctx context.Context, req *btapb.UpdateFunctionRequest) (*longrunningpb.Operation, error) {
return c.admin.UpdateFunction(withRequestParams(ctx, "function.name", req.GetFunction().GetName()), req)
}

func (c *grpcFunctionAdminClient) GetFunction(ctx context.Context, req *btapb.GetFunctionRequest) (*btapb.Function, error) {
return c.admin.GetFunction(withRequestParams(ctx, "name", req.GetName()), req)
}
Expand Down Expand Up @@ -591,6 +597,111 @@ func runCreateFunction(ctx context.Context, api functionAdminAPI, project, insta
return nil
}

func runUpdateFunction(ctx context.Context, api functionAdminAPI, project, instance string, w io.Writer, args ...string) error {
const updateUsage = "usage: cbt updatefunction <function-id> [wasm-file=<path>] [args=<name>:<type>,...] [return-type=<type>] [deletion-protection=<true|false>] [etag=<etag>]"
if len(args) < 2 {
return errors.New(updateUsage)
}
functionID := args[0]
if err := validateFunctionIdentifier(functionID, "function-id"); err != nil {
return err
}
parsed, err := parseArgs(args[1:], []string{"wasm-file", "args", "return-type", "deletion-protection", "etag"})
if err != nil {
return err
}

wasmPath, hasWasmFile, err := parseNonEmptyOption(parsed, "wasm-file")
if err != nil {
return err
}
argsSpec, hasArgs, err := parseNonEmptyOption(parsed, "args")
if err != nil {
return err
}
retSpec, hasReturnType, err := parseNonEmptyOption(parsed, "return-type")
if err != nil {
return err
}
if (hasArgs || hasReturnType) && !hasWasmFile {
return errors.New("updating \"args\" or \"return-type\" requires specifying \"wasm-file\"")
}

fn := &btapb.Function{
Name: functionResourceName(project, instance, functionID),
}
var maskPaths []string

if hasWasmFile {
wasmBytes, err := readWASMSource(wasmPath)
if err != nil {
return err
}
wasmFn := &btapb.Function_WasmFunction{
Source: wasmBytes,
}
fn.FunctionType = &btapb.Function_WasmFunction_{WasmFunction: wasmFn}
maskPaths = append(maskPaths, "wasm_function.source")

if hasArgs {
fnArgs, err := parseWASMFunctionArgs(argsSpec)
if err != nil {
return err
}
wasmFn.Arguments = fnArgs
maskPaths = append(maskPaths, "wasm_function.arguments")
}

if hasReturnType {
retType, err := parseWASMFunctionType(retSpec)
if err != nil {
return fmt.Errorf("return-type: %w", err)
}
wasmFn.ReturnType = retType
maskPaths = append(maskPaths, "wasm_function.return_type")
}
}

if dpStr, hasDP, err := parseNonEmptyOption(parsed, "deletion-protection"); err != nil {
return err
} else if hasDP {
dp, err := strconv.ParseBool(dpStr)
if err != nil {
return fmt.Errorf("parsing deletion-protection %q: %w", dpStr, err)
}
fn.DeletionProtection = dp
maskPaths = append(maskPaths, "deletion_protection")
}

if etagVal, hasEtag, err := parseNonEmptyOption(parsed, "etag"); err != nil {
return err
} else if hasEtag {
fn.Etag = etagVal
}

if len(maskPaths) == 0 {
return errors.New("updating function: nothing to update (specify wasm-file or deletion-protection)")
}

req := &btapb.UpdateFunctionRequest{
Function: fn,
UpdateMask: &fieldmaskpb.FieldMask{Paths: maskPaths},
}

baseCtx := withResourcePrefix(ctx, project)
op, err := api.UpdateFunction(baseCtx, req)
if err != nil {
return fmt.Errorf("updating function %q: %w", functionID, err)
}
updated, err := api.WaitFunctionOperation(baseCtx, op)
if err != nil {
return fmt.Errorf("updating function %q: %w", functionID, err)
}
_, sig := describeFunctionKindAndSignature(updated)
fmt.Fprintf(w, "Name: %s\nSignature: %s\n", updated.GetName(), sig)
return nil
}

func runListFunctions(ctx context.Context, api functionAdminAPI, project, instance string, w io.Writer, args ...string) error {
if len(args) != 0 {
return errors.New("usage: cbt listfunctions")
Expand Down Expand Up @@ -701,6 +812,12 @@ func doCreateFunction(ctx context.Context, args ...string) {
}
}

func doUpdateFunction(ctx context.Context, args ...string) {
if err := runUpdateFunction(ctx, defaultFunctionAdminAPI(), config.Project, config.Instance, os.Stdout, args...); err != nil {
log.Fatal(err)
}
}

func doListFunctions(ctx context.Context, args ...string) {
if err := runListFunctions(ctx, defaultFunctionAdminAPI(), config.Project, config.Instance, os.Stdout, args...); err != nil {
log.Fatal(err)
Expand Down
Loading
Loading