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
37 changes: 29 additions & 8 deletions cmd/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
* specific language governing permissions and limitations under the License.
*/


package cmd

import (
Expand All @@ -20,11 +19,13 @@ import (
"errors"
"fmt"
"github.com/antihax/optional"
"github.com/conductor-oss/conductor-cli/internal"
"github.com/conductor-sdk/conductor-go/sdk/client"
"github.com/conductor-sdk/conductor-go/sdk/model"
"github.com/conductor-oss/conductor-cli/internal"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"net/http"
neturl "net/url"
"os"
"strings"
"text/tabwriter"
Expand Down Expand Up @@ -239,30 +240,50 @@ func deleteSchedule(cmd *cobra.Command, args []string) error {
return nil
}

// setSchedulePaused pauses or resumes a schedule. OSS Conductor accepts only PUT on
// these endpoints, older Orkes deployments only GET, so try PUT and fall back to GET
// on 405. Any other status is returned as-is.
func setSchedulePaused(ctx context.Context, name, action string) error {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you are making these changes conductor-oss/go-sdk#276, let's go with that instead of setSchedulePaused.

api := internal.GetAPIClient()
path := fmt.Sprintf("/scheduler/schedules/%s/%s", neturl.PathEscape(name), action)

var result interface{}
_, err := api.Put(ctx, path, nil, &result)
if err == nil {
return nil
}

var swaggerErr client.GenericSwaggerError
if !errors.As(err, &swaggerErr) || swaggerErr.StatusCode() != http.StatusMethodNotAllowed {
return err
}

if _, getErr := api.Get(ctx, path, nil, &result); getErr != nil {
return getErr
}
return nil
}

func pauseSchedule(cmd *cobra.Command, args []string) error {
schedulerClient := internal.GetSchedulerClient()
if len(args) == 0 {
return cmd.Usage()
}

for i := 0; i < len(args); i++ {
_, _, err := schedulerClient.PauseSchedule(context.Background(), args[i])
if err != nil {
if err := setSchedulePaused(context.Background(), args[i], "pause"); err != nil {
return parseSchedulerAPIError(err, fmt.Sprintf("Failed to pause schedule '%s'", args[i]))
}
}
return nil
}

func resumeSchedule(cmd *cobra.Command, args []string) error {
schedulerClient := internal.GetSchedulerClient()
if len(args) == 0 {
return cmd.Usage()
}

for i := 0; i < len(args); i++ {
_, _, err := schedulerClient.ResumeSchedule(context.Background(), args[i])
if err != nil {
if err := setSchedulePaused(context.Background(), args[i], "resume"); err != nil {
return parseSchedulerAPIError(err, fmt.Sprintf("Failed to resume schedule '%s'", args[i]))
}
}
Expand Down
166 changes: 166 additions & 0 deletions cmd/scheduler_verb_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
/*
* Copyright 2026 Conductor Authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

package cmd

import (
"context"
"net/http"
"net/http/httptest"
"testing"

"github.com/conductor-sdk/conductor-go/sdk/client"
"github.com/conductor-sdk/conductor-go/sdk/settings"

"github.com/conductor-oss/conductor-cli/internal"
)

// The three deployment shapes setSchedulePaused has to satisfy cannot be covered by
// any single live server, so they are modelled here as stub handlers. Each records
// the verbs it received so the test can assert not just success but that the client
// converged on PUT and only fell back when it had to.

type verbRecorder struct {
methods []string
}

func (v *verbRecorder) handler(allow map[string]int) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
v.methods = append(v.methods, r.Method)
if code, ok := allow[r.Method]; ok {
w.WriteHeader(code)
return
}
w.WriteHeader(http.StatusMethodNotAllowed)
}
}

func withStubServer(t *testing.T, h http.Handler) {
t.Helper()
srv := httptest.NewServer(h)
t.Cleanup(srv.Close)
internal.SetAPIClient(client.NewAPIClient(
settings.NewAuthenticationSettings("", ""),
settings.NewHttpSettings(srv.URL+"/api"),
))
}

func TestSetSchedulePausedVerbNegotiation(t *testing.T) {
tests := []struct {
name string
allow map[string]int
wantMethods []string
wantErr bool
}{
{
// Upstream OSS Conductor: PUT only. One request, no fallback needed.
name: "oss accepts put",
allow: map[string]int{http.MethodPut: http.StatusOK},
wantMethods: []string{http.MethodPut},
},
{
// Orkes >= 2026-07-14 accepts both; PUT must win so we converge on it.
name: "orkes accepts both, put is preferred",
allow: map[string]int{
http.MethodPut: http.StatusOK,
http.MethodGet: http.StatusOK,
},
wantMethods: []string{http.MethodPut},
},
{
// Legacy Orkes: GET only. PUT is rejected 405, then GET succeeds.
name: "legacy orkes falls back to get",
allow: map[string]int{http.MethodGet: http.StatusOK},
wantMethods: []string{http.MethodPut, http.MethodGet},
},
{
// Neither verb works: surface the failure rather than reporting success.
name: "both rejected with 405 returns error",
allow: map[string]int{},
wantMethods: []string{http.MethodPut, http.MethodGet},
wantErr: true,
},
{
// 405 is the only status meaning "wrong verb". A 404 means the schedule —
// or the whole scheduler module — is absent, so retrying with GET would
// produce a second 404 and report that instead of the real cause.
name: "404 does not trigger the fallback",
allow: map[string]int{http.MethodPut: http.StatusNotFound},
wantMethods: []string{http.MethodPut},
wantErr: true,
},
{
// Likewise for auth failures: a fallback would just repeat the rejection.
name: "401 does not trigger the fallback",
allow: map[string]int{http.MethodPut: http.StatusUnauthorized},
wantMethods: []string{http.MethodPut},
wantErr: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
rec := &verbRecorder{}
withStubServer(t, rec.handler(tt.allow))

err := setSchedulePaused(context.Background(), "probe", "pause")
if tt.wantErr && err == nil {
t.Fatal("expected an error, got nil")
}
if !tt.wantErr && err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(rec.methods) != len(tt.wantMethods) {
t.Fatalf("methods = %v, want %v", rec.methods, tt.wantMethods)
}
for i, m := range tt.wantMethods {
if rec.methods[i] != m {
t.Fatalf("methods = %v, want %v", rec.methods, tt.wantMethods)
}
}
})
}
}

// A 5xx must NOT trigger the fallback. Retrying a server fault with a different verb
// would mask it and surface a misleading "method not supported" instead.
func TestSetSchedulePausedDoesNotFallBackOnServerError(t *testing.T) {
rec := &verbRecorder{}
withStubServer(t, rec.handler(map[string]int{
http.MethodPut: http.StatusInternalServerError,
http.MethodGet: http.StatusOK,
}))

if err := setSchedulePaused(context.Background(), "probe", "pause"); err == nil {
t.Fatal("expected the 500 to be returned, got nil")
}
if len(rec.methods) != 1 || rec.methods[0] != http.MethodPut {
t.Fatalf("methods = %v, want a single PUT with no GET fallback", rec.methods)
}
}

// The schedule name is interpolated into the path and must be escaped.
func TestSetSchedulePausedEscapesName(t *testing.T) {
var gotPath string
withStubServer(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gotPath = r.URL.EscapedPath()
w.WriteHeader(http.StatusOK)
}))

if err := setSchedulePaused(context.Background(), "needs escaping", "resume"); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if want := "/api/scheduler/schedules/needs%20escaping/resume"; gotPath != want {
t.Fatalf("path = %q, want %q", gotPath, want)
}
}
8 changes: 7 additions & 1 deletion internal/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
* specific language governing permissions and limitations under the License.
*/


package internal

import (
Expand Down Expand Up @@ -52,6 +51,13 @@ func GetSchedulerClient() client.SchedulerClient {
return client.NewSchedulerClient(apiClient)
}

// GetAPIClient returns the shared SDK API client for the rare case where a typed
// SDK client issues the wrong request and the CLI must drive the endpoint itself.
// Prefer the typed clients; reach for this only with a comment explaining why.
func GetAPIClient() *client.APIClient {
return apiClient
}

func GetTaskClient() *client.TaskResourceApiService {
return &client.TaskResourceApiService{APIClient: apiClient}
}
Expand Down
Loading