From 0f061900eea9f2965decbd9167e18c04617f6259 Mon Sep 17 00:00:00 2001 From: Aditya Manthramurthy Date: Fri, 14 Mar 2025 10:29:01 -0700 Subject: [PATCH] batch: use server's generate API when available (#5166) Uses the new GenerateBatchJobV2 API to request server to list supported batch jobs and to request job templates in `mc batch generate` --- cmd/batch-generate.go | 59 ++++++++++++++++++++++++++++++++++--------- go.mod | 2 +- go.sum | 4 +-- 3 files changed, 50 insertions(+), 15 deletions(-) diff --git a/cmd/batch-generate.go b/cmd/batch-generate.go index 5f1b6cf2..5c767102 100644 --- a/cmd/batch-generate.go +++ b/cmd/batch-generate.go @@ -19,9 +19,9 @@ package cmd import ( "fmt" - "strings" "github.com/minio/cli" + json "github.com/minio/colorjson" "github.com/minio/madmin-go/v3" "github.com/minio/mc/pkg/probe" ) @@ -40,26 +40,22 @@ USAGE: {{.HelpName}} TARGET JOBTYPE JOBTYPE: -` + supportedJobTypes() + ` + - replicate + - keyrotate + - expire +Use the special value "list" to request server to list supported job types. + FLAGS: {{range .VisibleFlags}}{{.}} {{end}} EXAMPLES: 1. Generate a new batch 'replication' job definition: {{.Prompt}} {{.HelpName}} myminio replicate > replication.yaml + 2. List all supported job types: + {{.Prompt}} {{.HelpName}} myminio list `, } -func supportedJobTypes() string { - var builder strings.Builder - for _, jobType := range madmin.SupportedJobTypes { - builder.WriteString(" - ") - builder.WriteString(string(jobType)) - builder.WriteString("\n") - } - return builder.String() -} - // checkBatchGenerateSyntax - validate all the passed arguments func checkBatchGenerateSyntax(ctx *cli.Context) { if len(ctx.Args()) != 2 { @@ -80,6 +76,45 @@ func mainBatchGenerate(ctx *cli.Context) error { adminClient, err := newAdminClient(aliasedURL) fatalIf(err, "Unable to initialize admin connection.") + if jobType == "list" { + supportedJobTypes, apiUnavailable, e := adminClient.GetSupportedBatchJobTypes(globalContext) + if e != nil { + fatalIf(probe.NewError(e), "Unable to list supported job types") + } + if apiUnavailable { + // Fallback to default supported jobs. + supportedJobTypes = madmin.SupportedJobTypes + } + + if globalJSON { + j, err := json.Marshal(supportedJobTypes) + if err != nil { + fatalIf(probe.NewError(err), "Unable to marshal supported job types") + } + fmt.Println(string(j)) + } else { + for _, jobType := range supportedJobTypes { + fmt.Println(jobType) + } + } + return nil + } + + // Try GenerateV2 API. + template, apiUnavailable, e := adminClient.GenerateBatchJobV2(globalContext, madmin.GenerateBatchJobOpts{ + Type: madmin.BatchJobType(jobType), + }) + if e != nil { + fatalIf(probe.NewError(e), "Unable to generate template for %s", args.Get(1)) + } + + // Check if server supports GenerateV2 API. + if !apiUnavailable { + fmt.Println(template) + return nil + } + + // As API is not supported we fallback to returning the static job template. var found bool for _, job := range madmin.SupportedJobTypes { if jobType == string(job) { diff --git a/go.mod b/go.mod index 7fbf7c30..afb4fef3 100644 --- a/go.mod +++ b/go.mod @@ -23,7 +23,7 @@ require ( github.com/minio/cli v1.24.2 github.com/minio/colorjson v1.0.8 github.com/minio/filepath v1.0.0 - github.com/minio/madmin-go/v3 v3.0.96 + github.com/minio/madmin-go/v3 v3.0.97 github.com/minio/minio-go/v7 v7.0.88 github.com/minio/pkg/v3 v3.1.0 github.com/minio/selfupdate v0.6.0 diff --git a/go.sum b/go.sum index b80b4d85..a2cc3807 100644 --- a/go.sum +++ b/go.sum @@ -142,8 +142,8 @@ github.com/minio/crc64nvme v1.0.1 h1:DHQPrYPdqK7jQG/Ls5CTBZWeex/2FMS3G5XGkycuFrY github.com/minio/crc64nvme v1.0.1/go.mod h1:eVfm2fAzLlxMdUGc0EEBGSMmPwmXD5XiNRpnu9J3bvg= github.com/minio/filepath v1.0.0 h1:fvkJu1+6X+ECRA6G3+JJETj4QeAYO9sV43I79H8ubDY= github.com/minio/filepath v1.0.0/go.mod h1:/nRZA2ldl5z6jT9/KQuvZcQlxZIMQoFFQPvEXx9T/Bw= -github.com/minio/madmin-go/v3 v3.0.96 h1:TFPqF1CveW12BgZ9Af41ocvbw4p63ptQQ+/YjTyRz48= -github.com/minio/madmin-go/v3 v3.0.96/go.mod h1:pMLdj9OtN0CANNs5tdm6opvOlDFfj0WhbztboZAjRWE= +github.com/minio/madmin-go/v3 v3.0.97 h1:P7XO+ofPexkXy9akxG9Xoif1zIkbmWKeKtG3AquVzEY= +github.com/minio/madmin-go/v3 v3.0.97/go.mod h1:pMLdj9OtN0CANNs5tdm6opvOlDFfj0WhbztboZAjRWE= github.com/minio/md5-simd v1.1.2 h1:Gdi1DZK69+ZVMoNHRXJyNcxrMA4dSxoYHZSQbirFg34= github.com/minio/md5-simd v1.1.2/go.mod h1:MzdKDxYpY2BT9XQFocsiZf/NKVtR7nkE4RoEpN+20RM= github.com/minio/minio-go/v7 v7.0.88 h1:v8MoIJjwYxOkehp+eiLIuvXk87P2raUtoU5klrAAshs=