|
| 1 | +// Copyright 2025 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package serverlesssparkcreatesparkbatch |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "fmt" |
| 20 | + |
| 21 | + dataproc "cloud.google.com/go/dataproc/v2/apiv1/dataprocpb" |
| 22 | + "github.com/goccy/go-yaml" |
| 23 | + "github.com/googleapis/genai-toolbox/internal/sources" |
| 24 | + "github.com/googleapis/genai-toolbox/internal/tools" |
| 25 | + "github.com/googleapis/genai-toolbox/internal/tools/serverlessspark/createbatch" |
| 26 | + "github.com/googleapis/genai-toolbox/internal/util/parameters" |
| 27 | +) |
| 28 | + |
| 29 | +const kind = "serverless-spark-create-spark-batch" |
| 30 | + |
| 31 | +func init() { |
| 32 | + if !tools.Register(kind, newConfig) { |
| 33 | + panic(fmt.Sprintf("tool kind %q already registered", kind)) |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +func newConfig(ctx context.Context, name string, decoder *yaml.Decoder) (tools.ToolConfig, error) { |
| 38 | + baseCfg, err := createbatch.NewConfig(ctx, name, decoder) |
| 39 | + if err != nil { |
| 40 | + return nil, err |
| 41 | + } |
| 42 | + return Config{baseCfg}, nil |
| 43 | +} |
| 44 | + |
| 45 | +type Config struct { |
| 46 | + createbatch.Config |
| 47 | +} |
| 48 | + |
| 49 | +// validate interface |
| 50 | +var _ tools.ToolConfig = Config{} |
| 51 | + |
| 52 | +// ToolConfigKind returns the unique name for this tool. |
| 53 | +func (cfg Config) ToolConfigKind() string { |
| 54 | + return kind |
| 55 | +} |
| 56 | + |
| 57 | +// Initialize creates a new Tool instance. |
| 58 | +func (cfg Config) Initialize(srcs map[string]sources.Source) (tools.Tool, error) { |
| 59 | + return createbatch.NewTool(cfg.Config, cfg, srcs, &SparkBatchBuilder{}) |
| 60 | +} |
| 61 | + |
| 62 | +type SparkBatchBuilder struct{} |
| 63 | + |
| 64 | +func (b *SparkBatchBuilder) Parameters() parameters.Parameters { |
| 65 | + return parameters.Parameters{ |
| 66 | + parameters.NewStringParameterWithRequired("mainJarFile", "Optional. The gs:// URI of the jar file that contains the main class. Exactly one of mainJarFile or mainClass must be specified.", false), |
| 67 | + parameters.NewStringParameterWithRequired("mainClass", "Optional. The name of the driver's main class. Exactly one of mainJarFile or mainClass must be specified.", false), |
| 68 | + parameters.NewArrayParameterWithRequired("jarFiles", "Optional. A list of gs:// URIs of jar files to add to the CLASSPATHs of the Spark driver and tasks.", false, parameters.NewStringParameter("jarFile", "A jar file URI.")), |
| 69 | + parameters.NewArrayParameterWithRequired("args", "Optional. A list of arguments passed to the driver.", false, parameters.NewStringParameter("arg", "An argument.")), |
| 70 | + parameters.NewStringParameterWithRequired("version", "Optional. The Serverless runtime version to execute with.", false), |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +func (b *SparkBatchBuilder) BuildBatch(params parameters.ParamValues) (*dataproc.Batch, error) { |
| 75 | + paramMap := params.AsMap() |
| 76 | + |
| 77 | + mainJar, _ := paramMap["mainJarFile"].(string) |
| 78 | + mainClass, _ := paramMap["mainClass"].(string) |
| 79 | + |
| 80 | + if mainJar == "" && mainClass == "" { |
| 81 | + return nil, fmt.Errorf("must provide either mainJarFile or mainClass") |
| 82 | + } |
| 83 | + if mainJar != "" && mainClass != "" { |
| 84 | + return nil, fmt.Errorf("cannot provide both mainJarFile and mainClass") |
| 85 | + } |
| 86 | + |
| 87 | + sparkBatch := &dataproc.SparkBatch{} |
| 88 | + if mainJar != "" { |
| 89 | + sparkBatch.Driver = &dataproc.SparkBatch_MainJarFileUri{MainJarFileUri: mainJar} |
| 90 | + } else { |
| 91 | + sparkBatch.Driver = &dataproc.SparkBatch_MainClass{MainClass: mainClass} |
| 92 | + } |
| 93 | + |
| 94 | + if jarFileUris, ok := paramMap["jarFiles"].([]any); ok { |
| 95 | + for _, uri := range jarFileUris { |
| 96 | + sparkBatch.JarFileUris = append(sparkBatch.JarFileUris, fmt.Sprintf("%v", uri)) |
| 97 | + } |
| 98 | + } else if mainClass != "" { |
| 99 | + return nil, fmt.Errorf("jarFiles is required when mainClass is provided") |
| 100 | + } |
| 101 | + |
| 102 | + if args, ok := paramMap["args"].([]any); ok { |
| 103 | + for _, arg := range args { |
| 104 | + sparkBatch.Args = append(sparkBatch.Args, fmt.Sprintf("%v", arg)) |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + return &dataproc.Batch{ |
| 109 | + BatchConfig: &dataproc.Batch_SparkBatch{ |
| 110 | + SparkBatch: sparkBatch, |
| 111 | + }, |
| 112 | + }, nil |
| 113 | +} |
0 commit comments